Hello!
I am very new to OpenGL and have a basic problem: I want to render 2 quads each with its own texture.
First I have an init function which loads the texture, generates the texture IDs and uploads the texture:
void initTex(TexObj& texObj, const char* imageFilename) // Images are .xpm files
{
QImage image = QImage(imageFilename);
texObj.w = image.width();
texObj.h = image.height();
QImage glImage;
glImage = QGLWidget::convertToGLFormat(image);
char* data = static_cast<char*>( calloc( texObj.w * texObj.h, 4) );
memcpy(data, glImage.bits(), texObj.w * texObj.h * 4);
texObj.data = data
glGenTextures(1, &texObj.texID);
glBindTexture(GL_TEXTURE_2D, texObj.texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texObj.w, texObj.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texObj.data);
}
Function initTex is called 2 times (for 2 different textures).
To render the 2 quads I use this function:
static void draw(TexObj& tex, float scale)
{
glEnable(GL_CULL_FACE);
glEnable( GL_TEXTURE_2D );
glFrontFace( GL_CCW );
glActiveTexture( GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, tex.texID);
// glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, tex->w, tex->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->data );
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ;
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ;
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ;
glTranslated( 0.0, 0.0, 0.0 );
glScalef( scale, scale, 1.0f );
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_POLYGON);
glTexCoord2f( 0.0, 0.0 );
glVertex2d(0.0, 1.0);
glTexCoord2f( 0.0, -1.0 );
glVertex2d(0.0, 0.0);
glTexCoord2f( 1.0, -1.0 );
glVertex2d(1.0, 0.0);
glTexCoord2f( 1.0, 0.0 );
glVertex2d(1.0, 1.0);
glEnd();
glDisable( GL_TEXTURE_2D );
glDisable(GL_CULL_FACE);
}
The problem is: When I let this code run the quads are completely white!
The strange thing: When I comment in the outcommented line (glTexImage2D), the quads are textured! It seems like as if the texture I load into the VRAM in function init() is disappeared and I have to upload it every frame.
Does anyone have an idea what could cause this?
Thanks!