Results 1 to 8 of 8

Thread: Setting up OpenGL shaders with QGLWidget

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Setting up OpenGL shaders with QGLWidget

    The title isn't really the question, but it's the best I could do to summarize my problem.

    I have a Qt program with a child of QGLWidget and everything is working just fine with OpenGL 3.3. The one problem I'm getting is that all the meshes I create are transparent even though their alpha values are set to 1 (remembering that OpenGL works on 0-1, not 0-255). I've played around with the OpenGL commands but can't get it to work. This all sounds and feels like its more of an OpenGL problem than a Qt problem, but I'm not being able to get much help from the people at the opengl.org forums and so, thinking that maybe I was missing something Qt-related, I returned to the documentation and found the following.

    In the QGLWidget docs, there's a little example of how a child should look like, and it contains:
    Qt Code:
    1. glEnable(GL_DEPTH_TEST);
    To copy to clipboard, switch view to plain text mode 
    However, the QGLFormat::depth() docs say that depth-testing is active by default. So why does QGLWidget enable it?

    In any case, here is my code attempting to set these things up.
    Qt Code:
    1. //fragment shader
    2. #version 330
    3. precision highp float;
    4.  
    5. uniform vec3 EyeAxis = vec3(0.0,0.0,1.0);
    6. uniform mat4 Modelview;
    7. uniform mat4 Projection;
    8. in vec3 ex_Color;
    9. in vec4 ex_Normal;
    10. out vec4 FragColor;
    11.  
    12. void main(void)
    13. {
    14. vec4 Normal = Projection*Modelview*ex_Normal;
    15. vec4 Eye = vec4(EyeAxis,1.0);
    16. float c = abs(dot(normalize(Eye.xyz),normalize(Normal.xyz)));
    17. FragColor = vec4((0.2+0.8*c)*ex_Color,1.0);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MyNGLWidget::Initialize()
    2. {
    3. //only called after drawing data (coordinates, colors, normals, etc) have been loaded
    4. GenBuffers();
    5. GenShaders();
    6. glEnable(GL_BLEND);
    7. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    8. glEnable(GL_DEPTH_TEST);
    9.  
    10. Initialized = true;
    11. updateGL();
    12. }
    13. void MyNGLWidget::initializeGL()
    14. {
    15. glewInit();
    16. format().setDepth(true); //even though its true by default
    17. format().setDepthBufferSize(32); //even though Qt tends to reject this value and stick to 24X8
    18. }
    To copy to clipboard, switch view to plain text mode 

    Any hints and tips?

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

    Default Re: Setting up OpenGL shaders with QGLWidget

    Isn't it the fault of GL_BLEND? What if you disable it?
    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.


  3. #3
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting up OpenGL shaders with QGLWidget

    Unfortunately, I started playing (and thus enabled) with glBlend precisely because of this problem...

    Ah, and yeah, I forgot to add that I also have
    Qt Code:
    1. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    To copy to clipboard, switch view to plain text mode 
    in every paint call.

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

    Default Re: Setting up OpenGL shaders with QGLWidget

    This seems to be purely OpenGL issue. Start disabling things until everything starts working. At least you'll know where to look.
    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.


  5. #5
    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: Setting up OpenGL shaders with QGLWidget

    (...) thinking that maybe I was missing something Qt-related
    So create an OpenGL app without Qt, this way you will know if this issue is related to Qt.
    What does it look like if you disable shaders (and GL_BLEND, as wysota mentioned) ?

  6. #6
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting up OpenGL shaders with QGLWidget

    I actually started with an OpenGL app without Qt (using glut and such things) and then transferred it into Qt. I didn't notice the transparency before, but it's quite possible I simply didn't notice it. Hell, I've been working on this Qt version for a while and only noticed it now (I'm currently only working with one object, so I have to get just the right angles to be able to see the object through itself).

    And I can't disable the shaders since I would then have to rewrite the entire code to work with glBegin()/glEnd() instead of the method I'm using now, but without GL_BLEND, it doesn't really change much. But I'm also pretty sure this is OpenGL related, so I'll just go ask on their forums.

    Thanks for the help.

  7. #7
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Windows

    Default Re: Setting up OpenGL shaders with QGLWidget

    I think the orientation of the faces might be wrong. Just use: glFrontFace(GL_CW);

  8. #8
    Join Date
    Sep 2011
    Posts
    1
    Platforms
    MacOS X

    Default Re: Setting up OpenGL shaders with QGLWidget

    setDepth(true) enables the *depth buffer* in the context and does nothing for depth testing.
    try add glDepthFunc(GL_LEQUAL); somewhere in your initialization.

    also, the QGLFormat need to be setup before you create the context, and then sent in to the constructor as the second parameter.
    Last edited by 23critters; 9th September 2011 at 18:16.

Similar Threads

  1. Replies: 2
    Last Post: 30th December 2010, 17:03
  2. OpenGL shaders and x11 forwarding
    By Wali in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2010, 21:38
  3. 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
  4. Replies: 0
    Last Post: 20th August 2010, 12:50
  5. OpenGL ES2.0 QGLWidget at GraphicsScene
    By chithize in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 19th August 2010, 05:11

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.