Opaque and Translucent Objects Intersection Artifacts

Member
Posts: 268
Joined: 2005.04
Post: #1
So I'm trying to add water to a terrain engine I'm working on. And just to start out I'm drawing a blended quad that intersects with the landscape at the height I want the water to appear. The problem is where the quad intersects the terrain there's some nasty drawing artifacts. When the quad is entirely above or below the terrain and doesn't intersect in any places it draws fine.

Is this a known problem with OpenGL or am I missing some crucial step?

Here's a picture of the problem (with textures turned off):
http://www.killerrobots.com/pictures/water_problem.jpg

And here's some of the code:

Code:
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glVertexPointer(3, GL_FLOAT, sizeof(VertexRec), &vertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexRec), &vertices[0].u);
glNormalPointer(GL_FLOAT, sizeof(VertexRec), &vertices[0].nX);
    
glBindTexture(GL_TEXTURE_2D, texture[0]);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

glColor4f( 1.0f, 1.0f, 1.0f, 0.5f );   // Full brightness, 50% alpha
glEnable( GL_BLEND );
glDepthMask( GL_FALSE );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );

glBegin(GL_QUADS);
   glColor3f(0.0, 0.0, 1.0);
   glVertex3f(50, 5.8, 0);
   glVertex3f(100, 5.8, 0);
   glVertex3f(100, 5.8, 100);
   glVertex3f(50, 5.8, 100);
glEnd();

glDepthMask( GL_TRUE );
glDisable( GL_BLEND );
Quote this message in a reply
Member
Posts: 164
Joined: 2002.04
Post: #2
Check that your near and far clipping planes are as close to each other as possible.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #3
Yea, this looks like your z buffering is too inaccurate. but if you have a static terrain, I would consider creating exactly matching polys in the first place, it would certainly not make the situation worse, at least, just more complicated.

- D.G
Quote this message in a reply
Member
Posts: 268
Joined: 2005.04
Post: #4
Okay, that was it. I had: "gluPerspective( 45.0f, sceneBounds.size.width / sceneBounds.size.height, 0.099f, 500.0f );" and I changed it to: "gluPerspective( 45.0f, sceneBounds.size.width / sceneBounds.size.height, 1.0f, 350.0f );" and now it works fine. I hate it when answers are that obvious...

Is there anyway to get more precision in the z-buffer? Switching to a 32-bit depth buffer maybe? I probably won't need really long views for this project, but it would be handy to know in case it comes up in the future.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #5
Quote:Originally posted by Bachus
Okay, that was it. I had: "gluPerspective( 45.0f, sceneBounds.size.width / sceneBounds.size.height, 0.099f, 500.0f );" and I changed it to: "gluPerspective( 45.0f, sceneBounds.size.width / sceneBounds.size.height, 1.0f, 350.0f );" and now it works fine. I hate it when answers are that obvious...

Is there anyway to get more precision in the z-buffer? Switching to a 32-bit depth buffer maybe? I probably won't need really long views for this project, but it would be handy to know in case it comes up in the future.


32 bit z-buffer is not always available, I think I can only get 24 on my gf2mx. So don't rely on that to fix your problems.

- D.G
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
The best way to get Z precision is to ensure that near is never less than 1. You can take far out quite a long way with no problems as long as you keep that restriction on near...
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #7
Quote:Originally posted by OneSadCookie
The best way to get Z precision is to ensure that near is never less than 1. You can take far out quite a long way with no problems as long as you keep that restriction on near...

The near and far planes can be anywhere you like provided the ratio between them isn't too great. If your object dimensions were very small you probably would have to have your near plane closer than 1.0 unit, so to avoid getting Z buffer shearing you'd have to make sure the far plane was proportionally closer as well.

With regards to Z buffer depth, under AGL you can ask for AGL_MAXIMUM_POLICY and AGL_DEPTH_SIZE = 32 to get a 32 bit Z buffer if one is available. If the hardware doesn't support it, you get whatever its maximum depth is instead. Of course, you should still test with a 16 bit Z buffer to make sure your game doesn't look terrible!

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES 2.0, 2D Alpha Transparency Artifacts Macmenace 3 7,487 Mar 28, 2010 11:18 PM
Last Post: AnotherJake
  Need ray-sphere intersection code MattDiamond 23 14,283 Aug 31, 2009 02:28 PM
Last Post: Gengar003
  Multisample AA Artifacts metacollin 4 4,790 Aug 14, 2009 07:11 PM
Last Post: metacollin
  Shadow Mapping - Self-Shadowing Z-Fighting Artifacts Bachus 16 20,239 Feb 11, 2009 12:24 PM
Last Post: arekkusu
  2d Polygon Intersection bizimCity 6 6,572 Aug 31, 2006 05:29 PM
Last Post: reubert