diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-03-27 08:27:46 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-03-27 08:27:46 +0500 |
commit | 92a8eb610f40c9770569ca63ca1bd705a6d3497d (patch) | |
tree | 7cb8edbf29e51aea24bc55be066b55033fe8fa1d /prge_model.c | |
parent | b1389bad67cccd3da6815c2d5a436c177f09594b (diff) |
audio, renaming
Diffstat (limited to 'prge_model.c')
-rw-r--r-- | prge_model.c | 209 |
1 files changed, 92 insertions, 117 deletions
diff --git a/prge_model.c b/prge_model.c index 44ded76..e2dc55f 100644 --- a/prge_model.c +++ b/prge_model.c @@ -1,70 +1,72 @@ -Vertex vert(V3 pos, V2 texc) +Vertex init_vert(V3 pos, V2 texc) { - Vertex v; - - v.pos = pos; - v.texc = texc; - + Vertex v = { + .pos = pos, + .texc = texc, + }; return v; } -void mesh_init_buffers(Mesh *mesh) +void init_mesh_buffers(Mesh *mesh) { - Assert(mesh->verts); - Assert(mesh->nverts > 0); - + ASSERT(mesh->verts); + ASSERT(mesh->nverts > 0); + + U32 sverts, + sindices; + + sverts = mesh->nverts*sizeof(Vertex); + sindices = mesh->nindices*sizeof(U32); + glGenVertexArrays(1, &mesh->vao); glBindVertexArray(mesh->vao); glGenBuffers(1, &mesh->vbo); glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); - glBufferData(GL_ARRAY_BUFFER, mesh->nverts*sizeof(Vertex), mesh->verts, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sverts, + mesh->verts, GL_STATIC_DRAW); if (mesh->indices && (mesh->nindices > 0)) { glGenBuffers(1, &mesh->ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->nindices*sizeof(U32), mesh->indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sindices, + mesh->indices, GL_STATIC_DRAW); } glEnableVertexAttribArray(PRGE_SHADER_POS_LOC); - glVertexAttribPointer(PRGE_SHADER_POS_LOC, 3, GL_FLOAT, GL_FALSE, - sizeof(Vertex), OffsetOfMember(Vertex, pos)); + glVertexAttribPointer(PRGE_SHADER_POS_LOC, 3, + GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)OFFSETOF(Vertex, pos)); + glEnableVertexAttribArray(PRGE_SHADER_TEXC_LOC); - glVertexAttribPointer(PRGE_SHADER_TEXC_LOC, 2, GL_FLOAT, GL_FALSE, - sizeof(Vertex), (void *)OffsetOfMember(Vertex, texc)); + glVertexAttribPointer(PRGE_SHADER_TEXC_LOC, 2, + GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)OFFSETOF(Vertex, texc)); + glBindVertexArray(0); } -Mesh *mesh_init(Arena *arena, V3 origin, V3 rotate, Vertex *verts, U32 nverts, U32 *indices, U32 nindices) +Mesh init_mesh(V3 origin, V3 rotate, S32 nverts, Vertex *verts, S32 nindices, U32 *indices) { - Mesh *mesh; - - mesh = arena_push(arena, sizeof(Mesh)); - - mesh->origin = origin; - mesh->rotate = rotate; + Mesh mesh; + + MEM0STRUCT(&mesh); - mesh->verts = verts; - mesh->nverts = nverts; + mesh.origin = origin; + mesh.rotate = rotate; - mesh->indices = indices; - mesh->nindices = nindices; + mesh.verts = verts; + mesh.nverts = nverts; - mesh->ntextures = 0; - MemoryZeroArray(mesh->textures); + mesh.indices = indices; + mesh.nindices = nindices; - mesh_init_buffers(mesh); + init_mesh_buffers(&mesh); return mesh; } -void mesh_add_texture(Mesh *mesh, Texture texture) -{ - Assert(mesh->ntextures+1 <= MAX_TEXTURE); - mesh->textures[mesh->ntextures++] = texture; -} - -void mesh_clear(Mesh *mesh) +void clear_mesh(Mesh *mesh) { glBindVertexArray(mesh->vao); glDisableVertexAttribArray(PRGE_SHADER_POS_LOC); @@ -79,43 +81,37 @@ void mesh_clear(Mesh *mesh) glDeleteBuffers(1, &mesh->ebo); } -void mesh_draw(Mesh *mesh) +void add_mesh_texture(Mesh *mesh, Texture texture) { - S32 i; - - Assert(mesh->vao); - - glBindVertexArray(mesh->vao); - - for (i = 0; i < mesh->ntextures; i++) { - glActiveTexture(GL_TEXTURE0+i); - glBindTexture(GL_TEXTURE_2D, mesh->textures[i].id); + if (mesh->ntextures+1 >= PRGE_MAX_TEXTURES) { + sys_printf("[WARNING] : Texture : \"%s\" : limit\n", texture.name); + return; } + mesh->textures[mesh->ntextures++] = texture; +} - if (mesh->ebo) - glDrawElements(GL_TRIANGLES, mesh->nindices, GL_UNSIGNED_INT, 0); - else - glDrawArrays(GL_TRIANGLES, 0, mesh->nverts); - - glBindVertexArray(0); -} - -Mesh *mesh_gen_quad(Arena *arena, V3 origin, V3 rotate, F32 w, F32 h) +Mesh gen_quad(Arena *arena, V3 origin, V3 rotate, F32 w, F32 h) { - Mesh *m; + Mesh mesh; + + S32 nverts; Vertex *verts; + + S32 nindices; U32 *indices; - U32 nverts, nindices; + + ASSERT(w > 0); + ASSERT(h > 0); nverts = 4; - verts = arena_push(arena, nverts*sizeof(Vertex)); - verts[0] = vert(v3(-w/2.0f, -h/2.0f, 0.0f), v2(0.0f, 0.0f)); - verts[1] = vert(v3(-w/2.0f, h/2.0f, 0.0f), v2(0.0f, 1.0f)); - verts[2] = vert(v3(w/2.0f, -h/2.0f, 0.0f), v2(1.0f, 0.0f)); - verts[3] = vert(v3(w/2.0f, h/2.0f, 0.0f), v2(1.0f, 1.0f)); + verts = push_arena(arena, nverts*sizeof(Vertex)); + verts[0] = init_vert(v3(-w/2.0f, -h/2.0f, 0.0f), v2(0.0f, 0.0f)); + verts[1] = init_vert(v3(-w/2.0f, h/2.0f, 0.0f), v2(0.0f, h)); + verts[2] = init_vert(v3(w/2.0f, -h/2.0f, 0.0f), v2(w, 0.0f)); + verts[3] = init_vert(v3(w/2.0f, h/2.0f, 0.0f), v2(w, h)); nindices = 6; - indices = arena_push(arena, nindices*sizeof(U32)); + indices = push_arena(arena, nindices*sizeof(U32)); indices[0] = 0; indices[1] = 1; indices[2] = 3; @@ -123,90 +119,69 @@ Mesh *mesh_gen_quad(Arena *arena, V3 origin, V3 rotate, F32 w, F32 h) indices[4] = 2; indices[5] = 3; - m = mesh_init(arena, origin, rotate, verts, nverts, indices, nindices); + mesh = init_mesh(origin, rotate, nverts, verts, nindices, indices); - return m; + return mesh; } -Mesh *mesh_gen_circle(Arena *arena, V3 origin, V3 rotate, F32 r, U32 nverts) +Mesh gen_circle(Arena *arena, V3 origin, V3 rotate, F32 r, S32 nverts) { - Mesh *m; + Mesh mesh; + Vertex *verts; + + S32 nindices; U32 *indices; - U32 nindices; + F32 angle, dangle; - U32 i, vi; + + S32 i, vi; if (nverts < 3) nverts = 3; - verts = arena_push(arena, nverts*sizeof(Vertex)); + verts = push_arena(arena, nverts*sizeof(Vertex)); - dangle = 2*F32_PI/(F32)nverts; + dangle = 2*F32PI/(F32)nverts; for (i = 0, angle = 0.0f; i < nverts; i++, angle += dangle) - verts[i] = vert(v3(f32cos(angle)*r, f32sin(angle)*r, 0.0f), V2_ZERO); + verts[i] = init_vert(v3(f32cos(angle)*r, f32sin(angle)*r, 0.0f), V2_ZERO); nindices = nverts*3; - indices = arena_push(arena, nindices*sizeof(U32)); + indices = push_arena(arena, nindices*sizeof(U32)); for (i = 0, vi = 1; i < nindices; i += 3, vi++) { indices[i] = 0; indices[i+1] = vi; indices[i+2] = (((vi+1) == (nverts)) ? 1 : (vi+1)); } - m = mesh_init(arena, origin, rotate, verts, nverts, indices, nindices); - - return m; -} - -Model *model_init(Arena *arena, V3 origin, V3 rotate, Mesh *meshes, U32 nmeshes) -{ - Model *res; + mesh = init_mesh(origin, rotate, nverts, verts, nindices, indices); - res = arena_push(arena, sizeof(Model)); - - res->origin = origin; - res->rotate = rotate; - res->meshes = meshes; - res->nmeshes = nmeshes; - - return res; + return mesh; } -Model *model_load(Arena *arena, V3 origin, V3 rotate, Str8 filename) +Model init_model(V3 origin, V3 rotate, S32 nmeshes, Mesh *meshes) { - Model *res; - - res = 0; - - /* TODO(pryazha): Load models with assimp? */ - - return res; + Model model = { + .origin = origin, + .rotate = rotate, + .nmeshes = nmeshes, + .meshes = meshes, + }; + return model; } -void model_draw(U32 shader, MAT4 *proj, MAT4 *view, Model *model) +Model load_model(V3 origin, V3 rotate, const char *fname) { - U32 i; - MAT4 modelmat, meshmodelmat, resmodelmat; - V3 resorigin, resrotate; - - glUseProgram(shader); - - shader_set_mat4fv(shader, PRGE_SHADER_PROJ_MAT, *proj); - shader_set_mat4fv(shader, PRGE_SHADER_VIEW_MAT, *view); + Model model; - modelmat = mat4rotate(MAT4_IDENTITY, model->rotate); - modelmat = mat4transl(modelmat, model->origin); + MEM0STRUCT(&model); - for (i = 0; i < model->nmeshes; i++) { - meshmodelmat = mat4rotate(MAT4_IDENTITY, model->meshes[i].rotate); - meshmodelmat = mat4transl(meshmodelmat, model->meshes[i].origin); + model.origin = origin; + model.rotate = rotate; - resmodelmat = mat4mul(modelmat, meshmodelmat); + sys_printf("[INFO] : PRGE : Trying to load %s\n", fname); - shader_set_mat4fv(shader, PRGE_SHADER_MODEL_MAT, resmodelmat); - mesh_draw(&model->meshes[i]); - } + /* TODO(pryazha): Load models with assimp? */ - glUseProgram(0); + return model; } |