-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "mesh.h" | ||
|
||
void mesh_createStatic(mesh_t *dest, int numBuffers) { | ||
glGenVertexArrays(1, &dest->vao); | ||
glBindVertexArray(dest->vao); | ||
|
||
glGenBuffers(numBuffers, dest->vbo); | ||
dest->numBuffers = numBuffers; | ||
|
||
dest->flags = MESH_STATIC_DRAW; | ||
} | ||
void mesh_free(mesh_t *mesh) { | ||
glDeleteVertexArrays(1, &mesh->vao); | ||
glDeleteBuffers(mesh->numBuffers, mesh->vbo); | ||
} | ||
|
||
void mesh_floatBufferData(mesh_t *mesh, int buffer, int type, int size, const float *data) { | ||
glBindVertexArray(mesh->vao); | ||
glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo[buffer]); | ||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * type * size, data, GL_STATIC_DRAW); | ||
glEnableVertexAttribArray(buffer); | ||
glVertexAttribPointer(buffer, type, GL_FLOAT, GL_FALSE, sizeof(float) * type, 0); | ||
} | ||
|
||
void mesh_elementBufferData(mesh_t *mesh, int buffer, int size, const unsigned int *data) { | ||
glBindVertexArray(mesh->vao); | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->vbo[buffer]); | ||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * size, data, GL_STATIC_DRAW); | ||
mesh->flags |= MESH_INDEXED; | ||
} | ||
|
||
void mesh_draw(mesh_t *mesh, int count, int offset) { | ||
glBindVertexArray(mesh->vao); | ||
if (mesh->flags & MESH_INDEXED) { | ||
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)(offset * sizeof(unsigned int))); | ||
} else { | ||
glDrawArrays(GL_TRIANGLES, offset, count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
#include <glad/glad.h> | ||
|
||
#define MESH_MAX_BUFFERS 8 | ||
|
||
enum { | ||
MESH_STATIC_DRAW = 0, | ||
MESH_INDEXED = 1 << 0 | ||
}; | ||
|
||
typedef struct mesh_t { | ||
GLuint vao; | ||
GLuint vbo[MESH_MAX_BUFFERS]; | ||
int numBuffers; | ||
int flags; | ||
} mesh_t; | ||
|
||
void mesh_createStatic(mesh_t *dest, int numBuffers); | ||
void mesh_free(mesh_t *mesh); | ||
|
||
void mesh_floatBufferData(mesh_t *mesh, int buffer, int type, int size, const float *data); | ||
void mesh_elementBufferData(mesh_t *mesh, int buffer, int size, const unsigned int *data); | ||
|
||
void mesh_draw(mesh_t *mesh, int count, int offset); |