GL_DEPTH_TEST causes rendering problems with triangles...
by Dave Calabrese · in iTorque 2D · 05/01/2010 (2:23 pm) · 5 replies
I've got my OpenGL ES code working and rendering great and very fast, however I'm now having a weird issue related to depth tests. What happens is any vertice whose Z value is not exactly 0.0f causes that entire portion of the object to not get drawn. So if the Z values of the 4 points are 0.1f,0.0f,0.0f,0.0f, then the entire top-left triangle will not be drawn at all (which makes sense - if there was something occluding it, which there isn't as I'm testing this with only one object in the scene).
Here is the code I'm using:
Here is a screenshot of the problem in action using the above code: picasaweb.google.com/109821053317077386155/General#5466414937401835458
Here is the code I'm using:
//PUAP -Mat use GL_MODULATE to tint, GL_REPLACE for texture only
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// Clear our color and depth buffers
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Array of render vertices
GLfloat renderObjVects[] =
{
-1.0f, -1.0f, 0.1f, // Bottom Left
1.0f, -1.0f, 0.0f, // Bottom Right
-1.0f, 1.0f, 0.0f, // Top Left
1.0f, 1.0f, 0.0f // Top Right
};
// Array of texture coordinates
GLfloat texCoords[] =
{
1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Enable Properties
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Set our blend types
glBlendFunc(GL_ONE, GL_SRC_COLOR);
// Enable depth checking so objects can layer
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepthf(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
// Enable our pointers.
glVertexPointer(3, GL_FLOAT, 0, renderObjVects);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
// Set our Projection Mode
glMatrixMode(GL_MODELVIEW);
// Set the shading model
glShadeModel(GL_SMOOTH);
// Clear things out
glColor4f(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glLoadIdentity();
// Bind this imagemap for use
glBindTexture( GL_TEXTURE_2D, thisData.mpPageTextureHandle->getGLName() );
// Now... let's draw!
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);Here is a screenshot of the problem in action using the above code: picasaweb.google.com/109821053317077386155/General#5466414937401835458
About the author
Recent Threads
#2
The glOrthoF calls screw things up. When I comment those out, it (so far) appears as if depth testing is working just fine. (Though you also need to add in a quick bit of math to convert the modified coordinates system when doing the actual rendering).
05/02/2010 (12:00 pm)
Think I'm on to something, here. In t2dSceneWindow.cc, in the onRender method, look for this block of code:// Is the Camera Shaking?
if ( mCameraShaking )
{
// Yes, so calculate the Screen Shake-Ratio.
Point2F shakeRatio( mCameraCurrent.mZoomedWindow.len_x() / F32(updateRect.len_x()), mCameraCurrent.mZoomedWindow.len_y() / F32(updateRect.len_y()) );
// Calculate the Camera Shake Magnitude based upon the Screen Shake-Ratio.
F32 shakeMagnitudeX = mCurrentShake * shakeRatio.x * 0.5f;
F32 shakeMagnitudeY = mCurrentShake * shakeRatio.y * 0.5f;
// Choose a random Shake.
mCameraShakeOffset.set( mGetT2DRandomF( -shakeMagnitudeX, shakeMagnitudeX ), mGetT2DRandomF( -shakeMagnitudeY, shakeMagnitudeY ) );
// Calculate Shaking Scene Portal.
Point2F windowToScene( (mCameraCurrent.mSceneMax.x-mCameraCurrent.mSceneMin.x) / F32(mBounds.len_x()),(mCameraCurrent.mSceneMax.y-mCameraCurrent.mSceneMin.y) / F32(mBounds.len_y()) );
Point2F sceneMin( (-offset.x + updateRect.point.x) * windowToScene.x + mCameraCurrent.mSceneMin.x + mCameraShakeOffset.mX, (-offset.y + updateRect.point.y) * windowToScene.y + mCameraCurrent.mSceneMin.y + mCameraShakeOffset.mY );
Point2F sceneMax( (-offset.x + updateRect.point.x + updateRect.extent.x) * windowToScene.x + mCameraCurrent.mSceneMin.x + mCameraShakeOffset.mX, (-offset.y + updateRect.point.y + updateRect.extent.y) * windowToScene.y + mCameraCurrent.mSceneMin.y + mCameraShakeOffset.mY );
glOrthof( sceneMin.x, sceneMax.x, sceneMax.y, sceneMin.y, 0, t2dSceneGraph::maxLayersSupported );
}
else
{
//Con::printf("@@@ mBounds = %f,%f",F32(mBounds.len_x()),F32(mBounds.len_y()));
// No, so calculate standard Scene Portal.
Point2F windowToScene( (mCameraCurrent.mSceneMax.x-mCameraCurrent.mSceneMin.x) / F32(mBounds.len_x()),(mCameraCurrent.mSceneMax.y-mCameraCurrent.mSceneMin.y) / F32(mBounds.len_y()) );
Point2F sceneMin( (-offset.x + updateRect.point.x) * windowToScene.x + mCameraCurrent.mSceneMin.x, (-offset.y + updateRect.point.y) * windowToScene.y + mCameraCurrent.mSceneMin.y );
Point2F sceneMax( (-offset.x + updateRect.point.x + updateRect.extent.x) * windowToScene.x + mCameraCurrent.mSceneMin.x, (-offset.y + updateRect.point.y + updateRect.extent.y) * windowToScene.y + mCameraCurrent.mSceneMin.y );
glOrthof( sceneMin.x, sceneMax.x, sceneMax.y, sceneMin.y, 0.0f, t2dSceneGraph::maxLayersSupported );
}The glOrthoF calls screw things up. When I comment those out, it (so far) appears as if depth testing is working just fine. (Though you also need to add in a quick bit of math to convert the modified coordinates system when doing the actual rendering).
#3
This decides what is culled or not, what is back face or front face.
I think your problem come from this.
See this : www.zeuscmd.com/tutorials/opengles/14-BackfaceCulling.php
Nicolas Buquet
www.buquet-net.com/cv/
05/03/2010 (12:45 am)
Your 2 polygons (triangles) aren't specified in the same way : the first is counter-clockwise while the second is clockwise.This decides what is culled or not, what is back face or front face.
I think your problem come from this.
See this : www.zeuscmd.com/tutorials/opengles/14-BackfaceCulling.php
Nicolas Buquet
www.buquet-net.com/cv/
#4
I don't think that's the problem I'm afraid. I have tried both clockwise and counterclockwise vertice ordering. Plus, nowhere in TGB is glEnable(GL_CULL_FACE) set, however it is disabled on numerous occasions (not sure why it's being disabled when never getting enabled though... kind of odd.).
EDIT: Got it. The problem was the glOrthof call. Need to set the zNear value to -1.0f, otherwise things get culled when they shouldn't. This is also why things worked when commenting that out, however fixing the problem rather then commenting it out means we don't have to translate the matrices passed in to our OpenGL calls.
So, make it read like this and it works fine (so far, at least...)
05/03/2010 (9:27 am)
Nicholas,I don't think that's the problem I'm afraid. I have tried both clockwise and counterclockwise vertice ordering. Plus, nowhere in TGB is glEnable(GL_CULL_FACE) set, however it is disabled on numerous occasions (not sure why it's being disabled when never getting enabled though... kind of odd.).
EDIT: Got it. The problem was the glOrthof call. Need to set the zNear value to -1.0f, otherwise things get culled when they shouldn't. This is also why things worked when commenting that out, however fixing the problem rather then commenting it out means we don't have to translate the matrices passed in to our OpenGL calls.
So, make it read like this and it works fine (so far, at least...)
glOrthof( sceneMin.x, sceneMax.x, sceneMax.y, sceneMin.y, -1.0f, t2dSceneGraph::maxLayersSupported );
#5
the engine isn't setup as general purpose OGL ES container but targeted at its specific behavior and I think it would be more safe to get in touch with its assumptions and build upon that instead of forcing TGB to bend as that could very well lead to other sideeffects later on at worst.
05/03/2010 (11:08 am)
Potentially the original implementation was targeted at something along the line of "layer" = depth coordinate and as layers > 0 a start of 0 there made very much sense (not checked but would make sense)the engine isn't setup as general purpose OGL ES container but targeted at its specific behavior and I think it would be more safe to get in touch with its assumptions and build upon that instead of forcing TGB to bend as that could very well lead to other sideeffects later on at worst.
Associate Luke Lamothe
Luma Arcade
Even by doing that, I was never able to get proper results with the depth buffer rendering iTGB... always something funny...