forked from ibeling/shapeedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapeedit.cpp
490 lines (399 loc) · 15.5 KB
/
shapeedit.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
////////////////////////////////////////////////////////////////////////
//
// Harvard Computer Science
// CS 277: Geometric Modeling
// Professor Steven Gortler
//
////////////////////////////////////////////////////////////////////////
#include <vector>
#include <string>
#include <memory>
#include <stdexcept>
#if __GNUG__
# include <tr1/memory>
#endif
#ifdef __MAC__
# include <OpenGL/gl3.h>
# include <GLUT/glut.h>
#else
#include <GL/glew.h>
# include <GL/glut.h>
#endif
#include "ppm.h"
#include "glsupport.h"
#include "mesh.h"
#include "poisson.h"
using namespace std; // for string, vector, iostream and other standard C++ stuff
using namespace std::tr1; // for shared_ptr
// G L O B A L S ///////////////////////////////////////////////////
// !!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!
// Before you start working on this assignment, set the following variable properly
// to indicate whether you want to use OpenGL 2.x with GLSL 1.0 or OpenGL 3.x+ with
// GLSL 1.3.
//
// Set g_Gl2Compatible = true to use GLSL 1.0 and g_Gl2Compatible = false to use GLSL 1.3.
// Make sure that your machine supports the version of GLSL you are using.
// Most of people can use GL 3.
//
// If g_Gl2Compatible=true, shaders with -gl2 suffix will be loaded.
// If g_Gl2Compatible=false, shaders with -gl3 suffix will be loaded.
// To complete the assignment you only need to edit the shader files that get loaded
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
static const bool g_Gl2Compatible = false;
static int g_width = 490; // screen width
static int g_height = 490; // screen height
static bool g_leftClicked = false; // is the left mouse button down?
static bool g_rightClicked = false; // is the right mouse button down?
static float g_objScale = 1.0; // scale factor for object
static int g_leftClickX, g_leftClickY; // coordinates for mouse left click event
static int g_rightClickX, g_rightClickY; // coordinates for mouse right click event
extern int g_iterationsPerSecond;
// our global shader states
struct SquareShaderState {
GlProgram program;
// Handles to uniform variables
GLint h_uTex;
GLint h_uSolid;
GLfloat h_uOffsetX;
GLfloat h_uOffsetY;
// Handles to vertex attributes
GLint h_aPosition;
GLint h_aTexCoord;
};
static shared_ptr<GlArrayObject> g_vao;
static shared_ptr<SquareShaderState> g_squareShaderState;
// our global texture instance
static shared_ptr<GlTexture> g_tex;
// our global geometries
struct GeometryPX {
GlArrayObject vao;
GlBufferObject posVbo, texVbo;
};
static shared_ptr<GeometryPX> g_geometry;
static shared_ptr<GeometryPX> g_handleGeometry;
// a handle is a pair of an index to a vertex of the mesh and a position
static vector<handleType> g_handles;
Mesh g_originalMesh;
static Mesh g_currentMesh, g_squareMesh;
static bool g_wireframeMode;
static vector<handleType>::iterator g_clickedHandle;
static bool g_updateScheduled = false;
// C A L L B A C K S ///////////////////////////////////////////////////
static void drawSquare(const SquareShaderState& shaderState, const GeometryPX& geometry, const GlTexture& tex, bool handle, Cvec2 offset) {
// using a VAO is necessary to run on OS X.
glBindVertexArray(geometry.vao);
// bind textures
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
// set glsl uniform variables
safe_glUniform1i(shaderState.h_uTex, 0); // 0 means GL_TEXTURE0
safe_glUniform1i(shaderState.h_uSolid, 0);
if (g_wireframeMode || handle)
safe_glUniform1i(shaderState.h_uSolid, 1);
safe_glUniform1f(shaderState.h_uOffsetX, (GLfloat)(offset[0]));
safe_glUniform1f(shaderState.h_uOffsetY, (GLfloat)(offset[1]));
// bind vertex buffers
glBindBuffer(GL_ARRAY_BUFFER, geometry.posVbo);
safe_glVertexAttribPointer(shaderState.h_aPosition,
2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, geometry.texVbo);
safe_glVertexAttribPointer(shaderState.h_aTexCoord,
2, GL_FLOAT, GL_FALSE, 0, 0);
safe_glEnableVertexAttribArray(shaderState.h_aPosition);
safe_glEnableVertexAttribArray(shaderState.h_aTexCoord);
if (g_wireframeMode && !handle)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE );
glDrawArrays(GL_TRIANGLES, 0, 3*g_currentMesh.getNumFaces());
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
safe_glDisableVertexAttribArray(shaderState.h_aPosition);
safe_glDisableVertexAttribArray(shaderState.h_aTexCoord);
// bind VAO to default
glBindVertexArray(0);
// check for errors
checkGlErrors();
}
static void drawHandles() {
for (vector<handleType>::iterator it = g_handles.begin(); it != g_handles.end(); ++it) {
drawSquare(*g_squareShaderState, *g_handleGeometry, *g_tex, true, it->second);
}
}
static pair<vector<handleType>::iterator, double> findClosestPoint(const int& xclick, const int& yclick, vector<handleType>& searchDomain) {
Cvec2 point = Cvec2(((double) xclick) / g_width, ((double) yclick) / g_height);
point = point * 2 - Cvec2(1.0, 1.0);
double min_dist = 1.0/CS175_EPS2;
vector<handleType>::iterator argmin;
for (vector<handleType>::iterator it = searchDomain.begin(); it != searchDomain.end(); ++it) {
const double dist = norm2(point - it->second);
if (dist < min_dist) {
min_dist = dist;
argmin = it;
}
}
return make_pair(argmin,min_dist);
}
static void addClosestVertexOrDeleteHandle(const int& xclick, const int& yclick) {
vector<pair<int, Cvec2> > vertices;
for (int i = 0; i < g_currentMesh.getNumVertices(); ++i)
vertices.push_back(make_pair(i, Cvec2(g_currentMesh.getVertex(i).getPosition())));
pair<vector<handleType>::iterator, double> closestVertex = findClosestPoint(xclick, yclick, vertices);
pair<vector<handleType>::iterator, double> closestHandle = findClosestPoint(xclick, yclick, g_handles);
if (g_handles.empty() || closestVertex.second < closestHandle.second)
g_handles.push_back(*(closestVertex.first));
else
g_handles.erase(closestHandle.first);
}
static void evolveCallback(int whatever) {
if (g_updateScheduled = !doIteration(g_currentMesh, g_handles))
glutTimerFunc(1000 / g_iterationsPerSecond, evolveCallback, 0);
glutPostRedisplay();
}
static void moveClosestHandle(const int& prevClickX, const int& prevClickY, const int& newClickX, const int& newClickY) {
if (g_handles.empty()) return;
Cvec2 displacement = Cvec2(2.0*((double)(newClickX - prevClickX)) / g_width, 2.0*((double)(newClickY - prevClickY)) / g_height);
g_clickedHandle->second += displacement;
afterMove(g_currentMesh, g_handles);
if (!g_updateScheduled) evolveCallback(0);
}
static void loadMeshGeometry(Mesh& m, GeometryPX& g) {
vector<GLfloat> pos, tex;
for (int i = 0; i < m.getNumFaces(); ++i) {
const Mesh::Face f = m.getFace(i);
for (int j = 0; j < f.getNumVertices(); ++j) {
const Mesh::Vertex v = f.getVertex(j);
pos.push_back((GLfloat)(v.getPosition()[0]));
pos.push_back((GLfloat)(v.getPosition()[1]));
tex.push_back((GLfloat)v.getTexCoords()[0]);
tex.push_back((GLfloat)v.getTexCoords()[1]);
}
}
const unsigned int size = pos.size() * sizeof(GLfloat);
glBindBuffer(GL_ARRAY_BUFFER, g.posVbo);
glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, size, &pos[0]);
checkGlErrors();
glBindBuffer(GL_ARRAY_BUFFER, g.texVbo);
glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, size, &tex[0]);
checkGlErrors();
}
// _____________________________________________________
//| |
//| display |
//|_____________________________________________________|
///
/// Whenever OpenGL requires a screen refresh
/// it will call display() to draw the scene.
/// We specify that this is the correct function
/// to call with the glutDisplayFunc() function
/// during initialization
static void display(void) {
glUseProgram(g_squareShaderState->program);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
loadMeshGeometry(g_currentMesh, *g_geometry);
drawSquare(*g_squareShaderState, *g_geometry, *g_tex, false, Cvec2());
drawHandles();
glutSwapBuffers();
// check for errors
checkGlErrors();
}
// _____________________________________________________
//| |
//| reshape |
//|_____________________________________________________|
///
/// Whenever a window is resized, a "resize" event is
/// generated and glut is told to call this reshape
/// callback function to handle it appropriately.
static void reshape(int w, int h) {
g_width = w;
g_height = h;
glViewport(0, 0, w, h);
glutPostRedisplay();
}
// _____________________________________________________
//| |
//| mouse |
//|_____________________________________________________|
///
/// Whenever a mouse button is clicked, a "mouse" event
/// is generated and this mouse callback function is
/// called to handle the user input.
static void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
// left mouse button has been clicked
g_leftClicked = true;
g_leftClickX = x;
g_leftClickY = g_height - y - 1;
Cvec2 point = Cvec2(((double)g_leftClickX) / g_width, ((double)g_leftClickY) / g_height);
point = point * 2 - Cvec2(1.0, 1.0);
g_clickedHandle = (findClosestPoint(g_leftClickX, g_leftClickY, g_handles)).first;
}
else {
// left mouse button has been released
g_leftClicked = false;
}
}
if (button == GLUT_RIGHT_BUTTON) {
if (state == GLUT_DOWN) {
// right mouse button has been clicked
g_rightClicked = true;
g_rightClickX = x;
g_rightClickY = g_height - y - 1;
addClosestVertexOrDeleteHandle(g_rightClickX, g_rightClickY);
}
else {
// right mouse button has been released
g_rightClicked = false;
}
}
glutPostRedisplay();
}
// _____________________________________________________
//| |
//| motion |
//|_____________________________________________________|
///
/// Whenever the mouse is moved while a button is pressed,
/// a "mouse move" event is triggered and this callback is
/// called to handle the event.
static void motion(int x, int y) {
const int newx = x;
const int newy = g_height - y - 1;
if (g_leftClicked) {
moveClosestHandle(g_leftClickX, g_leftClickY, newx, newy);
g_leftClickX = newx;
g_leftClickY = newy;
}
glutPostRedisplay();
}
static void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'h':
cout << " ============== H E L P ==============\n\n"
<< "h\t\thelp menu\n"
<< "s\t\tsave screenshot\n"
<< "w\t\ttoggle wireframe mode\n"
<< "\t\tright click to add or remove handles\n"
<< "\t\tleft click drag to move handles\n" << endl;
break;
case 'q':
exit(0);
case 's':
glFinish();
writePpmScreenshot(g_width, g_height, "out.ppm");
break;
case 'w':
g_wireframeMode = !g_wireframeMode;
}
glutPostRedisplay();
}
// H E L P E R F U N C T I O N S ////////////////////////////////////
static void initGlutState(int argc, char **argv) {
glutInit(&argc,argv);
#ifdef __MAC__
glutInitDisplayMode(GLUT_3_2_CORE_PROFILE | GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // core profile flag is required for GL 3.2 on Mac
#else
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // RGBA pixel channels and double buffering
#endif
glutInitWindowSize(g_width, g_height); // create a window
glutCreateWindow("CS 175: Hello World"); // title the window
glutDisplayFunc(display); // display rendering callback
glutReshapeFunc(reshape); // window reshape callback
glutMotionFunc(motion); // mouse movement callback
glutMouseFunc(mouse); // mouse click callback
glutKeyboardFunc(keyboard);
}
static void initGLState() {
glClearColor(255./255,255./255,1,0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
if (!g_Gl2Compatible)
glEnable(GL_FRAMEBUFFER_SRGB);
}
static void loadSquareShader(SquareShaderState& ss) {
const GLuint h = ss.program; // short hand
if (!g_Gl2Compatible) {
readAndCompileShader(ss.program, "shaders/sq-tex-gl3.vshader", "shaders/sq-tex-gl3.fshader");
}
else {
readAndCompileShader(ss.program, "shaders/sq-tex-gl2.vshader", "shaders/sq-tex-gl2.fshader");
}
// Retrieve handles to uniform variables
ss.h_uTex = safe_glGetUniformLocation(h, "uTex");
ss.h_uSolid = safe_glGetUniformLocation(h, "uSolid");
ss.h_uOffsetX = safe_glGetUniformLocation(h, "uOffsetX");
ss.h_uOffsetY = safe_glGetUniformLocation(h, "uOffsetY");
// Retrieve handles to vertex attributes
ss.h_aPosition = safe_glGetAttribLocation(h, "aPosition");
ss.h_aTexCoord = safe_glGetAttribLocation(h, "aTexCoord");
if (!g_Gl2Compatible)
glBindFragDataLocation(h, 0, "fragColor");
checkGlErrors();
}
static void initShaders() {
g_squareShaderState.reset(new SquareShaderState);
g_vao.reset(new GlArrayObject());
loadSquareShader(*g_squareShaderState);
}
static void initMeshAndGeometry() {
g_squareMesh.load("handle.mesh");
g_originalMesh.load("yummy.mesh");
g_currentMesh = g_originalMesh;
g_geometry.reset(new GeometryPX());
loadMeshGeometry(g_currentMesh, *g_geometry);
g_handleGeometry.reset(new GeometryPX());
loadMeshGeometry(g_squareMesh, *g_handleGeometry);
}
static void loadTexture(GLuint texHandle, const char *ppmFilename) {
int texWidth, texHeight;
vector<PackedPixel> pixData;
ppmRead(ppmFilename, texWidth, texHeight, pixData);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texHandle);
glTexImage2D(GL_TEXTURE_2D, 0, g_Gl2Compatible ? GL_RGB : GL_SRGB, texWidth, texHeight,
0, GL_RGB, GL_UNSIGNED_BYTE, &pixData[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
checkGlErrors();
}
static void initTextures() {
g_tex.reset(new GlTexture());
loadTexture(*g_tex, "yummy.ppm");
}
// M A I N /////////////////////////////////////////////////////////////
// _____________________________________________________
//| |
//| main |
//|_____________________________________________________|
///
/// The main entry-point for the HelloWorld example
/// application.
int main(int argc, char **argv) {
try {
initGlutState(argc,argv);
#ifndef __MAC__
glewInit(); // load the OpenGL extensions
#endif
cout << (g_Gl2Compatible ? "Will use OpenGL 2.x / GLSL 1.0" : "Will use OpenGL 3.x / GLSL 1.3") << endl;
#ifndef __MAC__
if ((!g_Gl2Compatible) && !GLEW_VERSION_3_0)
throw runtime_error("Error: card/driver does not support OpenGL Shading Language v1.3");
else if (g_Gl2Compatible && !GLEW_VERSION_2_0)
throw runtime_error("Error: card/driver does not support OpenGL Shading Language v1.0");
#endif
initGLState();
initShaders();
initMeshAndGeometry();
initTextures();
initPoisson(g_currentMesh);
glutMainLoop();
return 0;
}
catch (const runtime_error& e) {
cout << "Exception caught: " << e.what() << endl;
return -1;
}
}