-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLittleOBJExtension.cpp
117 lines (102 loc) · 4.29 KB
/
LittleOBJExtension.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
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
//
// Created by Jonathan Andersson on 2022-01-05.
//
#include "LittleOBJExtension.h"
void TranslateModel(Model* m, float x, float y, float z) {
for (long int i = 0; i < m->numVertices; i++) {
m->vertexArray[i].x += x;
m->vertexArray[i].y += y;
m->vertexArray[i].z += z;
}
ReloadModelData(m);
}
void RotateModelY(Model* m, float angle) {
for (long int i = 0; i < m->numVertices; i++) {
vec4 current = vec3tovec4(m->vertexArray[i]);
m->vertexArray[i] = vec4tovec3(Ry(angle) * current);
}
}
void DrawModelInstanced(Model *m, GLuint program,
char* vertexVariableName,
char* normalVariableName,
char* texCoordVariableName,
char* colorVariableName,
char* translationVariableName,
char* angleVariableName,
int count) {
if (m != NULL) {
GLint loc;
glBindVertexArray(m->vao); // Select VAO
glBindBuffer(GL_ARRAY_BUFFER, m->vb);
loc = glGetAttribLocation(program, vertexVariableName);
if (loc >= 0) {
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(loc);
}
else
fprintf(stderr, "DrawModelInstanced warning: '%s' not found in shader!\n", vertexVariableName);
if (normalVariableName!=NULL) {
loc = glGetAttribLocation(program, normalVariableName);
if (loc >= 0) {
glBindBuffer(GL_ARRAY_BUFFER, m->nb);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(loc);
}
else
fprintf(stderr, "DrawModelInstanced warning: '%s' not found in shader!\n",
normalVariableName);
}
// VBO for texture coordinate data NEW for 5b
if ((m->texCoordArray != NULL)&&(texCoordVariableName != NULL)) {
loc = glGetAttribLocation(program, texCoordVariableName);
if (loc >= 0) {
glBindBuffer(GL_ARRAY_BUFFER, m->tb);
glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(loc);
}
else
fprintf(stderr, "DrawModelInstanced warning: '%s' not found in shader!\n",
texCoordVariableName);
}
if (colorVariableName!=NULL) {
loc = glGetAttribLocation(program, colorVariableName);
if (loc >= 0) {
glBindBuffer(GL_ARRAY_BUFFER, m->cb);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(loc);
}
else
fprintf(stderr, "DrawModelInstanced warning: '%s' not found in shader!\n",
colorVariableName);
}
if (translationVariableName!=NULL) {
loc = glGetAttribLocation(program, translationVariableName);
if (loc >= 0) {
glBindBuffer(GL_ARRAY_BUFFER, m->isb);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(loc);
// Divisor 1 sets variable to entire model
// rather than for each vertex in model.
glVertexAttribDivisor(loc, 1);
}
else
fprintf(stderr, "DrawModelInstanced warning: '%s' not found in shader!\n",
translationVariableName);
}
if (angleVariableName!=NULL) {
loc = glGetAttribLocation(program, angleVariableName);
if (loc >= 0) {
glBindBuffer(GL_ARRAY_BUFFER, m->ab);
glVertexAttribPointer(loc, 1, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(loc);
// Divisor 1 sets variable to entire model
// rather than for each vertex in model.
glVertexAttribDivisor(loc, 1);
}
else
fprintf(stderr, "DrawModelInstanced warning: '%s' not found in shader!\n",
angleVariableName);
}
glDrawElementsInstanced(GL_TRIANGLES, m->numIndices, GL_UNSIGNED_INT, 0L, count);
}
}