PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

ZBuffer issues between ClassEDisplay->Render() and OpenGL

Planet9Dave · 13 Jan 2009, 01:44 UTC
Hi,

I'm merging EdgeLib animated models (E3DSurface) into an OpenGL-rendered world. I've aligned the camera field-of-fiews (setting the EdgeLib camera to 60 degrees via
Code:
display->Perspective3D(winW, winH, long(60 * 2048/180) );

), and because the OpenGL-rendered world is significantly bigger than EdgeLib assumes (translations appear to be limited to approximately +/- 512), rather than translate the Edge camera (using WorldTranslate) and models (using E3DSurface->Translate) to the desired positions, I simply offset the models relative to the Edge camera. I have verified (by interactively moving the camera around in the scene) that the animated models are correctly locked to the OpenGL-rendered world.

Now I have the one remaining issue that the models appear "in front of" objects in the OpenGL world, when they should be "behind" those objects. Is there any chance that the Z-buffer is being cleared in the Perspective3D() call, or the WorldReset() call? Or is Edge internally using a range of Z values other than the OpenGL default (for Windows)?

Thanks for any insight,
Dave
Hi Dave,

This problem seems to be caused by a disabled z-buffer. Are you sure you're using an OpenGL implementation, and not the EDGELIB internal renderer?

The internal renderer doesn't support a z-buffer and could be the cause of the behaviour in your application.
Planet9Dave · 13 Jan 2009, 20:29 UTC
I believe I'm correctly specifying that EdgeLib should use OpenGL. The top of my main.cpp reads:
Code:
//Use OpenGL
#define EGL_USEGL
#define EGL_PC

//Include EDGELIB
#include "edgemain.h"

// [non-edge-related includes]

//Link the EDGELIB static library
#pragma comment(lib, "edge.lib")

// include OpenGL support
#if defined(EGL_USEGL)
#include "gl/GL.h"
#pragma comment(lib, "plugingl.lib")
#pragma comment(lib, "OpenGL32.Lib")
#endif


If I were using the EdgeLib internal renderer, would it still be drawing the animated models on top of the OpenGL-rendered scene? Or would it be using its own context? Also, just to satisfy my own curiosity, does the internal renderer then pre-sort polygons/fragments in depth order and rasterize only what's visible?

At draw-time, I first draw the environment using OpenGL, then specifically enable depth-testing, texturing and alpha-blending around the EdgeLib model drawing:
Code:
#if defined(EGL_USEGL)
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
#endif
for ( aIter = avatars.begin(); aIter != avatars.end(); aIter++ ) {
Avatar * a = (*aIter).second;
if ( a ) {
a->DeadReckon( timedelta );
if ( ! a->IsLoaded() && ! a->LoadFailed() )
if ( ! a->Load( display, a->modelFile, a->textureFile ) )
continue;
a->Animate( timedelta );
a->Draw( display, xform );
}
}
#if defined(EGL_USEGL)
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
#endif


Avatar::Draw() is pretty much just:
Code:
model.Reset();
model.Translate( mx, my, mz );
model.Rotate( 0, ry, 0 );
model.Scale( s, s, s );
display->BindTexture(&texture;);
display->SetShading(E3D_GOURAUD | E3D_TEXTURE);
display->Render(&model;);

Are there other settings I'm forgetting?

Thanks again,
Dave
Hi Dave,

EDGELIB renders models as they are stored in the 3D surface structure, no sorting or depth testing is done when OpenGL is enabled. Perhaps the moving of the objects will cause the z-buffer to mess up, because they are all rendered in the same position?

A good test to see if OpenGL is enabled in EDGELIB is to make a call to Perspective2D, followed by a call to Blt.
Planet9Dave · 14 Jan 2009, 20:25 UTC
First, the models handled via EdgeLib are -not- rendered at the same position -- different values for the translation, rotation and scale are computed for each model.

Second, the EdgeLib-rendered models are rendered correctly with respect to each other, depth-wise: they correctly pass in front of and behind each other, the depth errors are -only- between objects rendered directly with OpenGL calls, and objects rendered with EdgeLib calls.

Third, I'm happy to try your test of calling Perspective2D() followed by Blt(), but what behavior should I see if OpenGL is enabled vs. disabled?

Thanks,
Dave
Hi Dave,

After reading your post I think the test is not neccessary anymore, because you can drawn an OpenGL scene correctly with working OpenGL functions.

In your sample you enable depth testing with the glEnable(GL_DEPTH_TEST) command. Unfortunately, I can't see the code which renders the OpenGL scene without the EDGELIB calls.

Perhaps depth testing is disabled in that part of the code? Also, make sure the z-buffer is actually filled with values. This can be specified with the glDepthMask(GL_TRUE) call. Another cause could be that the coordinate system isn't the same?
Planet9Dave · 15 Jan 2009, 22:34 UTC
Another cause could be that the coordinate system isn't the same?

I think we're finally getting to the root of the problem. Sorry it's taken me so long to realize! In addition to the viewport width & height, and field-of-view, the perspective projection matrix depends on the distances to the near & far clipping planes. What are those values set to, in EdgeLib? And is there a way for me to alter them?

Thanks,
Dave
Hi Dave,

The near and far clipping planes are set to 4096 and 100 * 4096 respectively. When you want to have more control over the perspective I recommend using the ClassEDisplay :: Frustum function. Alternatively you could receive the perspective matrix.

I suspect the real problem is using floating point values in your own rendering calls versus fixed point values in EDGELIB. If that's the case, I think the only good solution would be to convert your scene to fixed point coordinates and use EDGELIB to set the perspective.
Planet9Dave · 23 Jan 2009, 04:12 UTC
Thanks for the tip!

It's taken me a while to return to this issue, but using ClassEDisplay::Frustum() to specify the same near/far clipping planes as used in OpenGL has resolved the problem. Specifically (and I note that the conversion from float to fixed-point here appears to be 22.10, not 20.12 as documented, but I may have something else wrong somewhere in my system):

Code:
unsigned long winW = display->GetWidth();
unsigned long winH = display->GetHeight();
float fovYF = 60.0f;
float nearF = 1.0f, farF = 2400.0f;

float ymaxF, xmaxF;
ymaxF = nearF * tan( fovYF/2 * 3.1415927/180.0 );
xmaxF = ymaxF * winW / float(winH);

long xmaxL = long( xmaxF * 1024 );
long ymaxL = long( ymaxF * 1024 );
long nearL = long( nearF * 1024 );
long farL = long( farF * 1024 );
long fovYL = long( fovYF * 2048/180 );

display->Perspective3D( winW, winH, fovYL );
display->Frustum( -xmaxL, xmaxL, -ymaxL, ymaxL, nearL, farL );


Anyway, working great! Thanks for your assistance and patience!

Dave
Planet9Dave wrote:Anyway, working great! Thanks for your assistance and patience!


Hi Dave,

Glad we could help. Let us know when you have further questions.