Results 1 to 2 of 2

Thread: Tiling with OpenGL and textures

  1. #1
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Tiling with OpenGL and textures

    I try to make a wall of pictures (no overlapping).
    For this case I made simple proof of concept example code, but I encounterd one major drawback:
    This solution renders ONLY the last texture and replaces all previuosly loaded ons with that one.
    (Code compiles just fine)

    Qt Code:
    1. #include <QtCore>
    2. #include <QImage>
    3. #include <QApplication>
    4. #include <QtOpenGL>
    5. #include <QtDebug>
    6.  
    7. class Tile {
    8. public:
    9. Tile(GLfloat x, GLfloat y, QString path) : gl_x(x), gl_y(y) {
    10. QImage t;
    11. QImage b;
    12. b.load(path);
    13. t = QGLWidget::convertToGLFormat( b );
    14. glGenTextures( 1, &texid );
    15. qDebug() << "texid tile = " << texid;
    16. glBindTexture( GL_TEXTURE_2D, texid );
    17. glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() );
    18. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    19. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    20. }
    21. virtual ~Tile(){
    22. glDeleteTextures(1,&texid);
    23. }
    24. void draw(){
    25. glPushMatrix();
    26. glTranslatef(64.0f * gl_x, 64.0f * gl_y, 0.0f); // just to avoid total overlapping
    27. glBindTexture(1,texid);
    28. qDebug() << "draw texid" << texid;
    29. glBegin(GL_QUADS);
    30. glTexCoord2f(1.0f,1.0f);
    31. glVertex2f(0.0f,0.0f);
    32.  
    33. glTexCoord2f(1.0f,0.0f);
    34. glVertex2f(0.0f,128.0f);
    35.  
    36. glTexCoord2f(0.0f,0.0f);
    37. glVertex2f(128.0f,128.0f);
    38.  
    39. glTexCoord2f(0.0f,1.0f);
    40. glVertex2f(128.0f,0.0f);
    41. glEnd();
    42. glPopMatrix();
    43. }
    44. private:
    45. GLfloat gl_x;
    46. GLfloat gl_y;
    47. GLuint texid;
    48.  
    49. };
    50.  
    51.  
    52. // THE GL WIDGET CLASS
    53. class GLTest : public QGLWidget
    54. {
    55. public:
    56. GLTest(QWidget* parent = 0);
    57. virtual ~GLTest();
    58. protected:
    59. void initializeGL();
    60. void resizeGL(int w, int h);
    61. void paintGL();
    62.  
    63. QList<Tile*> tiles;
    64. };
    65.  
    66. GLTest::GLTest(QWidget* parent) : QGLWidget(parent)
    67. {
    68.  
    69. }
    70. GLTest::~GLTest()
    71. {
    72. for (QList<Tile*>::iterator i = tiles.begin(); i!=tiles.end(); ++i)
    73. {
    74. delete (*i);
    75. }
    76. }
    77.  
    78. void GLTest::initializeGL()
    79. {
    80. glClearColor(0.5f,0.5f,0.5f,1.0f);
    81. glEnable(GL_TEXTURE_2D);
    82. qDebug() << "init GL ";
    83. tiles.append(new Tile(0.0f, 2.0f, QString("./images/side1.png")));
    84. tiles.append(new Tile(1.0f, 3.0f, QString("./images/side2.png")));
    85. tiles.append(new Tile(3.0f, 4.0f, QString("./images/side4.png")));
    86.  
    87. }
    88.  
    89. void GLTest::resizeGL(int w, int h)
    90. {
    91.  
    92. glMatrixMode (GL_PROJECTION);
    93. glLoadIdentity ();
    94. glOrtho(0, w, h, 0, -1, 1);
    95. glMatrixMode(GL_MODELVIEW);
    96. glViewport(0,0,w, h);
    97. glLoadIdentity();
    98. }
    99.  
    100. void GLTest::paintGL()
    101. {
    102.  
    103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    104. glLoadIdentity();
    105. glColor3f(1.0f,1.0f,1.0f);
    106.  
    107. for (QList<Tile*>::iterator i = tiles.begin(); i!=tiles.end(); ++i)
    108. {
    109. (*i)->draw();
    110. }
    111.  
    112. }
    113.  
    114. int main(int argc, char** argv)
    115. {
    116. QApplication app(argc, argv);
    117. GLTest gltest;
    118. gltest.show();
    119. return app.exec();
    120. }
    To copy to clipboard, switch view to plain text mode 

    Thx for any help and suggestions.

  2. #2
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Tiling with OpenGL and textures

    Replace line 27 with
    Qt Code:
    1. glBindTexture(GL_TEXTURE_"D,texid);
    To copy to clipboard, switch view to plain text mode 
    and it works.. this nasty thingy made me hicup 2 days -.-

Similar Threads

  1. QGraphicsItem and OpenGL textures
    By Aldebaran in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2010, 08:37
  2. OpenGL Textures + Rotating
    By aleksgidenko in forum Qt Programming
    Replies: 9
    Last Post: 21st August 2009, 16:57
  3. OpenGl textures in Qt
    By Ashitaka in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2006, 15:45
  4. Opengl - Textures - Qt
    By Alex63 in forum Installation and Deployment
    Replies: 1
    Last Post: 29th June 2006, 09:32

Tags for this Thread

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.