-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
449 lines (412 loc) · 15.7 KB
/
Copy pathApp.cpp
File metadata and controls
449 lines (412 loc) · 15.7 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
// NOTE : Even tho I am enforcing to treat warnings as errors in cmake
// I am overriding some here just to be able to iterate fast to do this challenge
#pragma region CompilerSpecific
#if defined _WIN32
#pragma warning(disable: 4996 4267 4100 4189)
#endif
#pragma endregion
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <iostream>
#include <string>
#include <vector>
#define CONSOLE_LOG(x, ...) printf(x, __VA_ARGS__)
#define RESOURCES_DIR std::string("resources/shaders/")
#define APP_NAME std::string("Heat Map Vis App")
#define APP_WINDOW_INITIAL_WIDTH 800
#define APP_WINDOW_INITIAL_HEIGHT 800
namespace System
{
struct Memory
{
char * data = nullptr;
size_t size = 0;
};
Memory readFileToMemory(const char * fileName)
{
Memory memory;
FILE * file = fopen(fileName, "rb");
if (file != nullptr)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
memory.size = fileSize;
memory.data = new char[fileSize + 1];
fread(memory.data, 1, fileSize, file);
memory.data[fileSize] = '\0';
fclose(file);
}
return memory;
}
inline float convertToNormalizedDeviceCoordinates(float value)
{
return (2.0f * value - 1.0f);
}
namespace Windowing
{
void setWindowIcon(GLFWwindow * window, unsigned char * iconData = nullptr, int iconWidth = -1, int iconHeight = -1)
{
GLFWimage icon = { iconWidth, iconHeight, iconData };
if (!iconData || iconWidth == -1 || iconHeight == -1)
{
icon.width = 1;
icon.height = 1;
icon.pixels = new unsigned char[4] { 0, 0, 0, 255 };
}
glfwSetWindowIcon(window, 1, &icon);
if (!iconData || iconWidth == -1 || iconHeight == -1)
{
delete[] icon.pixels;
}
}
}
}
namespace Geometry
{
struct Data4D
{
std::vector<glm::vec4> vertices;
std::vector<unsigned int> indices;
};
struct Data2D
{
std::vector<glm::vec2> vertices;
std::vector<unsigned int> indices;
};
Data2D createRectangle(float width, float height, glm::vec2 center)
{
Data2D result;
result.vertices.reserve(4);
result.indices.reserve(6);
result.vertices.push_back(glm::vec2(-width * 0.5f, +height * 0.5f) + center);
result.vertices.push_back(glm::vec2(+width * 0.5f, +height * 0.5f) + center);
result.vertices.push_back(glm::vec2(+width * 0.5f, -height * 0.5f) + center);
result.vertices.push_back(glm::vec2(-width * 0.5f, -height * 0.5f) + center);
result.indices.push_back(0);
result.indices.push_back(1);
result.indices.push_back(2);
result.indices.push_back(2);
result.indices.push_back(3);
result.indices.push_back(0);
return result;
}
Data4D createRectangleWithTexture(float width, float height, glm::vec2 center)
{
Data4D result;
result.vertices.reserve(4);
result.indices.reserve(6);
result.vertices.push_back(glm::vec4(glm::vec2(-width * 0.5f, +height * 0.5f) + center, glm::vec2(0.0f, 1.0f)));
result.vertices.push_back(glm::vec4(glm::vec2(+width * 0.5f, +height * 0.5f) + center, glm::vec2(1.0f, 1.0f)));
result.vertices.push_back(glm::vec4(glm::vec2(+width * 0.5f, -height * 0.5f) + center, glm::vec2(1.0f, 0.0f)));
result.vertices.push_back(glm::vec4(glm::vec2(-width * 0.5f, -height * 0.5f) + center, glm::vec2(0.0f, 0.0f)));
result.indices.push_back(0);
result.indices.push_back(1);
result.indices.push_back(2);
result.indices.push_back(2);
result.indices.push_back(3);
result.indices.push_back(0);
return result;
}
}
namespace Math
{
struct ScalarField2D
{
ScalarField2D(int width, int height) : width(width), height(height)
{
data.resize(width * height);
}
std::pair<float, float> computeInverseOfDimensions()
{
if (width < 1 || height < 1)
{
std::cerr << "Warning, width and height in ScalarField2D are negative\n";
return std::pair(1.0f, 1.0f);
}
else
{
return std::make_pair(1.0f / float(width - 1), 1.0f / float(height - 1));
}
}
std::vector<float> data;
int width = -1;
int height = -1;
float min = 0.0f;
float max = 1.0f;
};
ScalarField2D generateConstant2DScalarField(int width = 10, int height = 10, float value = 0.0f)
{
assert(width > 0 && height > 0);
glm::clamp(value, 0.0f, 1.0f);
ScalarField2D result(width, height);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result.data[i + (j * width)] = value + 0.5f;
}
}
return result;
}
ScalarField2D generateLinearX2DScalarField(int width = 10, int height = 10)
{
assert(width > 0 && height > 0);
ScalarField2D result(width, height);
auto inverseDimensions = result.computeInverseOfDimensions();
for (int i = 0; i < width; i++)
{
float x = i * inverseDimensions.first;
for (int j = 0; j < height; j++)
{
result.data[i + (j * width)] = x;
}
}
return result;
}
ScalarField2D generateLinearXY2DScalarField(int width = 10, int height = 10)
{
assert(width > 0 && height > 0);
ScalarField2D result(width, height);
auto inverseDimensions = result.computeInverseOfDimensions();
for (int i = 0; i < width; i++)
{
float x = i * inverseDimensions.first;
x = System::convertToNormalizedDeviceCoordinates(x);
for (int j = 0; j < height; j++)
{
float y = j * inverseDimensions.second;
y = System::convertToNormalizedDeviceCoordinates(y);
result.data[i + (j * width)] = x + y;
}
}
return result;
}
ScalarField2D generateQuadratic2DScalarField(int width = 10, int height = 10)
{
assert(width > 0 && height > 0);
ScalarField2D result(width, height);
auto inverseDimensions = result.computeInverseOfDimensions();
for (int i = 0; i < width; i++)
{
float x = i * inverseDimensions.first;
x = System::convertToNormalizedDeviceCoordinates(x);
for (int j = 0; j < height; j++)
{
float y = j * inverseDimensions.second;
y = System::convertToNormalizedDeviceCoordinates(y);
result.data[i + (j * width)] = (x * x + y * y);
}
}
return result;
}
ScalarField2D generatePythonReference2DScalarField(int width = 10, int height = 10)
{
float scalarFieldMaxValue = FLT_MIN;
float scalarFieldMinValue = FLT_MAX;
assert(width > 0 && height > 0);
ScalarField2D result(width, height);
auto inverseDimensions = result.computeInverseOfDimensions();
for (int i = 0; i < width; i++)
{
float x = i * inverseDimensions.first;
x = 3.0f * System::convertToNormalizedDeviceCoordinates(x);
for (int j = 0; j < height; j++)
{
float y = j * inverseDimensions.second;
y = 3.0f * System::convertToNormalizedDeviceCoordinates(y);
float z = std::sinf(x * x + y * y);
if (z > scalarFieldMaxValue)
{
scalarFieldMaxValue = z;
}
if (z < scalarFieldMinValue)
{
scalarFieldMinValue = z;
}
result.data[i + (j * width)] = z;
}
}
CONSOLE_LOG("Min: %f\n", scalarFieldMinValue);
CONSOLE_LOG("Max: %f\n", scalarFieldMaxValue);
result.min = scalarFieldMinValue;
result.max = scalarFieldMaxValue;
return result;
}
}
namespace Graphics
{
struct BuffersHandles
{
unsigned int VAO;
unsigned int VBO;
unsigned int EBO;
};
BuffersHandles createBuffers(const Geometry::Data2D & data)
{
BuffersHandles result;
glGenVertexArrays(1, &result.VAO);
glGenBuffers(1, &result.VBO);
glGenBuffers(1, &result.EBO);
glBindVertexArray(result.VAO);
glBindBuffer(GL_ARRAY_BUFFER, result.VBO);
glBufferData(GL_ARRAY_BUFFER, data.vertices.size() * sizeof(glm::vec2), data.vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.indices.size() * sizeof(unsigned int), data.indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void *)0);
glEnableVertexAttribArray(0);
return result;
}
BuffersHandles createBuffers(const Geometry::Data4D & data)
{
BuffersHandles result;
glGenVertexArrays(1, &result.VAO);
glGenBuffers(1, &result.VBO);
glGenBuffers(1, &result.EBO);
glBindVertexArray(result.VAO);
glBindBuffer(GL_ARRAY_BUFFER, result.VBO);
glBufferData(GL_ARRAY_BUFFER, data.vertices.size() * sizeof(glm::vec4), data.vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.indices.size() * sizeof(unsigned int), data.indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (void *)0);
glEnableVertexAttribArray(0);
return result;
}
unsigned int createTexture(const Math::ScalarField2D & data)
{
unsigned int textureID = 0;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, data.width, data.height, 0, GL_RED, GL_FLOAT, &(data.data[0]));
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
int loadShader(const char * vertexShaderSource, const char * fragmentShaderSource)
{
int success = 0;
char infoLog[512] = { 0 };
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
printf("Failed to compile vertex shader %s\n", infoLog);
return -1;
}
CONSOLE_LOG("Vertex shader compiled\n");
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
printf("Failed to compile fragment shader %s\n", infoLog);
return -1;
}
CONSOLE_LOG("Fragment shader compiled\n");
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
printf("Failed to link program %s\n", infoLog);
return -1;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
CONSOLE_LOG("Shader program %d linked correctly\n", shaderProgram);
return shaderProgram;
}
}
int main(int, char **)
{
std::cout << APP_NAME << "\n";
if (!glfwInit())
{
std::cerr << "Error at glfwInit()\n";
return -1;
}
// NOTE : For glad in the online generator https://gen.glad.sh/
// I left OpenGL core 3.0 but on glfwWindowHints I have to do 3.3
// If I remember correclty that is a mismatch from Khronos Group
// when moving from fixed to programmable shaders, but just double
// check all this is correct (or go straight to latest OpenGL - 4.6)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
std::cout << "Initializing glfw window\n";
GLFWwindow * window = glfwCreateWindow(
APP_WINDOW_INITIAL_WIDTH,
APP_WINDOW_INITIAL_HEIGHT,
std::string(APP_NAME + " - Alejandro Guayaquil 2024").c_str(),
nullptr,
nullptr
);
if (!window)
{
std::cerr << "Error at glfwCreateWindow()\n";
glfwTerminate();
return -1;
}
System::Windowing::setWindowIcon(window);
glfwMakeContextCurrent(window);
std::cout << "Loading GL extensions\n";
if (!gladLoadGL(glfwGetProcAddress))
{
std::cerr << "Error at gladLoadGL()\n";
glfwDestroyWindow(window);
glfwTerminate();
return -1;
}
std::cout << "Reading shaders\n";
auto vertexShader = System::readFileToMemory((RESOURCES_DIR + "texture.vs").c_str());
auto fragmentShader = System::readFileToMemory((RESOURCES_DIR + "texture.fs").c_str());
auto shaderProgramID = Graphics::loadShader(vertexShader.data, fragmentShader.data);
std::cout << "Creating quad geometry and loading it to the GPU\n";
auto quadGeometry = Geometry::createRectangleWithTexture(2.0f, 2.0f, glm::vec2(0.0f));
auto graphicsBuffers = Graphics::createBuffers(quadGeometry);
std::cout << "Generating 2D scalar field and texture for it\n";
auto scalarField2D = Math::generatePythonReference2DScalarField(100, 100);
auto textureID = Graphics::createTexture(scalarField2D);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow * window, int width, int height)
{
CONSOLE_LOG("Resizing window %d %d\n", width, height);
glViewport(0, 0, width, height);
});
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glUseProgram(shaderProgramID);
glUniform1i(glGetUniformLocation(shaderProgramID, "uTextureSampler"), 0);
glUniform1f(glGetUniformLocation(shaderProgramID, "uScalarFieldMax"), scalarField2D.max);
glUniform1f(glGetUniformLocation(shaderProgramID, "uScalarFieldMin"), scalarField2D.min);
glUseProgram(0);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgramID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glBindVertexArray(graphicsBuffers.VAO);
glDrawElements(GL_TRIANGLES, quadGeometry.indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(window);
}
std::cout << "Destroying glfw window and exiting program\n";
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}