Results 1 to 2 of 2

Thread: how to use glBindTexture?

  1. #1
    Join Date
    Jul 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default how to use glBindTexture?

    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:

    Qt Code:
    1. #include "scene.h"
    2.  
    3. scene::scene(gameScreen* parent)
    4. {
    5. creator = parent;
    6. loadTexture("./water.png", backgroundID);
    7.  
    8. width = creator->width();
    9. height = creator->height();
    10. }
    11.  
    12. void scene::resize(int w, int h)
    13. {
    14. width = w;
    15. height = h;
    16. }
    17.  
    18. void scene::draw()
    19. {
    20. glEnable(GL_TEXTURE_2D);
    21. glColor3f(1,1,1);
    22. glBindTexture( GL_TEXTURE_2D, backgroundID );
    23.  
    24. /*QImage im("./water.png");
    25.   QImage tex = QGLWidget::convertToGLFormat(im);
    26.   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
    27.   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    28.   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);*/
    29.  
    30. glBegin(GL_QUADS);
    31. glTexCoord2d(0,0);glVertex3d(0,0,-1);
    32. glTexCoord2d(1,0);glVertex3d(width,0,-1);
    33. glTexCoord2d(1,1);glVertex3d(width,height,-1);
    34. glTexCoord2d(0,1);glVertex3d(0,height,-1);
    35. glEnd();
    36. glDisable(GL_TEXTURE_2D);
    37. }
    38.  
    39. void scene::loadTexture (char *filename, GLuint &textureID)
    40. {
    41. glEnable(GL_TEXTURE_2D); // Enable texturing
    42.  
    43. glGenTextures(1, &textureID); // Obtain an id for the texture
    44. glBindTexture(GL_TEXTURE_2D, textureID); // Set as the current texture
    45.  
    46. QImage im(filename);
    47. QImage tex = QGLWidget::convertToGLFormat(im);
    48. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
    49. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    50. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    51.  
    52. glDisable(GL_TEXTURE_2D);
    53. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jul 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to use glBindTexture?

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.