-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
212 lines (163 loc) · 6.13 KB
/
main.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
#include <glbinding/Binding.h>
#include <glbinding/callbacks.h>
#include <glbinding/gl/gl.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <chrono>
#include <glog/logging.h>
#include "kinect.h"
#include "shader.hpp"
#include "simple_file.hpp"
#include "vertex_format.h"
#define VS_FN "1.vs.glsl"
#define FS_FN "1.fs.glsl"
using namespace gl;
using namespace std::chrono;
using namespace simple_file;
std::string f_fn, v_fn;
GLFWwindow *g_window;
shader *s;
kinect k;
bool seed = false;
void keycb(GLFWwindow *window, int key, int, int, int) {
switch (key) {
case 'R': {
LOG(INFO) << "reloading shader";
s = new shader(read(v_fn).c_str(), read(f_fn).c_str());
break;
}
case 'P': {
seed = true;
}
default: { break; }
}
}
int main(int argc, char **argv) {
f_fn = std::string(FS_FN);
v_fn = std::string(VS_FN);
bool kinect_isgood = false;
uint16_t *depths = nullptr;
try {
k.setup();
int depthsize = k.get_buffer_size();
depths = new uint16_t[depthsize];
memset(depths, 0, depthsize);
kinect_isgood = true;
} catch (...) {
LOG(ERROR) << "running with no kinect";
}
if (!glfwInit()) {
LOG(FATAL) << "failed to initialize glfw";
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
g_window = glfwCreateWindow(640, 480, argv[0], nullptr, nullptr);
glfwMakeContextCurrent(g_window);
glbinding::Binding::initialize(false);
glbinding::setCallbackMaskExcept(glbinding::CallbackMask::After,
{"glGetError"});
glbinding::setAfterCallback([](const glbinding::FunctionCall &call) {
const auto error = glGetError();
if (error != GL_NO_ERROR)
LOG(ERROR) << "error in " << call.function->name() << ": " << std::hex
<< error << std::endl;
});
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
s = new shader(read(v_fn).c_str(), read(f_fn).c_str());
GLint u_time = glGetUniformLocation(s->program(), "time");
GLint u_mouse_pos = glGetUniformLocation(s->program(), "mousePos");
GLint u_depth = glGetUniformLocation(s->program(), "depth");
GLuint depth_texture = 0;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &depth_texture);
glBindTexture(GL_TEXTURE_2D, depth_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLint)GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLint)GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLint)GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLint)GL_NEAREST);
GLuint fb_vao;
glGenVertexArrays(1, &fb_vao);
glBindVertexArray(fb_vao);
int field_width(1000);
int field_area = field_width * field_width;
vert *orig_points = new vert[field_area];
vert *point_data = new vert[field_area];
for (int y = 0; y < field_width; y++) {
for (int x = 0; x < field_width; x++) {
glm::vec2 position = {(1.5 / field_width) * x - 0.7f,
(1.5 / field_width) * y - 0.7f};
point_data[field_width * y + x] = {position, {0.0, 0.0}, position};
orig_points[field_width * y + x] = {position, {0.0, 0.0}, position};
}
}
GLuint xform_in_buf;
glGenBuffers(1, &xform_in_buf);
glBindBuffer(GL_ARRAY_BUFFER, xform_in_buf);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert) * field_area, point_data,
GL_STREAM_DRAW);
GLint a_position = glGetAttribLocation(s->program(), "position");
glEnableVertexAttribArray(a_position);
glVertexAttribPointer(a_position, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
0);
GLint a_velocity = glGetAttribLocation(s->program(), "velocity");
glEnableVertexAttribArray(a_velocity);
glVertexAttribPointer(a_velocity, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
(void *)sizeof(glm::vec2));
GLint a_original_pos = glGetAttribLocation(s->program(), "originalPos");
glEnableVertexAttribArray(a_original_pos);
glVertexAttribPointer(a_original_pos, 2, GL_FLOAT, GL_FALSE,
6 * sizeof(GLfloat), (void *)(sizeof(glm::vec2) * 2));
GLuint xform_out_buf;
glGenBuffers(1, &xform_out_buf);
glBindBuffer(GL_ARRAY_BUFFER, xform_out_buf);
glBufferData(GL_ARRAY_BUFFER, field_area * sizeof(fb_vert), nullptr,
GL_STATIC_READ);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xform_out_buf);
fb_vert *feedback_buffer = new fb_vert[field_area];
glBindBuffer(GL_ARRAY_BUFFER, xform_in_buf);
glPointSize(2.0);
glfwSetKeyCallback(g_window, keycb);
auto t_prev = high_resolution_clock::now();
while (!glfwWindowShouldClose(g_window)) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
s->use();
auto t_now = high_resolution_clock::now();
float time = duration_cast<duration<float>>(t_now - t_prev).count();
t_prev = t_now;
glUniform1f(u_time, time);
if (kinect_isgood) {
glTexImage2D(GL_TEXTURE_2D, 0, (GLint)GL_R16UI, 640, 480, 0,
GL_RED_INTEGER, GL_UNSIGNED_SHORT, k.get_depthmap_pointer());
}
glUniform1i(u_depth, 0);
if (seed) {
glBufferData(GL_ARRAY_BUFFER, sizeof(vert) * field_area, orig_points,
GL_STREAM_DRAW);
seed = false;
}
glBeginTransformFeedback(GL_POINTS);
glDrawArrays(GL_POINTS, 0, field_area);
glEndTransformFeedback();
glfwSwapBuffers(g_window);
// feed vertex output back into input
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0,
sizeof(fb_vert) * field_area, feedback_buffer);
for (int i = 0; i < field_area; i++) {
point_data[i].position = feedback_buffer[i].outPosition;
point_data[i].velocity = feedback_buffer[i].outVelocity;
}
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vert) * field_area, point_data);
glfwPollEvents();
}
glDeleteTextures(1, &depth_texture);
glDeleteBuffers(1, &xform_out_buf);
glDeleteBuffers(1, &xform_in_buf);
glDeleteVertexArrays(1, &fb_vao);
glfwTerminate();
return 0;
}