-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.cu
More file actions
201 lines (169 loc) · 7.05 KB
/
Copy pathdisplay.cu
File metadata and controls
201 lines (169 loc) · 7.05 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
#pragma once
#include "common.cu"
std::map<int, int> key_states;
float mouse_scroll_y = 10.0f;
int visible_flags = View::VisibleFlag::frustum | View::VisibleFlag::tile_frustum;
static GLuint loadShaderFromSourceCode(GLenum type, const char* sourcecode, int length)
{
GLuint shaderId = glCreateShader(type);
glShaderSource(shaderId, 1, &sourcecode, &length);
glCompileShader(shaderId);
GLint isCompiled = 0;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &maxLength);
auto errorLog = std::make_unique<GLchar[]>(maxLength);
glGetShaderInfoLog(shaderId, maxLength, &maxLength, &errorLog[0]);
std::cout << "Error compiling " << std::endl
<< &errorLog[0] << std::endl;
glDeleteShader(shaderId); // Don't leak the shader.
return 0;
}
return shaderId;
}
static GLuint loadShaderFromFile(GLenum type, const char* filepath)
{
std::cout << "Loading shader '" << filepath << "'" << std::endl;
std::ifstream fstream;
fstream.open(filepath);
if (!fstream.is_open())
{
std::cout << "Unable to open file '" << filepath << "'" << std::endl;
return 0;
}
std::stringstream sstream;
std::string line;
while (std::getline(fstream, line))
sstream << line << '\n';
line = sstream.str();
GLuint shaderId = loadShaderFromSourceCode(type, line.c_str(), line.length());
if (!shaderId)
std::cout << "...with filepath '" << filepath << "'";
return shaderId;
}
struct shader_load_data_t {
GLenum type;
const char* filepath;
};
static GLuint createProgram(std::vector<shader_load_data_t> shader_load_data)
{
GLuint program;
program = glCreateProgram();
std::vector<GLuint> shaders;
shaders.reserve(shader_load_data.size());
for (auto& s : shader_load_data) {
GLuint shader = loadShaderFromFile(s.type, s.filepath);
shaders.push_back(shader);
glAttachShader(program, shader);
}
glLinkProgram(program);
for (auto& s : shaders)
glDeleteShader(s);
return program;
}
void display(uvec2 res, std::function<glm::vec4*(uvec3, View)> update) {
assert(glfwInit() == GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
auto window = glfwCreateWindow(res.x, res.y, "Clustered Shading Debug", nullptr, nullptr);
glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
glfwMakeContextCurrent(window);
assert(glewInit() == GLEW_OK);
glfwSwapInterval(1);
glClearColor(.2, .1, 0, 1);
glViewport(0, 0, res.x, res.y);
// std::this_thread::sleep_for(1s);
auto display_program = createProgram({
{GL_VERTEX_SHADER, "vertex.glsl"},
{GL_FRAGMENT_SHADER, "fragment.glsl"}
});
glUseProgram(display_program);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
enum { vertex_position, vertex_uv };
GLuint vao;
GLuint vbos[2];
glCreateVertexArrays(1, &vao);
glGenBuffers(2, vbos);
glBindVertexArray(vao);
glEnableVertexAttribArray(vertex_position);
glBindBuffer(GL_ARRAY_BUFFER, vbos[vertex_position]);
glm::vec2 vertex_positions[] = { {-1, -1}, {1, -1}, {1, 1}, {-1, -1}, {1, 1}, {-1, 1} };
glBufferData(GL_ARRAY_BUFFER, sizeof (vertex_positions), vertex_positions, GL_STATIC_DRAW);
glVertexAttribPointer(vertex_position, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(vertex_uv);
glBindBuffer(GL_ARRAY_BUFFER, vbos[vertex_uv]);
glm::vec2 vertex_uvs[] = { {0, 0}, {1, 0}, {1, 1}, {0, 0}, {1, 1}, {0, 1} };
glBufferData(GL_ARRAY_BUFFER, sizeof (vertex_uvs), vertex_uvs, GL_STATIC_DRAW);
glVertexAttribPointer(vertex_uv, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glfwSetKeyCallback(window, [] (GLFWwindow *window, int key, int scancode, int action, int mods) {
if (!mods && key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, true);
int value = action == GLFW_PRESS ? 1 : (action == GLFW_RELEASE ? -1 : 0);
if (value != 0)
key_states.insert_or_assign(key, value);
});
auto update_key_states = [] {
for (auto& [_, v] : key_states) {
v += sign(v);
}
};
glfwSetScrollCallback(window, [] (GLFWwindow* window, double dx, double dy) {
mouse_scroll_y = max(3.0f, mouse_scroll_y - dy);
});
uvec3 tile_coord = uvec3(grid_size - 1);
vec2 look_at = vec2(0, 0);
int lights_offset = 0;
while (!glfwWindowShouldClose(window)) {
// handle input
update_key_states();
glfwPollEvents();
dvec2 mouse;
glfwGetCursorPos(window, &mouse.x, &mouse.y);
tile_coord.x += (key_states[GLFW_KEY_D] % 8 == 1) - (key_states[GLFW_KEY_A] % 8 == 1);
tile_coord.y += (key_states[GLFW_KEY_W] % 8 == 1) - (key_states[GLFW_KEY_S] % 8 == 1);
tile_coord.z += (key_states[GLFW_KEY_Q] % 8 == 1) - (key_states[GLFW_KEY_E] % 8 == 1);
tile_coord = tile_coord % uvec3(grid_size);
if (key_states[GLFW_KEY_SPACE] == 1) {
look_at = vec2(0, 0);
mouse_scroll_y = 10.0f;
}
look_at.x += (key_states[GLFW_KEY_RIGHT] > 0) - (key_states[GLFW_KEY_LEFT] > 0);
look_at.y += (key_states[GLFW_KEY_DOWN] > 0) - (key_states[GLFW_KEY_UP] > 0);
lights_offset += (key_states[GLFW_KEY_M] % 8 == 1) - (key_states[GLFW_KEY_N] % 8 == 1);
if ((key_states[GLFW_KEY_1] % 8 == 1))
visible_flags ^= View::VisibleFlag::frustum;
if ((key_states[GLFW_KEY_2] % 8 == 1))
visible_flags ^= View::VisibleFlag::tile_frustum;
if ((key_states[GLFW_KEY_3] % 8 == 1))
visible_flags ^= View::VisibleFlag::tile_lights;
if ((key_states[GLFW_KEY_4] % 8 == 1))
visible_flags ^= View::VisibleFlag::lights;
View view {
.origin = (vec2(mouse.x, mouse.y) / vec2(res) * 2.0f - 1.0f) * vec2(-1, 1) * pi<float>(),
.look_at = look_at,
.zoom = mouse_scroll_y,
.lights_offset = lights_offset,
.visible_flags = visible_flags,
};
view.origin.y = max(min(view.origin.y, pi<float>() * 0.45f), -pi<float>() * 0.45f);
// draw the image using ray marching
vec4 *image = update(tile_coord, view);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, res.x, res.y, 0, GL_RGBA, GL_FLOAT, image);
// present the image to screen
glUseProgram(display_program);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
}
glfwTerminate();
}