-
Notifications
You must be signed in to change notification settings - Fork 1
/
gl.cpp
345 lines (289 loc) · 10.8 KB
/
gl.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
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
#include <Eigen/Core>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <igl/png/render_to_png.h>
#include <igl/ortho.h>
#include <igl/centroid.h>
#include <igl/opengl/report_gl_error.h>
#include <igl/opengl/init_render_to_texture.h>
#include <igl/look_at.h>
#include <igl/readOBJ.h>
#include <igl/readMESH.h>
#include <igl/readDMAT.h>
#include <igl/normalize_row_sums.h>
#include <igl/lbs_matrix.h>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <igl/opengl/glfw/background_window.h>
#include "time_utils.h"
#include "print_opengl_info.h"
#include "Shader.h"
#include "persp.h"
#include "wrap_igl.h"
#include "normalized_device_coordinate.h"
#include "depthbuffer_to_png.h"
#include "intersection_volume.h"
#include "Line.h"
#include "Quad.h"
#include "Box.h"
#include "minitrace.h"
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#endif
using namespace Eigen;
using namespace std;
// sizse for on-screen rendering
int scr_width = 800;
int scr_height = 800;
// size for off-screen rendering to texture
// note: scr_{width, height} changes with window resize callback
const int ren_width_ = 400;
const int ren_height_ = 400;
string filename;
string data_dir = "../data/";
string shader_dir_ = "../src/shaders/";
const auto getfilepath = [](const string& name, const string& ext){
return data_dir + name + "." + ext;
};
MatrixXf V, W, M, T;
MatrixXi F;
MatrixXi Tet; // !!!!!!!!!!!
GLuint vao;
bool wire_frame = false;
bool orthographic = false;
bool mouse_down = false;
bool save_png = true;
bool compute_selfintersection = true;
Affine3f model = Affine3f::Identity();
Affine3f view = Affine3f::Identity() * Translation3f(Vector3f(0, 0, -10));
Matrix4f projection = Matrix4f::Identity();
// a bit > 1 so that gl_FragCoord.z == 1 means reached far plane
// instead of the added possibility of a fragment just touching the far plane
float half = 1.001;
const auto set_view = [](Affine3f& view) {
if (orthographic) {
igl::look_at(Vector3f(0,0,-half), Vector3f(0,0,0), Vector3f(0,1,0), view.matrix());
} else {
view = Affine3f::Identity() * Translation3f(Vector3f(0, 0, -10));
}
};
// need to call this before setting glViewPort
// MacOS retina screen framebuffer size is twice that of window size
const auto set_viewport = [](GLFWwindow* window) {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
};
const auto make_centered_model = [](Affine3f& model){
double vol;
RowVector3f centroid;
igl::centroid(V, F, centroid, vol);
model = Affine3f::Identity();
model.translate(-centroid.transpose());
};
const auto reshape = [](GLFWwindow* window, int width, int height) {
::scr_width = width; ::scr_height = height;
float near, far, top, bottom, left, right;
if (orthographic) {
float near, far, top, right, left, bottom;
near = -half; far = half; top = half;
right = top * (double)::scr_width/(double)::scr_height;
left = -right; bottom = -top;
igl::ortho(left, right, bottom, top, near, far, projection);
} else {
near = 0.01;
far = 100;
top = tan(35./360.*M_PI)*near;
right = top * (double)::scr_width/(double)::scr_height;
left = -right;
bottom = -top;
persp(left, right, bottom, top, near, far, projection);
}
};
const auto reshape_current = [](GLFWwindow* window) {
int width_window, height_window;
glfwGetWindowSize(window, &width_window, &height_window);
reshape(window,width_window,height_window);
};
int main(int argc, char* argv[])
{
filename = "small";
if (argc > 1) { filename = string(argv[1]); }
igl::readMESH(getfilepath(filename, "mesh"), V, Tet, F);
igl::readDMAT(getfilepath(filename, "dmat"), W);
igl::normalize_row_sums(W, W); // normalization before LBS !!
normalized_device_coordinate(V);
Eigen::MatrixXd Vd = V.cast<double>().eval();
Eigen::MatrixXd Wd = W.cast<double>().eval();
Eigen::MatrixXd Md;
igl::lbs_matrix(Vd, Wd, Md);
Eigen::MatrixXf M;
M = Md.cast<float>().eval();
// initialize objects
SelfIntersectionVolume vol(V, W, M, F, ren_width_, ren_height_, shader_dir_);
auto screen = Quad<float>();
auto xaxis = Line<float>(Vector3f(-1,0,0), Vector3f(5,0,0));
auto yaxis = Line<float>(Vector3f(0,-1,0), Vector3f(0,5,0));
auto unitbox = Box<float>(half);
if (!glfwInit()) { std::cerr<<"Could not initialize glfw\n"; return -1; }
glfwSetErrorCallback([](int err, const char* msg) { std::cerr<<msg<<'\n'; });
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(scr_width, scr_height, "gl", NULL, NULL);
if (window == NULL) {
std::cerr<<"Failed to create GLFW window"<<'\n';
glfwTerminate();
return EXIT_FAILURE; }
glfwMakeContextCurrent(window);
glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cerr<<"Failed to load OpenGL and its extensions\n";
return EXIT_FAILURE;
}
print_opengl_info(window);
igl::opengl::report_gl_error("init");
std::cout<< R"(
Usage:
L,l toggle wireframe rendering
Z,z reset view to look along z-axis
R,r reset model, view, projection to default
P,p toggle orthographic viewings
S,s save color/depth to .png
)";
glfwSetWindowSizeCallback(window, reshape);
reshape_current(window);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
});
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window,true);
});
glfwSetCharModsCallback(window, [](GLFWwindow* window, unsigned int codepoint, int mods) {
std::cout<<"key: "<<static_cast<char>(codepoint)<<'\n';
switch(codepoint) {
case 'L':
case 'l':
wire_frame ^= 1; break;
case 'Z':
case 'z':
view.matrix().block(0,0,3,3).setIdentity(); break;
case 'R':
case 'r':
set_view(view);
reshape_current(window);
break;
case 'P':
case 'p':
orthographic ^= 1;
set_view(view);
reshape_current(window);
break;
case 'S':
case 's':
save_png ^= 1;
break;
case 'C':
case 'c':
compute_selfintersection ^= 1;
break;
default:
std::cout<<"Unrecognized key: "<<static_cast<char>(codepoint)<<'\n';
break;
}
});
glfwSetMouseButtonCallback(window, [](GLFWwindow* window, int button, int action, int mods){
mouse_down = action == GLFW_PRESS;
});
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double x, double y) {
static double mouse_last_x = x;
static double mouse_last_y = y;
double dx = x-mouse_last_x;
double dy = y-mouse_last_y;
if(mouse_down) {
float factor = abs(view.matrix()(2,3));
view.rotate(AngleAxisf(dx*factor/float(::scr_width), Vector3f(0,1,0)));
view.rotate(AngleAxisf(dy*factor/float(::scr_height),
view.matrix().topLeftCorner(3,3).inverse() * Vector3f(1,0,0)));
}
mouse_last_x = x;
mouse_last_y = y;
});
glfwSetScrollCallback(window, [](GLFWwindow* window, double xoffset, double yoffset) {
view.matrix()(2,3) = min(max(view.matrix()(2,3)+(float)yoffset,-100.0f), 0.f);
});
auto viz_shader = Shader({shader_dir_+"viz.vs"}, {shader_dir_+"viz.fs"});
auto screen_shader = Shader({shader_dir_+"screen.vs"}, {shader_dir_+"screen.fs"});
viz_shader.compile();
screen_shader.compile();
vertex_array(V, F, vao);
screen.create_vertex_array();
xaxis.create_vertex_array();
yaxis.create_vertex_array();
unitbox.create_vertex_array();
vol.prepare();
// compute view and projection matices
// Notice: could not be put inside prepare() based on current design
Eigen::RowVector3f A_center = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff());
float new_half = (V.rowwise()-A_center).rowwise().norm().maxCoeff() * 1.0001;
igl::ortho(-new_half, new_half, -new_half, new_half, 0, 2*new_half, vol.projection);
Eigen::Vector3f eye(A_center(0), A_center(1), -new_half+A_center(2));
Eigen::Vector3f target(A_center(0), A_center(1), A_center(2));;
Eigen::Vector3f up(0, 1, 0);
igl::look_at(eye, target, up, vol.view.matrix());
vol.model = Eigen::Affine3f::Identity();
vol.ortho_box_volume = pow(2*new_half, 3.0);
while (!glfwWindowShouldClose(window))
{
double tic = get_seconds();
if (compute_selfintersection) {
float volume = vol.compute(vol.T);
std::cout << "Volume: " << volume << std::endl;
compute_selfintersection = false;
}
// default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
set_viewport(window);
glClearColor(0.5, 0.5, 0.5, 1.);
glClearDepth(1.);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (wire_frame)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
viz_shader.compile();
viz_shader.use();
viz_shader.set_mat4("proj", projection);
viz_shader.set_mat4("view", view.matrix());
viz_shader.set_mat4("model", model.matrix());
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, F.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
xaxis.draw();
yaxis.draw();
unitbox.draw();
screen_shader.compile();
screen_shader.use();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
for (int i = 0; i < 2; ++i) {
screen_shader.set_mat4("mvp", (projection*view*(model*Translation3f(Vector3f(0,0,-2-2*i)))).matrix());
screen_shader.set_int("screen_texture", i);
glActiveTexture(GL_TEXTURE0+i);
glBindTexture(GL_TEXTURE_2D, vol.ren_tex[i]);
screen.draw();
}
glfwSwapBuffers(window);
glfwPollEvents();
sleep_by_fps(60, tic);
}
glDeleteVertexArrays(1, &vao);
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}