PDA

View Full Version : how to use glBindTexture?



hersheyzombie
30th August 2009, 07:58
Im trying to write a method that loads a texture from a filename into a GLuint textureID.

It seems like it is not binding the texture to the GLuint.

Here is my code:



#include "scene.h"

scene::scene(gameScreen* parent)
{
creator = parent;
loadTexture("./water.png", backgroundID);

width = creator->width();
height = creator->height();
}

void scene::resize(int w, int h)
{
width = w;
height = h;
}

void scene::draw()
{
glEnable(GL_TEXTURE_2D);
glColor3f(1,1,1);
glBindTexture( GL_TEXTURE_2D, backgroundID );

/*QImage im("./water.png");
QImage tex = QGLWidget::convertToGLFormat(im);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);*/

glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3d(0,0,-1);
glTexCoord2d(1,0);glVertex3d(width,0,-1);
glTexCoord2d(1,1);glVertex3d(width,height,-1);
glTexCoord2d(0,1);glVertex3d(0,height,-1);
glEnd();
glDisable(GL_TEXTURE_2D);
}

void scene::loadTexture (char *filename, GLuint &textureID)
{
glEnable(GL_TEXTURE_2D); // Enable texturing

glGenTextures(1, &textureID); // Obtain an id for the texture
glBindTexture(GL_TEXTURE_2D, textureID); // Set as the current texture

QImage im(filename);
QImage tex = QGLWidget::convertToGLFormat(im);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);

glDisable(GL_TEXTURE_2D);
}


If I uncomment the image loading and glTexImage2D part in draw(), then it works.
As is, I get a white rectangle.

Am I doing something wrong with the texture ID?

I thought that I should bind it in loadTexture() so that glTexImage2D affects the correct one, then when I bind it in draw() it will remember what it was textured with.

I have followed that logic in non-Qt applications and it worked, otherwise I would have posted this question to general programming.

Thanks.

hersheyzombie
30th August 2009, 16:32
I figured it out:

the call to the constructor for the scene was in the constructor of its parent (the GLwidget)

this meant that loadtexture() was being called before gl was initialized

i moved things around so loadtexture wound up happening during the parent's initializeGL function and then it worked