-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainGUI.cpp
391 lines (309 loc) · 10.7 KB
/
MainGUI.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
#include<GL/glui.h>
#include<GL/glut.h>
#include "smfParser.h"
#include "wingedEdgeStructureParser.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
#define WIDTH 800
#define HEIGHT 600
#define FLAT_SHADED 1
#define SMOOTH_SHADED 2
#define EDGES_SHADED 3
#define WIREFRAME 4
#define ROTATION_ID 10
#define OPEN_ID 11
#define SAVE_ID 12
#define QUIT_ID 13
#define RESET_ID 14
float obj_pos[] = {0.0, 0.0, 0.0};
float sphere_rotate [16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
//live variables
int window;
int color=1;
GLfloat x=200, y=200;
int display_type=FLAT_SHADED;
int segments=10;
float scale = 1.0;
float rotationX = 0.0, rotationY = 0.0;
int lastx, lasty;
char file[1024];
GLUI_Listbox *listbox;
/* This sets up the viewport so that the x- and y-scales are proportional */
/* to the sides of the window. If you draw a square it will be displayed */
/* as a square, regardless of how the window is resized. */
void setupViewport(int w, int h) {
//glViewport(0, 0, w, h);
GLUI_Master.auto_set_viewport();
glMatrixMode(GL_PROJECTION); // sets the matrix mode to projection -- to modify the 'view'
glLoadIdentity(); // intialize matrix to identity matrix
glFrustum( -((float)w/(float)h)*0.08, ((float)w/(float)h)*0.08, -0.08, 0.08, .1, 15.0 );
}
//things to be done once
void init() {
glClearColor(0.7, 0.7, 1.0, 0.0); //background color
setupViewport(WIDTH, HEIGHT);
file[0]='\0';
}
//handle controls
void controlBlock( int control )
{
if (control == ROTATION_ID)
{
glutPostRedisplay();
}
if (control == OPEN_ID)
{
string output;
output = exec("zenity --file-selection --file-filter='SMF files (smf) | *.smf' --title=\"Select a SMF file\" 2>/dev/null");
obj_pos[0]=0.0; obj_pos[1]=0.0; obj_pos[2]=-2.0;
initialize(output);
glutPostRedisplay();
}
//will be saved in output.smf
if (control == SAVE_ID)
{
wingedEdgeParser();
exec("zenity --info --text \"Successfully saved in output.smf\" 2>/dev/null");
}
if (control == RESET_ID)
{
scale = 1.0;
obj_pos[0]=0.0; obj_pos[1]=0.0; obj_pos[2]=-2.0;
sphere_rotate[0] = 1; sphere_rotate[1] = 0; sphere_rotate[2] = 0; sphere_rotate[3] = 0; sphere_rotate[4] = 0;
sphere_rotate[5] = 1; sphere_rotate[6] = 0; sphere_rotate[7] = 0; sphere_rotate[8] = 0; sphere_rotate[9] = 0;
sphere_rotate[10] = 1; sphere_rotate[11] = 0; sphere_rotate[12] = 0; sphere_rotate[13] = 0; sphere_rotate[14] = 0;
sphere_rotate[15] = 1;
glutPostRedisplay();
}
}
//code to handle motion
void handleMotion(int x, int y )
{
rotationX += (float) (y - lasty);
rotationY += (float) (x - lastx);
lastx = x;
lasty = y;
glutPostRedisplay();
}
//this is the display function which gets rendered
void handleDisplay()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef(obj_pos[0], obj_pos[1], obj_pos[2]);
glScalef( scale, scale, scale );
glMultMatrixf(sphere_rotate);
glRotatef( rotationY, 0.0, 1.0, 0.0 );
glRotatef( rotationX, 1.0, 0.0, 0.0 );
glColor3f(1.0, 1.0, 0.0);
if(display_type==FLAT_SHADED || display_type==EDGES_SHADED)
{
w_edge* faceEdgeArray[3];
glShadeModel(GL_FLAT);
glEnable(GL_NORMALIZE);
glBegin(GL_TRIANGLES);
for( int i=1; i<=numFaces; i++)
{
face* value = faceMap[i];
findAllEdgesofFace(value, faceEdgeArray);
//using 1st edge for 2 vertices
w_edge* edge1 = faceEdgeArray[0];
map<string, glvector*>::iterator it3 = faceNormalMap.find(faceNormalHash(i, edge1->start));
if(it3 != faceNormalMap.end())
{
glvector* faceNormal = it3->second;
glNormal3f(faceNormal->x, faceNormal->y, faceNormal->z);
}
else
{
cout << "error in FLAT Shading display" << endl;
}
if(edge1->left == value)
{
glVertex3f(edge1->start->x, edge1->start->y, edge1->start->z);
glVertex3f(edge1->end->x, edge1->end->y, edge1->end->z);
}
else if(edge1->right == value)
{
glVertex3f(edge1->end->x, edge1->end->y, edge1->end->z);
glVertex3f(edge1->start->x, edge1->start->y, edge1->start->z);
}
//using 2nd edge for 3rd vertex
w_edge* edge2 = faceEdgeArray[1];
if(edge1->start == edge2->start || edge1->end == edge2->start)
{
glVertex3f(edge2->end->x, edge2->end->y, edge2->end->z);
}
else
{
glVertex3f(edge2->start->x, edge2->start->y, edge2->start->z);
}
}
glEnd();
}
if(display_type==SMOOTH_SHADED)
{
w_edge* faceEdgeArray[3];
glShadeModel(GL_SMOOTH);
glBegin(GL_TRIANGLES);
for( int i=1; i<=numFaces; i++)
{
face* value = faceMap[i];
glvector *normal1, *normal2, *normal3;
findAllEdgesofFace(value, faceEdgeArray);
//using 1st edge for 2 vertices
w_edge* edge1 = faceEdgeArray[0];
normal1 = findVertexNormal(edge1->start);
normal2 = findVertexNormal(edge1->end);
if(edge1->left == value)
{
if(normal1 != NULL)
glNormal3f(normal1->x, normal1->y, normal1->z);
glVertex3f(edge1->start->x, edge1->start->y, edge1->start->z);
if(normal2 != NULL)
glNormal3f(normal2->x, normal2->y, normal2->z);
glVertex3f(edge1->end->x, edge1->end->y, edge1->end->z);
}
else if(edge1->right == value)
{
if(normal2 != NULL)
glNormal3f(normal2->x, normal2->y, normal2->z);
glVertex3f(edge1->end->x, edge1->end->y, edge1->end->z);
if(normal1 != NULL)
glNormal3f(normal1->x, normal1->y, normal1->z);
glVertex3f(edge1->start->x, edge1->start->y, edge1->start->z);
}
//using 2nd edge for 3rd vertex
w_edge* edge2 = faceEdgeArray[1];
if(edge1->start == edge2->start || edge1->end == edge2->start)
{
normal3 = findVertexNormal(edge2->end);
if(normal3 != NULL)
glNormal3f(normal3->x, normal3->y, normal3->z);
glVertex3f(edge2->end->x, edge2->end->y, edge2->end->z);
}
else
{
normal3 = findVertexNormal(edge2->start);
if(normal3 != NULL)
glNormal3f(normal3->x, normal3->y, normal3->z);
glVertex3f(edge2->start->x, edge2->start->y, edge2->start->z);
}
}
glEnd();
}
if(display_type==EDGES_SHADED)
{
glPolygonOffset(1.0, 1.0);
glBegin(GL_LINES);
for( map<string, w_edge*>::const_iterator it = wingedEdgeMap.begin(); it != wingedEdgeMap.end(); ++it )
{
string key = it->first;
w_edge* value = it->second;
glVertex3f(value->start->x, value->start->y, value->start->z);
glVertex3f(value->end->x, value->end->y, value->end->z);
}
glEnd();
}
if(display_type==WIREFRAME)
{
glBegin(GL_LINES);
for( map<string, w_edge*>::const_iterator it = wingedEdgeMap.begin(); it != wingedEdgeMap.end(); ++it )
{
string key = it->first;
w_edge* value = it->second;
glVertex3f(value->start->x, value->start->y, value->start->z);
glVertex3f(value->end->x, value->end->y, value->end->z);
}
glEnd();
}
glutSwapBuffers();
}
//handling reshape when window resized
void handleReshape(int w, int h) {
setupViewport(w, h);
glutSetWindow(window);
glutPostRedisplay();
}
void handleIdle(void) {
if (glutGetWindow() != window)
glutSetWindow(window);
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //double buffering, RGB mode, need depth buffer
glutInitWindowSize(WIDTH,HEIGHT); //width, height of the window
glutInitWindowPosition(0, 0); // location on the screen the window appears
window = glutCreateWindow("Assignment 1 GUI"); // creates the window
init(); //things to do once, mostly in beginning
glutDisplayFunc(handleDisplay); // tells glut which function to call to render a screen.
glutMotionFunc(handleMotion); // handle when motion (this generally means when mouse is moved with a button pressed)
//glutIdleFunc(handleIdle); // handle when no events are occuring
//glutReshapeFunc(handleReshape); // handle when reshape
//glutMouseFunc(handleMouse); // handle when mouse moved without any button pressed
//glutKeyboardFunc(handleKeyboard); // handle when keyboard key pressed
//GLUI_Master.set_glutKeyboardFunc( myGlutKeyboard );
//GLUI_Master.set_glutSpecialFunc( myGlutSpecial );
//GLUI_Master.set_glutMotionFunc( handleMotion );
GLUI_Master.set_glutReshapeFunc( handleReshape );
//Setting opengl lights
GLfloat light0_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
GLfloat light0_diffuse[] = {.6f, .6f, 1.0f, 1.0f};
GLfloat light0_position[] = {1.0f, 1.0f, 1.0f, 0.0f};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glEnable(GL_DEPTH_TEST); // enabling depth
GLUI *glui = GLUI_Master.create_glui_subwindow(window,
GLUI_SUBWINDOW_RIGHT);
new GLUI_StaticText( glui, "Assignment 1 GLUI" );
new GLUI_Separator(glui);
glui->set_main_gfx_window( window );
GLUI_Panel *obj_panel = glui->add_panel ("Display option panel");
listbox = new GLUI_Listbox(obj_panel, "Mesh display option", &display_type, 1, controlBlock);
listbox->add_item(FLAT_SHADED, "Flat Shaded");
listbox->add_item(SMOOTH_SHADED, "Smooth Shaded");
listbox->add_item(WIREFRAME, "Wireframe");
listbox->add_item(EDGES_SHADED, "Shaded with mesh edges");
//scale code
GLUI_Spinner *scale_spinner = new GLUI_Spinner( obj_panel, "Scale:", &scale);
scale_spinner->set_float_limits( .2f, 5.0 );
scale_spinner->set_alignment( GLUI_ALIGN_RIGHT );
//rotation code
GLUI_Panel * rotation_panel = glui->add_panel_to_panel(obj_panel, "", GLUI_PANEL_NONE);
GLUI_Rotation *sph_rot =
glui->add_rotation_to_panel(rotation_panel,"Rotate", sphere_rotate,
ROTATION_ID, controlBlock);
sph_rot->set_spin(1.0);
//translation code
GLUI_Panel * translate_panel = glui->add_panel_to_panel(obj_panel, "",
GLUI_PANEL_NONE);
GLUI_Translation * move_z =
glui->add_translation_to_panel(translate_panel,"Object Z",
GLUI_TRANSLATION_Z, &obj_pos[2]);
move_z->scale_factor = 0.1f;
glui->add_column_to_panel(translate_panel, true);
GLUI_Translation * move_around =
glui->add_translation_to_panel(translate_panel,"Object XY",
GLUI_TRANSLATION_XY, obj_pos);
move_around->scale_factor = 0.1f;
//other details
glui->add_separator();
glui->add_button("Open", OPEN_ID, controlBlock);
glui->add_button("Save", SAVE_ID, controlBlock);
glui->add_button("Reset", RESET_ID, controlBlock);
glui->add_button("Quit", QUIT_ID, (GLUI_Update_CB)exit);
/* register idle callback with GLUI, NOT with GLUT */
GLUI_Master.set_glutIdleFunc( handleIdle ); // handle when no events are occuring
glutMainLoop(); // once this is called, glut takes over --
// returns only when the window is closed
return EXIT_SUCCESS;
}