GameMaker Vertex Buffer not showing up

S

SilentX

Guest
The following code is supposed to draw a tiled floor, leaving out spaces for holes. However, it's not appearing.
I have created a shader, and left the code as what was automatically created.
I'm not sure if I have to draw this anywhere else in code, or whether vertex_submit takes care of that.

vertex_format_begin();
vertex_format_add_position_3d();
vertex_format_add_color();
vertex_format_add_texcoord();
global.my_format = vertex_format_end();

v_buff1 = vertex_create_buffer()

for(i = 0; i < 30; i++)
{
for(j = 0; j < 30; j++)
{
if(!instance_position(i*32,j*32,obj_hole))
{
if(i < 25 && j < 24)
{
vertex_begin(v_buff1,global.my_format);

vertex_position_3d(v_buff1,i*32,j*32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,0,0);

vertex_position_3d(v_buff1,(i*32)+32,j*32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,1,0);

vertex_position_3d(v_buff1,(i*32)+32,(j*32)+32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,1,1);

vertex_position_3d(v_buff1,i*32,(j*32)+32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,0,1);

vertex_end(v_buff1);

var tex = sprite_get_texture(spr_floor1,0);
shader_set(shader_0);
vertex_submit(v_buff1,pr_trianglefan,tex);
shader_reset();
}
}
}
}
 

Binsk

Member
I know in GameMaker 1.4 that pr_trianglefan is not supported for custom vertex formats. I couldn't find anything saying something similar in the GameMaker 2.x manual, but if you pull up vertex_submit in the manual you will notice that pr_trianglefan is not in the list of possible definition formats.

As such, I assume you cannot use it when using a custom vertex format.
 
M

Misu

Guest
Well... you doing it in a very inefficient way. The proper way way of creating a vertex buffers by is by follows:

1. when setting the vertex to create the triangles, its usually performed in create event while vertex_submit is only used once in draw event after using the projection.

2. Assigning a texture to a variable is only required once. You dont have to put that within the loop.

3. vertex_begin is better off used before the for loop instead of within.

4. You declare vertex_end after the loop is entirely completed.

5. You have not assigned a position to your vertex buffer (which might be the cause why its not showing up). If using GMS1, use the d3d_transformation commands. Remember to set d3d_transform_set_identity() at the end. If you using GMS2 (which also works in GMS1) use matrix_build to assign a matrix into a variable where you will apply it to your vertex buffer in draw event by using matrix_set() .

Code:
/////////CREATE EVENT///////////////
vertex_format_begin();
vertex_format_add_position_3d();
vertex_format_add_color();
vertex_format_add_texcoord();
global.my_format = vertex_format_end();

var tex = sprite_get_texture(spr_floor1,0);///texture is outside the for loop

v_buff1 = vertex_create_buffer()
vertex_begin(v_buff1,global.my_format);

for(i = 0; i < 30; i++)
{
for(j = 0; j < 30; j++)
{
if(!instance_position(i*32,j*32,obj_hole))
{
if(i < 25 && j < 24)
{
vertex_position_3d(v_buff1,i*32,j*32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,0,0);

vertex_position_3d(v_buff1,(i*32)+32,j*32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,1,0);

vertex_position_3d(v_buff1,(i*32)+32,(j*32)+32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,1,1);

vertex_position_3d(v_buff1,i*32,(j*32)+32,0);
vertex_color(v_buff1,c_white,1);
vertex_texcoord(v_buff1,0,1);
} 
} 
}
}
vertex_end(v_buff1);
//vertex_freeze(v_buff2); //in case you want to make the vertex read-only

//////////DRAW EVENT//////////
shader_set(shader_0);
d3d_transform_add_translation(0,0,0)///place the models position
vertex_submit(v_buff1,pr_trianglefan,tex);
d3d_transform_set_identity()
shader_reset();
 
Last edited:
Top