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:
Code:
glEnable(GL_DEPTH_TEST);
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.
Code:
//fragment shader
#version 330
precision highp float;
uniform vec3 EyeAxis = vec3(0.0,0.0,1.0);
uniform mat4 Modelview;
uniform mat4 Projection;
in vec3 ex_Color;
in vec4 ex_Normal;
out vec4 FragColor;
void main(void)
{
vec4 Normal = Projection*Modelview*ex_Normal;
vec4 Eye = vec4(EyeAxis,1.0);
float c = abs(dot(normalize(Eye.xyz),normalize(Normal.xyz)));
FragColor = vec4((0.2+0.8*c)*ex_Color,1.0);
}
Code:
void MyNGLWidget::Initialize()
{
//only called after drawing data (coordinates, colors, normals, etc) have been loaded
GenBuffers();
GenShaders();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
Initialized = true;
updateGL();
}
void MyNGLWidget::initializeGL()
{
glewInit();
format().setDepth(true); //even though its true by default
format().setDepthBufferSize(32); //even though Qt tends to reject this value and stick to 24X8
}
Any hints and tips?
Re: Setting up OpenGL shaders with QGLWidget
Isn't it the fault of GL_BLEND? What if you disable it?
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
Code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
in every paint call.
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.
Re: Setting up OpenGL shaders with QGLWidget
Quote:
(...) 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) ?
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.
Re: Setting up OpenGL shaders with QGLWidget
I think the orientation of the faces might be wrong. Just use: glFrontFace(GL_CW);
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.