• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

3D Depth gets the cold-shoulder (Solved)

RujiK

Member
So I'm rendering a very basic 3d model that has it's vertices placed one by one. This works but the model seems to completely (kinda) ignore 3d depth. Image below:

(Top lizard is what I have and the bottom is what I want.)

I'm not really sure how to fix this as depth is being handled by internally by GMS as far as I am aware.


Matrix code which may or may not be relevant:
GML:
   var _cy = dcos(cam_pitch),
        _sy = dsin(cam_pitch),
        _cz = dcos(cam_yaw),
        _sz = dsin(cam_yaw),
        _cx = dcos(cam_roll),
        _sx = dsin(cam_roll); 
        

    cam_view[0] =  _cz*_sy*_sx+_sz*_cx;  cam_view[4] = -_sz*_sy*_sx+_cz*_cx;  cam_view[8] =  -_cy*_sx; //x
    cam_view[1] = -_cz*_sy*_cx+_sz*_sx;  cam_view[5] =  _sz*_sy*_cx+_cz*_sx;  cam_view[9] =   _cy*_cx; //y
    cam_view[2] =  _cz*_cy;              cam_view[6] = -_sz*_cy;              cam_view[10] =  _sy;     //z
    
    cam_view[12] = round(-cam_x * cam_view[0] - cam_y * cam_view[4] - cam_z * cam_view[8]); //FLOOR causes jittering some shaders. (gl_position inconsistencies I guess)
    cam_view[13] = round(-cam_x * cam_view[1] - cam_y * cam_view[5] - cam_z * cam_view[9]);
    cam_view[14] = round(-cam_x * cam_view[2] - cam_y * cam_view[6] - cam_z * cam_view[10]); 
    cam_view[15] = 1;
    matrix_set( matrix_view, cam_view );
I'd appreciate any help. Thanks guys!
 

GMWolf

aka fel666
You need to call gpu_set_ztestenable(true). The default value is false.

I would recommend you check out all the gpu_* functions. They are quite handy, especially as you get into 3D.

p.s. I love your topic titles
 

RujiK

Member
Hmmm. It look's different, but still not right...
Here is a gif with all 4 combinations of zwrite and ztest:


Could the depth issue have anything to do with the clockwise and anti-clockwise orientation of my vertex polygons? I'm sure the model has a mix of both as it's hand written. I tried all the "zfunc" functions but that either turned off the entire model or didn't change anything.

Thanks for the help guys.

p.s. I love when @GMWolf answers all my 3d questions
 

GMWolf

aka fel666
Could the depth issue have anything to do with the clockwise and anti-clockwise orientation of my vertex polygons?
This will affect backface culling, but i believe backface culling is off by default.
The 'correct' setup should be to have both zwrite and ztest on.
zfunc should probably be set to lessequal, which is the default.

If you are rendering to a surface, make sure your surfaces actually have a depth buffer with surface_depth_disable. Though this should be off by default (so surfaces should have a depth buffer).


From your gif it looks like some of the back faces are poking through. With cull mode set to none, though this seems odd if you haven't set a cull mode.
Could you try rendering a simpler object, like a rotating cube and make sure that depth testing is happening correctly there? (A cube is usually a good way to debug these things as you can get thre face overlapping with differnt depths and draw orders).
 

RujiK

Member
@flyingsaucerinvasion I'm pretty sure my projection matrix is fine since you wrote it :p (It's from one of your examples.)

@GMWolf Soooo, my cube thingies work fine.


So I thought maybe my polygons on the lizard are just too small and the depth buffer isn't precise enough. So I made the lizards BIGGER.

And there was still leg clipping. And then I realized my legs are actually just too high up. They're literally poking through the top of the model and depth is working exactly as intended. The lizards were just too small for me to tell.

So yeah, I'm an idiot. Thanks guys!
 

GMWolf

aka fel666
@flyingsaucerinvasion I'm pretty sure my projection matrix is fine since you wrote it :p (It's from one of your examples.)

@GMWolf Soooo, my cube thingies work fine.


So I thought maybe my polygons on the lizard are just too small and the depth buffer isn't precise enough. So I made the lizards BIGGER.

And there was still leg clipping. And then I realized my legs are actually just too high up. They're literally poking through the top of the model and depth is working exactly as intended. The lizards were just too small for me to tell.

So yeah, I'm an idiot. Thanks guys!
Oh great you got it working!!!
In can't believe I didn't think about depth buffer precision!
What are your znear and zfar values used to build the matrix? They are probably too far apart
 

RujiK

Member
@GMWolf I'm not actually sure what the znear and zfar values are. How do I check?

My matrix view code is in the first post but I guess the znear and zfar values are in a different matrix???(?)
 

GMWolf

aka fel666
@GMWolf I'm not actually sure what the znear and zfar values are. How do I check?

My matrix view code is in the first post but I guess the znear and zfar values are in a different matrix???(?)
Yeah those are defined in your projection matrix. You have one of those right?
If the values are too far apart, you will not have enough precision in the depth buffer and close triangles can pass through each other.
 
Top