Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Depth buffering problem

This topic is 2924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I have created a rendering engine using OpenGLES 2.0 and have done the following to set up depth buffering:

glGenRenderbuffers(1, &m_colorRenderbuffer);

glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);

[m_context renderbufferStorage:GL_RENDERBUFFER fromDrawable: eaglLayer];

int width, height;





// Create a depth buffer that has the same size as the color buffer.

glGenRenderbuffers(1, &m_depthRenderbuffer);

glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);

// Create the framebuffer object.

GLuint framebuffer;

glGenFramebuffers(1, &framebuffer);

glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);


GL_RENDERBUFFER, m_colorRenderbuffer);


GL_RENDERBUFFER, m_depthRenderbuffer);

glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);


//Setting the viewport transform

glViewport(0, 0, width, height);

//Setting the projection transform

//NOTE: I just rewrote the glOrthof function as it's not availabile in ES2.0

glOrthof(-width/2.0f, width/2.0f, -height/2.0f, height/2.0f, -1.0f, 1.0f);

That is all done while initializing the renderer. In the render function I do the following:

glClearColor(1.0f, 0.5f, 0.0f, 1);


vector<Visual *>::const_iterator visualHolder = visuals->begin();

while (visualHolder != visuals->end())


Visual * visual = *visualHolder;


// Set the diffuse color.

vec3 color = visual->Color * 1.0f;

glVertexAttrib4f(m_attributes.DiffuseMaterial, color.x, color.y, color.z, 0);

// Set the model-view transform.

mat4 scale = mat4::Scale(visual->Scale);

scale.z.z = visual->zVal;

mat4 rotation = visual->Orientation.ToMatrix();

mat4 translation = mat4::Translate(visual->Position.x, visual->Position.y, 0);

mat4 modelview = scale * rotation * translation;

glUniformMatrix4fv(m_uniforms.Modelview, 1, 0, modelview.Pointer());

// Set the normal matrix. Used for lighting

// It's orthogonal, so its Inverse-Transpose is itself!

mat3 normalMatrix = modelview.ToMat3();

glUniformMatrix3fv(m_uniforms.NormalMatrix, 1, 0, normalMatrix.Pointer());

const Drawable& drawable = m_drawables[visual->DrawablePointer];

glBindBuffer(GL_ARRAY_BUFFER, drawable.VertexBuffer);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, drawable.IndexBuffer);

glDrawElements(GL_TRIANGLES, drawable.IndexCount, GL_UNSIGNED_SHORT, 0);



The list of visuals contains the position, scale, zValue etc... of each instance of an object. visual->drawablePointer is a pointer to the vertex buffer, index buffer and index count of the object to be drawn. Now, when I have depth testing off it just draws in the order that the visual list is created. When I turn depth testing on, however, the list is just drawn back to front. Z values aren't being utilized. However, the zValues are still being used in the lighting equations because changing the zValues changes the intensity of the light on each object. I'm horribly confused as to why the depth buffer is ignoring my z values. Oh, and to be clear, the Orthof function that I"m using is one I wrote as 2.0 doesn't have one built in. The following is the code for the function just in case:

void RenderingEngine::glOrthof(float left, float right, float bottom, float top, float near, float far)


//To see whats going on here go to:


//That site orders matrices in column order, I've adjusted accordingly as I'm using row order.

float ortho[16];

//First row

ortho[0] = 2/(right-left);

ortho[1] = ortho[2] = ortho[3] = 0;

//Second row

ortho[4] = 0;

ortho[5] = 2/(top - bottom);

ortho[6] = ortho[7] = 0;

//Third row

ortho[8] = ortho[9] = 0;

ortho[10] = (-2)/(far - near);

ortho[11] = 0;

//Fourth row

ortho[12] = -(right + left)/(right - left);

ortho[13] = -(top+bottom)/(top - bottom);

ortho[14] = -(far + near)/(far - near);

ortho[15] = 1;

glUniformMatrix4fv(m_uniforms.Projection, 1, 0, &ortho[0]);


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!