forked from yiming-cai/OursCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.cpp
More file actions
226 lines (194 loc) · 6.98 KB
/
Copy pathCube.cpp
File metadata and controls
226 lines (194 loc) · 6.98 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "Cube.h"
std::vector<std::string> Cube::cubeTexturesAddress = {
"../assets/CubeTexture/cube_01.jpg",
"../assets/CubeTexture/cube_02.jpg",
"../assets/CubeTexture/cube_03.jpg",
"../assets/CubeTexture/cube_04.jpg",
"../assets/CubeTexture/cube_05.jpg",
"../assets/CubeTexture/cube_06.jpg",
"../assets/CubeTexture/cube_07.jpg",
"../assets/CubeTexture/cube_08.jpg",
"../assets/CubeTexture/cube_09.jpg",
"../assets/CubeTexture/cube_10.jpg",
"../assets/CubeTexture/cube_11.jpg",
"../assets/CubeTexture/cube_12.jpg",
"../assets/CubeTexture/cube_13.jpg",
"../assets/CubeTexture/cube_14.jpg",
"../assets/CubeTexture/cube_15.jpg",
"../assets/CubeTexture/cube_16.jpg",
"../assets/CubeTexture/cube_17.jpg"
};
std::vector<GLint> Cube::cubeTextureId = std::vector<GLint>(CUBE_TEXTURE_NUM, -1);
GLfloat Cube::cubeBoundBoxV[72] = {
0,0,0, 1,0,0, 1,1,0, 0,1,0, // front face counterclockwise
0,0,-1, 0,1,-1, 1,1,-1, 1,0,-1, // back face clockwise
0,0,-1, 0,0,0, 0,1,0, 0,1,-1, // left face clockwise
1,0,-1, 1,1,-1, 1,1,0, 1,0,0, // right face counterclockwise
0,1,0, 1,1,0, 1,1,-1, 0,1,-1, // top face counterclockwise
0,0,0, 0,0,-1, 1,0,-1, 1,0,0 // bottom face clockwise
};
GLfloat Cube::cubeBoundBoxN[18] = {
0,0,1,
0,0,-1,
-1,0,0,
1,0,0,
0,1,0,
0,-1,0
};
// The order is such that:
// the front face has the first 4 vertices, counterclockwise facing towards camera
// the front face has the last 4 vertices, counterclockwise facing away from camera
int Cube::cubeVerticesLen = 108;
GLfloat Cube::cubeVertices[108] = {
0,0,0, 1,0,0, 1,1,0,
1,1,0, 0,1,0, 0,0,0, // front face counterclockwise
0,0,-1, 0,1,-1, 1,1,-1,
1,1,-1, 1,0,-1, 0,0,-1, // back face clockwise
0,0,-1, 0,0,0, 0,1,0,
0,1,0, 0,1,-1, 0,0,-1, // left face clockwise
1,0,-1, 1,1,-1, 1,1,0,
1,1,0, 1,0,0, 1,0,-1, // right face counterclockwise
0,1,0, 1,1,0, 1,1,-1,
1,1,-1, 0,1,-1, 0,1,0, // top face counterclockwise
0,0,0, 0,0,-1, 1,0,-1,
1,0,-1, 1,0,0, 0,0,0 // bottom face clockwise
};
int Cube::cubeColorsLen = 108;
int Cube::cubeNormalsLen = 108;
GLfloat Cube::cubeNormals[108] = {
0,0,1, 0,0,1, 0,0,1, 0,0,1, 0,0,1, 0,0,1, // FRONT
0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, // BACK
-1,0,0, -1,0,0, -1,0,0, -1,0,0, -1,0,0, -1,0,0, // LEFT
1,0,0, 1,0,0, 1,0,0, 1,0,0, 1,0,0, 1,0,0, // RIGHT
0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, // UP
0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0 // DOWN
};
GLuint Cube::loadTexture(int pos)
{
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < 6; ++i) {
unsigned char *data = stbi_load(cubeTexturesAddress[pos].c_str(), &width, &height, &nrChannels, 0);
if (data) {
//printf("%d %d %d\n", width, height, nrChannels);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else {
printf("wrong in load skybox %s", cubeTexturesAddress[pos].c_str());
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return textureID;
}
Cube::Cube(int id, float size, glm::vec3 color) {
info.id = id;
info.size = size;
info.color = color;
info.textureId = -1;
this->id = id;
this->selected = 0;
this->haveTexture = 0;
this->size = size;
this->nameID = OBJECT_CUBE;
this->vertices = cubeVertices;
for (int i = 0; i < 36; ++i) {
for (int j = 0; j < 3; ++j)
cubeColors[3 * i + j] = color[j];
}
this->colors = cubeColors;
this->normals = cubeNormals;
this->boundBoxN = cubeBoundBoxN;
this->boundBoxV = cubeBoundBoxV;
this->shader = Shader_Geometry;
this->toWorld = glm::scale(glm::mat4(1.0f),glm::vec3(size,size,size));
verticesLen = cubeVerticesLen;
normalsLen = cubeNormalsLen;
colorsLen = cubeColorsLen;
//printf("in address %d\n", &P);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &CBO);
glGenBuffers(1, &NBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticesLen * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, NBO);
glBufferData(GL_ARRAY_BUFFER, normalsLen * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glBufferData(GL_ARRAY_BUFFER, colorsLen * sizeof(GLfloat), colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
//printf("Load Cube!\n");
}
Cube::Cube(int id, float size, int textureSeq)
{
info.id = id;
info.size = size;
info.color = glm::vec3(0);
info.textureId = textureSeq;
this->id = id;
this->selected = 0;
this->haveTexture = 1;
this->size = size;
this->nameID = OBJECT_CUBE;
this->vertices = cubeVertices;
for (int i = 0; i < 36; ++i) {
for (int j = 0; j < 3; ++j)
cubeColors[3 * i + j] = 1;
}
this->colors = cubeColors;
this->normals = cubeNormals;
this->boundBoxN = cubeBoundBoxN;
this->boundBoxV = cubeBoundBoxV;
this->shader = Shader_Geometry;
this->toWorld = glm::scale(glm::mat4(1.0f), glm::vec3(size, size, size));
verticesLen = cubeVerticesLen;
normalsLen = cubeNormalsLen;
colorsLen = cubeColorsLen;
//printf("in address %d\n", &P);
if (cubeTextureId.size() <= textureSeq) textureSeq = cubeTextureId.size() - 1;
if (cubeTextureId[textureSeq] == -1) {
cubeTextureId[textureSeq] = loadTexture(textureSeq);
}
textureID = cubeTextureId[textureSeq];
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &CBO);
glGenBuffers(1, &NBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticesLen * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, NBO);
glBufferData(GL_ARRAY_BUFFER, normalsLen * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glBufferData(GL_ARRAY_BUFFER, colorsLen * sizeof(GLfloat), colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
//printf("Load Cube!\n");
}
Cube::~Cube()
{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &NBO);
glDeleteBuffers(1, &CBO);
}