summaryrefslogtreecommitdiff
path: root/libs/pwyazh/mesh.h
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-01-19 17:33:44 +0500
committerpryazha <pryadeiniv@mail.ru>2025-01-19 17:33:44 +0500
commitbd49bd525f4c6c6c15c4142bf42d1dd38be6fc16 (patch)
tree9e69f473c34b53e9e57d8af1873c39698bf5c80e /libs/pwyazh/mesh.h
initial commit
Diffstat (limited to 'libs/pwyazh/mesh.h')
-rw-r--r--libs/pwyazh/mesh.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/libs/pwyazh/mesh.h b/libs/pwyazh/mesh.h
new file mode 100644
index 0000000..9443a13
--- /dev/null
+++ b/libs/pwyazh/mesh.h
@@ -0,0 +1,76 @@
+#ifndef MESH_H
+#define MESH_H
+
+Vertex
+vertex(V3F pos, V3F normal, V2F tex_coords)
+{
+ Vertex result;
+ result.pos = pos;
+ result.normal = normal;
+ result.tex_coords = tex_coords;
+ return(result);
+}
+
+Mesh *
+mesh_init(Arena *arena,
+ Vertex *vertices, U32 vertex_count,
+ U32 *indices, U32 index_count)
+{
+ Mesh *mesh = 0;
+ U32 vbo, ebo;
+
+ if (!vertices)
+ {
+ printf("[ERROR]: Vertices is null\n");
+ return(mesh);
+ }
+ if (!indices)
+ {
+ printf("[ERROR]: Indices is null\n");
+ return(mesh);
+ }
+
+ Assert(arena);
+ mesh = arena_push_size(arena, sizeof(Mesh));
+
+ mesh->vertices = vertices;
+ mesh->vertex_count = vertex_count;
+
+ mesh->indices = indices;
+ mesh->index_count = index_count;
+
+ glGenVertexArrays(1, &mesh->vao);
+ glBindVertexArray(mesh->vao);
+
+ glGenBuffers(1, &vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER, vertex_count*sizeof(Vertex), vertices, GL_STATIC_DRAW);
+
+ glGenBuffers(1, &ebo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count*sizeof(U32), indices, GL_STATIC_DRAW);
+
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
+ (void *)(OffsetOfMember(Vertex, pos)));
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
+ (void *)(OffsetOfMember(Vertex, normal)));
+ glEnableVertexAttribArray(2);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
+ (void *)(OffsetOfMember(Vertex, tex_coords)));
+
+ glBindVertexArray(0);
+
+ return(mesh);
+}
+
+void
+mesh_draw(Mesh *mesh)
+{
+ glBindVertexArray(mesh->vao);
+ glDrawElements(GL_TRIANGLES, mesh->index_count, GL_UNSIGNED_INT, 0);
+ glBindVertexArray(0);
+}
+
+#endif /* MESH_H */ \ No newline at end of file