-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.c
109 lines (77 loc) · 2.38 KB
/
mesh.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "mesh.h"
#include "util.h"
#include <epoxy/gl.h>
#include <stdlib.h>
#include <string.h>
struct mesh_internal {
GLuint ebo;
GLuint vbo;
GLuint vao;
};
struct mesh* mesh_init(struct mesh* m, int nv,
struct vertex* vtx, int ni, int* ind)
{
struct mesh_internal* mi;
m->num_inds = ni;
m->num_verts = nv;
m->inds = calloc(ni, sizeof(int));
m->verts = calloc(nv, sizeof(struct vertex));
m->internal = calloc(1, sizeof(struct mesh_internal));
memcpy(m->inds, ind, ni * sizeof(int));
memcpy(m->verts, vtx, nv * sizeof(struct vertex));
mi = m->internal;
glGenBuffers(1, &mi->ebo);
glGenBuffers(1, &mi->vbo);
glGenVertexArrays(1, &mi->vao);
glBindVertexArray(mi->vao);
glBindBuffer(GL_ARRAY_BUFFER, mi->vbo);
glBufferData(GL_ARRAY_BUFFER,
m->num_verts * sizeof(struct vertex),
m->verts, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mi->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
m->num_inds * sizeof(int),
m->inds, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
sizeof(struct vertex), NULL);
glBindVertexArray(0);
m->guid = generate_guid();
return m;
}
void mesh_term(struct mesh* m)
{
struct mesh_internal* mi;
mi = m->internal;
glDeleteBuffers(1, &mi->ebo);
glDeleteBuffers(1, &mi->vbo);
glDeleteVertexArrays(1, &mi->vao);
free(mi);
m->internal = NULL;
free(m->verts);
free(m->inds);
free(m->guid);
}
void mesh_draw(struct mesh* m)
{
struct mesh_internal* mi;
mi = m->internal;
glBindVertexArray(mi->vao);
//glDrawElements(GL_TRIANGLES, m->num_inds, GL_UNSIGNED_INT, NULL);
//glDrawArrays(GL_POINTS, 0, m->num_verts);
//glDrawArrays(GL_LINES, 0, m->num_verts);
glDrawElements(GL_TRIANGLES, m->num_inds, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
}
void mesh_update(struct mesh* m, struct vertex* vtx)
{
struct mesh_internal* mi;
mi = m->internal;
glBindVertexArray(mi->vao);
memcpy(m->verts, vtx, m->num_verts * sizeof(struct vertex));
glBindBuffer(GL_ARRAY_BUFFER, mi->vbo);
glBufferData(GL_ARRAY_BUFFER,
m->num_verts * sizeof(struct vertex),
m->verts, GL_STATIC_DRAW);
glBindVertexArray(0);
}