Results 1 to 3 of 3

Thread: How to share textures between QGLWidgets

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to share textures between QGLWidgets

    I am currently having problems sharing texture objects between several (currently two) different QGLWidgets. I first create a custom QGLWidget and I override its initializeGL() function and do texture initialization in there. Here is the following code:

    Qt Code:
    1. SharedGLWidget::SharedGLWidget(QWidget *parent) :
    2. QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::Rgba | QGL::DepthBuffer), parent)
    3. {
    4. }
    5.  
    6. void SharedGLWidget::initializeGL()
    7. {
    8. GLbyte *pBytes;
    9. GLint iWidth, iHeight, iComponents;
    10. GLenum eFormat;
    11.  
    12. // Load texture
    13. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    14. pBytes = gltLoadTGA("../SharedTest/stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);
    15. glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
    16. free(pBytes);
    17.  
    18. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    19. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    20. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    21. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    22.  
    23. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    24. glEnable(GL_TEXTURE_2D);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Then I create a second QGLWidget and do normal Qt rendering stuff with the exception of loading the textures again.

    Qt Code:
    1. GLWidget::GLWidget(QWidget *parent, const QGLWidget *sharedWidget, Qt::WindowFlags f)
    2. : QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::Rgba | QGL::DepthBuffer), parent, sharedWidget, f)
    3. , xRot(0.0f)
    4. , yRot(0.0f)
    5. {
    6. }
    7.  
    8. void GLWidget::initializeGL()
    9. {
    10. // Light values and coordinates
    11. GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
    12. GLfloat sourceLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
    13. GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
    14.  
    15. glEnable(GL_DEPTH_TEST); // Hidden surface removal
    16. glFrontFace(GL_CCW); // Counter clock-wise polygons face out
    17. glEnable(GL_CULL_FACE); // Do not calculate inside of jet
    18.  
    19. // Enable lighting
    20. glEnable(GL_LIGHTING);
    21.  
    22. // Setup and enable light 0
    23. glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
    24. glLightfv(GL_LIGHT0,GL_AMBIENT,sourceLight);
    25. glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
    26. glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
    27. glEnable(GL_LIGHT0);
    28.  
    29. // Enable color tracking
    30. glEnable(GL_COLOR_MATERIAL);
    31.  
    32. // Set Material properties to follow glColor values
    33. glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    34.  
    35. // Black blue background
    36. glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    37.  
    38. glEnable(GL_TEXTURE_2D);
    39. }
    40.  
    41. void GLWidget::paintGL()
    42. {
    43. M3DVector3f vNormal;
    44. M3DVector3f vCorners[5] = { { 0.0f, .80f, 0.0f }, // Top 0
    45. { -0.5f, 0.0f, -.50f }, // Back left 1
    46. { 0.5f, 0.0f, -0.50f }, // Back right 2
    47. { 0.5f, 0.0f, 0.5f }, // Front right 3
    48. { -0.5f, 0.0f, 0.5f }}; // Front left 4
    49.  
    50. // Clear the window with current clearing color
    51. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    52.  
    53. // Save the matrix state and do the rotations
    54. glPushMatrix();
    55. // Move object back and do in place rotation
    56. glTranslatef(0.0f, -0.25f, -4.0f);
    57. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    58. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    59.  
    60. // Draw the Pyramid
    61. glColor3f(1.0f, 1.0f, 1.0f);
    62. glBegin(GL_TRIANGLES);
    63. // Bottom section - two triangles
    64. glNormal3f(0.0f, -1.0f, 0.0f);
    65. glTexCoord2f(1.0f, 1.0f);
    66. glVertex3fv(vCorners[2]);
    67.  
    68. glTexCoord2f(0.0f, 0.0f);
    69. glVertex3fv(vCorners[4]);
    70.  
    71. glTexCoord2f(0.0f, 1.0f);
    72. glVertex3fv(vCorners[1]);
    73.  
    74.  
    75. glTexCoord2f(1.0f, 1.0f);
    76. glVertex3fv(vCorners[2]);
    77.  
    78. glTexCoord2f(1.0f, 0.0f);
    79. glVertex3fv(vCorners[3]);
    80.  
    81. glTexCoord2f(0.0f, 0.0f);
    82. glVertex3fv(vCorners[4]);
    83.  
    84. // Front Face
    85. m3dFindNormal(vNormal, vCorners[0], vCorners[4], vCorners[3]);
    86. glNormal3fv(vNormal);
    87. glTexCoord2f(0.5f, 1.0f);
    88. glVertex3fv(vCorners[0]);
    89. glTexCoord2f(0.0f, 0.0f);
    90. glVertex3fv(vCorners[4]);
    91. glTexCoord2f(1.0f, 0.0f);
    92. glVertex3fv(vCorners[3]);
    93.  
    94. // Left Face
    95. m3dFindNormal(vNormal, vCorners[0], vCorners[1], vCorners[4]);
    96. glNormal3fv(vNormal);
    97. glTexCoord2f(0.5f, 1.0f);
    98. glVertex3fv(vCorners[0]);
    99. glTexCoord2f(0.0f, 0.0f);
    100. glVertex3fv(vCorners[1]);
    101. glTexCoord2f(1.0f, 0.0f);
    102. glVertex3fv(vCorners[4]);
    103.  
    104. // Back Face
    105. m3dFindNormal(vNormal, vCorners[0], vCorners[2], vCorners[1]);
    106. glNormal3fv(vNormal);
    107. glTexCoord2f(0.5f, 1.0f);
    108. glVertex3fv(vCorners[0]);
    109.  
    110. glTexCoord2f(0.0f, 0.0f);
    111. glVertex3fv(vCorners[2]);
    112.  
    113. glTexCoord2f(1.0f, 0.0f);
    114. glVertex3fv(vCorners[1]);
    115.  
    116. // Right Face
    117. m3dFindNormal(vNormal, vCorners[0], vCorners[3], vCorners[2]);
    118. glNormal3fv(vNormal);
    119. glTexCoord2f(0.5f, 1.0f);
    120. glVertex3fv(vCorners[0]);
    121. glTexCoord2f(0.0f, 0.0f);
    122. glVertex3fv(vCorners[3]);
    123. glTexCoord2f(1.0f, 0.0f);
    124. glVertex3fv(vCorners[2]);
    125. glEnd();
    126.  
    127.  
    128. // Restore the matrix state
    129. glPopMatrix();
    130.  
    131. // Buffer swap
    132. swapBuffers();
    133. }
    To copy to clipboard, switch view to plain text mode 

    In my main.cpp I pass the first QGLWidget with just the texture initialization as the sharedWidget for the second QGLWidget:

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. SharedGLWidget* glwidget1 = new SharedGLWidget;
    6.  
    7. GLWidget* widget = new GLWidget(0, glwidget1);
    8. widget->show();
    9.  
    10. app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I am not sure what I am missing but when widget is shown the texture isn't showing up (the pyramid is drawn but with no texturing). I am wondering what I am missing and if anyone has any idea as to how I can do this. I am only creating one texture object thus I was trying to not use glGenBuffers for texture objects but if this is something that has to be done I will try to do it while I wait for some responses. I am curious if I do go that route, how the glBind call would be preformed in the rendering widget. Thanks for any help!

  2. #2
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to share textures between QGLWidgets

    I did find one potential issue, but it didn't resolve my problem. I moved all of the initializeGL() calls from the shared widget to the constructor because I forgot that initializeGL() is only called before paintGL is called and not called at all if sharedwidget->show() isn't called. However this didn't solve my problem

  3. #3
    Join Date
    Apr 2015
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to share textures between QGLWidgets

    You can share texture object use GLwidget construct function ,
    QGLWidget::QGLWidget ( QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WindowFlags f = 0 ),it will share texture object with shareWidget.But I want share texture object with multi qglwidget ,

Similar Threads

  1. QGLWidgets + QAXWidget
    By Pablo in forum Newbie
    Replies: 0
    Last Post: 22nd January 2010, 15:03
  2. QSettings and QGLWidgets
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2008, 19:01
  3. Share a 3D texture in four QGLWidgets
    By jp-deivis in forum Qt Programming
    Replies: 6
    Last Post: 21st October 2007, 20:44
  4. Textures + Multiple QGLWidgets
    By KShots in forum Qt Programming
    Replies: 8
    Last Post: 19th October 2007, 22:23
  5. QGLWidgets
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 21st August 2006, 23:18

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.