-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.c
65 lines (49 loc) · 1.78 KB
/
model.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "model.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <glad/glad.h>
void mk_model(model_t This, const float* vertices, size_t vertexSize, const unsigned int* indices, size_t indexSize) {
/* Create a Vertex Array Object: */
glGenVertexArrays(1, &This[0]);
glBindVertexArray(This[0]);
/* Generate a Vertex Buffer Object: */
glGenBuffers(1, &This[1]);
glBindBuffer(GL_ARRAY_BUFFER, This[1]);
glBufferData(GL_ARRAY_BUFFER, vertexSize, vertices, GL_STATIC_DRAW);
/* Create the attributes for the shader: */
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
/* Create a Index Buffer Object: */
glGenBuffers(1, &This[2]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, This[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize, indices, GL_STATIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
This[3] = (unsigned int)(indexSize / sizeof(unsigned int));
}
void model_begin(model_t This) {
glBindVertexArray(This[0]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
}
void model_end() {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);
}
void model_draw(model_t This, unsigned int mode) {
glDrawElements(mode, This[3], GL_UNSIGNED_INT, NULL);
}
void model_finalize(model_t This) {
glDeleteVertexArrays(1, &This[0]);
glDeleteBuffers(1, &This[1]);
glDeleteBuffers(1, &This[2]);
}