PDA

View Full Version : OpenGL ES: Depth Buffer Problem



_klen_
11-22-2010, 07:01 AM
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.

http://i51.tinypic.com/35asuqh.png
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.

http://i54.tinypic.com/63rw2f.jpg

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 (http://www.gamedev.net/community/forums/topic.asp?topic_id=588396).

Thx for you help.

Klen

_klen_
11-22-2010, 07:10 AM
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_posi tion_ptr, 3, GL_FLOAT, GL_FALSE, 0, modelVerts);

/* Enable VERTEX NORMAL */
glEnableVertexAttribArray(programObjGlbl.a_vertex_ normal_ptr);
glVertexAttribPointer(programObjGlbl.a_vertex_norm al_ptr, 3, GL_FLOAT, GL_FALSE, 0, modelNormals);

/*Enable Colour*/
glEnableVertexAttribArray(programObjGlbl.a_vertex_ color_ptr);
glVertexAttribPointer(programObjGlbl.a_vertex_colo r_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

ZbuffeR
11-22-2010, 07:37 AM
And do you ask for a depth buffer at context creation time ?
Same as you ask for double-buffered and RGBA ?

_klen_
11-22-2010, 09:20 AM
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.

ZbuffeR
11-22-2010, 10:05 AM
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.

_klen_
11-22-2010, 11:52 AM
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

ZbuffeR
11-22-2010, 01:08 PM
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);

_klen_
11-23-2010, 06:11 AM
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.
http://i55.tinypic.com/5me9gm.jpg

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

Thx again,
Klen

ZbuffeR
11-23-2010, 08:36 AM
Click on my nickname, then "view profile", then choose number of stars for rating.

Glad that helped :)

_klen_
11-24-2010, 03:26 AM
Done. Thx again.