Why can I not load a texture into my app?

可紊 提交于 2019-12-24 15:44:57

问题


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?


回答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.




回答2:


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!!!



来源:https://stackoverflow.com/questions/10698522/why-can-i-not-load-a-texture-into-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!