OpenGL ES: Depth Buffer Problem

Hello,

I am just starting to work with OpenGL ES and need some help. I am trying to load a .obj model into my game and have problems with how the the model renders. I narrowed it down to the depth buffer probelm

The mesh I am trying to render has some surfaces within the object. Like in this example.


The problem is that the faces within the object don’t get filtered by the depth buffer test, instead they are rendered as normal front faces.

0.) Is such mash considered as invalid?
1.) How come that blender seams to have no problems with this issue?
2.) Any suggestions on how to fix this? There were some indications about the problem with depth precision.

I posted similar question also at Game Dev.

Thx for you help.

Klen

Might make sense to share the openGL code with you.

My OpenGL ES 2.0 configuration:

glEnable(GL_TEXTURE_2D);
glGenTextures(1, &(programObjGlbl.textureObject));

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

glDepthRangef(0.0,1.0);
glEnable(GL_DEPTH_TEST);

glViewport(0, 0, screen->w, screen->h);

My Render Function:

glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glClearColor(0.3, 0.5, 0.5, 0.1);

/* Enable Model view / Projection 
glUniformMatrix4fv(programObjGlbl.u_mvpMatrix_ptr, 1, GL_FALSE, (GLfloat*)&mvpMatrix.m[0][0]);

/* Enable VERTEX POSITION */
glEnableVertexAttribArray(programObjGlbl.a_vertex_position_ptr);
glVertexAttribPointer(programObjGlbl.a_vertex_position_ptr, 3, GL_FLOAT, GL_FALSE, 0, modelVerts);

/* Enable VERTEX NORMAL */
glEnableVertexAttribArray(programObjGlbl.a_vertex_normal_ptr);
glVertexAttribPointer(programObjGlbl.a_vertex_normal_ptr, 3, GL_FLOAT, GL_FALSE, 0, modelNormals);

/*Enable Colour*/
glEnableVertexAttribArray(programObjGlbl.a_vertex_color_ptr);
glVertexAttribPointer(programObjGlbl.a_vertex_color_ptr	, 3, GL_FLOAT, GL_FALSE, 0, modelColor);

glDrawArrays(GL_TRIANGLES, 0, campusModelGlbl->numtriangles * 3);

glDisableVertexAttribArray(programObjGlbl.a_vertex_position_ptr);
glDisableVertexAttribArray(programObjGlbl.a_vertex_normal_ptr);

Thx,
Klen

And do you ask for a depth buffer at context creation time ?
Same as you ask for double-buffered and RGBA ?

Sorry. I don’t understand what do you mean by context cation time.

ok : the code you showed looks correct.
But you have to show more code : the part that creates the GL window or image or surface or whatever it is called in GL ES.

I am suing SDL library to create OpenGL window.

/* Init the SDL library, this provides graphics, audio, keyboard etc */
result = SDL_Init(SDL_INIT_EVERYTHING);
assert(result == 0);

/* Create a screen 'image' and make it full screen */
screen = SDL_SetVideoMode(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, 0, SDL_HWSURFACE | SDL_FULLSCREEN);
assert(screen);

/* Give it an appropriate title and icon when it gets minimised*/
SDL_WM_SetCaption("OpenGL Grid", "OpenGL Grid");
SDL_ShowCursor(SDL_DISABLE);

/* Init the SDL Gles library */
result = SDL_GLES_Init(SDL_GLES_VERSION_2_0);
assert(result == 0);

/* Create the render context */
context = SDL_GLES_CreateContext();
assert(context);

/* Makes the rendering context we just created current */
result = SDL_GLES_MakeCurrent(context);
assert(result == 0);

/* Prepare the shader were going to use, and init the opneGL */
Init();

Thx for your patience,
Klen

You do not request any depth buffer.

https://garage.maemo.org/docman/view.php/1368/483/SDL__gles_8h.html#e98eb0be1689fd26cf4e878aec2ee3e0

Try setting SDL_GLES_Attr with SDL_GLES_DEPTH_SIZE to 24 (for 24 bits of depth precision) or 16 if your platform can not handle more precision.
/** Sets a specific context attribute (before calling SDL_CreateContext()).

  • @param attr attribute to set
  • @param value new value
  • @return 0 if the attribute exists, -1 otherwise.
    */
    extern DECLSPEC int SDLCALL SDL_GLES_SetAttribute(SDL_GLES_Attr attr, int value);

Hello ZbufferR,

It was exactly this. I only needed to set the Depth Buffer to the context.


...
/*Set Depth Buffer to your context*/
SDL_GLES_SetAttribute(SDL_GLES_DEPTH_SIZE, 24);
...

I also edited my OpenGL configuration, but am not suer if it affects anything. I will post it in case someone will want to compare with what they have.


/* Configure the Open GL renderer */
/*Enable back face culling*/
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
/* Depth buffer setup */
glClearDepthf( 1.0f );
/* Enables Depth Testing */
glEnable( GL_DEPTH_TEST );
/* The Type Of Depth Test To Do */
glDepthFunc( GL_LEQUAL );
/* Set the background color*/
glClearColor(0.3, 0.5, 0.5, 0.1);
/*Set viewport*/
glViewport(0, 0, screen->w, screen->h);

Now the models are rendering as they should. Thank you very much for your help. Here is the result.

Does the forum have any rating system, where I could rate your help.

Thx again,
Klen

Click on my nickname, then “view profile”, then choose number of stars for rating.

Glad that helped :slight_smile:

Done. Thx again.