-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGlutFunctions.cpp
231 lines (208 loc) · 6.19 KB
/
GlutFunctions.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
//
// glutFunctions.cpp
// lsystems
//
// Created by Chris Hartman on 4/21/18.
//
#include "Cmd.h"
#include "Context.h"
#include "Lsystem.h"
#include <algorithm>
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <GLKit/GLKMatrix4.h>
#else
#include <GL/glut.h>
#endif
#include <sstream>
#include <vector>
void readtheconfigfile();
using std::endl;
using std::find_if;
using std::runtime_error;
using std::vector;
namespace {
int main_menu_id;
GLdouble tx = -0.5, ty = 0, sc = 1;
int level = 1;
vector<Lsystem> systems;
auto curfractal=systems.end();
double p1 = 0;
double thresh = 0.003;
const double THRESHMAX = 1.0;
const double THRESHMIN = .0001;
inline void setOrthographicMatrix(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
#ifdef __APPLE__
GLKMatrix4 newOrtho = GLKMatrix4MakeOrtho(float(left), float(right), float(bottom), float(top), -1.0f, 1.0f);
glLoadMatrixf(newOrtho.m);
#else
gluOrtho2D(left, right, bottom, top);
#endif
}
void display() {
Consttype vars;
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT);
vars["p1"] = p1;
Context context(vars,curfractal->getExpressions());
curfractal->evaluateExpressions(context);
auto drawStrategyPtr = drawStrategyFactory(curfractal->getDrawStrategyToken());
drawStrategyPtr->evaluateExpressions(context);
curfractal->draw(level,*drawStrategyPtr);
glutSwapBuffers();
while (auto jj = glGetError())
std::cerr << gluErrorString(jj) << endl;
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
setOrthographicMatrix(-0.5, 1.5, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLineWidth(2.0);
glColor3d(.8, .8, .5);
}
void change_window_title() {
std::ostringstream os1;
os1 << "L' Systems Fractal: " << curfractal->getname()
<< " Level: " << level;
glutSetWindowTitle(os1.str().c_str());
}
void adjust_level(int newlevel) {
level = newlevel;
change_window_title();
glutPostRedisplay();
}
void handle_frac_menu(int value) {
curfractal = systems.begin()+value;
adjust_level(1);
glutPostRedisplay();
}
void handle_main_menu(int value) {
if (value < 100)
handle_frac_menu(value);
else if (value == 103)
readtheconfigfile();
}
void keyboard(unsigned char dakey, int /*unused*/, int /*unused*/) {
const GLdouble SCALEAMOUNT = 1.4;
const double MOVESIZE = .01;
if (dakey >= '0' && dakey <= '9')
adjust_level(dakey - '0');
else
switch (dakey) {
case 27: //ESC
exit(0);
case '=':
glTranslated(-tx, -ty, 0);
glScaled(SCALEAMOUNT, SCALEAMOUNT, 1);
sc *= SCALEAMOUNT;
glTranslated(tx, ty, 0);
glutPostRedisplay();
break;
case '-':
glTranslated(-tx, -ty, 0);
glScaled(1.0 / SCALEAMOUNT, 1.0 / SCALEAMOUNT, 1);
sc /= SCALEAMOUNT;
glTranslated(tx, ty, 0);
glutPostRedisplay();
break;
case 'l':
glTranslated(MOVESIZE / sc, 0, 0);
tx += MOVESIZE / sc;
glutPostRedisplay();
break;
case 'j':
glTranslated(-MOVESIZE / sc, 0, 0);
tx -= MOVESIZE / sc;
glutPostRedisplay();
break;
case 'i':
glTranslated(0, MOVESIZE / sc, 0);
ty += MOVESIZE / sc;
glutPostRedisplay();
break;
case 'k':
glTranslated(0, -MOVESIZE / sc, 0);
ty -= MOVESIZE / sc;
glutPostRedisplay();
break;
case ',':
adjust_level(level == 0 ? 0 : level - 1);
break;
case '.':
adjust_level(level + 1);
break;
case 'u':
p1 += .005;
if (p1 > 1.)
p1 = 1.;
glutPostRedisplay();
break;
case 'y':
p1 -= .005;
if (p1 < 0.)
p1 = 0;
glutPostRedisplay();
break;
case '[':
thresh *= 1.5;
if (thresh > THRESHMAX)
thresh = THRESHMAX;
glutPostRedisplay();
break;
case ']':
thresh /= 1.5;
if (thresh < THRESHMIN)
thresh = THRESHMIN;
glutPostRedisplay();
break;
case '\\':
thresh = THRESHMIN;
adjust_level(200);
glutPostRedisplay();
break;
case 'f':
glutPostRedisplay(); //!!! Why?
}
}
void reshape(int w, int h) {
glViewport(0, 0, GLsizei(w), GLsizei(h));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
setOrthographicMatrix(-0.5, 1.5, -1.0 * h / w, 1.0 * h / w);
else
setOrthographicMatrix(-0.5 * w / h, 1.5 * w / h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
} // namespace
void readtheconfigfile() {
systems = readlsystemfile();
curfractal = find_if(systems.begin(),systems.end(),[](const auto &f){return f.isactive();});
if (curfractal==systems.end())
throw(runtime_error("No active fractals in config file"));
change_window_title();
}
void runGlut(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(801, 801);
glutInitWindowPosition(100, 100);
glutCreateWindow("L' Systems");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
main_menu_id = glutCreateMenu(handle_main_menu);
glutAddMenuEntry("Reread config file", 103);
for (auto ii = 0u; ii < systems.size(); ++ii)
if (systems[ii].isactive())
glutAddMenuEntry(systems[ii].getname().c_str(), int(ii));
glutAttachMenu(GLUT_RIGHT_BUTTON);
handle_frac_menu(0);
glutMainLoop();
}