#include "model.h" #include "gldefs.h" #include "macros.h" #include "sys.h" #include mat get_model_matrix(v3 pos, v3 angles, v3 scale) { mat result = make_scale(scale); result = rotate_mat(result, angles); result = translate_mat(result, pos); return result; /* mat translate = make_scale(transform.scale); mat rotate = make_rotate(transform.scale); mat scale = make_translate(transform.scale); return mulmat(mulmat(translate, rotate), scale); */ } static void init_mesh_buffers(struct mesh *mesh) { gl_gen_vertex_arrays(1, &mesh->vao); gl_gen_buffers(1, &mesh->vbo); gl_gen_buffers(1, &mesh->ebo); gl_bind_vertex_array(mesh->vao); gl_bind_buffer(gl_array_buffer, mesh->vbo); gl_bind_buffer(gl_element_array_buffer, mesh->ebo); u64 size = mesh->nvertices * sizeof(struct vertex); gl_buffer_data(gl_array_buffer, size, mesh->vertices, gl_static_draw); size = mesh->nindices * sizeof(u32); gl_buffer_data(gl_element_array_buffer, size, mesh->indices, gl_static_draw); u64 vertex_size = sizeof(struct vertex); gl_enable_vertex_attrib_array(pos_loc); gl_vertex_attrib_pointer(pos_loc, 3, gl_float, 0, vertex_size, (void *)offsetof(struct vertex, pos)); gl_enable_vertex_attrib_array(norm_loc); gl_vertex_attrib_pointer(norm_loc, 3, gl_float, 0, vertex_size, (void *)offsetof(struct vertex, norm)); gl_enable_vertex_attrib_array(texc_loc); gl_vertex_attrib_pointer(texc_loc, 2, gl_float, 0, vertex_size, (void*)offsetof(struct vertex, texc)); gl_bind_buffer(gl_element_array_buffer, 0); gl_bind_buffer(gl_array_buffer, 0); gl_bind_vertex_array(0); } struct mesh init_mesh(v3 pos, v3 angles, v3 scale, i32 nvertices, struct vertex *vertices, i32 nindices, u32 *indices, struct texture *texture) { struct mesh mesh = {0}; mesh.pos = pos; mesh.angles = angles; mesh.scale = scale; mesh.nvertices = nvertices; mesh.vertices = vertices; mesh.nindices = nindices; mesh.indices = indices; mesh.texture = texture; init_mesh_buffers(&mesh); return mesh; } void clear_mesh(struct mesh *mesh) { gl_bind_vertex_array(mesh->vao); gl_disable_vertex_attrib_array(pos_loc); gl_disable_vertex_attrib_array(norm_loc); gl_disable_vertex_attrib_array(texc_loc); gl_bind_vertex_array(0); gl_delete_vertex_arrays(1, &mesh->vao); gl_delete_buffers(1, &mesh->vbo); gl_delete_buffers(1, &mesh->ebo); } struct mesh gen_quad(struct arena *arena, v3 pos, v3 angles, v3 scale, f32 width, f32 height) { if (width < 0.1f) info("quad width less than 0.1... no"); width = max(0.1f, width); if (height < 0.1f) info("quad height less than 0.1... no"); height = max(0.1f, height); if (scale.x < 0.1f || scale.y < 0.1f || scale.z < 0.1f) info("quad scale less than 0.1... no"); scale = maxv3((v3){0.1f, 0.1f, 0.1f}, scale); i32 nvertices = 4; u64 size = nvertices * sizeof(struct vertex); struct vertex *vertices = push_arena(arena, size); memset(vertices, 0, size); vertices[0].pos = (v3){-width/2.0f, -height/2.0f, 0.0f}; vertices[1].pos = (v3){-width/2.0f, height/2.0f, 0.0f}; vertices[1].texc = (v2){0.0f, height}; vertices[2].pos = (v3){width/2.0f, -height/2.0f, 0.0f}; vertices[2].texc = (v2){width, 0.0f}; vertices[3].pos = (v3){width/2.0f, height/2.0f, 0.0f}; vertices[3].texc = (v2){width, height}; i32 nindices = 6; size = nindices * sizeof(u32); u32 *indices = push_arena(arena, size); indices[0] = 0; indices[1] = 1; indices[2] = 3; indices[3] = 0; indices[4] = 2; indices[5] = 3; return init_mesh(pos, angles, scale, nvertices, vertices, nindices, indices, 0); } struct model init_model(v3 pos, v3 angles, v3 scale, i32 nmeshes, struct mesh *meshes) { struct model model = {0}; model.pos = pos; model.angles = angles; model.scale = scale; model.nmeshes = nmeshes; model.meshes = meshes; return model; }