Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Texture Issues

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    17

    Texture Issues

    Hey guys,

    I need some help as this is driving me nuts. I want to texture a quadric sphere, I have a header file and a .cpp file with the instructions to load a .bmp file, but all I get is a blank screen and no errors. I will include what I have so far:

    fileFunctions.h

    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

    fileFunctions.cpp

    #include <GL/glut.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "fileFunctions.h"

    // Returns a pointer to the bitmap image of the bitmap specified
    // by filename. Also returns the bitmap header information.
    // No support for 8-bit bitmaps.
    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
    {
    FILE *filePtr;
    BITMAPFILEHEADER bitmapFileHeader;
    unsigned char *bitmapImage;
    int imageIdx = 0;
    unsigned char tempRGB;
    filePtr = fopen(filename, "rb");
    if (filePtr == NULL) return NULL;
    fread(&amp;bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
    fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
    for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
    {
    tempRGB = bitmapImage[imageIdx];
    bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
    bitmapImage[imageIdx + 2] = tempRGB;

    }
    }

    Planets.cpp

    #include <GL/glut.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "fileFunctions.h"

    using namespace std;

    GLUquadricObj *mercury;
    GLuint txtMercury;
    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char* bitmapData;

    void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glBindTexture(GL_TEXTURE_2D,txtMercury);
    gluSphere(mercury, 1.0, 32, 32);
    glFlush();
    }

    void init(void) {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    bitmapData = LoadBitmapFile("Venus.bmp",&amp;bitmapInfoHeader);
    glGenTextures(1,&amp;txtMercury);
    glBindTexture(GL_TEXTURE_2D,txtMercury);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    mercury=gluNewQuadric();
    gluQuadricTexture(mercury,GLU_TRUE);
    gluQuadricDrawStyle(mercury,GLU_FILL);
    gluQuadricNormals(mercury,GLU_SMOOTH);
    free(bitmapData);
    }

    int main(int argc, char** argv){
    glutInit(&amp;argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("20918381 Planets");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    }

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture Issues

    Hi,
    You are missing the return statement from your loadbitmapFile func.
    Code :
    return bitmapImage;
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    17

    Re: Texture Issues

    OMG thank you so much Mobeen thats really helped!

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    17

    Re: Texture Issues

    I also have a question please Mobeen or anyone:

    1). How do load and apply textures to other quadric shapes within the same program. As i am trying to design a solar system and cant seem to apply or load different textures.

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture Issues

    Quote Originally Posted by Subatomichero
    I also have a question please Mobeen or anyone:

    1). How do load and apply textures to other quadric shapes within the same program. As i am trying to design a solar system and cant seem to apply or load different textures.
    You would need to create the texture objects first. Lets say u have three textures to load, image0.bmp, image1.bmp and image2.bmp. We use
    Code :
    GLuint textureID[3];
    glGenTextures(3, textureID);
    for(int i=0;i<3;i++) {
    unsigned char* bitmapData = LoadImage("image"+i+".bmp");
    glBindTexture(GL_TEXTURE_2D,textureID[i]);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHeader.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_UNSIGNED_BYTE, bitmapData);
    free(bitmapData);//if u use malloc to allocate the bitmapData 
    }

    Then in your render loop you would apply the appropriate transform and before drawing the quadric bind its texture doing somthing like this

    Code :
    //apply modelview transform
    for(int i=0;i<3;i++) 
    {
      //apply object's transform matrix here
      glBindTexture(GL_TEXTURE_2D, textureID[i]);
       //draw object here
    }

    Hope this helps,
    Mobeen
    Regards,
    Mobeen

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    17

    Re: Texture Issues

    Hey Mobeen,

    Wonderful, thanks so much....I just have 1 little problem. I now get an error saying "92 C:\Users\Martin\Desktop\planets\planets.cpp invalid operands of types `const char*' and `const char[5]' to binary `operator+' "

    Here is my code after ur recommendations:

    #include <GL/glut.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "fileFunctions.h"

    using namespace std;

    GLfloat viewangle = 0, tippangle = 0;
    GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
    GLfloat d[3] = {0.1, 0.1, 0.1};

    float angle[6];

    GLUquadricObj *sun;
    GLUquadricObj *mercury;
    GLuint textureID[9];

    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char* bitmapData;

    void drawSun(){
    glBindTexture(GL_TEXTURE_2D,textureID[0]);
    glRotatef(angle[0],0.0,1,0.0);
    gluSphere(sun, 1.0, 32, 32);
    }
    void drawMercury(){
    glBindTexture(GL_TEXTURE_2D,textureID[1]);
    glRotatef(angle[1],0.0,1,0.0);
    gluSphere(mercury, 0.5, 32, 32);
    }

    void rotate(){
    angle[0]+=0.005;
    if (angle[0]>360)angle[0]-=360;
    angle[1]+= 0.05;
    if (angle[1]>360) angle[1]-=360;
    glutPostRedisplay();
    }

    void Special_Keys (int key, int x, int y)
    {
    switch (key) {
    case GLUT_KEY_LEFT : viewangle -= 5; break;
    case GLUT_KEY_RIGHT: viewangle += 5; break;
    case GLUT_KEY_UP : tippangle -= 5; break;
    case GLUT_KEY_DOWN : tippangle += 5; break;
    }
    glutPostRedisplay();
    }
    void keyPressed(unsigned char key, int x, int y){
    switch (key) {
    case 'j' : d[0] += 0.1; break;
    case 'k' : d[1] += 0.1; break;
    case 'l' : d[2] += 0.1; break;

    case 'x' : xAngle += 5; break;
    case 'y' : yAngle += 5; break;
    case 'z' : zAngle += 5; break;

    }
    glutPostRedisplay();
    }

    void display() {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable (GL_DEPTH_TEST);
    glLoadIdentity();
    glRotatef (tippangle, 1,0,0); // Up and down arrow keys 'tip' view.
    glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.
    glPushMatrix ();
    glTranslatef (d[0], d[1], d[2]); // Move box down X axis.
    drawSun();
    glTranslatef(0.5,0.5,-2);
    drawMercury();
    glRotatef (zAngle, 0,0,1);
    glRotatef (yAngle, 0,1,0);
    glRotatef (xAngle, 1,0,0);
    glPopMatrix ();
    glFlush();
    glutSwapBuffers();
    }

    void init(void) {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    glGenTextures(9, textureID);
    for(int i=0;i<9;i++) {
    unsigned char*bitmapData = LoadImage("image"+i+".bmp");
    glBindTexture(GL_TEXTURE_2D,textureID[i]);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
    free(bitmapData);
    }
    sun=gluNewQuadric();
    mercury=gluNewQuadric();
    gluQuadricTexture(sun,GLU_TRUE);
    gluQuadricDrawStyle(sun,GLU_FILL);
    gluQuadricNormals(sun,GLU_SMOOTH);
    gluQuadricTexture(mercury,GLU_TRUE);
    gluQuadricDrawStyle(mercury,GLU_FILL);
    gluQuadricNormals(mercury,GLU_SMOOTH);

    }

    int main(int argc, char** argv){
    glutInit(&amp;argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("20918381 Planets");
    angle[0]=0;
    angle[1]=0;
    glutIdleFunc(rotate);
    glutKeyboardFunc(keyPressed);
    glutSpecialFunc(Special_Keys);
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    }

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture Issues

    Hi,
    yeah i just passed in the info. so that u get the idea. The reason u get this error is because the func. expects a char* so u must pass in a char pointer and we are trying to add two const char* which is wrong. Instead u should do this,
    replace this line
    Code :
    unsigned char*bitmapData = LoadImage("image"+i+".bmp");
    with this
    Code :
    char imageName[50]={'\0'};
    sprintf(imageName, "image%d.bmp",i);
    unsigned char* bitmapData = LoadImage(imageName);
    This assumes that the image names are less than 50 chars.

    See if this sorts out your problem.

    Regards,
    Mobeen
    Regards,
    Mobeen

  8. #8
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    17

    Re: Texture Issues

    OMG another error now, ill post it and as you've already guessed only been working on openGL for a short while as a 2nd year uni student

    94 C:\Users\Martin\Desktop\planets\planets.cpp cannot convert `char*' to `HINSTANCE__*' for argument `1' to `void* LoadImageA(HINSTANCE__*, const CHAR*, UINT, int, int, UINT)'

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture Issues

    CHange your func. name from LoadImage to MyLoadImage or anything else. It is conflicting with the WInAPI func. of the same name.
    Regards,
    Mobeen

  10. #10
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    17

    Re: Texture Issues

    ive tried changing unsigned char* bitmapData = LoadImage(imageName); to MyLoadImage as suggested, but a new error shows that the function needs to be declared

    regards

    Martin

    p.s thank you so much for your help so far mobeen

Page 1 of 3 123 LastLast

Similar Threads

  1. Depth texture issues - All white texture
    By Illiander in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-17-2014, 03:56 AM
  2. Texture Mapping Issues
    By jackthomas in forum COLLADA
    Replies: 0
    Last Post: 08-06-2009, 09:24 PM
  3. Texture Projection Issues
    By kevin_bkt in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 03-26-2009, 10:31 AM
  4. Texture atlas through texture indirection issues.
    By qorpse in forum OpenGL: GLSL
    Replies: 4
    Last Post: 01-04-2007, 03:26 AM
  5. Texture compression issues
    By Moz in forum OpenGL: Advanced Coding
    Replies: 30
    Last Post: 02-06-2002, 07:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean