-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
447 lines (350 loc) · 12.8 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
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
#define FREEGLUT_STATIC
#include "gl_core_3_3.h"
#include <GL/glut.h>
#include <GL/freeglut_ext.h>
#define TW_STATIC
#include <AntTweakBar.h>
#include <ctime>
#include <memory>
#include <vector>
#include <string>
#include <cstdlib>
#include "glprogram.h"
#include "MyImage.h"
#include "VAOImage.h"
#include "VAOMesh.h"
#include "matlabDeformer.h"
using namespace std;
string textureFile;
std::shared_ptr<Deformer> deformer;
GLProgram MyMesh::prog, MyMesh::pickProg, MyMesh::pointSetProg;
GLTexture MyMesh::colormapTex;
MyMesh M;
int viewport[4] = { 0, 0, 1280, 960 };
int actPrimType = MyMesh::PE_VERTEX;
int saveImageResolution = 3072;
bool showATB = true;
int deformerType = 0;
bool continuousDeform = true;
int numIterationPerDeform = 1;
void loadP2PConstraints();
std::vector<std::string> dataset_names;
int idataset = 0;
std::string datadir;
std::string meshName = "mesh.obj";
std::string p2pFileName = "p2p";
void saveMesh(const std::string &filename, bool srcMesh=false)
{
using MapMatX2f = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, 2,Eigen:: RowMajor> >;
using MapMatX3i = Eigen::Map<const Eigen::Matrix<int, Eigen::Dynamic, 3, Eigen::RowMajor> >;
if (srcMesh){
matlabEval("x=X;");
}
else{
eigen2matlab("x", MapMatX2f(M.mesh.X.data(), M.nVertex, 2).cast<double>());
}
eigen2matlab("t", MapMatX3i(M.mesh.T.data(), M.nFace,3).cast<double>());
if (!M.mesh.UV.empty())
eigen2matlab("uv", MapMatX2f(M.mesh.UV.data(), M.nVertex, 2).cast<double>());
matlabEval( "writeOBJ('" +filename + "', struct('V', x, 'F', t+1, 'UV', uv));" );
//matlabEval( "writeOBJ('" +filename + "', struct('V', [x uv(:,2)], 'F', t+1, 'UV', uv));" );
}
void resetDeform()
{
Eigen::Matrix<float, Eigen::Dynamic, 2, Eigen::RowMajor> x;
matlab2eigen("single(fC2R(X))", x, true);
if (x.rows() == 0) return;
M.upload(x.data(), x.rows(), nullptr, M.nFace, nullptr);
if (deformer) deformer->resetDeform();
}
void createDeformer()
{
deformer.reset();
switch (deformerType)
{
case 0:
deformer.reset(new MatlabDeformer(M));
break;
default:
break;
}
}
void updatePosConstraints(int newConstraint)
{
if (deformer) deformer->updateP2PConstraints(newConstraint);
}
void deformMesh()
{
if (deformer) deformer->deform();
}
void loadP2PConstraints()
{
if (!getMatEngine().hasVar("P2PVtxIds"))
return;
Eigen::MatrixXi p2pidx;
matlab2eigen("int32(P2PVtxIds)-1", p2pidx, true);
Eigen::Matrix<float, Eigen::Dynamic, 2, Eigen::RowMajor> p2pdst;
matlab2eigen("single(fC2R(P2PCurrentPositions))", p2pdst, true);
M.setConstraintVertices(p2pidx.data(), p2pdst.data(), p2pidx.size());
// load anchors from matlab if it's there
if (getMatEngine().hasVar("interpAnchID"))
M.auxVtxIdxs=matlab2vector<int>("int32(interpAnchID)-1", true);
updatePosConstraints(1);
}
void loadData(std::string dataset)
{
double numMeshVertex = matlab2scalar("numMeshVertex", 1e4);
matlabEval("clear;");
scalar2matlab("numMeshVertex", numMeshVertex);
string2matlab("working_dataset", dataset);
matlabEval("deformer_main;"); ///////
dataset = matlab2string("working_dataset");
for (int i = 0; i < dataset_names.size(); i++) if (dataset_names[i] == dataset) { idataset = i; break; }
string2matlab("working_dataset", dataset);
datadir = matlab2string("datadir");
using namespace Eigen;
Matrix<float, Dynamic, 2, RowMajor> x, uv;
Matrix<int, Dynamic, 3, RowMajor> t;
matlabEval("assert(~isreal(X) && ~isreal(uv));");
matlab2eigen("single(fC2R(X))", x, true);
matlab2eigen("single(fC2R(uv))", uv, true);
matlab2eigen("int32(T)-1", t, true);
if (x.rows() == 0 || t.rows() == 0) return;
textureFile = matlab2string("imgfilepath");
M.tex.setImage(textureFile);
M.tex.setClamping(GL_CLAMP_TO_EDGE);
M.mTextureScale = 1.f;
if (getMatEngine().hasVar("textureScale"))
M.mTextureScale = float(matlab2scalar("textureScale"));
if (getMatEngine().hasVar("textureClampingFlag")) {
int i = matlab2scalar("textureClampingFlag");
if(i==1) M.tex.setClamping(GL_REPEAT);
else if(i==2) M.tex.setClamping(GL_MIRRORED_REPEAT);
}
M.showTexture = true;
M.edgeWidth = 0;
M.upload(x, t, uv.data());
M.updateBBox();
createDeformer();
loadP2PConstraints();
}
/////////////////////////////////////////////////////////////////////////
void dumpGLInfo(bool dumpExtensions=false)
{
GLint major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
printf("GL Vendor : %s\n", glGetString( GL_VENDOR ));
printf("GL Renderer : %s\n", glGetString( GL_RENDERER ));
printf("GL Version : %s\n", glGetString( GL_VERSION ));
printf("GL Version : %d.%d\n", major, minor);
printf("GLSL Version : %s\n", glGetString( GL_SHADING_LANGUAGE_VERSION ));
if( dumpExtensions ) {
GLint nExtensions;
glGetIntegerv(GL_NUM_EXTENSIONS, &nExtensions);
for( int i = 0; i < nExtensions; i++ ) {
printf("%s\n", glGetStringi(GL_EXTENSIONS, i));
}
}
}
int mousePressButton;
int mouseButtonDown;
int mousePos[2];
float fps = 0;
bool msaa = true;
void display()
{
static clock_t lastTime = 0;
static int numRenderedFrames = 0;
const int numFramesPerReport = 100;
if( (numRenderedFrames+1) % numFramesPerReport == 0 ) {
clock_t now = clock();
float dual = (now - lastTime) / float(CLOCKS_PER_SEC);
fps = numFramesPerReport / dual;
//printf("%d frames in %f sec (%f fps)\n", numFramesPerReport, dual, fps);
lastTime = now;
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
if (msaa) glEnable(GL_MULTISAMPLE);
else glDisable(GL_MULTISAMPLE);
glViewport(0, 0, viewport[2], viewport[3]);
M.draw(viewport);
if(showATB) TwDraw();
glutSwapBuffers();
//glFinish();
numRenderedFrames++;
}
void onKeyboard(unsigned char code, int x, int y)
{
if (!TwEventKeyboardGLUT(code, x, y)){
//if (code == 'q' && (GLUT_ACTIVE_CTRL & glutGetModifiers())){
switch (code){
case 17:
exit(0);
case 'f':
glutFullScreenToggle();
break;
case 'd':
for (int i = 0; i < numIterationPerDeform; i++){
deformMesh();
display();
}
break;
case ' ':
showATB = !showATB;
break;
}
}
glutPostRedisplay();
}
void onMouseButton(int button, int updown, int x, int y)
{
if (!showATB || !TwEventMouseButtonGLUT(button, updown, x, y)){
mousePressButton = button;
mouseButtonDown = updown;
if (updown == GLUT_DOWN){
if (button == GLUT_LEFT_BUTTON){
if (glutGetModifiers()&GLUT_ACTIVE_CTRL){
M.pick(x, y, viewport, actPrimType, M.PO_NONE);
if (M.actVertex >= 0){
if (M.auxVtxIdxs.size() > 1) M.auxVtxIdxs.erase(M.auxVtxIdxs.begin()+1, M.auxVtxIdxs.end());
M.auxVtxIdxs.insert(M.auxVtxIdxs.begin(), M.actVertex);
}
}
else{
int r = M.pick(x, y, viewport, M.PE_VERTEX, M.PO_ADD);
updatePosConstraints(r);
//if (continuousDeform) deformMesh(); // may waste computation
}
}
else if(button == GLUT_RIGHT_BUTTON) {
M.pick(x, y, viewport, M.PE_VERTEX, M.PO_REMOVE);
updatePosConstraints(-1); // one vertex removed
for (int i = 0; i < numIterationPerDeform; i++)
deformMesh();
}
}
else{ // updown == GLUT_UP
if (!continuousDeform && button == GLUT_LEFT_BUTTON)
updatePosConstraints(0);
}
mousePos[0] = x;
mousePos[1] = y;
}
glutPostRedisplay();
}
void onMouseMove(int x, int y)
{
if (!showATB || !TwEventMouseMotionGLUT(x, y)){
if (mouseButtonDown == GLUT_DOWN){
if (mousePressButton == GLUT_MIDDLE_BUTTON){
float ss = M.drawscale();
M.mTranslate[0] += (x - mousePos[0]) * 2 / ss / viewport[2];
M.mTranslate[1] -= (y - mousePos[1]) * 2 / ss / viewport[3];
}
else if(mousePressButton == GLUT_LEFT_BUTTON){
M.moveCurrentVertex(x, y, viewport);
if (continuousDeform){
updatePosConstraints(0);
deformMesh();
display();
}
}
}
}
mousePos[0] = x; mousePos[1] = y;
glutPostRedisplay();
}
void onMouseWheel(int wheel_number, int direction, int x, int y)
{
if (glutGetModifiers() & GLUT_ACTIVE_CTRL){
}
else
M.mMeshScale *= direction > 0 ? 1.1f : 0.9f;
glutPostRedisplay();
}
int initGL(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE);
glutInitWindowSize(960, 960);
glutInitWindowPosition( 200, 50 );
glutCreateWindow(argv[0]);
// !Load the OpenGL functions. after the opengl context has been created
if( ogl_LoadFunctions() == ogl_LOAD_FAILED )
return -1;
glClearColor(1.f, 1.f, 1.f, 0.f);
glutReshapeFunc([](int w, int h){ viewport[2] = w; viewport[3] = h; TwWindowSize(w, h);});
glutDisplayFunc(display);
glutKeyboardFunc(onKeyboard);
glutMouseFunc( onMouseButton );
glutMotionFunc( onMouseMove );
glutMouseWheelFunc( onMouseWheel );
glutCloseFunc([]() {exit(0); });
return 0;
}
void createTweakbar()
{
TwBar *bar = TwGetBarByName("MeshViewer");
if (bar) TwDeleteBar(bar);
//Create a tweak bar
bar = TwNewBar("MeshViewer");
TwDefine(" MeshViewer size='220 150' color='0 128 255' text=dark alpha=128 position='5 5' label='Mesh Viewer'"); // change default tweak bar size and color
TwAddVarRO(bar, "#Vertex", TW_TYPE_INT32, &M.nVertex, " group='Mesh View'");
TwAddVarCB(bar, "WireFrame", TW_TYPE_BOOLCPP,
[](const void *v, void *) { M.edgeWidth = *(bool*)(v); },
[](void *v, void *) { *(bool*)(v) = M.edgeWidth>0; },
nullptr, " group='Mesh View' ");
TwAddVarCB(bar, "Texture", TW_TYPE_BOOLCPP,
[](const void *v, void *) { M.showTexture = *(bool*)(v); if (M.showTexture){ M.tex.setImage( MyImage(textureFile) ); } },
[](void *v, void *) { *(bool*)(v) = M.showTexture; },
nullptr, " group='Mesh View' help='show texture on mesh' ");
//////////////////////////////////////////////////////////////////////////
TwType datasetTWType = TwDefineEnumFromString("Datasets", catStr(dataset_names).c_str());
TwAddVarCB(bar, "Shape", datasetTWType,
[](const void *v, void *d) {
idataset = *(int*)v;
if (idataset < dataset_names.size()) loadData(dataset_names[idataset]);
},
[](void *v, void *) { *(int*)v = idataset; },
nullptr, " ");
TwAddButton(bar, "Save Image", [](void *d) {
std::string imgfile = datadir + (deformer ? deformer->name() : meshName) + ".png";
M.saveResultImage(imgfile.c_str(), saveImageResolution); },
nullptr, " key=p ");
TwAddButton(bar, "Save data", [](void *d){ if (deformer) deformer->saveData(); },
nullptr, " key=S ");
}
int main(int argc, char *argv[])
{
if (initGL(argc, argv)){
fprintf(stderr, "!Failed to initialize OpenGL!Exit...");
exit(-1);
}
MyMesh::buildShaders();
//////////////////////////////////////////////////////////////////////////
TwInit(TW_OPENGL_CORE, NULL);
//Send 'glutGetModifers' function pointer to AntTweakBar;
//required because the GLUT key event functions do not report key modifiers states.
TwGLUTModifiersFunc(glutGetModifiers);
glutSpecialFunc([](int key, int x, int y){ TwEventSpecialGLUT(key, x, y); glutPostRedisplay(); }); // important for special keys like UP/DOWN/LEFT/RIGHT ...
TwCopyStdStringToClientFunc([](std::string& dst, const std::string& src){dst = src; });
//////////////////////////////////////////////////////////////////////////
atexit([]{ deformer.reset(); TwDeleteAllBars(); TwTerminate(); glutExit(); }); // Called after glutMainLoop ends
glutIdleFunc([]() {
if (deformer && deformer->needIteration && !deformer->converged()) { deformer->deform(); glutPostRedisplay(); }
else this_thread::sleep_for(50ms);
});
glutTimerFunc(1000, [](int) {
getMatEngine().connect("");
matlabEval("list_datasets;");
dataset_names = matlab2strings("datasets");
createTweakbar();
loadData("");
},
0);
//////////////////////////////////////////////////////////////////////////
glutMainLoop();
return 0;
}