-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.cpp
More file actions
180 lines (152 loc) · 5.83 KB
/
Cube.cpp
File metadata and controls
180 lines (152 loc) · 5.83 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "Cube.h"
Cube::Cube(bool DEBUG, bool procedural) : Mesh(false, DEBUG), procedural(procedural){
if(procedural){
proceduralCube(1, 0.5f);
}
attachMesh();
}
void Cube::proceduralCube(int subd, float step){
float stepSize = step/float(subd);
std::cout << "Step size: " << stepSize << std::endl;
int x, y =0;
glm::vec3 *p;
glm::vec3 *n;
for(int face = 0; face < 6; face++){
x = 0;
for(float s = 0; s <= 1.0f; s +=stepSize){
x++;
y = 0;
for(float t = 0; t <= 1.0f; t += stepSize){
switch(face){
case 0:
p = new glm::vec3(1 - 2*s, 1-2*t, 1);
n = new glm::vec3(0, 0, 1);
break;
case 1:
p = new glm::vec3(1, 1 - 2*s, 1 - 2*t);
n = new glm::vec3(1, 0, 0);
break;
case 2:
p = new glm::vec3(1-2*s, -1, 1-2*t);
n = new glm::vec3(0, -1, 0);
break;
case 3:
p = new glm::vec3(-1, 1-2*s, 1- 2*t);
n = new glm::vec3(-1, 0, 0);
break;
case 4:
p = new glm::vec3(1-2*s, 1, 1-2*t);
n = new glm::vec3(0, 1, 0);
break;
case 5:
p = new glm::vec3(1-2*s, 1-2*t, -1);
n = new glm::vec3(0, 0, -1);
break;
}
y += 1;
// *p += translation;
std::cout << "Adding normal" << std::endl;
addNormal(*n);
addVertex(*p);
}
}
}
// print();
std::cout << "X: " << x << "\nY: " << y << std::endl;
this->numIndices = x;
genIndices(x, y);
// attachMesh();
}
void Cube::genIndices(int xNum, int yNum){
int k1, k2;
int k = yNum;
// std::cout << "xNum " << xNum << std::endl;
for(int face = 0; face < 6; face++){
for(int x = 0; x < xNum -1; x++){
k1 = (face*pow(xNum, 2)) + x*xNum +1;
k2 = k + k1;
// std::cout << "K1 : " << k1 << std::endl;
for(int y = 0; y < yNum -1; y++, k1++, k2++){
int a = k1;
int b = a -1;
int c = k2;
int d = c - 1;
addTriIndex(a, c, b);
addTriIndex(c, d, b);
}
}
}
}
void Cube::attachMesh(){
// std::cout << "Number of vertices: " << sizeof(v)/sizeof(v[0]) << std::endl;
// // std::cerr << "v: " + sizeof(v) << std::endl;
// for(int i =0 ; i < sizeof(v)/sizeof(v[0]); i++){
// std::cout << v[i] << std::endl;
// }
std::cout << "procedural: " << procedural << std::endl;
if(!procedural){
float verts[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
};
// std::cout << "vertex number : " << sizeof(v)/sizeof(v[0]) << std::endl;
glGenVertexArrays(1, &meshVAO);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glBindVertexArray(meshVAO);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
Mesh::numVertices = 36;
}else{
Mesh::attachMesh();
//vertex
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
//normal
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
}
}
void Cube::createFaces(){
}