I am a freshman to opengl. I want to display a bmp image file in a window as a texture. I used glut library. but my code cannot work well as I expected. It display a white quads which should be the bmp image. Who can point out my failure, thanks!
here is code:
GLuint texture;
AUX_RGBImageRec *LoadBMP(char *filename)
{
FILE *f;
if(filename==NULL)
return NULL;
f=fopen(filename,“r”);
if(!f)
return NULL;
fclose(f);
return auxDIBImageLoadA(filename);
}
int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *textureImage[1];
memset(textureImage,0,sizeof(void *)*1);
if (textureImage[0]=LoadBMP(“image130.bmp”))
{
Status=TRUE;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
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;
}
//int LoadGLTextures()
//{
//
//
//
//
// int status=FALSE;
// AUX_RGBImageRec *TextureImage=NULL;
// if(TextureImage=LoadBMP(“image130.bmp”))
// {
// status=TRUE;
// glGenTextures(1,&texture);
// glBindTexture(GL_TEXTURE_2D,texture);
// glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage->sizeX,TextureImage->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage->data);
// glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//
// if(TextureImage)
// {
// if(TextureImage->data)
// free(TextureImage->data);
// free(TextureImage);
// }
// }
// return status;
//}
//
//
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display(void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-2.0);
//glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);
glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3f(-1,-1,0.0);
glTexCoord2d(1,0);glVertex3f(-1,1,0.0);
glTexCoord2d(1,1);glVertex3f(1,1,0.0);
glTexCoord2d(0,1);glVertex3f(1,-1,0.0);
glEnd();
//glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glVertex2d(0.0,0);
glVertex2d(2,2);
glEnd();
glFlush();
glutSwapBuffers();
}
void init()
{
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
LoadGLTextures();
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}