forked from Nolab0/POGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.cpp
More file actions
384 lines (309 loc) · 14.1 KB
/
init.cpp
File metadata and controls
384 lines (309 loc) · 14.1 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
#include "init.h"
#include "particles.h"
#include <algorithm>
GLuint program_id;
std::vector<Vertex> vertices;
unsigned long sceneSize;
float fogStartDistance = 15;
ParticleType particleType = RAIN;
unsigned long particleSize;
float temperature = 0.0f;
void updateFog(){
GLint fogStartDistanceUniform = glGetUniformLocation(program_id, "fogStart");
glUniform1f(fogStartDistanceUniform, fogStartDistance);
}
void updateTemp(){
GLint temperatureUniform = glGetUniformLocation(program_id, "temperature");
glUniform1f(temperatureUniform, temperature);
}
std::string load(const std::string &filename) {
std::ifstream input_src_file(filename, std::ios::in);
std::string ligne;
std::string file_content="";
if (input_src_file.fail()) {
std::cerr << "FAILURE: can not load " << filename << "\n";
return "";
}
while(getline(input_src_file, ligne)) {
file_content = file_content + ligne + "\n";
}
file_content += '\0';
input_src_file.close();
return file_content;
}
std::vector<Material> loadMTL(const std::string& filePath) {
std::vector<Material> materials;
std::ifstream file(filePath);
std::string line;
Material currentMaterial;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string type;
iss >> type;
if (type == "newmtl") {
if (!currentMaterial.name.empty()) {
materials.push_back(currentMaterial);
currentMaterial = Material();
}
iss >> currentMaterial.name;
} else if (type == "Kd") {
iss >> currentMaterial.diffuse.x >> currentMaterial.diffuse.y >> currentMaterial.diffuse.z;
} else if (type == "Ka") {
iss >> currentMaterial.ambient.x >> currentMaterial.ambient.y >> currentMaterial.ambient.z;
} else if (type == "Ks") {
iss >> currentMaterial.specular.x >> currentMaterial.specular.y >> currentMaterial.specular.z;
}
}
if (!currentMaterial.name.empty()) {
materials.push_back(currentMaterial);
}
return materials;
}
std::vector<Vertex> loadOBJ(const std::string& filePath, const std::vector<Material>& materials) {
std::vector<glm::vec3> positions;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
std::vector<Vertex> vertices;
std::ifstream file(filePath);
std::string line;
std::string currentMaterialName;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string type;
iss >> type;
if (type == "v") {
glm::vec3 position;
iss >> position.x >> position.y >> position.z;
positions.push_back(position);
} else if (type == "vt") {
glm::vec2 uv;
iss >> uv.x >> uv.y;
uvs.push_back(uv);
} else if (type == "vn") {
glm::vec3 normal;
iss >> normal.x >> normal.y >> normal.z;
normals.push_back(normal);
} else if (type == "usemtl") {
iss >> currentMaterialName;
} else if (type == "f") {
std::string faceData[3];
iss >> faceData[0] >> faceData[1] >> faceData[2];
for (int i = 0; i < 3; i++) {
std::istringstream faceIss(faceData[i]);
std::string vertexData[3];
std::getline(faceIss, vertexData[0], '/');
std::getline(faceIss, vertexData[1], '/');
std::getline(faceIss, vertexData[2], '/');
Vertex vertex;
vertex.position = positions[std::stoi(vertexData[0]) - 1];
vertex.originalPosition = {vertex.position.x, vertex.position.y, vertex.position.z};
vertex.uv = uvs[std::stoi(vertexData[1]) - 1];
vertex.normal = normals[std::stoi(vertexData[2]) - 1];
for (const auto& material : materials) {
if (material.name == currentMaterialName) {
glm::vec3 ambientColor = material.ambient;
glm::vec3 diffuseColor = material.diffuse;
glm::vec3 specularColor = material.specular;
glm::vec3 lightPosition = glm::vec3(0.0f, 15.0f, 0.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 normal = glm::normalize(vertex.normal);
glm::vec3 lightDirection = glm::normalize(lightPosition - vertex.position);
float ambientIntensity = 0.1f;
float diffuseIntensity = glm::max(glm::dot(normal, lightDirection), 0.0f);
float specularIntensity = 0.5f;
glm::vec3 ambientComponent = ambientIntensity * ambientColor;
glm::vec3 diffuseComponent = diffuseIntensity * diffuseColor * lightColor;
glm::vec3 specularComponent = specularIntensity * specularColor * lightColor;
vertex.color = ambientComponent + diffuseComponent + specularComponent;
break;
}
}
vertices.push_back(vertex);
}
}
}
return vertices;
}
GLFWwindow* init_glfw(){
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
exit(-1);
}
GLFWwindow* window = glfwCreateWindow(800, 600, "3D Object Viewer", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
exit(-1);
}
glfwMakeContextCurrent(window);
return window;
}
void init_glew(){
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
glfwTerminate();
exit(-1);
}
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
std::vector<Material> cubeMat = loadMTL("../objects/snow.mtl");
std::vector<Vertex> cube = loadOBJ("../objects/snow.obj", cubeMat);
void deep_copy(std::vector<Vertex> &dest, std::vector<Vertex> &src){
dest.clear();
dest.insert(dest.end(), src.begin(), src.end());
}
void impactSnow(float x, float z){
std::vector<Vertex> cubeCpy;
deep_copy(cubeCpy, cube);
for(int i = 0; i < cubeCpy.size(); i++){
cubeCpy[i].position.x += x;
cubeCpy[i].position.z += z;
cubeCpy[i].position.y += 0.2;
}
vertices.insert(vertices.end(), cubeCpy.begin(), cubeCpy.end());
}
std::vector<Material> circle1Mat = loadMTL("../objects/circle1.mtl");
std::vector<Vertex> circle1 = loadOBJ("../objects/circle1.obj", circle1Mat);
std::vector<Material> circle2Mat = loadMTL("../objects/circle2.mtl");
std::vector<Vertex> circle2 = loadOBJ("../objects/circle2.obj", circle2Mat);
std::vector<Material> circle3Mat = loadMTL("../objects/circle3.mtl");
std::vector<Vertex> circle3 = loadOBJ("../objects/circle3.obj", circle3Mat);
bool anim[MAX_SPLASH] = {0};
void displaySplash(float x, float z, int iter, int &splashIndex){
int index;
if (iter == 1){
auto result = std::find(std::begin(anim), std::end(anim), false);
if (result == std::end(anim)) {
std::cout << "No 'false' found in the array." << std::endl;
}
index = std::distance(std::begin(anim), result);
anim[index] = true;
splashIndex = index;
}
else
index = splashIndex;
long offset = sceneSize + particleSize * MAX_PARTICLES + index * (circle1.size() + circle2.size() + circle3.size());
switch (iter) {
case 1:
for(int i = 0; i < circle1.size(); i++){
vertices[offset + i].position.x = vertices[offset + i].originalPosition.x + x;
vertices[offset + i].position.z = vertices[offset + i].originalPosition.z + z;
vertices[offset + i].position.y = vertices[offset + i].originalPosition.y + 0.2;
}
break;
case 5:
for(int i = 0; i < circle2.size(); i++){
vertices[offset + i].position.y = 50;
vertices[offset + circle1.size() + i].position.x = vertices[offset + circle1.size() + i].originalPosition.x + x;
vertices[offset + circle1.size() + i].position.z = vertices[offset + circle1.size() + i].originalPosition.z + z;
vertices[offset + circle1.size() + i].position.y = vertices[offset + circle1.size() + i].originalPosition.y + 0.2;
}
break;
case 10:
for(int i = 0; i < circle3.size(); i++){
vertices[offset + circle1.size() + i].position.y = 50;
vertices[offset + circle1.size() + circle2.size() + i].position.x = vertices[offset + circle1.size() + circle2.size() + i].originalPosition.x + x;
vertices[offset + circle1.size() + circle2.size() + i].position.z = vertices[offset + circle1.size() + circle2.size() + i].originalPosition.z + z;
vertices[offset + circle1.size() + circle2.size() + i].position.y = vertices[offset + circle1.size() + circle2.size() + i].originalPosition.y + 0.2;
}
break;
}
}
void deleteCircle3(int splashIndex){
anim[splashIndex] = false;
for(int i = 0; i < circle3.size(); i++){
vertices[sceneSize + particleSize * MAX_PARTICLES + splashIndex * (circle1.size() + circle2.size() + circle3.size()) + circle1.size() + circle2.size() + i].position.y = 50;
}
}
void set_snow(){
std::vector<Material> mat = loadMTL("../objects/snowflake.mtl");
std::vector<Vertex> snowflake = loadOBJ("../objects/snowflake.obj", mat);
vertices.erase(vertices.begin() + sceneSize, vertices.end());
particleType = SNOW;
particleSize = snowflake.size();
init_particles(snowflake, sceneSize);
}
void set_hail(){
std::vector<Material> mat = loadMTL("../objects/hail.mtl");
std::vector<Vertex> hail = loadOBJ("../objects/hail.obj", mat);
vertices.erase(vertices.begin() + sceneSize, vertices.end());
particleType = HAIL;
particleSize = hail.size();
init_particles(hail, sceneSize);
}
void set_rain(){
std::vector<Material> mat = loadMTL("../objects/drop.mtl");
std::vector<Vertex> waterDrop = loadOBJ("../objects/drop.obj", mat);
vertices.erase(vertices.begin() + sceneSize, vertices.end());
particleType = RAIN;
particleSize = waterDrop.size();
init_particles(waterDrop, sceneSize);
std::vector<Vertex> animation;
animation.insert(animation.end(), circle1.begin(), circle1.end());
animation.insert(animation.end(), circle2.begin(), circle2.end());
animation.insert(animation.end(), circle3.begin(), circle3.end());
for (int i = 0; i < MAX_SPLASH; i++) {
vertices.insert(vertices.end(), animation.begin(), animation.end());
anim[i] = false;
}
}
void set_sun(){
vertices.erase(vertices.begin() + sceneSize, vertices.end());
particleType = NONE;
}
View init_obj_and_shaders(std::vector<Vertex> &vertices, GLuint &vertexBuffer){
std::vector<Material> materials = loadMTL("../objects/city.mtl");
vertices = loadOBJ("../objects/city.obj", materials);
sceneSize = vertices.size();
particleType = NONE;
std::string vertexShaderSource = load("../fogShaders/vertex.glsl");
std::string fragmentShaderSource = load("../fogShaders/fragment.glsl");
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
const char* vertexShaderCode = vertexShaderSource.c_str();
glShaderSource(vertexShader, 1, &vertexShaderCode, nullptr);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const char* fragmentShaderCode = fragmentShaderSource.c_str();
glShaderSource(fragmentShader, 1, &fragmentShaderCode, nullptr);
glCompileShader(fragmentShader);
program_id = glCreateProgram();
glAttachShader(program_id, vertexShader);
glAttachShader(program_id, fragmentShader);
glLinkProgram(program_id);
glUseProgram(program_id);
GLuint vertexArrayID;
glGenVertexArrays(1, &vertexArrayID);
glBindVertexArray(vertexArrayID);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
GLint positionAttrib = glGetAttribLocation(program_id, "position");
glEnableVertexAttribArray(positionAttrib);
glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
GLint uvAttrib = glGetAttribLocation(program_id, "uv");
glEnableVertexAttribArray(uvAttrib);
glVertexAttribPointer(uvAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, uv)));
GLint normalAttrib = glGetAttribLocation(program_id, "normal");
glEnableVertexAttribArray(normalAttrib);
glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));
GLint colorAttrib = glGetAttribLocation(program_id, "color");
glEnableVertexAttribArray(colorAttrib);
glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, color)));
GLint fogStartDistanceUniform = glGetUniformLocation(program_id, "fogStart");
glUniform1f(fogStartDistanceUniform, fogStartDistance);
GLint temperatureUniform = glGetUniformLocation(program_id, "temperature");
glUniform1f(temperatureUniform, temperature);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
GLint projectionUniform = glGetUniformLocation(program_id, "projection");
glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
GLint modelUniform = glGetUniformLocation(program_id, "model");
GLint viewUniform = glGetUniformLocation(program_id, "view");
glUniformMatrix4fv(modelUniform, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewUniform, 1, GL_FALSE, glm::value_ptr(view));
return {
view, viewUniform
};
}