PDA

View Full Version : Cannot understand Equal depth function



Nicholas Matear
09-09-2010, 03:21 PM
I have a game I'm working on with a friend, and I'm doing most of the low-level coding since I'm the better of the two with it.

One problem I am having is I'm trying to draw a set of gridlines onto a flat "tilemap" of quads. I first draw all the quads (for simplicity's sake, I draw them all as 1x1 quads, with Z set to 0 on every vertex), then I try to draw grid lines on them. The problem I run into is that with glDepthfunc(GL_EQUAL) (sorry if the constants are wrong, I use the OpenTK framework for .NET), the lines never show up. GL_LESS makes them show up, but all over the scene and not just overtop of the quads I've drawn. GL_GREATER gives the correct view, except that once I add walls into the mix, then the gridlines start showing up in the walls where they shouldn't.

I've got the rendering code below. There's an amount of cruft since I've really simplified the whole thing to get to the root of the issue as well as hard-coded the quads instead of how it actually draws them.

Anyways, my question is; what am I doing wrong that GL_EQUAL will cause lines to simply not show up when drawn, whereas GL_LESS or GL_GREATER will but with unwanted side effects, and what's the easiest way to fix this using only the depth buffer (I can think of a solution to this using the stencil buffer, but I'd rather not use that if I can fix this using just the depth buffer)


GL.ClearColor(0, 0, 0, 1)
GL.LineWidth(2.0)

GL.DepthFunc(DepthFunction.Less)
GL.ClearDepth(Int32.MaxValue)
GL.Enable(EnableCap.DepthTest)

GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha)
GL.Enable(EnableCap.Blend)

GL.MatrixMode(MatrixMode.Texture)
GL.LoadIdentity()

================================================== ========

GL.Clear(ClearBufferMask.DepthBufferBit Or ClearBufferMask.ColorBufferBit)

'Set up the matricies. Each has rotation to create the camera view
'Translation on each controls a different part of camera movement

'Perspective; translation does panning
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
Dim UseWidth As Single = (Context.Width / (64 * Camera.Zoom)) / 2
Dim UseHeight As Single = (Context.Height / (64 * Camera.Zoom)) / 2
GL.Ortho(-UseWidth, UseWidth, -UseHeight, UseHeight, 1, 1000)
GL.Rotate(-VerticalRotation, 1, 0, 0)
GL.Translate(Camera.Position)

'Modelview; translation does floor changing
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadIdentity()
GL.Rotate(Horizontalrotation, 0, 0, 1)

GL.DepthFunc(DepthFunction.Less)

GL.Begin(BeginMode.Quads)
GL.Color4(0.6, 0.6, 0.6, 1.0)

GL.Vertex3(2, 2, 0)
GL.Vertex3(2, 3, 0)
GL.Vertex3(3, 3, 0)
GL.Vertex3(3, 2, 0)

GL.Vertex3(2, 3, 0)
GL.Vertex3(2, 4, 0)
GL.Vertex3(3, 4, 0)
GL.Vertex3(3, 3, 0)

GL.Vertex3(3, 3, 0)
GL.Vertex3(3, 4, 0)
GL.Vertex3(4, 4, 0)
GL.Vertex3(4, 3, 0)

GL.Vertex3(3, 2, 0)
GL.Vertex3(3, 3, 0)
GL.Vertex3(4, 3, 0)
GL.Vertex3(4, 2, 0)

GL.End()

GL.DepthFunc(DepthFunction.Equal)

GL.Begin(BeginMode.Lines)
GL.Color4(0.0, 0.0, 1.0, 0.7)

Dim Position As Integer = ((Camera.Position.X * GridlineSine) + (Camera.Position.Y * GridlineCosine)) / GridlineAdjustScrollRate

For X As Integer = Position - 30 To Position + 20
GL.Vertex3(-X, -1000, 0)
GL.Vertex3(-X, 1000, 0)
Next

Position = ((Camera.Position.Y * GridlineSine) - (Camera.Position.X * GridlineCosine)) / GridlineAdjustScrollRate

For X As Integer = Position - 30 To Position + 20
GL.Vertex3(-1000, -X, 0)
GL.Vertex3(1000, -X, 0)
Next

GL.End()

GL.Enable(EnableCap.Texture2D)

'==============================
' POST-RENDER
'==============================

Context.SwapBuffers()

HannesH
09-09-2010, 04:26 PM
Draw surface.

Disable writing to the depth buffer.
Make the depth test always pass.

Draw Grid.

Set Depthbuffer modes back.

Draw anything else.

Nicholas Matear
09-09-2010, 11:49 PM
I'm googling this, but I can't find how to disable writing to the depth buffer specifically without simply disabling depth testing, and the method I'm using to draw the gridlines would, in combination with your solution, result in garbage gridlines showing up in areas where nothing else save the background (which is drawn earlier and also not shown above since it wasn't relevant or so I thought) is drawn.

EDIT: A little more looking into this has resulted in one odd occurrence: if I draw the quads a second time, with GL_EQUAL and a different colour for differentiation, I don't see them show up. if I use GL_LESS, I do. Seeing as how I'm drawing them in exactly the same place, without changing the modelview or perspective matricies.. shouldn't I see them with GL_EQUAL, not GL_LESS? My depth buffer is 32 bit, if that makes a difference.

Pierre Boudier
09-10-2010, 01:46 AM
I'm googling this, but I can't find how to disable writing to the depth buffer

http://www.opengl.org/sdk/docs/man3/xhtml/glDepthMask.xml

Nicholas Matear
09-10-2010, 11:17 AM
Oh.. well, now I feel embarrassed at not finding that. Thanks for the help, my problem's solved :)