spline path object occlusion in Fragmentarium...
this is the bit of shader code that makes it work...
gl_FragDepth = ((1000.0 / (1000.0 - 0.00001)) +
(1000.0 * 0.00001 / (0.00001 - 1000.0)) /
clamp(Ray.Pos/length(Ray.Direction), 0.00001, 1000.0));
this is the C++ side...
void DisplayWidget::qMultMatrix(QMatrix4x4 mat) {
if (sizeof(qreal) == sizeof(GLfloat))
glMultMatrixf((GLfloat*)mat.constData());
#ifndef QT_OPENGL_ES
else if (sizeof(qreal) == sizeof(GLdouble))
glMultMatrixd((GLdouble*)mat.constData());
#endif
else {
GLfloat fmat[16];
#ifdef WIN32
float const *r = mat.constData();
#else
double const *r = mat.constData();
#endif
for (int i = 0; i < 16; ++i)
fmat[i] = r[i];
glMultMatrixf(fmat);
}
}
void DisplayWidget::qglPerspective(GLdouble fov, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
fov = 180.0 * ( 2.0 * atan2(1.0,(1.0/fov)) / 3.1415926535);
QMatrix4x4 matrix;
matrix.setToIdentity();
matrix.perspective(fov, aspect, zNear, zFar);
qMultMatrix(matrix);
}
first, draw with shader program onto quad...
glDepthFunc(GL_ALWAYS); // always passes test so we write color
glEnable( GL_DEPTH_TEST ); // enable depth testing
glDepthMask(GL_TRUE); // enable depth buffer writing
...GL commands to render quad...
... before drawing the spline paths set perspective...
qglPerspective(fov, aspect, 0.00001, 1000.0);
where
aspect is the QGLWidget dimensions width() / height() and
fov is the value from the FOV slider
then render splines with
glDepthFunc(GL_LESS); // closer to eye passes test
glDepthMask(GL_FALSE); // no Writing to depth buffer for splines
glEnable( GL_DEPTH_TEST ); // only testing
...GL commands to render splines...
result: GL primitives respect GLSL depth values
only tested with Qt4.8.5/linux, curious to hear input input from others, after reading everything I could find and trying examples that just never worked right in this particular case I have settled on the above approach because it seems to do the trick.
(i thought this might be of interest to others and would be better up front rather than hidden at the end of a very loooong thread)edit: using QMatrix means no more dependency on glu library for perspective