-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainWindow.cpp
346 lines (285 loc) · 8.5 KB
/
mainWindow.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
#include "common.h"
#include "mainWindow.h"
#include "expression.h"
#include "fractal.h"
#include "fbo.h"
char* name;
int windowID;
float window_aspect;
bool fullScreen=false;
// GLUT Utility Variables
float zoom = 1;
float mouse_x, mouse_y;
float arcmouse_x, arcmouse_y, arcmouse_z;
bool right_mouse_button= false;
bool left_mouse_button = false;
GLfloat rot_matrix[16] = {1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
Queue<CliffordAttractor> fractals;
CliffordAttractor *mainFractal;
Color clearColor = kBlack;
bool grayscale = false;
int createMainWindow(string _name, bool saving) {
window_width = glutGet(GLUT_SCREEN_WIDTH);
window_height = glutGet(GLUT_SCREEN_HEIGHT);
window_aspect = window_width / static_cast<float>(window_height);
// int doubleBuffer = (saving ? GLUT_SINGLE : GLUT_DOUBLE);
int doubleBuffer = GLUT_DOUBLE;
glutInitDisplayMode(doubleBuffer | GLUT_RGBA);
GLsizei windowX = ( saving ? 1 : (glutGet(GLUT_SCREEN_WIDTH)-window_width)/2 );
GLsizei windowY = ( saving ? 1 : (glutGet(GLUT_SCREEN_HEIGHT)-window_height)/2 );
glutInitWindowPosition(windowX, windowY);
glutInitWindowSize(window_width, window_height);
windowID = glutCreateWindow(_name.c_str());
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
// Enable Blending for transparency
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_DEPTH_TEST);
ExternalRenderer::setImageWidth(window_width);
ExternalRenderer::setImageHeight(window_height);
return windowID;
}
/*
Adjust the camera to fit Fractal f.
*/
void adjustBounds(AttractorFractal& f) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, window_width/window_height, 1, 1500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
BoundingBox bbox = f.getbb();
float x, y, z, r, fdistance;
x = (bbox.max[0] + bbox.min[0]) / 2.0f;
y = (bbox.max[1] + bbox.min[1]) / 2.0f;
z = (bbox.max[2] + bbox.min[2]) / 2.0f;
r = sqrt((bbox.max[0] - x)*(bbox.max[0] - x) + (bbox.max[1] - y)*(bbox.max[1] - y) + (bbox.max[2] - z)*(bbox.max[2] - z));
fdistance = r / .3697f;
gluLookAt(0.0f, 0.0f, fdistance*zoom, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(-x, -y, -z);
glMultMatrixf(rot_matrix);
}
//****************************************
void drawLoader() {
static float angle = 1.0;
static double framedelay = 1000.0 / 60.0;
static double lastTime = 0;
double currTime = glutGet(GLUT_ELAPSED_TIME);
if (currTime - lastTime >= framedelay) {
lastTime = currTime;
angle+=5;
}
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
GLfloat position[] = { -15, -15, 30, 1 };
glLightfv(GL_LIGHT0, GL_POSITION, position);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glColor4f(1,1,1,fadeAlpha);
glLoadIdentity();
gluLookAt(0,-2,0,0,0,0,1,0,0);
glRotatef(angle,1,0,0);
glTranslatef(0.2,0,0);
for (float Xangle = 0; Xangle < 360; Xangle += 30) {
glTranslatef(-0.2,0,0);
glRotatef(Xangle,0,1,0);
glTranslatef(0.2,0,0);
glutSolidCube(0.05);
glTranslatef(-0.2,0,0);
glRotatef(90,0,0,1);
glTranslatef(0.2,0,0);
glutSolidCube(0.05);
}
}
void Repaint() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (!waiting) {
fadeAlpha -= fadeSpeed;
if (fadeAlpha <= 0) {
glShadeModel(GL_FLAT);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_COLOR_MATERIAL);
}
adjustBounds(*mainFractal);
if (mainFractal->paint()) {
if (rendering) {
TwDraw();
} else {
return;
}
} else {
waiting = true;
}
}
if (waiting) {
fadeAlpha = 1.0;
}
if (rendering && fadeAlpha > 0) {
drawLoader();
}
glFlush();
glutSwapBuffers();
}
void Idle() {
if (waiting) {
glutPostRedisplay();
} else if (fadeAlpha > 0) {
fadeAlpha -= fadeSpeed;
glutPostRedisplay();
}
}
//****************************************
/*
Update viewport in response to change in window size
*/
void Reshape(int w, int h){
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
if (rendering) TwWindowSize(w, h);
glutPostRedisplay();
}
void resize() {
glutReshapeWindow(window_width,window_height); // Default window size
GLsizei windowX = (glutGet(GLUT_SCREEN_WIDTH)-window_width)/2;
GLsizei windowY = (glutGet(GLUT_SCREEN_HEIGHT)-window_height)/2;
glutPositionWindow(windowX, windowY); // Center window
}
void resize(int w, int h) {
window_width = w;
window_height = h;
ExternalRenderer::setImageWidth(window_width);
ExternalRenderer::setImageHeight(window_height);
resize();
Reshape(w, h);
}
//****************************************
/*
Return a vector from fractal center to world coordinates of mouse
*/
Vec3f arcSnap(float x, float y) {
x = (2.0*x / window_width) - 1;
y = (2.0*y / window_height) - 1;
float mag2 = x * x + y * y;
float mag = sqrt(mag2);
if (mag > 1) {
x = x*0.999 / mag; // mult by .999 to account for edge cases of rounding up
y = y*0.999 / mag;
}
float z = sqrt(1.0 - (x*x + y*y));
return Vec3f::makeVec(x, y, z);
}
//****************************************
void MouseButton(int button, int state, int x, int y) {
if (rendering) {
if (!TwEventMouseButtonGLUT(button, state, x, y)) {
y = window_height - y;
if (button == GLUT_LEFT_BUTTON) {
Vec3f arc_coords = arcSnap(x, y);
arcmouse_x = arc_coords[0];
arcmouse_y = arc_coords[1];
arcmouse_z = arc_coords[2];
left_mouse_button = !state; // state==0 if down
}
if (button == GLUT_RIGHT_BUTTON) {
right_mouse_button = !state; // state==0 if down
}
mouse_x = x, mouse_y = y;
}
glutPostRedisplay();
}
}
//****************************************
void MouseMotion(int x, int y) {
if (rendering) {
if (!TwEventMouseMotionGLUT(x, y)) {
y = window_height - y;
if (left_mouse_button) {
// Arc-ball rotational movement
Vec3f arc_coords = arcSnap(x, y);
float fx = arc_coords[0];
float fy = arc_coords[1];
float fz = arc_coords[2];
// Find rotational axis
float normal_x = arcmouse_y*fz - arcmouse_z*fy;
float normal_y = arcmouse_z*fx - arcmouse_x*fz;
float normal_z = arcmouse_x*fy - arcmouse_y*fx;
// Find rotational angle
float ax = sqrt(normal_x*normal_x +
normal_y*normal_y +
normal_z*normal_z);
float ay = arcmouse_x*fx + arcmouse_y*fy + arcmouse_z*fz;
float angle = atan2(ax, ay)*180/3.14159;
// Modify and save rotation matrix
glLoadIdentity();
glRotatef(angle, normal_x, normal_y, normal_z);
glMultMatrixf(rot_matrix);
glGetFloatv(GL_MODELVIEW_MATRIX, rot_matrix);
arcmouse_x = fx, arcmouse_y = fy, arcmouse_z = fz;
} else if (right_mouse_button && y && mouse_y) {
// Zoom: Multiplies current zoom by ratio between initial and current y
float smy = mouse_y+window_height;
float sy = y+window_height;
float dy;
if (sy < 0 && smy < 0) {
dy = abs(smy/sy);
} else {
dy = abs(sy/smy);
}
zoom *= dy;
}
mouse_x = x, mouse_y = y;
}
glutPostRedisplay();
}
}
void Keyboard(unsigned char key, int x, int y){
if (rendering) {
if (!TwEventKeyboardGLUT(key, x, y)) {
switch(key){
case 32: // (Spacebar) Mutate fractal
mainFractal->mutateConstants();
zoom = 1;
glutPostRedisplay();
break;
case 'F': // Toggle fullscreen
case 'f':
if(fullScreen){
resize();
fullScreen = false;
}
else{
fullScreen = true;
glutFullScreen();
}
glutPostRedisplay();
break;
case 's': // Save to test.png
ExternalRenderer::outputToImage("test");
break;
case 27 : // (ESC) close the program
glutDestroyWindow(windowID);
exit(0);
break;
}
}
glutPostRedisplay();
}
}
void registerCallbacks() {
// set the event handling methods
TwGLUTModifiersFunc(glutGetModifiers);
glutDisplayFunc(Repaint);
glutReshapeFunc(Reshape);
glutMouseFunc(MouseButton);
glutMotionFunc(MouseMotion);
glutKeyboardFunc(Keyboard);
glutIdleFunc(Idle);
glutFullScreen();
fullScreen=true;
}