PDA

View Full Version : texture problem?



txwd0033
09-14-2009, 12:59 AM
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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,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_FILTE R,GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,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;
}

Dark Photon
09-14-2009, 05:09 AM
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!
Divide and conquer. If you're having trouble with something, rip it out and plug in something much simpler. See if the problem goes away. Rinse, repeat.

For instance, first rip out that BMP loader gizmo, and start with this known-working LoadGLTexture function in your code that uses a hard-coded texture:


void LoadGLTextures()
{
unsigned char image[4][4] = { { 0 ,0 ,255,255 },
{ 255,0 ,0 ,255 },
{ 255,0 ,0 ,255 },
{ 255,255,255,255 } };

glGenTextures( 1,&texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D ( GL_TEXTURE_2D,0, GL_RGBA8,
2, 2,
0,
GL_RGBA, GL_UNSIGNED_BYTE,
image );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
}

The texture is a 2x2 texture with nearest filtering with colors (left-to-right, bottom-to-top) of: blue, red, red, white.

Now play around with formats and content of that hard-coded array.

Then plug in your BMP loader. When it breaks, you'll know you just added the problem.

txwd0033
09-14-2009, 07:22 AM
thank your response!
I find the mistake was caused by my image's width. In Opengl, texture image's width must be power of 2.

thank you again!

Dark Photon
09-14-2009, 05:09 PM
...image's width. In Opengl, texture image's width must be power of 2.
No, not correct. OpenGL drivers can support this, in at least two ways.

If your OpenGL drivers support the ARB_texture_non_power_of_two (http://www.opengl.org/registry/specs/ARB/texture_non_power_of_two.txt) extension, you can have ordinary textures be non-power-of-two resolution.

Alternatively, if your OpenGL drivers support rectangle textures (ARB_texture_rectangle (http://www.opengl.org/registry/specs/ARB/texture_rectangle.txt) or NV_texture_rectangle (http://www.opengl.org/registry/specs/NV/texture_rectangle.txt) extensions), then you can get non-power-of-two resolution that way.

If neither of these is supported, then your OpenGL drivers don't support non-power-of-two resolutions. But that's a limitation of your OpenGL driver or GPU, not of OpenGL in general.

Note that OpenGL 2.0+ requires ARB_texture_non_power_of_two (http://www.opengl.org/registry/specs/ARB/texture_non_power_of_two.txt) support.