GameMaker [SOLVED] 3D Rendering issue.

Binsk

Member
SOLVED?: I'm not sure why, but if I switch from matrix_build_projection_perspective to matrix_build_projection_perspective_fov it renders as expected. That is what I normally use anyway so I'm going to call that the solution.

Howdy! I am no newbie to the 3D scene with GameMaker and elsewhere which is why I am baffled here. I'm just trying to set up a simple scene for testing purposes that renders a 3D plane as a floor.

As far as I can tell I'm not missing anything but I'm just getting a black screen (minus my debugging 2D circle which renders fine). Can someone just glance at my code and tell me if I'm missing something obvious?

CREATE CODE:
(create vbuffer for plane)
Code:
vertex_format_begin();
vertex_format_add_position_3d();
vertex_format_add_color();
vbFormat = vertex_format_end();


vbFloor = vertex_create_buffer();
vertex_begin(vbFloor, vbFormat);
    // Defined as a strip
vertex_position_3d(vbFloor, -.5, -5, 0);
vertex_color(vbFloor, c_white, 1.);
vertex_position_3d(vbFloor, .5, -5, 0);
vertex_color(vbFloor, c_white, 1.);
vertex_position_3d(vbFloor, -.5, 5, 0);
vertex_color(vbFloor, c_white, 1.);
vertex_position_3d(vbFloor, .5, 5, 0);
vertex_color(vbFloor, c_white, 1.);

vertex_end(vbFloor);
vertex_freeze(vbFloor);

gpu_set_cullmode(cull_noculling);
DRAW CODE:
Code:
///@desc Render Scene
draw_set_color(c_white);
draw_set_alpha(1.);

// Save old matrices for reset
var _proj_old = matrix_get(matrix_projection),
    _view_old = matrix_get(matrix_view),
    _world_old = matrix_get(matrix_world);
 
// Build new matrices
var _proj_new = matrix_build_projection_perspective(room_width, room_height, 0.01, 1024);
var _view_new =  matrix_build_lookat(-128, -128, 128, 0, 0, 0, 0, 0, 1);

matrix_set(matrix_projection, _proj_new);
matrix_set(matrix_view, _view_new);

// Render a floor:
matrix_set(matrix_world, matrix_build(0, 0, 0, 0, 0, 0, 50, 50, 50));
shader_set(dummyShd);
vertex_submit(vbFloor, pr_trianglestrip, -1);
shader_reset();
 
matrix_set(matrix_projection, _proj_old);
matrix_set(matrix_view, _view_old);
matrix_set(matrix_world, _world_old);

draw_circle(100, 100, 50, false);
My shader code is the default shader minus texture handling. I've ruled out the shader as I've forced it to just display white and I still get nothing.

I'm sure that I'm just missing something really stupid so if someone with fresh eyes can take a gander it would be appreciated. I've been programming a lot and my brain is a bit pooped so I think I'm glazing over something.

Thanks in advance.
 
Last edited:
Even though I only have GMS1, I'm curious what the issue was. Would you mind posting here each projection matrix? The one you used before that didn't work, as well as the one that did?

You should be able to get the matrix in a useful format with this:
Code:
var _m = matrix_get(matrix_projection);
var _str =
    string_format(_m[0],0,12) + ", " + string_format(_m[4],0,12) + ", " + string_format(_m[8],0,12) + ", " + string_format(_m[12],0,12) + "#" +
    string_format(_m[1],0,12) + ", " + string_format(_m[5],0,12) + ", " + string_format(_m[9],0,12) + ", " + string_format(_m[13],0,12) + "#" +
    string_format(_m[2],0,12) + ", " + string_format(_m[6],0,12) + ", " + string_format(_m[10],0,12) + ", " + string_format(_m[14],0,12) + "#" +
    string_format(_m[3],0,12) + ", " + string_format(_m[7],0,12) + ", " + string_format(_m[11],0,12) + ", " + string_format(_m[15],0,12);
clipboard_set_text(_str);
 

Binsk

Member
Sure.

The working matrix (with the fov argument):
Code:
0.750000000000, 0.000000000000, 0.000000000000, 0.000000000000
0.000000000000, 1.000000000000, 0.000000000000, 0.000000000000
0.000000000000, 0.000000000000, 1.000009775162, -0.010000097565
0.000000000000, 0.000000000000, 1.000000000000, 0.000000000000
The bad matrix (without the fov argument):
Code:
0.000019531250, 0.000000000000, 0.000000000000, 0.000000000000
0.000000000000, 0.000026041665, 0.000000000000, 0.000000000000
0.000000000000, 0.000000000000, 1.000009775162, -0.010000097565
0.000000000000, 0.000000000000, 1.000000000000, 0.000000000000
Certainly a difference and these matrices both look weird to me. Number 2 (the bad one), position m_00 and m_11 are using orthographic projection values, it seems (calculating it by hand). However I can't figure out where m_22 and _m32 came from as I simply can't get these values from my settings. It might have something to do with m_23 being +1 instead of -1?

I'm not an expert on projection matrices but GM is certainly doing something funky.

EDIT: For future reference, my room_width = 1024 and room_height = 768.
 
If the second matrix is orthographic, then it is for a gigantic view size. 102400x76800. But it cannot be an orthographic projection because of the 1 in the third column, fourth row. An orthographic projection will have the 1 in the fourth column, fourth row. It must be a perspecive projection with a vary wide field of view... like almost 180 degrees. That would probably explain why you can't see anything when using that projection. As you approach a field of view of 180 degrees, using the kind of projection math we have here, everything in front of the camera collapses into a single point.

The values at m_22 and m_32 are related to the near and far clipping distances, and they determine the values written to the depth buffer (citation needed). Unless I'm mistaken, I believe they are calculated like this:
m_22 = f/(f-n)
m_32 = (f*n)/(n-f)
It looks like your near and far distances are 0.01 and 1024.

I'm going to take a stab at an explanation here and say that maybe the near and far values you provided to matrix_build_projection_perspective are for some reason causing it to calculate a bad field of view value. Maybe because the far value is so much greater than the near value??
 
Last edited:
Top