Results 1 to 5 of 5

Thread: Do offscreen render(openGL) with Qt5

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Do offscreen render(openGL) with Qt5

    can't you test your fbo instructions separately in another program? You may also have a look at my own fbo implementation sample (ignore the lighting part of the program):

    Qt Code:
    1. void GlWidget::paintGL()
    2. {
    3. BakeDynamicTexture();
    4.  
    5. // RenderShapes();
    6.  
    7. glClearColor(0, 0, 0.0f, 1.0f);
    8. // //! [2]
    9. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    10. glViewport(0, 0, width(),height());
    11.  
    12. QMatrix4x4 mMatrix;
    13. QMatrix4x4 vMatrix;
    14.  
    15. QMatrix4x4 cameraTransformation;
    16. cameraTransformation.rotate(alpha, 0, 1, 0);
    17. cameraTransformation.rotate(beta, 1, 0, 0);
    18.  
    19. QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    20. QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
    21. vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
    22.  
    23. QMatrix4x4 mvMatrix;
    24. mMatrix.setToIdentity();
    25.  
    26. mvMatrix = vMatrix * mMatrix;
    27.  
    28. QMatrix3x3 normalMatrix;
    29. normalMatrix = mvMatrix.normalMatrix();
    30.  
    31. QMatrix4x4 lightTransformation;
    32. lightTransformation.rotate(lightAngle, 0, 1, 0);
    33.  
    34. QVector3D lightPosition = lightTransformation * QVector3D(0, 1, 1);
    35.  
    36. //! [3]
    37. //##########################
    38. // lightingShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    39. // shpFill->RenderCube (&lightingShaderProgram, dynamicTexture);
    40. // lightingShaderProgram.release();
    41. //##########################
    42.  
    43. //! [5]
    44. lightingShaderProgram.bind();
    45.  
    46. lightingShaderProgram.setUniformValue("mvpMatrix", pMatrix * mvMatrix);
    47. lightingShaderProgram.setUniformValue("mvMatrix", mvMatrix);
    48. lightingShaderProgram.setUniformValue("normalMatrix", normalMatrix);
    49. lightingShaderProgram.setUniformValue("lightPosition", vMatrix * lightPosition);
    50.  
    51. shpFill->RenderLightingCube(&lightingShaderProgram, dynamicTexture);
    52.  
    53. mMatrix.setToIdentity();
    54. mMatrix.translate(lightPosition);
    55. mMatrix.rotate(lightAngle, 0, 1, 0);
    56. mMatrix.rotate(45, 1, 0, 0);
    57. mMatrix.scale(0.1);
    58.  
    59. coloringShaderProgram.bind();
    60. coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    61.  
    62. shpFill->colorLightingCube(&coloringShaderProgram);
    63. }
    To copy to clipboard, switch view to plain text mode 


    Baking part:
    Qt Code:
    1. void GlWidget::BakeDynamicTexture()
    2. {
    3. fbo->bind();
    4.  
    5. RenderShapes();
    6.  
    7. fbo->release();
    8.  
    9. dynamicTexture = fbo->texture();
    10. }
    To copy to clipboard, switch view to plain text mode 

    RenderShapes (instead of drawing a shape file here, simply draw something else, check if it draws successfully)
    Qt Code:
    1. void GlWidget::RenderShapes()
    2. {
    3. QMatrix4x4 sMatrix;
    4.  
    5. glClearColor(0.1f, 0.5f, 0.0f, 1.0f);
    6. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    7. glViewport(0, 0, 256,256);
    8.  
    9. shaderProgram.bind();
    10.  
    11. sMatrix.setToIdentity();
    12.  
    13. shaderProgram.setUniformValue("mvpMatrix", sMatrix);
    14.  
    15. shpFill->Render(&shaderProgram);
    16.  
    17. shaderProgram.release();
    18. }
    To copy to clipboard, switch view to plain text mode 

    RenderLightingCube:
    Qt Code:
    1. void FillShape::RenderLightingCube(QGLShaderProgram* lightingShaderProgram, GLuint cubeTexture)
    2. {
    3. lightingShaderProgram->setUniformValue("ambientColor", QColor(32, 32, 32));
    4. lightingShaderProgram->setUniformValue("diffuseColor", QColor(128, 128, 128));
    5. lightingShaderProgram->setUniformValue("specularColor", QColor(255, 255, 255));
    6. lightingShaderProgram->setUniformValue("ambientReflection", (GLfloat) 1.0);
    7. lightingShaderProgram->setUniformValue("diffuseReflection", (GLfloat) 1.0);
    8. lightingShaderProgram->setUniformValue("specularReflection", (GLfloat) 1.0);
    9. lightingShaderProgram->setUniformValue("shininess", (GLfloat) 100.0);
    10. lightingShaderProgram->setUniformValue("texture", 0);
    11.  
    12. glActiveTexture(GL_TEXTURE0);
    13. glBindTexture(GL_TEXTURE_2D, cubeTexture);
    14. glActiveTexture(0);
    15.  
    16. lightingShaderProgram->setAttributeArray("vertex", cubeVertices.constData());
    17. lightingShaderProgram->enableAttributeArray("vertex");
    18. lightingShaderProgram->setAttributeArray("normal", cubeNormals.constData());
    19. lightingShaderProgram->enableAttributeArray("normal");
    20. lightingShaderProgram->setAttributeArray("textureCoordinate", cubeTextureCoordinates.constData());
    21. lightingShaderProgram->enableAttributeArray("textureCoordinate");
    22.  
    23. glDrawArrays(GL_TRIANGLES, 0, cubeVertices.size());
    24.  
    25. lightingShaderProgram->disableAttributeArray("vertex");
    26. lightingShaderProgram->disableAttributeArray("normal");
    27. lightingShaderProgram->disableAttributeArray("textureCoordinate");
    28.  
    29. lightingShaderProgram->release();
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to saman_artorious for this useful post:

    stereoMatching (25th June 2013)

  3. #2
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Do offscreen render(openGL) with Qt5

    can't you test your fbo instructions separately in another program?
    Sorry but I don't get it, do you mean test it with different shader codes?

Similar Threads

  1. Render image on QLabel using openGl
    By vinodpaul in forum Newbie
    Replies: 2
    Last Post: 14th August 2012, 17:57
  2. QGLWidget render or QGraphicsView with GL viewport render
    By QTInfinity in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2011, 11:34
  3. OpenGL render to texture
    By Nicuvëo in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2011, 12:07
  4. Replies: 1
    Last Post: 7th May 2010, 17:20
  5. Drawing to an offscreen buffer
    By Dutch112 in forum Qt Programming
    Replies: 5
    Last Post: 6th May 2009, 16:49

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
  •  
Qt is a trademark of The Qt Company.