Results 1 to 14 of 14

Thread: qml porting changes the opengl scene placement

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default qml porting changes the opengl scene placement

    Hello forum,

    I had a simple opengl triangle application . Check the screen-shot:

    http://imgur.com/ZNO7bcu

    When I port the same application to qml following the documentation - "Scene Graph - OpenGL Under QML", I do get the triangle as an underlay, but the placement of the triangle changed as follows:

    http://imgur.com/6NVKDnC


    The basic OpenGL is exactly same as the original one . Thecode sippet that shapes the triangle is as follows:

    Qt Code:
    1. //setup triangle geometry
    2. //setup triangle vertices
    3. vertices[0].color=glm::vec3(1,0,0);
    4. vertices[1].color=glm::vec3(0,1,0);
    5. vertices[2].color=glm::vec3(0,0,1);
    6.  
    7. vertices[0].position=glm::vec3(-1,-1,0);
    8. vertices[1].position=glm::vec3(0,1,0);
    9. vertices[2].position=glm::vec3(1,-1,0);
    10.  
    11. //setup triangle indices
    12. indices[0] = 0;
    13. indices[1] = 1;
    14. indices[2] = 2;
    To copy to clipboard, switch view to plain text mode 

    I am getting different placement of the triangle with qml where opengl is underlay.

    Any thoughts?


    Thanks

  2. #2
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    No reply so far. Here goes the link to the source which you can download and run at your end.

    https://sajis997@bitbucket.org/sajis...anglescene.git

    Then you can compare with the screen-shot i sent in my first post and provide me with some hint.


    Thanks

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml porting changes the opengl scene placement

    Do you remember to initialize and reset the GL environment every frame as mentioned in the docs (QQuickWindow::​resetOpenGLState())?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    I believe I am releasing the binding resources after every render call explicitly:

    Qt Code:
    1. void TriangleScene::render()
    2. {
    3. ..................................
    4. .................................
    5. //draw the triangle
    6. glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,0);
    7.  
    8. glDisableVertexAttribArray(mTriangleShader->getAttribute("vVertex"));
    9. glDisableVertexAttribArray(mTriangleShader->getAttribute("vColor"));
    10. glBindVertexArray(0);
    11.  
    12. mTriangleShader->UnUse();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Anyway , i tried as you suggested and it did not change the behavior. I hope that you skimmed through the source that I provided you with. I called the function in the following manner:

    Qt Code:
    1. void TriangleSceneItem::handleWidowChanged(QQuickWindow *win)
    2. {
    3. if(win)
    4. {
    5. QSurfaceFormat f = win->format();
    6. f.setMajorVersion(4);
    7. f.setMinorVersion(3);
    8. f.setSamples(4);
    9. f.setStencilBufferSize(8);
    10. f.setProfile(QSurfaceFormat::CompatibilityProfile);
    11.  
    12. win->setFormat(f);
    13.  
    14. win->setClearBeforeRendering(false);
    15.  
    16. connect(win,SIGNAL(beforeSynchronizing()),this,SLOT(sync()),Qt::DirectConnection);
    17. connect(win,SIGNAL(sceneGraphInvalidated()),this,SLOT(cleanup()),Qt::DirectConnection);
    18. connect(win,SIGNAL(beforeRendering()),mScene,SLOT(paint()),Qt::DirectConnection);
    19.  
    20. win->resetOpenGLState();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml porting changes the opengl scene placement

    You are not preparing the viewport before rendering thus you are using the one from QtQuick which is probably not what you wnat.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    I am setting the viewport in the following function and it is called before the render call. I have checked it with the debugger.

    Qt Code:
    1. void TriangleScene::paint()
    2. {
    3. if(!mInitialized)
    4. initialise();
    5.  
    6. resize(mWinWidth,mWinHeight);
    7. render();
    8. }
    9.  
    10.  
    11. void TriangleScene::resize(int w, int h)
    12. {
    13. if(h < 1)
    14. h = 1;
    15.  
    16. mWinWidth = w;
    17. mWinHeight = h;
    18.  
    19. glViewport(0,0,(GLsizei)mWinWidth,(GLsizei)mWinHeight);
    20.  
    21. mProjectionMatrix = glm::ortho(-1,1,-1,1);
    22. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml porting changes the opengl scene placement

    So you are calling resize passing in the values you are setting in the function? Are you sure this is correct?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    Quote Originally Posted by wysota View Post
    So you are calling resize passing in the values you are setting in the function? Are you sure this is correct?
    Which function are you pointing at ? is that paint() ? The mWinWidth , mWinHeight are initialized before with the following snippet:

    Qt Code:
    1. void TriangleSceneItem::sync()
    2. {
    3. if(mScene)
    4. {
    5. QTimer *timer = new QTimer();
    6. connect(timer,SIGNAL(timeout()),this,SLOT(updateTime()),Qt::DirectConnection);
    7. timer->start();
    8.  
    9. mScene->setViewportSize(window()->width() * window()->devicePixelRatio(),
    10. window()->height() * window()->devicePixelRatio());
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    You can see that the viewport setting(setViewportSize(....)) is called with every signal :

    Qt Code:
    1. connect(win,SIGNAL(beforeSynchronizing()),this,SLOT(sync()),Qt::DirectConnection);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml porting changes the opengl scene placement

    I am referring to resize().

    Your sync() leaks memory.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    Quote Originally Posted by wysota View Post
    So you are calling resize passing in the values you are setting in the function? Are you sure this is correct?
    Could you point out what is wrong as long as the width and height are already set during sync().

    Thanks for pointing it out. I thought that it is de-allocated automatically since the class is a QObject subclass. Anyway I made the necessay changes; made the timer member variable of the class and doing the connection there instead of initializing with each and every call of sync(); But it did not change the behavior either.

    Where else should I look into ?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml porting changes the opengl scene placement

    Quote Originally Posted by sajis997 View Post
    Could you point out what is wrong as long as the width and height are already set during sync().
    Just look at your code, you should spot it right away. Look at how mWinHeight and mWinWidth are used.

    Thanks for pointing it out. I thought that it is de-allocated automatically since the class is a QObject subclass.
    It has no parent so no deallocation will take place.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    I am not doing much of different that what is described in http://doc.qt.io/qt-5/qtquick-sceneg...l-example.html . Lets compare the sync() there and mine:

    Qt Code:
    1. void Squircle::sync()
    2. {
    3. if (!m_renderer) {
    4. m_renderer = new SquircleRenderer();
    5. connect(window(), SIGNAL(beforeRendering()), m_renderer, SLOT(paint()), Qt::DirectConnection);
    6. }
    7. m_renderer->setViewportSize(window()->size() * window()->devicePixelRatio());
    8. m_renderer->setT(m_t);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Whereas I am doing the following:

    Qt Code:
    1. void TriangleSceneItem::sync()
    2. {
    3. if(mScene)
    4. {
    5. connect(mTimer,SIGNAL(timeout()),this,SLOT(updateTime()),Qt::DirectConnection);
    6.  
    7. mScene->setViewportSize(window()->width() * window()->devicePixelRatio(),
    8. window()->height() * window()->devicePixelRatio());
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    I am creating the timer for the animation and then setting the viewport size that is lated used during rendering. The renderer intialization is done inside the constructor.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml porting changes the opengl scene placement

    As I said, look at the code of resize, not sync. By the way, what does initialize() do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qml porting changes the opengl scene placement

    This what TriangleScene::initialize() do :

    Qt Code:
    1. void TriangleScene::initialise()
    2. {
    3. //return if already initialized
    4. if(mInitialized)
    5. return;
    6.  
    7. if(!initializeOpenGLFunctions())
    8. {
    9. std::cerr << "Modern OpenGL Functions could not be initialized." << std::endl;
    10. std::exit(EXIT_FAILURE);
    11. }
    12.  
    13. GL_CHECK_ERRORS;
    14.  
    15. //make the projection and model-view matrix
    16. //identity matrices
    17. mProjectionMatrix = glm::mat4(1.0f);
    18. mModelViewMatrix = glm::mat4(1.0f);
    19.  
    20. loadShaders();
    21.  
    22. //setup the triangle geometry
    23. mVertices[0].color = glm::vec3(1.0,0.0,0.0);
    24. mVertices[1].color = glm::vec3(0.0,1.0,0.0);
    25. mVertices[2].color = glm::vec3(0.0,0.0,1.0);
    26.  
    27. mVertices[0].position = glm::vec3(-1,-1,0);
    28. mVertices[1].position = glm::vec3(0,1,0);
    29. mVertices[2].position = glm::vec3(1,-1,0);
    30.  
    31. //setup the triangle indices
    32. mIndices[0] = 0;
    33. mIndices[1] = 1;
    34. mIndices[2] = 2;
    35.  
    36. //check for opengl errors
    37. GL_CHECK_ERRORS;
    38.  
    39. //setup the triangle vao and vbo stuff
    40. glGenVertexArrays(1,&mVaoID);
    41.  
    42. glGenBuffers(1,&mVerticesID);
    43. glGenBuffers(1,&mIndicesID);
    44.  
    45. //get the size of the Vertex struct
    46. GLsizei stride = sizeof(Vertex);
    47.  
    48. glBindVertexArray(mVaoID);
    49.  
    50. glBindBuffer(GL_ARRAY_BUFFER,mVerticesID);
    51. //pass triangle vertices to the buffer object
    52. glBufferData(GL_ARRAY_BUFFER, //specifies the target buffer object
    53. sizeof(mVertices),//specifies the size in bytes
    54. &mVertices[0], // pointer to the data
    55. GL_STATIC_DRAW); //usage pattern of the data
    56.  
    57. GL_CHECK_ERRORS;
    58.  
    59. glEnableVertexAttribArray(mTriangleShader->getAttribute("vVertex"));
    60. glVertexAttribPointer(mTriangleShader->getAttribute("vVertex"),3,GL_FLOAT,GL_FALSE,stride,0);
    61.  
    62. GL_CHECK_ERRORS;
    63.  
    64. //enable vertex attribute array for the color
    65. glEnableVertexAttribArray(mTriangleShader->getAttribute("vColor"));
    66. glVertexAttribPointer(mTriangleShader->getAttribute("vColor"),3,GL_FLOAT,GL_FALSE,stride,(const GLvoid*)offsetof(Vertex,color));
    67.  
    68. GL_CHECK_ERRORS;
    69.  
    70. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mIndicesID);
    71. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(mIndices),&mIndices[0],GL_STATIC_DRAW);
    72.  
    73. glDisableVertexAttribArray(mTriangleShader->getAttribute("vVertex"));
    74. glDisableVertexAttribArray(mTriangleShader->getAttribute("vColor"));
    75. glBindVertexArray(0);
    76.  
    77. GL_CHECK_ERRORS;
    78.  
    79. mInitialized = true;
    80.  
    81. }
    82.  
    83. I think the issue lies here and I am examining it right now. Any hint from your end within the function will accelerate the process.
    84.  
    85.  
    86. Thanks
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 8th October 2013, 20:54
  2. Current webcam image behind an OpenGL Scene
    By vrcat in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2009, 15:26
  3. placement new question
    By Teerayoot in forum General Programming
    Replies: 3
    Last Post: 23rd May 2007, 23:13
  4. QTabWidget Placement and Display
    By mclark in forum Newbie
    Replies: 1
    Last Post: 16th January 2007, 21:34
  5. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 16:38

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.