-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatmap.cpp
175 lines (155 loc) · 5.99 KB
/
Heatmap.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
//
// Created by Garret on 12/29/2022.
//
#include <cmath>
#include <cstring>
#include "Heatmap.h"
#include <glad/gl.h>
#include <glm/vec2.hpp>
#ifdef GL_DEBUG
#define GL_CALL(_CALL) do { _CALL; GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "%s:%d GL error 0x%x returned from '%s'.\n", __FILE__, __LINE__, gl_err, #_CALL); } while (0) // Call with error check
#define GL_CHECK do { GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "%s:%d GL error 0x%x returned \n", __FILE__, __LINE__, gl_err); } while (0) // Just error check
#else
#define GL_CALL(_CALL) _CALL
#define GL_CHECK (void)0
#endif
struct Color {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
};
const Color WHITE = {255, 255, 255, 255};
const Color BLACK = {0, 0, 0, 255};
Heatmap::Heatmap(int width, int height) :
width(width),
height(height),
textureId(0),
vao(0),
vbo(0),
ebo(0),
shader("resources/texture.vsh.glsl", "resources/texture.fsh.glsl") {
data = malloc(sizeof(Color) * width * height);
memset(data, 0, sizeof(Color) * width * height);
// initialize the VBO, VAO
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
GL_CHECK;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
GL_CHECK;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GL_CHECK;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
GL_CHECK;
// position attribute
GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) 0));
GL_CALL(glEnableVertexAttribArray(0));
// color attribute
GL_CALL(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (3 * sizeof(float))));
GL_CALL(glEnableVertexAttribArray(1));
// texture coord attribute
GL_CALL(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (6 * sizeof(float))));
GL_CALL(glEnableVertexAttribArray(2));
// glDisableVertexAttribArray(0);
// glBindVertexArray(0);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
GL_CHECK;
// allocate an OpenGL texture
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *) data);
GL_CHECK;
// configure initial shader params
shader.use();
GL_CHECK;
glUniform1i(glGetUniformLocation(shader.id, "texture1"), 0);
auto textureOffsetId = glGetUniformLocation(shader.id, "textureOffset");
auto textureScaleId = glGetUniformLocation(shader.id, "textureScale");
GLfloat offset[2] = {};
GLfloat scale[2] = {1.0f, 1.0f};
glUniform2fv(textureOffsetId, 1, offset);
glUniform2fv(textureScaleId, 1, scale);
}
Heatmap::~Heatmap() {
free(data);
glDeleteTextures(1, &textureId);
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
}
/// Helper function that generates a random integer between bounds.
/// \param min Minimum value, inclusive.
/// \param max Maximum value, inclusive.
/// \return Random integer
int getRandomValue(int min, int max) {
if (min > max) {
int tmp = max;
max = min;
min = tmp;
}
return (rand() % (abs(max - min) + 1) + min);
}
void Heatmap::fillWithNoise() {
const float FACTOR = 0.5f;
Color *pixels = (Color *) data;
for (int i = 0; i < width * height; i++) {
if (getRandomValue(0, 99) < (int) (FACTOR * 100.0f)) pixels[i] = WHITE;
else pixels[i] = BLACK;
}
updateTexture();
}
void Heatmap::updateTexture() {
GL_CHECK;
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *) data);
GL_CHECK;
// configure initial shader params
shader.use();
GL_CHECK;
glUniform1i(glGetUniformLocation(shader.id, "texture1"), 0);
auto textureOffsetId = glGetUniformLocation(shader.id, "textureOffset");
auto textureScaleId = glGetUniformLocation(shader.id, "textureScale");
GLfloat offset[2] = {};
GLfloat scale[2] = {1.0f, 1.0f};
glUniform2fv(textureOffsetId, 1, offset);
glUniform2fv(textureScaleId, 1, scale);
#if 0
GL_CALL(glActiveTexture(GL_TEXTURE0));
GL_CALL(glBindTexture(GL_TEXTURE_2D, textureId));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *) data));
GL_CALL(glGenerateMipmap(GL_TEXTURE_2D));
GL_CALL(glBindTexture(GL_TEXTURE_2D, 0));
#endif
}
void Heatmap::draw() {
GL_CALL(glActiveTexture(GL_TEXTURE0));
GL_CALL(glBindTexture(GL_TEXTURE_2D, textureId));
shader.use();
GL_CALL(glBindVertexArray(vao));
GL_CALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0));
}