为什么我不能将纹理加载到我的应用程序中? [英] Why can I not load a texture into my app?

查看:102
本文介绍了为什么我不能将纹理加载到我的应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为纹理映射是一项非常简单的任务.实际上,我实施了很多次,但是这次却失败了,不知道为什么?而且我可以保证加载纹理的路径是正确的.还有其他引起我困惑的原因吗?

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?

这是我的代码:

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?

推荐答案

问题是我在加载纹理之前设置了GL_LINE模式,但未能注意到这一点.因此,在设置GL_FILL模式后,一切都很好!!

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

这篇关于为什么我不能将纹理加载到我的应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆