c++ - Weird Result with Depth Test -


i'm having problem when enable gl_depth_test when rendering objects, here's happens:

enter image description here

gl_cull_face disable on one.

it happens perspective view! have , idea of what's happening?

i'm using qt 4.8 this.

of course, if disable depth test, looks fine, objects overlays in wrong way, expected.

update

i create projection matrix this:

projectionmatrix.settoidentity(); projectionmatrix.perspective(45, float(width) / float(height), 0, 20.0); 

where width , height related viewport size.

here's code draw scene:

void glwidget::paintgl() {     glclearcolor(0.4765625, 0.54296875, 0.6171875, 1.0);     glclear(gl_color_buffer_bit | gl_depth_buffer_bit);      editor->setuniformvalue("projectmatrix", controller->getprojectionmatrix());     editor->setuniformvalue("viewmatrix", controller->getviewmatrix());      /** dibujemos los grids **/     gldisable(gl_depth_test);     gldisable(gl_cull_face);     (int i=0; i<3; i++)     {         if ((front && i==1) || (side && i==2) || (i==0))         {             editor->setuniformvalue("modelmatrix", objects.at(i).modelmatrix);             editor->setuniformvalue("normalmatrix", objects[i].getnormalmatrix());             editor->setuniformvalue("texture", textureon);              glenableclientstate(gl_vertex_array);             editor->enableattributearray("vertices");             objects[i].vertexbuffer->bind();             editor->setattributebuffer("vertices", gl_float, 0, 3);             gldisableclientstate(gl_vertex_array);              glenableclientstate(gl_texture_coord_array);             editor->enableattributearray("texuv");             objects[i].uvbuffer->bind();             editor->setattributebuffer ("texuv", gl_float, 0, 2);             gldisableclientstate(gl_texture_coord_array);              glenableclientstate(gl_normal_array);             editor->enableattributearray("normals");             objects[i].normalbuffer->bind();             editor->setattributebuffer ("normals", gl_float, 0, 3);             gldisableclientstate(gl_normal_array);              objects[i].elementbuffer->bind();             glbindtexture(gl_texture_2d, objects[i].texture);             gldrawelements(gl_triangles, objects[i].indices.size(), gl_unsigned_short, (void*)0);         }     }      /** ahora para las operaciones especificas de cada objeto **/     glenable(gl_depth_test);     //glenable(gl_cull_face);     (int i=3; i<objects.size(); i++)     {         //objects[i].modelmatrix.scale(1.0, 1.0, 1.0);         //objects[i].modelmatrix.rotate(1.0, 0.0, 1.0, 0.0);          editor->setuniformvalue("modelmatrix", objects.at(i).modelmatrix);         editor->setuniformvalue("normalmatrix", objects[i].getnormalmatrix());         editor->setuniformvalue("texture", textureoff);         editor->setuniformvalue("diffusecolor", objects.at(i).diffusecolor);         editor->setuniformvalue("shininess", objects.at(i).shininess);         editor->setuniformvalue("hardness", objects.at(i).hardness);         editor->setuniformvalue("lpos1", objects.at(i).l1pos);         editor->setuniformvalue("lpos2", objects.at(i).l2pos);         editor->setuniformvalue("lpos3", objects.at(i).l3pos);         editor->setuniformvalue("lpos4", objects.at(i).l4pos);          glenableclientstate(gl_vertex_array);         editor->enableattributearray("vertices");         objects[i].vertexbuffer->bind();         editor->setattributebuffer("vertices", gl_float, 0, 3);         gldisableclientstate(gl_vertex_array);          glenableclientstate(gl_normal_array);         editor->enableattributearray("normals");         objects[i].normalbuffer->bind();         editor->setattributebuffer ("normals", gl_float, 0, 3);         gldisableclientstate(gl_normal_array);          objects[i].elementbuffer->bind();         gldrawelements(gl_triangles, objects[i].indices.size(), gl_unsigned_short, (void*)0);     } } 

in short lines, first draw grids according camera facing, in order give functionallity blender's. then, draw objects. i'm using indexed vbos achieve this.

i draw them in ccw, i'm 99% sure triangles being passed opengl in way.

the near value of [edit perspective] projection matrix zero. results in depth values equal 1 after perspective divide (.xyz/.w). @raxvan had right idea. try this...

projectionmatrix.perspective(45, float(width) / float(height), 0.01, 20.0); 

of course large scenes want large depth range, if make big wind z-fighting fragments @ different distances still have same depth values , ever gets rasterized first wins (which seems problem you're having). try keep depth range no bigger factor of 10,000 apart.

if have large scenes, might need @ alternative methods. start, use higher precision depth buffer. if that's not enough, maybe use 2 projection matrices , split scene near , far objects. causes issues if objects intersect boundary. here's related links...


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -