PDA

View Full Version : Image Background



Leandros
02-19-2010, 06:45 AM
How can i do, for put an Image like Background?
I can load my image tga, but i dont know mesh the background and the texture like a color.

Thanks a lot.

ZbuffeR
02-20-2010, 01:03 AM
Just draw a large textured quad.
http://www.opengl.org/sdk/docs/man/
Read docs or a tutorial on texture mapping for details, but it boils down to:
1) load image file into an in memory array (apparently you already got this step)
2) glGenTextures , glBindTexture, glTexImage2D once each.
http://www.opengl.org/sdk/docs/man/xhtml/glGenTextures.xml
http://www.opengl.org/sdk/docs/man/xhtml/glBindTexture.xml
http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml
etc
3) in your render loop, draw a large quad with appropriate texture coordinates and vertex coordinates, like :
glPushMatrix(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(GL_PROJECTION);
glLoadIdentity();

GL_QUADS

glTexCoord 1 0
glVertex 1 -1

glTexCoord 1 1
glVertex 1 1

glTexCoord 0 1
glVertex -1 1

glTexCoord 0 0
glVertex -1 -1


glPopMatrix(GL_PROJECTION);
glPopMatrix(GL_PROJECTION);



One beginner pitfall is that the default texture filtering parameter needs mipmaps, which is a good thing for both quality and performance, but if your forget that you have to generate mipmaps, your texture will only appear white.
To fix this, there are several ways, choose one (roughly sorted from best to worst) :

1-) Modern OpenGL :
call this right after glTexImage2D :
glGenerateMipmap(GL_TEXTURE_2D);

2-) classic GL :
glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );

3-) generate yourself each mipmap level, and feed each trough glTexImage2D.

4-) ... or disable mipmaps, but this is a shame