Sign in to follow this  
0beron

OpenGL glBindTexture problem

Recommended Posts

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
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

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
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  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627643
    • Total Posts
      2978363
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
       
      References:
      Code: https://pastebin.com/Hcshj3FQ
      The video shows the difference between blender and my rotation:
       
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
       
    • By test opty
      Hello all,
       
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now