Skip to content

Commit

Permalink
static mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitslime committed Jul 15, 2018
1 parent 3476c97 commit b65d725
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cvar.h"
#include "cmd.h"
#include "console.h"
#include "mesh.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -58,13 +59,29 @@ int main() {
con_init();
text_init();

// text_bind(t);
// con_draw(0, 0);
mesh_t mesh;
mesh_createStatic(&mesh, 2);
mesh_floatBufferData(&mesh, 0, 3, 3, (float[]){
0, 1, 0,
1, -1, 0,
-1, -1, 0
});

mesh_elementBufferData(&mesh, 1, 3, (unsigned int[]) {
0, 1, 2
});

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(0);
mesh_draw(&mesh, 3, 0);

text_bind(t);
con_draw(0, 0);

Expand Down
39 changes: 39 additions & 0 deletions src/mesh.c
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);
}
}
24 changes: 24 additions & 0 deletions src/mesh.h
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);

0 comments on commit b65d725

Please sign in to comment.