/
model.cpp
83 lines (69 loc) · 1.59 KB
/
model.cpp
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
#include "model.h"
Model::Model() {
}
void Model::init() {
#ifdef __APPLE__
glGenVertexArraysAPPLE(1, &vao);
glBindVertexArrayAPPLE(vao);
#else
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
#endif
program = InitShader(vShader, fShader);
glUseProgram(program);
buffers = new GLuint[bufferCount];
glGenBuffers(bufferCount, buffers);
buildModel();
modelView = glGetUniformLocation(program, "modelView");
projection = glGetUniformLocation(program, "projection");
unbindModel();
}
void Model::buildTri(vec3* points, int index, vec3 p1, vec3 p2, vec3 p3) {
points[index] = p1;
points[index+1] = p2;
points[index+2] = p3;
}
void Model::unbindModel() {
#ifdef __APPLE__
glBindVertexArrayAPPLE(0);
#else
glBindVertexArray(0);
#endif
}
void Model::bindModel() {
#ifdef __APPLE__
glBindVertexArrayAPPLE(vao);
#else
glBindVertexArray(vao);
#endif
glUseProgram(program);
}
void Model::render() {
bindModel();
if(!customRender()) {
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
}
unbindModel();
}
//{{{ matrix set functions
/*void Model::setWorldView(mat4 mat) {
glUniformMatrix4fv(worldView, 1, GL_TRUE, mat);
}*/
void Model::setModelView(mat4 mat) {
glUseProgram(program); //ensure that the correct program is bound
glUniformMatrix4fv(modelView, 1, GL_TRUE, mat);
}
void Model::setProjection(mat4 mat) {
glUseProgram(program); //ensure that the correct program is bound
glUniformMatrix4fv(projection, 1, GL_TRUE, mat);
}
//}}}
//{{{ optional functions
bool Model::customRender() {
return false;
}
void Model::preRender() {
}
void Model::postRender() {
}
//}}}