-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvao.c
163 lines (137 loc) · 3.67 KB
/
vao.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <glisy/gl.h>
#include <glisy/vao.h>
#include <glisy/buffer.h>
void
glisy_vao_init(glisy_vao *vao) {
if (!vao) return;
vao->length = 0;
vao->useElements = GL_FALSE;
GLsizei attrsize = sizeof(glisy_vao_attribute) * GLISY_MAX_VAO_ATTRIBS;
memset(vao->attributes, 0, attrsize);
glGenVertexArrays(1, &vao->handle);
}
void
glisy_vao_bind(glisy_vao *vao) {
if (!vao) return;
glBindVertexArray(vao->handle);
}
void
glisy_vao_unbind(glisy_vao *vao) {
if (!vao) return;
glBindVertexArray(0);
}
void
glisy_vao_dispose(glisy_vao *vao) {
if (!vao) return;
glDeleteVertexArrays(1, &vao->handle);
vao->handle = 0;
vao->length = 0;
vao->useElements = GL_FALSE;
}
void
glisy_vao_update(glisy_vao *vao, glisy_buffer *elements) {
if (!vao) return;
// number of bound attributes for this VAO
GLuint length = vao->length;
// vertex buffer objects of VAO attribute length
GLuint vbo[length];
// bind vao
glisy_vao_bind(vao);
// init vbo
glGenBuffers(length, vbo);
// bind elements if given
if (elements) {
glisy_buffer_bind(elements);
vao->useElements= GL_TRUE;
} else {
vao->useElements= GL_FALSE;
}
// bind vao attributes
for (int i = 0; i < length; ++i) {
GLuint location = i;
glisy_vao_attribute *attr = &vao->attributes[i];
// ensure .normalized is true or false
if (GL_TRUE != attr->buffer.normalized) {
attr->buffer.normalized = GL_FALSE;
}
// bind current vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo[i]);
// set vbo data from attribute
glBufferData(GL_ARRAY_BUFFER,
attr->buffer.size,
attr->buffer.data,
attr->buffer.usage);
// bind attribute to location
glVertexAttribPointer(location,
attr->buffer.dimension,
attr->buffer.type,
attr->buffer.normalized,
0, 0);
// enable attribute at location
glEnableVertexAttribArray(location);
}
}
GLuint
glisy_vao_push(glisy_vao *vao, glisy_vao_attribute *attr) {
if (!vao) return GL_FALSE;
if (!attr) return GL_FALSE;
if (vao->length < GLISY_MAX_VAO_ATTRIBS) {
attr->location = vao->length;
vao->attributes[vao->length++] = *attr;
}
return vao->length;
}
GLuint
glisy_vao_pop(glisy_vao *vao) {
if (!vao) return GL_FALSE;
//@TODO(jwerle): implement glisy_vao_pop
return 0;
}
GLuint
glisy_vao_splice(glisy_vao *vao, GLint start, GLuint count) {
if (!vao) return GL_FALSE;
//@TODO(jwerle): implement glisy_vao_splice
return 0;
}
GLuint
glisy_vao_set(glisy_vao *vao, GLuint location, glisy_vao_attribute *attr) {
if (!vao) return GL_FALSE;
if (!attr) return GL_FALSE;
GLuint max = GLISY_MAX_VAO_ATTRIBS;
if (vao->length < max && location < max) {
attr->location = location;
vao->attributes[location] = *attr;
vao->length = location < vao->length ? vao->length : location;
}
return vao->length;
}
GLuint
glisy_vao_remove(glisy_vao *vao, GLuint location) {
if (!vao) return GL_FALSE;
//@TODO(jwerle): implement glisy_vao_remove
return 0;
}
void
glisy_vao_attribute_bind(glisy_vao_attribute *attribute) {
if (!attribute) return;
GLfloat data[4];
GLuint dimension = attribute->buffer.dimension;
GLuint location = attribute->location;
memcpy(&data, &attribute->buffer.data, sizeof(data));
switch (dimension) {
case 4:
glVertexAttrib4f(location, data[0], data[1], data[2], data[3]);
break;
case 3:
glVertexAttrib3f(location, data[0], data[1], data[2]);
break;
case 2:
glVertexAttrib2f(location, data[0], data[1]);
break;
case 1:
glVertexAttrib1f(location, data[0]);
break;
}
}