-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLTools.h
409 lines (381 loc) · 12.2 KB
/
GLTools.h
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
#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <string>
#include <random>
#include <fstream>
#include <sstream>
#include "HSV.h"
#include <SFML/Graphics.hpp>
namespace nr {
namespace util {
std::string ReadFile(const std::string& fileName) {
std::string srcCode;
std::ifstream vShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
vShaderFile.open(fileName);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
vShaderFile.close();
return vShaderStream.str();
}
sf::Image LoadImage(const std::string& fileName) {
sf::Image img;
if (img.loadFromFile(fileName)) {
return img;
}
throw std::exception();
}
}
namespace driver {
unsigned int NUM_POINTS;
bool wireframeMode_ = false;
enum class VERTEXATTRIBUTE : GLuint {
POSITION = 0,
COLOR = 1,
};
class Camera {
private:
float scroll{ 0 };
float pitch{ 0 };
float yaw{ 90 };
float roll{ 0 };
glm::vec3 cameraPosition_;
glm::vec3 cameraFront_;
glm::vec3 cameraUp_;
glm::mat4 viewMatrix_;
const float cameraSpeed_ = 15;
const float eulerSpeed_ = 4;
void UpdateRotation() {
glm::vec3 direction = glm::vec3(
cos(glm::radians(yaw)) * cos(glm::radians(pitch)),
sin(glm::radians(pitch)),
sin(glm::radians(yaw)) * cos(glm::radians(pitch))
);
cameraFront_ = glm::normalize(direction);
}
public:
Camera()
:viewMatrix_(glm::mat4(1.0f)) {
cameraPosition_ = glm::vec3(5.0f, 100.0f, 10.0f);
cameraUp_ = glm::vec3(0.0f, 1.0f, 0.0f);
cameraFront_ = glm::vec3(-2.0f, -2.0f, -1.0f);
UpdateRotation();
}
inline void MoveNorth() {
cameraPosition_ = cameraPosition_ + cameraSpeed_ * cameraFront_;
}
inline void MoveSouth() {
cameraPosition_ -= cameraSpeed_ * cameraFront_;
}
inline void MoveWest() {
cameraPosition_ += cameraSpeed_ * glm::normalize(glm::cross(cameraFront_, cameraUp_));
}
inline void MoveEast() {
cameraPosition_ -= cameraSpeed_ * glm::normalize(glm::cross(cameraFront_, cameraUp_));
}
inline void LookUp() {
pitch += eulerSpeed_;
UpdateRotation();
}
inline void LookDown() {
pitch -= eulerSpeed_;
UpdateRotation();
}
inline void LookLeft() {
yaw -= eulerSpeed_;
UpdateRotation();
}
inline void LookRight() {
yaw += eulerSpeed_;
UpdateRotation();
}
inline glm::vec3 Position() const noexcept { return cameraPosition_; }
inline glm::vec3 Up() const noexcept { return cameraUp_; }
inline glm::vec3 Front() const noexcept { return cameraFront_; }
};
class Shader {
private:
std::string shaderName_;
std::string shaderSource_;
GLuint shaderID_;
public:
Shader(const int& shaderType_, const std::string& shaderName, const std::string& shaderSourceFile)
:
shaderName_(shaderName),
shaderSource_(nr::util::ReadFile(shaderSourceFile)) {
// allocate an id for the shader
shaderID_ = glCreateShader(shaderType_);
// bind the source code
auto str = shaderSource_.data();
glShaderSource(shaderID_, 1, &str, NULL);
// compile shader
glCompileShader(shaderID_);
}
bool CheckShader() {
int success;
char infoLog[512];
glGetShaderiv(shaderID_, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shaderID_, 512, NULL, infoLog);
std::cout << infoLog << std::endl;
return false;
}
std::cout << "SHADER FINE" << std::endl;
return true;
}
void Destroy() {
}
inline GLuint ID() const noexcept { return shaderID_; }
~Shader() {
Destroy();
}
};
auto camera_ = std::make_unique<nr::driver::Camera>();
class Program {
private:
std::vector<std::unique_ptr<nr::driver::Shader>> shaders_;
GLuint programID_;
inline GLuint GetLocation(const std::string& uniformName) const {
return glGetUniformLocation(programID_, uniformName.data());
}
public:
void RegisterShader(std::unique_ptr<Shader>&& shader) {
shaders_.emplace_back(std::move(shader));
}
bool Run() {
// check if shaders are fine first
for (const auto& shader : shaders_) {
if (!shader->CheckShader()) return false;
}
// create the program
programID_ = glCreateProgram();
// attatch the registered shaders
std::for_each(shaders_.begin(), shaders_.end(), [this](const std::unique_ptr<Shader>& shader) {
glAttachShader(programID_, shader->ID());
});
// link the program
glLinkProgram(programID_);
int success;
char infoLog[512];
glGetProgramiv(programID_, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(programID_, 512, NULL, infoLog);
std::cout << infoLog << std::endl;
}
// decouple the shaders
std::for_each(shaders_.begin(), shaders_.end(), [](std::unique_ptr<Shader>& shader) {
glDeleteShader(shader->ID());
});
return success;
}
void Use() {
glUseProgram(programID_);
}
void SetUniformVec3(const std::string& uniformName, const glm::vec3& vec) {
GLuint uniformLoc = GetLocation(uniformName);
glUniform3f(uniformLoc, vec.x, vec.y, vec.z);
}
void SetUniformMat4(const std::string& uniformName, const glm::mat4& mat) {
GLuint uniformLoc = GetLocation(uniformName);
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, glm::value_ptr(mat));
}
void SetUniformInt(const std::string& uniformName, const int& val) {
GLuint uniformLoc = GetLocation(uniformName);
glUniform1i(uniformLoc, val);
}
void SetUniformFloat(const std::string& uniformName, const float& val) {
GLuint uniformLoc = GetLocation(uniformName);
glUniform1f(uniformLoc, val);
}
};
}
}
namespace nr {
namespace callbacks {
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
switch (key) {
case GLFW_KEY_SPACE: {
if (glfwGetKey(window, GLFW_KEY_SPACE) != GLFW_PRESS) break;
nr::driver::wireframeMode_ = !nr::driver::wireframeMode_;
if (nr::driver::wireframeMode_)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
}
case GLFW_KEY_S:
{
nr::driver::camera_->MoveSouth();
break;
}
case GLFW_KEY_W:
{
nr::driver::camera_->MoveNorth();
break;
}
case GLFW_KEY_A:
{
nr::driver::camera_->MoveEast();
break;
}
case GLFW_KEY_D:
{
nr::driver::camera_->MoveWest();
break;
}
case GLFW_KEY_LEFT:
{
nr::driver::camera_->LookLeft();
break;
}
case GLFW_KEY_RIGHT:
{
nr::driver::camera_->LookRight();
break;
}
case GLFW_KEY_UP: {
nr::driver::camera_->LookUp();
break;
}
case GLFW_KEY_DOWN: {
nr::driver::camera_->LookDown();
break;
}
}
}
}
}
namespace nr {
namespace driver {
std::unique_ptr<nr::driver::Program> shaderProgram_;
GLFWwindow* window_;
glm::mat4 projectionMatrix_;
GLuint VAO_;
GLuint VBO_;
GLuint EBO_;
namespace init {
inline bool InitContext() {
glfwMakeContextCurrent(nr::driver::window_);
return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
}
bool InitWindow(const unsigned int& width, const unsigned int& height, const char* title) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
nr::driver::window_ = glfwCreateWindow(width, height, title, NULL, NULL);
return nr::driver::window_;
}
inline void InitCallbacks() {
glfwSetKeyCallback(nr::driver::window_, &nr::callbacks::KeyCallback);
}
void InitMesh(std::vector<float>& vertices, std::vector<unsigned int>& indices) {
// load up the height map
sf::Image heightMap = nr::util::LoadImage("einstein.jpg");
sf::Vector2u mapDim{ heightMap.getSize() };
// generate a X-Z vertex plane
const float VERTEX_X_SPACE = 1;
const float VERTEX_Z_SPACE = 1;
const float MAX_Y = pow(2,4);
const sf::Uint8* pxls = heightMap.getPixelsPtr();
for (unsigned int j = 0; j < mapDim.y; ++j) {
for (unsigned int i = 0; i < mapDim.x; ++i) {
sf::Color col = heightMap.getPixel(i, j);
auto colavg = (col.r + col.g + col.b) / static_cast<float>((255*3));
float y = MAX_Y * colavg;
vertices.insert(vertices.end(),
{ i * VERTEX_X_SPACE,
y ,
j * VERTEX_Z_SPACE
});
HSV::RGB vertexCol{ HSV::HSVtoRGB( (360 * y / MAX_Y), 100, 100) };
vertices.insert(vertices.end(),
{
vertexCol.r / 255,
vertexCol.g / 255,
vertexCol.b / 255
});
}
}
for (unsigned int j = 0; j < mapDim.y - 1; ++j) {
for (unsigned int i = 0; i < mapDim.x - 1; ++i) {
unsigned int index = j * mapDim.x + i;
indices.push_back(index);
indices.push_back(index + 1);
if (i % 2 == 0) {
indices.insert(indices.end(),
{
index + mapDim.x + 1,
index,
index + mapDim.x,
index + mapDim.x + 1
});
continue;
}
indices.insert(indices.end(),
{
index + mapDim.x,
index + mapDim.x,
index + mapDim.x + 1,
index + 1
});
}
}
NUM_POINTS = vertices.size();
}
void InitArrays() {
std::vector<float> vertices;
std::vector<unsigned int> indices;
InitMesh(vertices, indices);
glGenVertexArrays(1, &VAO_);
glBindVertexArray(VAO_);
glGenBuffers(1, &VBO_);
glGenBuffers(1, &EBO_);
glBindBuffer(GL_ARRAY_BUFFER, VBO_);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.size(), vertices.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(float), indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(static_cast<GLuint>(nr::driver::VERTEXATTRIBUTE::POSITION), 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (void*)0);
glVertexAttribPointer(static_cast<GLuint>(nr::driver::VERTEXATTRIBUTE::COLOR), 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(static_cast<GLuint>(nr::driver::VERTEXATTRIBUTE::COLOR));
glEnableVertexAttribArray(static_cast<GLuint>(nr::driver::VERTEXATTRIBUTE::POSITION));
}
void InitShaders() {
shaderProgram_ = std::make_unique<nr::driver::Program>();
shaderProgram_->RegisterShader(std::make_unique<nr::driver::Shader>(GL_VERTEX_SHADER, "vertexShader", "vertexShader.vert"));
shaderProgram_->RegisterShader(std::make_unique<nr::driver::Shader>(GL_FRAGMENT_SHADER, "fragmentShader", "fragmentShader.frag"));
}
bool InitProgram(const unsigned int& windowWidth, const unsigned int& windowHeight, const char* windowName) {
if (!glfwInit() || !InitWindow(windowWidth, windowHeight, windowName) || !InitContext()) return false;
InitCallbacks();
InitArrays();
InitShaders();
shaderProgram_->Run();
return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
}
}
void Render() {
shaderProgram_->Use();
projectionMatrix_ = glm::mat4(1.0f);
projectionMatrix_ = glm::perspective(glm::radians(45.0f), (float)1000 / (float)1000, 0.1f, 10000.0f);
shaderProgram_->SetUniformMat4("projectionMatrix", projectionMatrix_);
int frameNumber = 0;
while (!glfwWindowShouldClose(window_)) {
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 viewMatrix_ = glm::mat4(1.0f);
viewMatrix_ = glm::lookAt(camera_->Position(), camera_->Position() + camera_->Front(), camera_->Up());
shaderProgram_->Use();
shaderProgram_->SetUniformMat4("viewMatrix", viewMatrix_);
shaderProgram_->SetUniformFloat("time", glfwGetTime());
glBindVertexArray(VAO_);
glDrawElements(GL_TRIANGLES, NUM_POINTS*3, GL_UNSIGNED_INT, 0);
glfwPollEvents();
glfwSwapBuffers(window_);
++frameNumber;
}
}
}
}