• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[GMS2][Solved] Z Testing not working?

Nehemek

Member
Hello, I'm trying to draw a model and this is what is displaying:

(This is what the shape is supoused to be, this on gms1, of course here uses a more complex shader and it's on a different perspective:
)

The issue is that it appears as if z testing is doing nothing at all. The triangles behind the others are drawing.
This is my current setup:

gpu_set_cullmode(cull_noculling);
gpu_set_ztestenable(true);
gpu_set_zwriteenable(true);


Another perspective:
 
I don't have GMS2, but I have a couple of hunches. What are your znear and zfar values? Also, it sounds like you might be using a shader, what does the vertex shader look like?
 

Nehemek

Member
I thought maybe my loading script was the problem. Here are two triangles I handcoded. The white one's coordinates should be behind.

This is the vertex shader:
attribute vec3 in_Position;
attribute vec3 in_Normal;
attribute vec2 in_TextureCoord0;

varying vec2 v_vTexCoord;

void main()
{
vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;

v_vTexCoord = in_TextureCoord0;
}

This is the fragment:
varying vec2 v_vTexCoord;

void main()
{
gl_FragColor = texture2D(gm_BaseTexture,v_vTexCoord);
}

This is my room creation code:
///initialize_game()
/*Define all things of creation*/

vertex_format_begin();
vertex_format_add_position_3d();
vertex_format_add_normal();
vertex_format_add_texcoord();
global.format_simple = vertex_format_end();

var buffer = buffer_load_obj("mushi_1.obj");
//global.cube = vertex_create_buffer_from_buffer(buffer,global.format_simple);
global.texture = sprite_add("texture.png",0,0,0,0,0);

global.cube = vertex_create_buffer();
vertex_begin(global.cube,global.format_simple);


vertex_position_3d(global.cube,-10,10,-10);
vertex_normal(global.cube,0,1,0);
vertex_texcoord(global.cube,1,1);
vertex_position_3d(global.cube,10,10,-10);
vertex_normal(global.cube,0,1,0);
vertex_texcoord(global.cube,1,1);
vertex_position_3d(global.cube,-10,10,10);
vertex_normal(global.cube,0,1,0);
vertex_texcoord(global.cube,1,1);

vertex_position_3d(global.cube,-10,-10,-10);
vertex_normal(global.cube,0,1,0);
vertex_texcoord(global.cube,0,0);
vertex_position_3d(global.cube,10,-10,-10);
vertex_normal(global.cube,0,1,0);
vertex_texcoord(global.cube,0,0);
vertex_position_3d(global.cube,-10,-10,10);
vertex_normal(global.cube,0,1,0);
vertex_texcoord(global.cube,0,0);
vertex_end(global.cube);
vertex_freeze(global.cube);

global.system_camera = camera_create();

view_camera[0] = global.system_camera;
view_visible[0] = 1;
view_wport[0] = room_width;
view_hport[0] = room_height;

global.matrix_projection = matrix_build_projection_perspective_fov(45,room_width/room_height,0,3200);
global.matrix_lookat = matrix_build_lookat(0,200,40,0,0,0,0,1,0);

camera_set_proj_mat(global.system_camera,global.matrix_projection);
camera_set_view_mat(global.system_camera,global.matrix_lookat);

gpu_set_cullmode(cull_noculling);
gpu_set_ztestenable(false);
gpu_set_zwriteenable(true);
gpu_set_zfunc(cmpfunc_greaterequal);


layer_create(0,"system");
instance_create_layer(0,0,"system",system_engine);

And finally this is the drawing code:
//draw

draw_clear(make_colour_rgb(100,100,100));

matrix_set(matrix_world,matrix_build(0,0,0,0,0,0,1,1,1));

shader_set(sh_simple);

draw_set_color(c_white);
vertex_submit(global.cube,pr_trianglelist,sprite_get_texture(global.texture,0));

shader_reset();
matrix_set(matrix_world,matrix_build_identity());
 
M

Misu

Guest
you've got ztest disabled

gpu_set_ztestenable(false);
Yeah I was helpping him earlier on Discord and he mainly had it set to true



I told him to set it to false to see if it triggers something and it did not do anything.
Although he should have switch it back to true in case if he does find a solution.

Anyway the closest I could go into finding the prob is that it might have something to do with how GMS2 draws depth according to vertex buffers he is using a script called buffer_load_obj.

You can find this script online. Check it -> https://pastebin.com/TUNjer8T
 

Nehemek

Member
Hello. I set it to true and nothing changed. Regardless the custom loading format, well I created a handcoded model to test if it was my script and the same thing happens.

Znear is 0. The models are 200 units away from the camera.
 
Top