This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMaker.cpp
379 lines (268 loc) · 13 KB
/
GameMaker.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "GameMaker.hpp"
//#include <glm/gtx/string_cast.hpp>
int i = 0;
float j = 0.0;
GameMaker::GameMaker(int mazeHeight, int mazeWidth) {
GameMaker::mazeHeight = mazeHeight;
GameMaker::mazeWidth = mazeWidth;
// Generate Maze
mg.Init(mazeHeight, mazeWidth);
mg.printMazeMap();
}
GameMaker::GameMaker(int mazeHeight, int mazeWidth, Physics::PhysicsWorld *pw) {
GameMaker::mazeHeight = mazeHeight;
GameMaker::mazeWidth = mazeWidth;
// Generate Maze
mg.Init(mazeHeight, mazeWidth);
mg.printMazeMap();
physicsWorld = pw;
}
void GameMaker::transferDataToGPUMemory(void) {
shaderTexture = LoadShaders("shaders/VertexShader.vert", "shaders/FragmentShader.frag");
shaderLamp = LoadShaders("shaders/lampShader.vs", "shaders/lampShader.fs");
shaderMaterial = LoadShaders("shaders/materials.vs", "shaders/materials.fs");
shaderMaterialAndTexture = LoadShaders("shaders/lampShader.vs", "shaders/lampShader.fs");
shaderBasicLightning = LoadShaders("shaders/basic_lighting.vs", "shaders/basic_lighting.fs");
shaderStandard = LoadShaders("shaders/StandardShading.vs", "shaders/StandardShading.fs");
shaderPhong = LoadShaders("shaders/phong_ilumination.vs", "shaders/phong_ilumination.fs");
transferCubeToGPUMemory();
transferFloorToGPUMemory();
transferHoleToGPUMemory();
loadPlayer();
uvmap = loadDDS("uvmap.DDS");
cubeMap = new CubeMap();
cubeMap->load();
}
void GameMaker::update(double dt){
physicsWorld->dynamicsWorld->stepSimulation(dt, 0.017);
setMVP();
drawMap();
vec3 playerPos = playerBody->getWorldPosition();
if ((int) playerPos.x == (int) end_point.getX() && (int) playerPos.z == (int) end_point.getZ()) {
// win
win = true;
}
}
void GameMaker::transferCubeToGPUMemory(void) {
Texture_crate = loadBMP_custom("images/crate.bmp");
static const GLfloat cube[] = {
// 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, // base
// 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // cima
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // parede 1
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // parede 2
1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // parede 3
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // parede 4
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f
};
// One color for each vertex. They were generated randomly.
static const GLfloat g_uv_buffer_data[] = {
0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f,
1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f,
1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
0.0f,0.0f, 1.0f,0.0f, 1.0f,1.0f,
0.0f,0.0f, 0.0f,1.0f, 1.0f,1.0f,
0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f,
1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
0.0f,0.0f, 1.0f,0.0f, 1.0f,1.0f,
0.0f,0.0f, 0.0f,1.0f, 1.0f,1.0f,
};
// Move vertex data to video memory; specifically to VBO called vertexbuffer
glGenBuffers(1, &cubeVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, cubeVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube), cube, GL_STATIC_DRAW);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
}
void GameMaker::transferFloorToGPUMemory(void) {
GameMaker::floor = new Object();
floor->loadOBJtoGPU("floor.obj", vec3(1.0f, 1.0f, 1.0f));
floor->loadLight(vec3(1.0f, 1.0f, 1.0f), vec3(1.0f, 1.0f, 1.0f));
}
void GameMaker::transferHoleToGPUMemory(void) {
Texture_hole = (GLuint*) malloc(4 * sizeof(GLuint));
Texture_hole[0] = loadBMP_custom("images/hole_1.bmp");
Texture_hole[1] = loadBMP_custom("images/hole_2.bmp");
Texture_hole[2] = loadBMP_custom("images/hole_3.bmp");
Texture_hole[3] = loadBMP_custom("images/hole_4.bmp");
static const GLfloat floor[] = {
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, // base
1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f
};
// One color for each vertex. They were generated randomly.
static const GLfloat g_uv_buffer_data_floor[] = {
1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f, // imagem rodada
1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f,
//0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f, // imagem normal
//1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
};
GameMaker::hole = new Object();
hole->loadToGPU(floor, g_uv_buffer_data_floor, 3*2);
}
glm::mat4 GameMaker::setMVP(void) {
// Projection matrix : 45∞ Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
Projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f);
// Camera matrix
View = glm::lookAt(
glm::vec3(cameraAt.x, cameraAt.y, cameraAt.z), // Camera is at (4,3,-3), in World Space
glm::vec3(lookAt.x, lookAt.y, lookAt.z), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix depends on the object, so it is defined somewhere in the program
Model = glm::rotate(Model, glm::radians(angulo), glm::vec3(0,1,0));
return Projection * View * Model;
}
void GameMaker::drawCube(glm::vec3 trans) {
glUseProgram(shaderTexture);
glm::mat4 Trans = glm::translate(glm::mat4(1.0f), trans);
//glm::mat4 Trans = glm::translate(glm::mat4(1.0f), glm::vec3(transX, 0.0f, transZ));
MVP = Projection * View * Model * Trans;
// Send our transformation to the currently Vid shader,
// in the "MVP" uniform, which is now MVP
glUniformMatrix4fv(glGetUniformLocation(shaderTexture, "MVP"), 1, GL_FALSE, &MVP[0][0]);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture_crate);
// Set our "myTextureSampler" sampler to use Texture Unit 0
glUniform1i(glGetUniformLocation(shaderTexture, "myTextureSampler"), 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, cubeVertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size : U+V => 2
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);;
glDrawArrays(GL_TRIANGLES, 0, 2*3*5);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
void GameMaker::drawFloor(glm::vec3 trans, bool start, bool end) {
floor->setMVP(Projection, View, Model);
floor->setTranslate(trans);
vec3 lightPos = playerBody->getWorldPosition();
if(win)
lightPos = winPos;
glm::mat4 rotationMat(1); // Creates a identity matrix
rotationMat = glm::rotate(rotationMat, glm::radians(rotateX), glm::vec3(1, 0, 0));
rotationMat = glm::rotate(rotationMat, glm::radians(rotateZ), vec3(0,0,1));
lightPos = glm::vec3(rotationMat * glm::vec4(lightPos, 1.0));
if (start)
floor->drawWithBasicLightningShader(lightPos, vec3(1,1,1), vec3(1,0.5,0.5), shaderBasicLightning);
else if (end)
floor->drawWithBasicLightningShader(lightPos, vec3(1,1,1), vec3(0.5,1,0.5), shaderBasicLightning);
else {
// floor->drawWithStandardShader(Texture_floor, playerBody->getWorldPosition(), shaderStandard);
floor->drawWithBasicLightningShader(lightPos, vec3(1,1,1), vec3(0.7,0.7,0.7), shaderBasicLightning);
}
}
void GameMaker::drawHole(glm::vec3 trans) {
hole->setMVP(Projection, View, Model);
hole->setTranslate(trans);
hole->drawWithTextureShader(Texture_hole[0], shaderTexture);
}
GameMaker::~GameMaker() {
cleanupDataFromGPU();
}
void GameMaker::cleanupDataFromGPU() {
glDeleteBuffers(1, &cubeVertexBuffer);
glDeleteBuffers(1, &floorVertexBuffer);
glDeleteBuffers(1, &playerVertexBuffer);
glDeleteBuffers(1, &holeVertexBuffer);
glDeleteBuffers(1, &uvbuffer);
glDeleteBuffers(1, &uvbuffer_floor);
glDeleteBuffers(1, &uvbuffer_hole);
glDeleteBuffers(1, &uvbuffer_player);
glDeleteVertexArrays(1, &VertexArrayID);
glDeleteProgram(shaderTexture);
glDeleteProgram(shaderLamp);
glDeleteProgram(shaderMaterial);
glDeleteProgram(shaderMaterialAndTexture);
}
void GameMaker::loadPlayer() {
GameMaker::player = new Object();
player->loadOBJtoGPU("sphere.obj");
}
void GameMaker::drawPlayer() {
player->setMVP(Projection, View, Model);
player->setScale(vec3(0.5f, 0.5f, 0.5f));
player->drawWithLampShader(vec3(1.0f, 1.0f, 1.0f), shaderLamp);
if(!win){
player->setTranslate(playerBody->getWorldPosition());
}else{
if(winPos.x==-999){
winPos = playerBody->getWorldPosition();
}
}
}
void GameMaker::loadPhysics() {
char** map = mg.getMatrixForOpenGL();
for (int i = 0; i < mg.mapHeight; i++) {
for (int j = 0; j < mg.mapWidth; j++) {
int x_pos = i - ((mazeWidth*2)/2), y_pos = j - ((mazeHeight*2)/2);
if (map[i][j] == 'S') {
start_point = btVector3(x_pos, 0.6f, y_pos);
cameraAt.y = glm::max(mazeHeight*2, mazeWidth*2);
}
if (map[i][j] == 'X') {
btCollisionShape* colShape = physicsWorld->createBoxShape(btVector3(0.5f, 0.5f, 0.5f));
Physics::PhysicsBody* pb = physicsWorld->createPhysicsBody(btVector3(x_pos, 0.0f, y_pos), colShape, btScalar(0));
objectBodies.push_back(pb);
} else if (map[i][j] == 'B') {
} else {
btCollisionShape* colShape = physicsWorld->createBoxShape(btVector3(0.5f, 0.001f, 0.5f));
Physics::PhysicsBody* pb = physicsWorld->createPhysicsBody(btVector3(x_pos, -0.5f, y_pos), colShape, btScalar(0));
objectBodies.push_back(pb);
if (map[i][j] == 'E') {
end_point = btVector3(x_pos, 0.5f, y_pos);
}
}
}
}
std::cout << "Start: " << start_point.getX() << " " << start_point.getZ() << std::endl;
btCollisionShape* colShape = physicsWorld->createSphereShape(0.35f);
playerBody = physicsWorld->createPhysicsBody(start_point, colShape, btScalar(1));
}
void GameMaker::drawMap() {
char** map = mg.getMatrixForOpenGL();
int k = 0;
for (int i = 0; i < mg.mapHeight; i++) {
for (int j = 0; j < mg.mapWidth; j++) {
if (map[i][j] == 'X') {
drawCube(glm::vec3(objectBodies[k]->getWorldPosition().x - 0.5f, objectBodies[k]->getWorldPosition().y - 0.5f, objectBodies[k]->getWorldPosition().z - 0.5f));
} else if (map[i][j] == 'B') {
//int x_pos = i - ((mazeWidth*2)/2) -1, z_pos = j - ((mazeHeight*2)/2) -1;
//drawHole(glm::vec3(x_pos + 0.5f, -0.5, z_pos + 0.5f));
k--;
} else {
if (map[i][j] == 'S') {
drawFloor(glm::vec3(objectBodies[k]->getWorldPosition().x, objectBodies[k]->getWorldPosition().y, objectBodies[k]->getWorldPosition().z), true, false);
} else if (map[i][j] == 'E') {
drawFloor(glm::vec3(objectBodies[k]->getWorldPosition().x, objectBodies[k]->getWorldPosition().y, objectBodies[k]->getWorldPosition().z), false, true);
} else {
drawFloor(glm::vec3(objectBodies[k]->getWorldPosition().x, objectBodies[k]->getWorldPosition().y, objectBodies[k]->getWorldPosition().z), false, false);
}
}
k++;
}
}
drawPlayer();
cubeMap->draw(MVP, shaderTexture);
//std::cout << " gravity: " << physicsWorld->dynamicsWorld->getGravity().getX() << " " << physicsWorld->dynamicsWorld->getGravity().getY() << " " << physicsWorld->dynamicsWorld->getGravity().getZ() << " " << std::endl;
//std::cout << " velocity: " << playerBody->body->getLinearVelocity().getX() << " " << playerBody->body->getLinearVelocity().getY() << " " << playerBody->body->getLinearVelocity().getZ() << " " << std::endl;
//std::cout << " position: " << playerBody->getWorldPosition().x << " " << playerBody->getWorldPosition().y << " " << playerBody->getWorldPosition().z << std::endl;
//std::cout << " " << std::endl;
}