Results 1 to 14 of 14

Thread: Problems with shaders and their context's

  1. #1
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems with shaders and their context's

    Hi!
    I am doing an application that renders different views of an object. I do the rendering of the objects i have in a modified QGLWidget(glWidget). So to have different views of the objects that i render, i instantiate glWidget X Times with different id's.
    Getting to the point glWidget has a shader, that does phong shading and the problem is, that when i run more than one view(more than one instantiation of glWidget), only the first glWidget shows the correct shader. The other 3 give a warning message:
    QGLShaderProgram::bind: program is not valid in the current context.

    Here is how i instantiate glWidget:
    Qt Code:
    1. GLWidget *topleft = new GLWidget(1);
    2. GLWidget *topright = new GLWidget(2);
    3. GLWidget *botleft = new GLWidget(3);
    4. GLWidget *botright = new GLWidget(4);
    To copy to clipboard, switch view to plain text mode 

    And here is how i create the shader program:
    Qt Code:
    1. shaderProgram = new QGLShaderProgram(context());
    To copy to clipboard, switch view to plain text mode 

    So every instantiation of GLWidget should have a different context and (because its a different object) a different shader, so it should work?

    Can someone see the problem, or how it is to be solved?

    Thanks,
    Elbe

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    Have you tried to call makeCurrent() before creating shader ?

  3. #3
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with shaders and their context's

    If i put this for the widget itself, with all the id's, it doesnt work at all.
    If i just put it for widget with id 1 it works only for id one. As before.
    Last edited by Elberion; 21st June 2011 at 00:43.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    Can you post some code ? Where are the shaders created, in the initializeGL() method ?
    Does it work if you use different shader for each widget ?

  5. #5
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with shaders and their context's

    Shader is created in inicializeGL funktion:
    Qt Code:
    1. shaderProgram = new QGLShaderProgram(context());
    2.  
    3. shaderProgram->addShaderFromSourceFile(QGLShader::Vertex,"../phong.vert");
    4. shaderProgram->addShaderFromSourceFile(QGLShader::Fragment,"../phong.frag");
    5. shaderProgram->link();
    To copy to clipboard, switch view to plain text mode 
    shaderProgram is a private QGLShaderProgram.
    This is what every instantiation of GLWidget calls.

    I also have buttons to change between visualisations. Here the slots(in GLWidget)
    Qt Code:
    1. void GLWidget::setWireframe()
    2. {
    3. shaderProgram->release();
    4. //glEnable(GL_LIGHTING);
    5. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    6. activateShader =false;
    7. scene->activateShader(currentPrimitive,false);
    8. updateGL();
    9. }
    10. void GLWidget::setFlat()
    11. {
    12. shaderProgram->release();
    13. //glEnable(GL_LIGHTING);
    14. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    15. glShadeModel(GL_FLAT);
    16. activateShader =false;
    17. scene->activateShader(currentPrimitive,false);
    18. updateGL();
    19. }
    20.  
    21. void GLWidget::setGouraud()
    22. {
    23. shaderProgram->release();
    24. //glEnable(GL_LIGHTING);
    25. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    26. glShadeModel(GL_SMOOTH);
    27. activateShader =false;
    28. scene->activateShader(currentPrimitive,false);
    29. updateGL();
    30. }
    31.  
    32. void GLWidget::setPhong()
    33. {
    34. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    35. shaderProgram->bind();
    36. activateShader =true;
    37. scene->activateShader(currentPrimitive,true);
    38. updateGL();
    39. }
    To copy to clipboard, switch view to plain text mode 
    So these are the bind that through the Warning:
    QGLShaderProgram::bind: program is not valid in the current context.

    What do you mean with different shader for each widget?

    Thanks very much for answering!
    Elbe

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    Never mind, I thought that the shader is somehow shared, my mistake.
    Try calling makeCurrent() at the beginning of each setWireFrame, setFlat etc. method, maybe the first opengl context stays current the whole time.

  7. #7
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with shaders and their context's

    Stays the same, the first instantiation of glWidget show the shader as it has to be, the other three just give the wrong context warning message.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    Ok, and how is the activateShader implemented ?

  9. #9
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with shaders and their context's

    It is just setting a private boolean to true or to false depending on if we want to use the shader or not.
    So if this boolean is true, in an object render function i send a uniform value to the shader, that is basicaly the color.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    I see that you release the shader, but can you post the code where you actually bind it ?

  11. #11
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with shaders and their context's

    Qt Code:
    1. void GLWidget::setPhong()
    2. {
    3. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    4. shaderProgram->bind();
    5. activateShader =true;
    6. scene->activateShader(currentPrimitive,true);
    7. updateGL();
    8. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    Oh yes, I haven't noticed that. So when is setPhong called ? Is makeCurrent() called before calling shader->bind() ?
    I was little confused, I thought that you are setting shaders in setWireFrame, setGouraud as well. If setPhong is the only place where you call bind(), make sure you made OpenGL context current before binding the shader.
    edit:
    makeCurrent should be called explicitly before any method that does OpenGL calls, except for initializeGL, updateGL and renderGL, so makeCurrent() at first line of setPhong().

  13. #13
    Join Date
    May 2011
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with shaders and their context's

    Thank you very much, that was it!
    It was all about setting that makeCurrent in the right place!
    Thanks again!

  14. #14
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems with shaders and their context's

    Great, no problem

Similar Threads

  1. QT4.7 & QGLFormat::CoreProfile & shaders
    By saraksh in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2010, 22:04
  2. Replies: 0
    Last Post: 10th November 2010, 13:56
  3. OpenGL shaders and x11 forwarding
    By Wali in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2010, 21:38
  4. 4.5.2 =>4.6.3 OpenGL shaders+qrphicsview issue
    By medved6 in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2010, 19:33
  5. GLSL shaders, multitexturing and Qt
    By jcox23 in forum Qt Programming
    Replies: 0
    Last Post: 20th January 2010, 19:37

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.