-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_file.cpp
More file actions
468 lines (392 loc) · 16.2 KB
/
main_file.cpp
File metadata and controls
468 lines (392 loc) · 16.2 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
Niniejszy program jest wolnym oprogramowaniem; możesz go
rozprowadzać dalej i / lub modyfikować na warunkach Powszechnej
Licencji Publicznej GNU, wydanej przez Fundację Wolnego
Oprogramowania - według wersji 2 tej Licencji lub(według twojego
wyboru) którejś z późniejszych wersji.
Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on
użyteczny - jednak BEZ JAKIEJKOLWIEK GWARANCJI, nawet domyślnej
gwarancji PRZYDATNOŚCI HANDLOWEJ albo PRZYDATNOŚCI DO OKREŚLONYCH
ZASTOSOWAŃ.W celu uzyskania bliższych informacji sięgnij do
Powszechnej Licencji Publicznej GNU.
Z pewnością wraz z niniejszym programem otrzymałeś też egzemplarz
Powszechnej Licencji Publicznej GNU(GNU General Public License);
jeśli nie - napisz do Free Software Foundation, Inc., 59 Temple
Place, Fifth Floor, Boston, MA 02110 - 1301 USA
*/
#define GLM_FORCE_RADIANS
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <stdlib.h>
#include <stdio.h>
#include "myCube.h"
#include "constants.h"
#include "allmodels.h"
#include "lodepng.h"
#include "shaderprogram.h"
#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
float aspectRatio = 1;
GLuint tex;
GLuint carTexture;
GLuint treeTexture;
GLuint buildingTexture;
GLuint skyTexture;
double carSpeed = 0;
double speed_y = 0;
double angle_y = 0;
// Kamera
double cameraDistance = 20;
double cameraAngle = 0;
double cameraHeight = 5;
// Samochód
bool isAccelerating = false;
bool isDecelerating = false;
double acceleration = 0.2f;
double backAcceleration = 0.05f;
double noGasDeceleration = 0.05f;
double deceleration = 1.2f;
//Procedura obsługi błędów
void error_callback(int error, const char* description) {
fputs(description, stderr);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_LEFT) {
if (action == GLFW_PRESS) angle_y = 1.8f;
else if (action == GLFW_RELEASE) angle_y = 0;
}
if (key == GLFW_KEY_RIGHT) {
if (action == GLFW_PRESS) angle_y = -1.8f;
else if (action == GLFW_RELEASE) angle_y = 0;
}
if (key == GLFW_KEY_UP) {
if (action == GLFW_PRESS) {
isAccelerating = true;
}
else if (action == GLFW_RELEASE) {
isAccelerating = false;
}
}
if (key == GLFW_KEY_DOWN) {
if (action == GLFW_PRESS) {
isDecelerating = true;
}
else if (action == GLFW_RELEASE) {
isDecelerating = false;
}
}
}
GLuint readTexture(const char* filename) {
GLuint tex;
glActiveTexture(GL_TEXTURE0);
//Wczytanie do pamięci komputera
std::vector<unsigned char> image; //Alokuj wektor do wczytania obrazka
unsigned width, height; //Zmienne do których wczytamy wymiary obrazka
//Wczytaj obrazek
unsigned error = lodepng::decode(image, width, height, filename);
//Import do pamięci karty graficznej
glGenTextures(1, &tex); //Zainicjuj jeden uchwyt
glBindTexture(GL_TEXTURE_2D, tex); //Uaktywnij uchwyt
//Wczytaj obrazek do pamięci KG skojarzonej z uchwytem
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)image.data());
float maxAnisotropy;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &maxAnisotropy);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 16.0f);
return tex;
}
class Model {
public:
std::vector<glm::vec4> verts;
std::vector<glm::vec4> colors;
std::vector<glm::vec4> normals;
std::vector<glm::vec2> texCoords;
std::vector<unsigned int> indices;
};
std::vector<Model> loadModel(std::string plik) {
using namespace std;
vector<Model> models;
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(plik, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals);
cout << importer.GetErrorString() << endl;
cout << "plik: " << plik << endl << "Meshes: " << scene->mNumMeshes << endl << "Materials: " << scene->mNumMaterials << endl << "Textures: " << scene->mNumTextures << endl << "Lights: " << scene->mNumLights << endl << "Cameras: " << scene->mNumCameras << endl;
for (int k = 0; k < scene->mNumMeshes; k++) {
Model model;
aiMesh* mesh = scene->mMeshes[k];
vector<glm::vec4> modelVerts;
vector<glm::vec4> modelColors;
vector<glm::vec4> modelNormals;
vector<glm::vec2> modelTexCoords;
vector<unsigned int> modelIndices;
for (int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D vertex = mesh->mVertices[i];
modelVerts.push_back(glm::vec4(vertex.x, vertex.y, vertex.z, 1));
aiVector3D normal = mesh->mNormals[i];
modelNormals.push_back(glm::vec4(normal.x, normal.y, normal.z, 0));
aiColor4D color;
if (mesh->HasVertexColors(0)) {
color = mesh->mColors[0][i];
} else {
color = aiColor4D(1.0f, 1.0f, 1.0f, 1.0f); // Domyślny kolor, jeśli brak kolorów wierzchołków
}
modelColors.push_back(glm::vec4(color.r, color.g, color.b, color.a));
unsigned int liczba_zest = mesh->GetNumUVChannels();
unsigned int wymiar_wsp_tex = mesh->mNumUVComponents[0];
if (liczba_zest > 0 && wymiar_wsp_tex > 0) { // Jeżeli mesh zawiera koordynaty tekstury
aiVector3D texCoord = mesh->mTextureCoords[0][i];
modelTexCoords.push_back(glm::vec2(texCoord.x, texCoord.y));
}
}
for (int i = 0; i < mesh->mNumFaces; i++) {
aiFace& face = mesh->mFaces[i];
for (int j = 0; j < face.mNumIndices; j++) {
modelIndices.push_back(face.mIndices[j]);
}
}
model.verts = modelVerts;
model.colors = modelColors;
model.normals = modelNormals;
model.texCoords = modelTexCoords;
model.indices = modelIndices;
models.push_back(model);
}
return models;
}
void windowResizeCallback(GLFWwindow* window, int width, int height) {
if (height == 0) return;
aspectRatio = (float)width / (float)height;
glViewport(0, 0, width, height);
}
//Procedura inicjująca
void initOpenGLProgram(GLFWwindow* window) {
initShaders();
glClearColor(0, 0, 0, 1); //Ustaw kolor czyszczenia bufora kolorów
glEnable(GL_DEPTH_TEST); //Włącz test głębokości na pikselach
glfwSetKeyCallback(window, keyCallback);
glfwSetWindowSizeCallback(window, windowResizeCallback);
tex = readTexture("models/textures/trasa.png");
carTexture = readTexture("models/textures/mclaren.png");
buildingTexture = readTexture("models/textures/budowla.png");
treeTexture = readTexture("models/textures/drzewo.png");
skyTexture = readTexture("models/textures/sky.png");
}
//Zwolnienie zasobów zajętych przez program
void freeOpenGLProgram(GLFWwindow* window) {
freeShaders();
glDeleteTextures(1, &tex);
glDeleteTextures(1, &carTexture);
glDeleteTextures(1, &buildingTexture);
glDeleteTextures(1, &treeTexture);
}
glm::vec3 treePosition = glm::vec3(80, 0, 80);
glm::vec3 tree2Position = glm::vec3(-20, 0, 20);
glm::vec3 tree3Position = glm::vec3(-40, 0, -40);
glm::vec3 buildingPosition = glm::vec3(20, 0, 10);
std::vector<std::pair<glm::vec3, float>> objectData = {
{treePosition, 1.5f}, // Drzewo 1
{tree2Position, 1.5f}, // Drzewo 2
{tree3Position, 1.5f}, // Drzewo 3
{buildingPosition, 8.5f}, // Budynek
};
bool checkCollision(float carX, float carY, float carZ, const std::vector<std::pair<glm::vec3, float>>& objectData) {
glm::vec3 carPosition = glm::vec3(carX, carY, carZ);
for (const auto& data : objectData) {
const glm::vec3& objectPosition = data.first;
float collisionDistanceThreshold = data.second;
glm::vec3 delta = carPosition - objectPosition;
float distance = glm::length(delta);
//std::cout << distance << " | ";
if (distance < collisionDistanceThreshold) {
return true;
}
}
return false;
}
void texKostka(glm::mat4 P, glm::mat4 V, glm::mat4 M) {
float myCubeTexCoords[] = {
4.0f, 0.0f, 0.0f, 4.0f, 0.0f, 0.0f,
4.0f, 0.0f, 4.0f, 4.0f, 0.0f, 4.0f,
4.0f, 0.0f, 0.0f, 4.0f, 0.0f, 0.0f,
4.0f, 0.0f, 4.0f, 4.0f, 0.0f, 4.0f,
4.0f, 0.0f, 0.0f, 4.0f, 0.0f, 0.0f,
4.0f, 0.0f, 4.0f, 4.0f, 0.0f, 4.0f,
4.0f, 0.0f, 0.0f, 4.0f, 0.0f, 0.0f,
4.0f, 0.0f, 4.0f, 4.0f, 0.0f, 4.0f,
4.0f, 0.0f, 0.0f, 4.0f, 0.0f, 0.0f,
4.0f, 0.0f, 4.0f, 4.0f, 0.0f, 4.0f,
4.0f, 0.0f, 0.0f, 4.0f, 0.0f, 0.0f,
4.0f, 0.0f, 4.0f, 4.0f, 0.0f, 4.0f,
};
spTextured->use(); //Aktywuj program cieniujący
glUniformMatrix4fv(spTextured->u("P"), 1, false, glm::value_ptr(P)); //Załaduj do programu cieniującego macierz rzutowania
glUniformMatrix4fv(spTextured->u("V"), 1, false, glm::value_ptr(V)); //Załaduj do programu cieniującego macierz widoku
glUniformMatrix4fv(spTextured->u("M"), 1, false, glm::value_ptr(M)); //Załaduj do programu cieniującego macierz modelu
glEnableVertexAttribArray(spTextured->a("vertex"));
glVertexAttribPointer(spTextured->a("vertex"), 4, GL_FLOAT, false, 0, myCubeVertices); //Współrzędne wierzchołków bierz z tablicy myCubeVertices
glEnableVertexAttribArray(spTextured->a("texCoord"));
glVertexAttribPointer(spTextured->a("texCoord"), 2, GL_FLOAT, false, 0, myCubeTexCoords); //Współrzędne teksturowania bierz z tablicy myCubeTexCoords
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(spTextured->u("tex"), 0);
glDrawArrays(GL_TRIANGLES, 18, 6);
glDisableVertexAttribArray(spTextured->a("vertex"));
glDisableVertexAttribArray(spTextured->a("color"));
}
void drawModel(glm::mat4 P, glm::mat4 V, glm::mat4 M, std::vector<Model> models, GLuint texture) {
spLambertTextured->use();
for (int i = 0; i < models.size(); i++)
{
glUniformMatrix4fv(spLambertTextured->u("P"), 1, false, glm::value_ptr(P)); //Załaduj do programu cieniującego macierz rzutowania
glUniformMatrix4fv(spLambertTextured->u("V"), 1, false, glm::value_ptr(V)); //Załaduj do programu cieniującego macierz widoku
glUniformMatrix4fv(spLambertTextured->u("M"), 1, false, glm::value_ptr(M)); //Załaduj do programu cieniującego macierz modelu
// Przypisz dane
glEnableVertexAttribArray(spLambertTextured->a("vertex")); // Włącz atrybut vertex
glVertexAttribPointer(spLambertTextured->a("vertex"), 4, GL_FLOAT, false, 0, models[i].verts.data());
glEnableVertexAttribArray(spLambertTextured->a("normal")); // Włącz atrybut normal
glVertexAttribPointer(spLambertTextured->a("normal"), 4, GL_FLOAT, false, 0, models[i].normals.data());
glEnableVertexAttribArray(spLambertTextured->a("color")); // Włącz atrybut normal
glVertexAttribPointer(spLambertTextured->a("color"), 4, GL_FLOAT, false, 0, models[i].colors.data());
glEnableVertexAttribArray(spLambertTextured->a("texCoord0")); // Włącz atrybut texCoord
glVertexAttribPointer(spLambertTextured->a("texCoord0"), 2, GL_FLOAT, false, 0, models[i].texCoords.data());
glUniform1i(spLambertTextured->u("textureMap0"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(spLambertTextured->u("textureMap1"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, skyTexture);
glDrawElements(GL_TRIANGLES, models[i].indices.size(), GL_UNSIGNED_INT, models[i].indices.data());
glDisableVertexAttribArray(spLambertTextured->a("vertex"));
glDisableVertexAttribArray(spLambertTextured->a("normal"));
glDisableVertexAttribArray(spLambertTextured->a("color"));
glDisableVertexAttribArray(spLambertTextured->a("texCoord"));
}
}
//Procedura rysująca zawartość sceny
void drawScene(GLFWwindow* window, float carX, float carY, float carZ, float carAngle, std::vector<Model> floor, std::vector<Model> car, std::vector<Model> tree, std::vector<Model> building, std::vector<Model> track) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Wyczyść bufor koloru i bufor głębokości
glm::vec3 carPosition = glm::vec3(carX, carY, carZ);
glm::vec3 cameraPosition = glm::vec3(carX - cameraDistance * sin(carAngle), cameraHeight,carZ - cameraDistance * cos(carAngle));
//std::cout << "CAMERA POSITION: " << cameraPosition.x << " " << cameraPosition.y << " " << cameraPosition.z << " || ";
glm::mat4 P = glm::perspective(30.0f * PI / 180.0f, aspectRatio, 1.0f, 1200.0f);
glm::mat4 V = glm::lookAt(
cameraPosition,
carPosition,
glm::vec3(0, 1, 0)
);
// Rysowanie samochodu
glm::mat4 M = glm::mat4(1.0f);
M = glm::translate(M, carPosition);
M = glm::rotate(M, carAngle, glm::vec3(0, 1, 0));
drawModel(P, V, M, car, carTexture);
// Rysowanie drzewa
M = glm::mat4(1.0f);
M = glm::translate(M, treePosition); // Pozycja drzewa w świecie
drawModel(P, V, M, tree, treeTexture);
// Rysowanie drzewa
M = glm::mat4(1.0f);
M = glm::translate(M, tree2Position); // Pozycja drzewa w świecie
drawModel(P, V, M, tree, treeTexture);
// Rysowanie drzewa
M = glm::mat4(1.0f);
M = glm::translate(M, tree3Position); // Pozycja drzewa w świecie
drawModel(P, V, M, tree, treeTexture);
// Rysowanie budynku
M = glm::mat4(1.0f);
M = glm::translate(M, buildingPosition); // Pozycja budowli w świecie
M = glm::translate(M, glm::vec3(0, -0.4f, 0));
M = glm::scale(M, glm::vec3(5.5f, 5.5f, 5.5f));
drawModel(P, V, M, building, buildingTexture);
// Rysowanie terenu dywaniku
//M = glm::mat4(1.0f); //Macierz jednostkowa
//M = glm::translate(M, glm::vec3(0, -1.3f, 0)); // obnizenie terenu
//M = glm::scale(M, glm::vec3(100, 1, 100)); // Skalowanie terenu
texKostka(P, V, M);
// Rysowanie toru
M = glm::mat4(1.0f); //Macierz jednostkowa
M = glm::translate(M, glm::vec3(0, -0.3f, 0)); // obnizenie terenu
drawModel(P, V, M, track, carTexture);
glfwSwapBuffers(window); //Skopiuj bufor tylny do bufora przedniego
}
int main(void)
{
GLFWwindow* window; //Wskaźnik na obiekt reprezentujący okno
glfwSetErrorCallback(error_callback);//Zarejestruj procedurę obsługi błędów
if (!glfwInit()) { //Zainicjuj bibliotekę GLFW
fprintf(stderr, "Nie można zainicjować GLFW.\n");
exit(EXIT_FAILURE);
}
window = glfwCreateWindow(800, 800, "Drive Simulator", NULL, NULL); //Utwórz okno 500x500 o tytule "OpenGL" i kontekst OpenGL.
if (!window) //Jeżeli okna nie udało się utworzyć, to zamknij program
{
fprintf(stderr, "Nie można utworzyć okna.\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window); //Od tego momentu kontekst okna staje się aktywny i polecenia OpenGL będą dotyczyć właśnie jego.
glfwSwapInterval(1); //Czekaj na 1 powrót plamki przed pokazaniem ukrytego bufora
if (glewInit() != GLEW_OK) { //Zainicjuj bibliotekę GLEW
fprintf(stderr, "Nie można zainicjować GLEW.\n");
exit(EXIT_FAILURE);
}
initOpenGLProgram(window); //Operacje inicjujące
float carX = 0;
float carY = 0;
float carZ = 0;
float carAngle = 0;
std::vector<Model> floor = loadModel("models/test3.obj");
std::vector<Model> car = loadModel("models/car.obj");
std::vector<Model> tree = loadModel("models/tree.obj");
std::vector<Model> building = loadModel("models/building.obj");
std::vector<Model> track = loadModel("models/track.obj");
double prevTime = glfwGetTime();
//Główna pętla
while (!glfwWindowShouldClose(window)) {
if (isAccelerating) {
carSpeed += acceleration;
}
if (isDecelerating) {
if (carSpeed > 0) {
carSpeed -= deceleration;
}
if (carSpeed > -20) carSpeed -= backAcceleration;
}
if (!isAccelerating && !isDecelerating) {
if (carSpeed > 0) {
carSpeed -= noGasDeceleration;
if (carSpeed < 1) {
carSpeed = 0;
}
}
if (carSpeed < 0) {
carSpeed += backAcceleration;
}
}
// Wyliczamy X i Z
carAngle += angle_y * glfwGetTime();
carX += carSpeed * sin(carAngle) * glfwGetTime();
carY += speed_y * glfwGetTime();
carZ += carSpeed * cos(carAngle) * glfwGetTime();
std::cout << "CAR: " << carX << " " << carY << " " << carZ << " || SPEED: " << carSpeed << " units/sec " << std::endl;
glfwSetTime(0); // Zeruj timer
drawScene(window, carX, carY, carZ, carAngle, floor, car, tree, building, track); // Wykonaj procedurę rysującą
if (checkCollision(carX, carY, carZ, objectData)) {
carSpeed = 0;
};
glfwPollEvents(); // Wykonaj procedury callback w zależności od zdarzeń jakie zaszły.
}
freeOpenGLProgram(window);
glfwDestroyWindow(window); //Usuń kontekst OpenGL i okno
glfwTerminate(); //Zwolnij zasoby zajęte przez GLFW
exit(EXIT_SUCCESS);
}