/
test.cpp
473 lines (329 loc) · 12.7 KB
/
test.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
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
469
470
471
472
473
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/ext.hpp>
#include "lib/objmanager.h"
#include "lib/mesh.h"
void render(GLFWwindow*);
void init();
#define glInfo(a) std::cout << #a << ": " << glGetString(a) << std::endl
int nbVertex;
float angleX =0, angleY =0, anglePhiLight = 0, angleTetaLight = 0;
void ajoutSol(const Vector3D& p0, const Vector3D& p1, const Vector3D& p2, const Vector3D& p3, const Vector3D& normal,
int tailleTableau, float* tableauVertex, float* tableauNormal);
//Input management for rotation
void inputHandling(GLFWwindow* window){
//For object rotation
if(glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
angleY -= 0.1f;
else if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
angleY += 0.1f;
if(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
angleX += 0.1f;
else if(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
angleX -= 0.1f;
//For light placement
if(glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS)
anglePhiLight -= 0.1f;
else if(glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS)
anglePhiLight += 0.1f;
if(glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS)
angleTetaLight -= 0.1f;
else if(glfwGetKey(window, GLFW_KEY_KP_5) == GLFW_PRESS)
angleTetaLight += 0.1f;
//For exit
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
// This function is called on any openGL API error
void debug(GLenum, // source
GLenum, // type
GLuint, // id
GLenum, // severity
GLsizei, // length
const GLchar *message,
const void *) // userParam
{
std::cout << "DEBUG: " << message << std::endl;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
std::cerr << "Could not init glfw" << std::endl;
return -1;
}
// This is a debug context, this is slow, but debugs, which is interesting
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
std::cerr << "Could not init window" << std::endl;
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
GLenum err = glewInit();
if(err != GLEW_OK)
{
std::cerr << "Could not init GLEW" << std::endl;
std::cerr << glewGetErrorString(err) << std::endl;
glfwTerminate();
return -1;
}
// Now that the context is initialised, print some informations
glInfo(GL_VENDOR);
glInfo(GL_RENDERER);
glInfo(GL_VERSION);
glInfo(GL_SHADING_LANGUAGE_VERSION);
// And enable debug
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
//glDebugMessageCallback(debug, nullptr);
// This is our openGL init function which creates ressources
init();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
render(window);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
// Build a shader from a string
GLuint buildShader(GLenum const shaderType, std::string const src)
{
GLuint shader = glCreateShader(shaderType);
const char* ptr = src.c_str();
GLint length = src.length();
glShaderSource(shader, 1, &ptr, &length);
glCompileShader(shader);
GLint res;
glGetShaderiv(shader, GL_COMPILE_STATUS, &res);
if(!res)
{
std::cerr << "shader compilation error" << std::endl;
char message[1000];
GLsizei readSize;
glGetShaderInfoLog(shader, 1000, &readSize, message);
message[999] = '/0';
std::cerr << message << std::endl;
glfwTerminate();
exit(-1);
}
return shader;
}
// read a file content into a string
std::string fileGetContents(const std::string path)
{
std::ifstream t(path);
std::stringstream buffer;
buffer << t.rdbuf();
return buffer.str();
}
// build a program with a vertex shader and a fragment shader
GLuint buildProgram(const std::string vertexFile, const std::string fragmentFile)
{
auto vshader = buildShader(GL_VERTEX_SHADER, fileGetContents(vertexFile));
auto fshader = buildShader(GL_FRAGMENT_SHADER, fileGetContents(fragmentFile));
GLuint program = glCreateProgram();
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
GLint res;
glGetProgramiv(program, GL_LINK_STATUS, &res);
if(!res)
{
std::cerr << "program link error" << std::endl;
char message[1000];
GLsizei readSize;
glGetProgramInfoLog(program, 1000, &readSize, message);
message[999] = '/0';
std::cerr << message << std::endl;
glfwTerminate();
exit(-1);
}
return program;
}
/****************************************************************
******* INTERESTING STUFFS HERE ********************************
***************************************************************/
// Store the global state of your program
struct
{
GLuint programView; // shaders for view camera
GLuint vao; // a vertex array object
GLuint depthTexture; // texture from the light camera
GLuint fbo; //framebuffer for shadow
} gs;
void init()
{
//std::string racineProjet = "C:/Users/etu/workspace/code/Rendu temps reel/";
std::string racineProjet = "C:/Users/etu/Documents/GitHub/Gamagora-Rendu_temps_reel-TP/";
//std::string racineProjet = "B:/Utilisateur/git/code/Gamagora-Rendu_temps_reel-TP/";
// Build our program and an empty VAO
gs.programView = buildProgram((racineProjet+(std::string)"basic.vsl").c_str(), (racineProjet+(std::string)"basic.fsl").c_str());
Mesh m;
m = ObjManager::loadFromOBJ(Vector3D(0,0,0), (racineProjet+(std::string)"monkey.obj").c_str());
nbVertex = m.nbface()+6; //nbface + quad "sol"
float* data = (float*) malloc(nbVertex*4*sizeof(float));
float* dataNormal = (float*) malloc(nbVertex * 4 * sizeof(float));
std::vector<Vector3D> vertex = m.getvertex();
std::vector<int> face = m.getface();
std::vector<Vector3D> normals = m.getNormals();
std::vector<int> normalIds = m.getNormalIds();
int i=0;
for(int j=0; j<face.size(); j++){
//set vertex
data[i] = vertex[face[j]].x;
data[i+1] = vertex[face[j]].y;
data[i+2] = vertex[face[j]].z;
data[i+3] = 1;
dataNormal[i] = normals[normalIds[j]].x;
dataNormal[i+1] = normals[normalIds[j]].y;
dataNormal[i+2] = normals[normalIds[j]].z;
dataNormal[i+3] = 1;
i+=4;
}
//ajout du quad pour faire le sol
ajoutSol(Vector3D(-15,-1,-15), Vector3D(15,-1,-15), Vector3D(15,-1,15), Vector3D(-15,-1,15), Vector3D(0,1,0),
nbVertex*4, data, dataNormal);
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, nbVertex*4*4, data, GL_STATIC_DRAW);
GLuint buffer2;
glGenBuffers(1, &buffer2);
glBindBuffer(GL_ARRAY_BUFFER, buffer2);
glBufferData(GL_ARRAY_BUFFER, nbVertex*4*4, dataNormal, GL_STATIC_READ);
glCreateVertexArrays(1, &gs.vao);
glBindVertexArray(gs.vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(12);
glBindBuffer(GL_ARRAY_BUFFER, buffer2);
glVertexAttribPointer(13, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(13);
glBindVertexArray(0);
glEnable(GL_DEPTH_TEST);
// create the depth texture
glGenTextures(1, &gs.depthTexture);
glBindTexture(GL_TEXTURE_2D, gs.depthTexture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 640, 480);
// Framebuffer
glGenFramebuffers(1, &gs.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, gs.fbo);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gs.depthTexture, 0);
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gs.depthTexture);
glBindVertexArray(0);
free(data); free(dataNormal);
}
void render(GLFWwindow* window)
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
inputHandling(window);
/********** Section camera view **********/
glm::vec3 cameraPosition(0,2,7);
glm::vec4 cameraPositionTransformed =
glm::rotate(glm::mat4(1.0f), angleX, glm::vec3(0,1,0)) *
glm::rotate(glm::mat4(1.0f), angleY, glm::vec3(1,0,0))* glm::vec4(cameraPosition, 1.0f);
cameraPosition = glm::vec3(XYZ(cameraPositionTransformed));
// come from http://www.opengl-tutorial.org/fr/beginners-tutorials/tutorial-3-matrices/
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 50.0f);
// Camera matrix
glm::mat4 ViewCamera = glm::lookAt(
cameraPosition, // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 mvpCamera = Projection * ViewCamera * Model; // Remember, matrix multiplication is the other way around
//glProgramUniformMatrix4fv(gs.programView, 23, 1, GL_FALSE, &mvpCamera[0][0]);
/********** Section lumière **********/
glm::vec3 lightPosition(0, 5.f, 15.f);
glm::vec4 lightPositionTransformed =
glm::rotate(glm::mat4(1.0f), anglePhiLight, glm::vec3(0,1,0)) *
glm::rotate(glm::mat4(1.0f), angleTetaLight, glm::vec3(1,0,0)) * glm::vec4(lightPosition,1.0f);
/*** calcul du mvp de la caméra lumière (déplacement de la lumière donc calcule ici) ***/
lightPosition = glm::vec3(XYZ(lightPositionTransformed));
Projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 50.0f);
// Light Camera matrix
glm::mat4 ViewLightCamera = glm::lookAt(
lightPosition, // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 mvpLightCamera = Projection * ViewLightCamera * Model; // Remember, matrix multiplication is the other way around
//glProgramUniformMatrix4fv(gs.programView, 22, 1, GL_FALSE, &mvpLightCamera[0][0]);
float color[3] = { 0, 1, 0 };
glProgramUniform3fv(gs.programView, 3, 1, color);
glProgramUniform3fv(gs.programView, 4, 1, glm::value_ptr(lightPosition));
glBindFramebuffer(GL_FRAMEBUFFER, gs.fbo);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glUseProgram(gs.programView);
glBindVertexArray(gs.vao);
{
glProgramUniformMatrix4fv(gs.programView, 22, 1, GL_FALSE, &mvpLightCamera[0][0]);
glProgramUniformMatrix4fv(gs.programView, 23, 1, GL_FALSE, &mvpLightCamera[0][0]);
glProgramUniform3fv(gs.programView, 3, 1, color);
glProgramUniform3fv(gs.programView, 4, 1, glm::value_ptr(lightPosition));
glDrawArrays(GL_TRIANGLES, 0, nbVertex*4);
}
//glBindFramebuffer(GL_FRAMEBUFFER, 0);
//glBindVertexArray(0);
//glUseProgram(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
//glUseProgram(gs.programView);
//glBindVertexArray(gs.vao);
{
glProgramUniformMatrix4fv(gs.programView, 23, 1, GL_FALSE, &mvpCamera[0][0]);
glProgramUniformMatrix4fv(gs.programView, 22, 1, GL_FALSE, &mvpLightCamera[0][0]);
glProgramUniform3fv(gs.programView, 3, 1, color);
glProgramUniform3fv(gs.programView, 4, 1, glm::value_ptr(lightPosition));
glDrawArrays(GL_TRIANGLES, 0, nbVertex*4);
}
glBindVertexArray(0);
glUseProgram(0);
}
void ajoutSol(const Vector3D& p0, const Vector3D& p1, const Vector3D& p2, const Vector3D& p3, const Vector3D& normal,
int tailleTableau, float* tableauVertex, float* tableauNormal){
int index = tailleTableau - (6*4);
Vector3D vertexSol[6] = {p0, p1, p2, p2, p3, p0};
for(Vector3D& vec : vertexSol){
tableauVertex[index] = vec.x;
tableauVertex[index+1] = vec.y;
tableauVertex[index+2] = vec.z;
tableauVertex[index+3] = 1;
tableauNormal[index] = normal.x;
tableauNormal[index+1] = normal.y;
tableauNormal[index+2] = normal.z;
tableauNormal[index+3] = 1;
index+=4;
}
}