-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptep.cpp
360 lines (292 loc) · 9.61 KB
/
ptep.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
#include <algorithm>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>
#include <limits>
#include <type_traits>
#include <GL/glew.h>
#include <GL/gl.h>
#include <SDL2/SDL.h>
#include <assimp/scene.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
#include "glUtil.h"
#include "readFile.h"
#include "modelLoader.h"
#include "shaderLoader.h"
enum RenderMode
{
ALL,
COLOR,
NORMAL,
LIGHT
};
int main(int argc, char** argv)
{
if (argc < 3)
{
throw std::runtime_error("Nesprávný počet parametrů. Správné použité je: \nptep <model> <shader s texturou 0> <shader s texturou 1> ...");
}
auto modelFilename = std::string(argv[1]);
auto textureFilenames = std::vector<std::string>(argv + 2, argv + argc);
int currentTextureIdx = 0;
SDL_Init(SDL_INIT_VIDEO);
int windowWidth = 1024;
int windowHeight = 768;
auto window = SDL_CreateWindow(
"PTEP",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
768, 768,
SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
auto glCtx = SDL_GL_CreateContext(window);
GLenum err = glewInit();
if (GLEW_OK != err)
{
// glew nejede
std::ostringstream stream;
stream << "GLEW error: " << glewGetErrorString(err);
throw std::runtime_error(stream.str());
}
std::cout << "Jedeme: " << glewGetString(GLEW_VERSION) << std::endl;
std::cout << "Loadujeme model " << modelFilename << std::endl;
std::vector<Vertex> vertices;
std::vector<GLuint> indices;
loadModel(modelFilename, vertices, indices);
std::cout << "Vytváříme buffery" << std::endl;
GLuint vbo;
GLuint ibo;
glCall(glGenBuffers, 1, &vbo);
glCall(glGenBuffers, 1, &ibo);
glCall(glBindBuffer, GL_ARRAY_BUFFER, vbo);
glCall(glBindBuffer, GL_ELEMENT_ARRAY_BUFFER, ibo);
glCall(glBufferData, GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glCall(glBufferData, GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
std::cout << "Vstupujeme do hlavní smyčky" << std::endl;
float modelRoty = 0.0f;
float roty = 0.0f;
float rotx = 1.0f;
float dist = -3.0f;
GLuint textureProgram = 0;
std::string lastProgramSource;
GLuint tangentSpaceProgram = 0;
unsigned lastTicks = SDL_GetTicks();
bool rotate = true;
bool showTangent = false;
bool loadShaders = true;
RenderMode renderMode = ALL;
auto ticks = SDL_GetTicks();
auto ticksDelta = 0;
auto run = true;
while (run)
{
auto ticks = SDL_GetTicks();
modelRoty += rotate ? 0.0002 * (ticks - lastTicks) : 0.0;
lastTicks = ticks;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
run = false;
break;
case SDL_WINDOWEVENT:
switch (event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
windowWidth = event.window.data1;
windowHeight = event.window.data2;
glCall(glViewport, 0, 0, windowWidth, windowHeight);
break;
default:
break;
}
break;
case SDL_MOUSEMOTION:
if (event.motion.state & SDL_BUTTON_LMASK)
{
roty += event.motion.xrel * 0.01f;
rotx += event.motion.yrel * 0.01f;
}
if (event.motion.state & SDL_BUTTON_RMASK)
{
}
break;
case SDL_MOUSEWHEEL:
dist += event.wheel.y * 0.1f;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_r: rotate = !rotate; break;
case SDLK_t: showTangent = !showTangent; break;
case SDLK_a: renderMode = ALL; break;
case SDLK_s: renderMode = COLOR; break;
case SDLK_d: renderMode = NORMAL; break;
case SDLK_f: renderMode = LIGHT; break;
case SDLK_F5: loadShaders = true; break;
case SDLK_LEFT:
currentTextureIdx += textureFilenames.size() - 1; currentTextureIdx %= textureFilenames.size();
std::cout << "Přepínám na texturu " << textureFilenames[currentTextureIdx] << " s idx " << currentTextureIdx << std::endl;
loadShaders = true;
break;
case SDLK_RIGHT:
currentTextureIdx++; currentTextureIdx %= textureFilenames.size();
std::cout << "Přepínám na texturu " << textureFilenames[currentTextureIdx] << " s idx " << currentTextureIdx << std::endl;
loadShaders = false;
break;
}
break;
default:
break;
}
}
auto lastTicks = ticks;
ticks = SDL_GetTicks();
ticksDelta = ticks - lastTicks;
try
{
auto programSource = readFile(textureFilenames[currentTextureIdx]);
if (programSource != lastProgramSource)
{
if (textureProgram != 0)
{
glDeleteProgram(textureProgram);
textureProgram = 0;
}
lastProgramSource = programSource;
textureProgram = createTextureProgram(programSource);
}
glCall(glEnable, GL_DEPTH_TEST);
glCall(glLineWidth, 2.0f);
glCall(glClear, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto projection = glm::perspective(90.0f, float(windowWidth) / float(windowHeight), 0.1f, 100.0f );
auto view = glm::rotate(
glm::rotate(
glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, dist)),
rotx,
glm::vec3(1.0f, 0.0f, 0.0f)
),
roty, glm::vec3(0.0f, 1.0f, 0.0f)
);
auto model = glm::rotate(
glm::mat4(1.0f),
modelRoty, glm::vec3(0.0f, 1.0f, 0.0f)
);
// loadneme shadery, pokud je to treba
if (loadShaders)
{
std::cout << "Nahráváme shadery" << std::endl;
loadShaders = false;
if (textureProgram == 0)
{
glCall(glDeleteProgram, textureProgram);
textureProgram = 0;
}
if (tangentSpaceProgram == 0)
{
glCall(glDeleteProgram, textureProgram);
tangentSpaceProgram = 0;
}
try
{
textureProgram = createTextureProgram(readFile(textureFilenames[currentTextureIdx]));
}
catch (std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
try
{
std::vector<std::string> vertexShaders;
std::vector<std::string> geometryShaders;
std::vector<std::string> fragmentShaders;
vertexShaders.push_back(readFile("glsl/shared/vert.glsl"));
vertexShaders.push_back(readFile("glsl/tangentSpace/vert.glsl"));
geometryShaders.push_back(readFile("glsl/tangentSpace/geometry.glsl"));
fragmentShaders.push_back(readFile("glsl/tangentSpace/fragment.glsl"));
tangentSpaceProgram = createProgram(vertexShaders, geometryShaders, fragmentShaders);
}
catch (std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
}
std::vector<GLuint> programs = {textureProgram};
if (showTangent)
{
programs.push_back(tangentSpaceProgram);
}
for (auto program : programs)
{
if (program != 0)
{
glCall(glUseProgram, program);
auto mvLocation = glCall(glGetUniformLocation, program, "mvMat");
auto mvNormLocation = glCall(glGetUniformLocation, program, "mvNormMat");
auto pLocation = glCall(glGetUniformLocation, program, "pMat");
auto mvMat = view * model;
auto mvNormMat = glm::transpose(glm::inverse(glm::mat3(mvMat)));
auto pMat = projection;
glCall(glUniformMatrix4fv, mvLocation, 1, GL_FALSE, glm::value_ptr(mvMat));
glCall(glUniformMatrix3fv, mvNormLocation, 1, GL_FALSE, glm::value_ptr(mvNormMat));
glCall(glUniformMatrix4fv, pLocation, 1, GL_FALSE, glm::value_ptr(pMat));
auto lightPosLocation = glCall(glGetUniformLocation, program, "lightPos");
if (lightPosLocation != -1)
{
auto lightPos = glm::vec4(200.0f, 200.0f, 0.0f, 1.0f);
lightPos = view * lightPos;
glCall(glUniform4fv, lightPosLocation, 1, glm::value_ptr(lightPos));
}
auto timeLocation = glCall(glGetUniformLocation, program, "time");
if (timeLocation != 0)
{
glCall(glUniform1f, timeLocation, ticks * 0.001);
}
// modifikatory dle RenderMode
auto renderColor = glCall(glGetUniformLocation, program, "renderColor");
auto renderNormal = glCall(glGetUniformLocation, program, "renderNormal");
auto renderLight = glCall(glGetUniformLocation, program, "renderLight");
if (renderColor != 0)
glCall(glUniform1i, renderColor, renderMode == COLOR);
if (renderNormal != 0)
glCall(glUniform1i, renderNormal, renderMode == NORMAL);
if (renderLight != 0)
glCall(glUniform1i, renderLight, renderMode == LIGHT);
for (int i = 0; i < 4; i++)
glCall(glEnableVertexAttribArray, i);
// pozice
glCall(glVertexAttribPointer, 0u, 3, GL_FLOAT, GL_FALSE, (GLsizei) sizeof(Vertex), (void*) offsetof(Vertex, _x));
// texcoord
glCall(glVertexAttribPointer, 1u, 2, GL_FLOAT, GL_FALSE, (GLsizei) sizeof(Vertex), (void*) offsetof(Vertex, _u));
// normala
glCall(glVertexAttribPointer, 2u, 3, GL_FLOAT, GL_FALSE, (GLsizei) sizeof(Vertex), (void*) offsetof(Vertex, _nx));
// tangent
glCall(glVertexAttribPointer, 3u, 4, GL_FLOAT, GL_FALSE, (GLsizei) sizeof(Vertex), (void*) offsetof(Vertex, _tanx));
glCall(glDrawElements, GL_TRIANGLES, (GLsizei) indices.size(), GL_UNSIGNED_INT, nullptr);
for (int i = 0; i < 5; i++)
glCall(glDisableVertexAttribArray, i);
glCall(glUseProgram, 0);
}
}
SDL_GL_SwapWindow(window);
}
catch (std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
}
glCall(glDeleteBuffers, 1, &vbo);
glCall(glDeleteBuffers, 1, &ibo);
SDL_GL_DeleteContext(glCtx);
SDL_DestroyWindow(window);
SDL_Quit();
}