1

I think texture mapping is a really easy task. Actually, I implemented it many times but failed in this time and don't know why? And I can guarantee that the route to load the texture is right. Any other reasons for my confusion?

Here is my code:

GLuint mytexture;
// two functions below come from NeHe's tut. I think it works well.
AUX_RGBImageRec *LoadBMP(CHAR *Filename)                
{
    FILE *File=NULL;                                    
if (!Filename)                                      
{
    return NULL;                                    
}
File=fopen(Filename,"r");                           
if (File)                                           
{
    fclose(File);                                   
    return auxDIBImageLoadA(Filename);              
}
return NULL;                                        
}

int LoadGLTextures()                                    
{
int Status=FALSE;                                   
AUX_RGBImageRec *TextureImage[1];                   
memset(TextureImage,0,sizeof(void *)*1);            

if (TextureImage[0]=LoadBMP("NeHe.bmp"))
{
    Status=TRUE;                                    
    glGenTextures(1, &mytexture);                   
    glBindTexture(GL_TEXTURE_2D, mytexture);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, 
        GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
}

if (TextureImage[0])                                
{
    if (TextureImage[0]->data)                      
    {
        free(TextureImage[0]->data);                
    }

    free(TextureImage[0]);                          
}
return Status;                                      
}

//next is my Init() code:
bool DemoInit( void )
{
if (!LoadGLTextures())                          
{
    return FALSE;                                   
}
glEnable(GL_TEXTURE_2D);
........//other init is ok
}

bool DemoRender()
{
...///render other things
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mytexture);
glColor3f(0,0,1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(200, 0);
glTexCoord2f(1, 1); glVertex2f(200, 200);
glTexCoord2f(0, 1); glVertex2f(0, 200);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}

Pretty clear, ha? However, the final result only has a blue rectangle without the texture. Anybody could give me a hint?

Bart
  • 19,692
  • 7
  • 68
  • 77
TonyLic
  • 647
  • 1
  • 13
  • 26
  • Nothing jumping out at me as incorrect. Have you tried glGetError after initialization, and once after each render loop? – Tim May 22 '12 at 08:54
  • 5
    Please stop using GLAUX. Use a real [image loading library.](http://www.opengl.org/wiki/Image_Libraries) – Nicol Bolas May 22 '12 at 08:58
  • To Tim: Yes I use glGetError and the result is 0. By the way, instead of texture, I directly render the TextureImage[0] with glDrawPixels. I can see the picture in a particular angle. Maybe it proves the loading process is ok. But I think I need texture. – TonyLic May 22 '12 at 10:01
  • To Nicol Bolas: OK, I'll try another way to load image. But actually, I can use glDrawPixels to get the image. So I think the problem may not be the GLAUX. – TonyLic May 22 '12 at 10:05
  • @TonyLic: Well, GLAUX may work for you, but is just so outdated, its rotting smell even escapes the storage media it's on ;) How about using DevIL? – datenwolf May 22 '12 at 12:49

2 Answers2

1

Assuming TextureImage[0]->data is correctly populated:

However, the final result only has a blue rectangle without the texture.

You're using the default GL_MODULATE texture environment. Either switch glColor3f(0,0,1) to glColor3f(1,1,1) or use GL_DECAL.

You might also try a glPixelStorei(GL_UNPACK_ALIGNMENT, 1) before your glTexImage2D() since you're using GL_RGB for format.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Thanks everybody here. I find the problem is I set the GL_LINE mode before I load the texture and I failed to notice that. Really stupid ha? But thank you all the same for your excellent suggestions! I think I've learned something! – TonyLic May 23 '12 at 03:03
0

The problem is I set the GL_LINE mode before I load the texture and I failed to notice that. So after I set the GL_FILL mode, everything is fine!!!

TonyLic
  • 647
  • 1
  • 13
  • 26