glBindTexture problem

Started by
3 comments, last by Kalidor 17 years, 8 months ago
I'm having trouble getting more than one texture to load. I'm now completely mystified, since I've searched right left and centre and I'm not seeing the behviour I am told I should. If I load two textures (code below) then anything that is drawn with GL_TEXTURE_2D enabled gets the second of the two textures. If I call glBindTexture AT ALL, then neither texture works and I get flat coloured triangles. I'm using PyOpenGL on windows XP. OpenGL setup stuff:

        map(glEnable,(GL_DEPTH_TEST, GL_NORMALIZE,
                       GL_BLEND))
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        self.ID =  self.loadImage("test.png")
        #self.ID2 = self.loadImage("texture.png")
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
        lghtamb=[0.1,0.1,0.1,1.0]
        lghtdif= [1.0,1.0,1.0,1.0]
        lpos =[ 1.0, 1.0, 0.0, 1.0 ]
        glLightfv(GL_LIGHT1, GL_AMBIENT, lghtamb)
        glLightfv(GL_LIGHT1, GL_DIFFUSE, lghtdif)
        glLightfv(GL_LIGHT1, GL_POSITION,lpos)
        glEnable(GL_LIGHT1)
        glEnable(GL_LIGHTING)
        glPointSize(5.0)
On of the loadImages is commented out, to make the first one work. Uncommenting it seems to overwrite the first one. loadImage function:

    def loadImage(self,imageName):
        im = open(imageName)
        try:
            ix, iy, image = im.size[0],
                            im.size[1], 
                            im.tostring("raw", "RGBA", 0, -1)
        except SystemError:
            ix, iy, image = im.size[0],
                            im.size[1],                              
                            im.tostring("raw", "RGBX", 0, -1)

        ID = glGenTextures(1)
        print "Allocated texture %d"%ID
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        glTexImage2D(GL_TEXTURE_2D, 0, 4,
                       ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
        return ID
(I can confirm that this works fine for a single texture load, with no call to glBindTexture.)

            glEnable(GL_TEXTURE_2D)
           # glBindTexture(GL_TEXTURE_2D,self.overlay)
            glDisable(GL_DEPTH_TEST)
            glDepthMask(GL_FALSE)
            glBegin(GL_QUADS)
            glTexCoord2f(0.0,0.0)
            glp((a[0]-12,a[1]-12,0))
            glTexCoord2f(1.0,0.0)
            glp((a[0]+12,a[1]-12,0))
            glTexCoord2f(1.0,1.0)
            glp((a[0]+12,a[1]+12,0))
            glTexCoord2f(0.0,1.0)
            glp((a[0]-12,a[1]+12,0))
            glEnd()
            glDepthMask(GL_TRUE)
            glEnable(GL_DEPTH_TEST)
            glDisable(GL_TEXTURE_2D)
One place where I want to use the texture. self.overlay takes the value of ID from the texture load. I have checked this. If I uncomment the glBindTexture, then neither texture works at all. I thought glBindTexture was supposed to tell OpenGL which texture to use, not switch it off!!? Can anyone see where I've gone wrong here?
Advertisement
You need to add a glBindTexture call in your loading code, like so -

    def loadImage(self,imageName):        im = open(imageName)        try:            ix, iy, image = im.size[0],                            im.size[1],                             im.tostring("raw", "RGBA", 0, -1)        except SystemError:            ix, iy, image = im.size[0],                            im.size[1],                                                          im.tostring("raw", "RGBX", 0, -1)        ID = glGenTextures(1)        print "Allocated texture %d"%ID        glBindTexture(GL_TEXTURE_2D, ID)        glPixelStorei(GL_UNPACK_ALIGNMENT,1)        glTexImage2D(GL_TEXTURE_2D, 0, 4,                       ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)        return ID


glBindTexture does a lot more than its creditted for, and glGenTextures does a lot less.

When you call glGenTextures a texture isn't actually generated, rather, OpenGL just hands you back an unused texture handle, and flags that number as used. You could bypass it altogether and make up your own numbers (but that would be reinventing the wheel). But essentially, all that first call does it hand you a number.

glBindTexture, on the other hand, actually allocates the texture when you hand it a texture handle which isn't allocated yet. So without that call, no textures are actually being allocated (thus defaulting to the NULL texture) and glTexImage2D since it operates on the current bound texture object. So you have to tell it which one to write to with the glBindTexture call.

I'm not sure it works when you never call glBindTexture though - the default texture object should be the NULL texture (ID 0), and passing 0 as the texture argument to glTexImage2D should make it just ignore the call... no idea why it actually works with 1 texture.

Anyway, try adding that glBindTexture call in there and see if that changes anything.
self.ID =  self.loadImage("test.png")#self.ID2 = self.loadImage("texture.png")glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)


You have to duplicate the texture parameters for both "test.png" and "texture.png".

self.ID =  self.loadImage("test.png")glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)self.ID2 = self.loadImage("texture.png")glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)


In your loadImage function, I would also bind the texture id.
Wow, thankyou for the quick responses... I'll give all this a try


EDIT:

Thanks, that sorted it!
Even though you have it working now, I'm going to explain what was happening before so that you (the reader, not just the OP) understand more about texturing in OpenGL.

In OpenGL 1.0 there was only one texture to be used, the default texture. You needed to upload a new image to the default texture via glTexImage* whenever you wanted to texture an object with a different image. Then came OpenGL 1.1 which introduced named texture objects. As Mushu said, glGenTextures simply returns some number of unused texture names, while glBindTexture creates/binds the texture object. The texture name 0 is reserved for the default 1D, 2D, 3D, and cube textures (and others defined in extensions, such as this). Calling glBindTexture with texture set to 0 simply binds the default texture of type target.

The glTexImage* and glTexParameter* functions act on the currently bound texture object. If you haven't called glBindTexture then the currently bound texture is the default texture.

So what was happening in your setup function here...
Quote: self.ID = self.loadImage("test.png")
#self.ID2 = self.loadImage("texture.png")
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
...when you weren't calling glBindTexture in the loadImage function was that you were loading test.png's image data into the default texture, then (assuming that second line was uncommented) overwriting that with texture.png's image data, then setting the default texture's parameters. That's why everything was being rendered using the second image.

The reason nothing seemed to work when you were only calling glBindTexture in the render function is again because you had only specified image data for the default texture and set its parameters, but then you were binding (creating) different textures and using them with the default texture parameters (default here meaning the texture parameters that are given to a newly created texture object, including the default mipmapping minification filter) and without specifying any image data.

I hope that helps clear up some thinking on how OpenGL texturing (and the current OpenGL object model in general) works.

PS: And because it is a common mistake I figure I'll mention this here too. While glTexImage* and glTexParameter* set per texture object state, glTexEnv* sets per texture unit state (the current texture unit is set with glActiveTexture).

This topic is closed to new replies.

Advertisement