-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cpp
386 lines (365 loc) · 11.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
#include "main.h" // this header
#include <stdlib.h> // standard library
#include <math.h> // fmod
#include <stdio.h> // cout
#include <iostream> // cout
#include <vector>
#include "HalfEdgeDSRendering.h" // visualizing the data structure
// ==============
// === BASICS ===
// ==============
int main(int argc, char** argv)
{
// initialize openGL window
glutInit(&argc, argv);
glutInitWindowPosition(300, 200);
glutInitWindowSize(600, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Praktikum GMCAD");
// link functions to certain openGL events
glutDisplayFunc(renderScene);
glutReshapeFunc(reshape);
glutMouseFunc(mousePressed);
glutMotionFunc(mouseMoved);
glutKeyboardFunc(keyPressed);
// further initializations
setDefaults();
initializeGL();
// data structure stuff
std::cout << heDS << std::endl << std::endl;
if (heDS.getHalfEdges().size() > 0) activeHE = heDS.getHalfEdges().front();
else activeHE = nullptr;
selectedHE = nullptr;
// activate main loop
glutMainLoop();
return 0;
}
void setDefaults()
{
// scene Information
transX = 0.0f;
transY = 0.0f;
transZ = -8.0f;
angleX = 0.0f;
angleY = 0.0f;
// mouse information
mouseX = 0;
mouseY = 0;
mouseButton = 0;
mouseSensitivy = 1.0f;
}
void initializeGL()
{
// black screen
glClearColor(0, 0, 0, 0);
// enable depth buffer
glEnable(GL_DEPTH_TEST);
// Use Point Smoothing
glPointSize(5.0f);
glLineWidth(2.0f);
// set shading model
glShadeModel(GL_SMOOTH);
// set lighting (white light)
GLfloat global_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat ambientLight[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat shininess = 0.9f * 128.0f;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightf(GL_LIGHT0, GL_SHININESS, shininess);
glEnable(GL_LIGHT0);
GLfloat lp[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lp);
// enable lighting by glColor
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
// key bindings => cout
coutHelp();
}
void reshape(GLint width, GLint height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float)width / (float)height, 0.1f, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// =================
// === RENDERING ===
// =================
void renderCS()
{
// x
glPushMatrix();
glColor3f(1, 0, 0);
renderArrow(Vec3f(0.0f, 0.0f, 0.0f), Vec3f(1.0f, 0.0f, 0.0f), 0.04f);
glPopMatrix();
// y
glPushMatrix();
glColor3f(0, 1, 0);
renderArrow(Vec3f(0.0f, 0.0f, 0.0f), Vec3f(0.0f, 1.0f, 0.0f), 0.04f);
glPopMatrix();
// z
glPushMatrix();
glColor3f(0, 0, 1);
renderArrow(Vec3f(0.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f, 1.0f), 0.04f);
glPopMatrix();
}
void renderScene()
{
// clear and set camera
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// lighting from the viewer perspective
GLfloat lp[] = { 0.0f, 0.0f, 0.5f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lp);
// translate scene in viewing direction
glTranslatef(transX, transY, transZ);
// rotate scene
glRotatef(angleX, 0.0f, 1.0f, 0.0f);
glRotatef(angleY, 1.0f, 0.0f, 0.0f);
glEnable(GL_LIGHTING);
renderDS(heDS);
if(selectedHE)
renderHE(selectedHE, Vec3f(0.004f, 0.337f, 0.141f), 0.027f);
renderHEActive(activeHE);
for(auto loopIterator = activeHE->nextHE;
loopIterator != activeHE;
loopIterator = loopIterator->nextHE)
{
renderHE(loopIterator, Vec3f(0.004f, 0.141f, 0.337f), 0.023);
}
renderCS();
// swap Buffers
glFlush();
glutSwapBuffers();
}
// =================
// === CALLBACKS ===
// =================
void keyPressed(unsigned char key, int x, int y)
{
switch (key)
{
// ESC => exit
case 27:
exit(0);
break;
// help file
case 'h':
case 'H':
coutHelp();
break;
// reset view
case 'r':
case 'R':
setDefaults();
glutPostRedisplay(); // use this whenever 3D data changed to redraw the scene
break;
//Highlight Navigation
case 'w':
case 'W': //cycle loops on current face
{
Loop* currentLoop = activeHE->toLoop;
Face* currentFace = currentLoop->toFace;
if (currentLoop == currentFace->outerLoop) { //jump to inner loops
if (currentFace->innerLoop) {
activeHE = currentFace->innerLoop->toHE;
}
else {
std::cout << "Current Face has no inner loops!" << std::endl;
}
}
else {
if (currentLoop->nextLoop == currentFace->innerLoop) { //cycled through all inner, jump to outer
activeHE = currentFace->outerLoop->toHE;
}
else {
activeHE = currentLoop->nextLoop->toHE;
}
}
break;
}
case 's':
case 'S': //rev cycle loops on current face
{
Loop* currentLoop = activeHE->toLoop;
Face* currentFace = currentLoop->toFace;
if (currentLoop == currentFace->outerLoop) { //jump to inner loops
if (currentFace->innerLoop) {
activeHE = currentFace->innerLoop->prevLoop->toHE;
}
else {
std::cout << "Current Face has no inner loops!" << std::endl;
}
}
else {
if (currentLoop->prevLoop == currentFace->innerLoop->prevLoop) { //cycled through all inner, jump to outer
activeHE = currentFace->outerLoop->toHE;
}
else {
activeHE = currentLoop->prevLoop->toHE;
}
}
break;
}
case 'd':
case 'D':
activeHE = activeHE->nextHE;
break;
case 'a':
case 'A':
activeHE = activeHE->prevHE;
break;
case 'q':
case 'Q':
activeHE = activeHE->getEdgeSibling();
break;
case 'e':
case 'E':
std::cout << "Euler-Poincare R = " << heDS.EulerPoincareRings() << std::endl;
break;
case '\r':
case '\n':
{
std::cout << "Enter EulerOp {MEV, MVE, MEL, KEMH}: " << std::flush;
std::string eulerop = "Torus";
//std::cin >> eulerop;
if (eulerop == "MEV") {
std::cout << "Enter coordinates for new Vertex [x y z]: ";
float x, y, z;
std::cin >> x;
std::cin >> y;
std::cin >> z;
heDS.MEV(activeHE->toLoop->toFace->toSolid, activeHE->toLoop, activeHE->startV,
nullptr, nullptr, Vec3f(x, y, z));
}
else if (eulerop == "MVE") {
std::cout << "Enter coordinates for new Vertex [x y z]: ";
float x, y, z;
std::cin >> x;
std::cin >> y;
std::cin >> z;
heDS.MVE(activeHE->toLoop->toFace->toSolid, activeHE->toEdge,
nullptr, nullptr, Vec3f(x, y, z));
}
else if (eulerop == "MEL") {
if (!selectedHE || selectedHE->startV == activeHE->startV) {
std::cout << "MEL requires two distinct Vertices to be chosen first!";
break;
}
Loop*commonLoop = heDS.getCommonLoop(activeHE->startV, selectedHE->startV);
if (!commonLoop) {
std::cout << "The selected vertices share no common loop!";
break;
}
heDS.MEL(activeHE->toLoop->toFace->toSolid, commonLoop, selectedHE->startV, activeHE->startV,
nullptr, nullptr, nullptr);
selectedHE = nullptr;
}
else if (eulerop == "KEMH") {
heDS.KEMH(activeHE->toLoop->toFace->toSolid, activeHE->toEdge,
activeHE->startV, nullptr);
}
else {
std::cout << "Unrecognized" << std::endl;
}
break;
}
case 'f':
case 'F':
selectedHE = activeHE;
break;
case 't':
case 'T': {
std::cout << "Enter coordinates for Center of Torus [x y z]: ";
float x, y, z, r, w;
int segC, segA;
std::cin >> x;
std::cin >> y;
std::cin >> z;
std::cout << "Enter torus radius: ";
std::cin >> r;
std::cout << "Enter torus width: ";
std::cin >> w;
std::cout << "Enter segment count [cross, along]: ";
std::cin >> segC;
std::cin >> segA;
heDS.createTorus(x,y,z,r,w,segC,segA);
activeHE = *heDS.getHalfEdges().rbegin();
break;
}
case 'z':
case 'Z': {
heDS.createTorus(0, 0, -1, 4, 1, 8, 16);
activeHE = *heDS.getHalfEdges().rbegin();
break;
}
}
glutPostRedisplay();
}
void mousePressed(int button, int state, int x, int y)
{
mouseButton = button;
mouseX = x;
mouseY = y;
}
void mouseMoved(int x, int y)
{
// rotate (cap angleY within [-85°, +85°])
if (mouseButton == GLUT_LEFT_BUTTON)
{
angleX = fmod(angleX + (x-mouseX) * mouseSensitivy, 360.0f);
angleY += (y-mouseY) * mouseSensitivy;
if (angleY > 85) angleY = 85;
if (angleY < -85) angleY = -85;
glutPostRedisplay();
}
// zoom (here translation in z)
if (mouseButton == GLUT_RIGHT_BUTTON)
{
transZ -= 0.2f * (y-mouseY) * mouseSensitivy;
glutPostRedisplay();
}
// translation in xy
if (mouseButton == GLUT_MIDDLE_BUTTON)
{
transX += 0.05f * (x-mouseX) * mouseSensitivy;
transY -= 0.05f * (y-mouseY) * mouseSensitivy;
glutPostRedisplay();
}
// update mouse for next relative movement
mouseX = x;
mouseY = y;
}
// ===============
// === VARIOUS ===
// ===============
void coutHelp()
{
std::cout << std::endl;
std::cout << "====== KEY BINDINGS ======" << std::endl;
std::cout << "ESC: exit" << std::endl;
std::cout << "H: show this (H)elp file" << std::endl;
std::cout << "R: (R)eset view" << std::endl;
std::cout << "Enter: Enter Eulerop parameters in terminal (see \"USER EULERS\" below)" << std::endl;
std::cout << "F: Select current HE as first parameter for Eulerop" << std::endl;
std::cout << "====== USER EULERS ======" << std::endl;
std::cout << "MEV: Select Vertex1 by navigating to a HE starting there, enter coordinates in terminal" << std::endl;
std::cout << "MVE: Select Edge to be split by navigating to a child HE, enter coordinates in terminal" << std::endl;
std::cout << "MEL: Select Vertex1 by navigating to a HE and selecting it (F), then activate a Vertex2-originating HE" << std::endl;
std::cout << "KEMH: Select Edge to be killed by navigating to the HE going FROM THE OUTSIDE to the new inner loop" << std::endl;
std::cout << "====== DS NAVIGATION =====" << std::endl;
std::cout << "D: Next half edge in current loop" << std::endl;
std::cout << "A: Prev half edge in current loop" << std::endl;
std::cout << "W: Next loop on current surface" << std::endl;
std::cout << "S: Prev loop on current surface" << std::endl;
std::cout << "Q: Switch to sibling half edge" << std::endl;
std::cout << "E: Calulate Euler-Poincare" << std::endl;
std::cout << "==========================" << std::endl;
std::cout << std::endl;
}