Sign in to follow this  
0beron

OpenGL glBindTexture problem

Recommended Posts

0beron    122
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?

Share this post


Link to post
Share on other sites
Mushu    1396
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.

Share this post


Link to post
Share on other sites
Deception666    182

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.

Share this post


Link to post
Share on other sites
Kalidor    1087
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now