-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cpp
More file actions
322 lines (303 loc) · 9.13 KB
/
sample.cpp
File metadata and controls
322 lines (303 loc) · 9.13 KB
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
#include "modelerview.h"
#include "modelerapp.h"
#include "modelerdraw.h"
#include "boids.h"
#include <FL/gl.h>
#include <string>
#include "modelerglobals.h"
// To make a SampleModel, we inherit off of ModelerView
class SampleModel : public ModelerView
{
public:
SampleModel(int x, int y, int w, int h, char *label)
: ModelerView(x,y,w,h,label)
{
m_framerate = 20;
}
virtual void draw();
std::string generateGrammar(int);
std::string generateAltGrammar(int);
private:
std::string m_rules;
std::string m_alt_rules;
int m_r_depth;
double m_framerate;
std::vector<Boid*> m_boids;
};
// We need to make a creator function, mostly because of
// nasty API stuff that we'd rather stay away from.
ModelerView* createSampleModel(int x, int y, int w, int h, char *label)
{
return new SampleModel(x,y,w,h,label);
}
/** @brief SampleModel::draw() - Overridden draw call; draw our sample model
*
**/
void SampleModel::draw()
{
// if low-fps mode is enabled
if(VAL(FRAMERATE))
{
//skip every VAL(FRAMERATE) frames
if (m_framerate != 0)
{
m_framerate--;
return;
}
else
m_framerate = 20;
}
// This call takes care of a lot of the nasty projection
// matrix stuff. Unless you want to fudge directly with the
// projection matrix, don't bother with this ...
ModelerView::draw();
// generate the grammar rules based on our recursion depth setting
// (only need to do this if the settings have changed)
int r_depth = int (VAL(R_DEPTH) + 0.5);
if(r_depth != m_r_depth)
{
m_rules = generateGrammar(r_depth);
m_alt_rules = generateAltGrammar(r_depth);
m_r_depth = r_depth; // remember setting for the next draw() call
}
// convert color from float to int
int leaf_color = int (VAL(L_COLOR) + 0.5);
int branch_color = int (VAL(B_COLOR) + 0.5);
int boid_color = int (VAL(BOID_COLOR) + 0.5);
// initialize our boids if we haven't already
if(m_boids.empty())
m_boids = initializeBoids(m_boids, 8);
// move & draw the boids
glPushMatrix();
setColor(boid_color);
moveBoids(m_boids);
drawBoids(m_boids);
setColor(branch_color);
glPopMatrix();
// draw the floor
setAmbientColor(.1f,.1f,.1f);
setDiffuseColor(COLOR_GHOST_WHITE);
glPushMatrix();
glTranslated(-5,0,-5);
drawBox(10,0.01f,10);
glPopMatrix();
// draw the plant model
glPushMatrix();
glTranslated(VAL(XPOS), VAL(YPOS), VAL(ZPOS));
glRotated(VAL(ROTATE), 0.0, 1.0, 0.0);
glRotated(-90, 1.0, 0.0, 0.0);
setColor(branch_color);
// we draw the plant model based on our grammar string
for(unsigned int i = 0; i < m_rules.length(); i++)
{
switch(m_rules[i])
{
// draw a branch with a leaf
case '0':
drawCylinder(VAL(HEIGHT), VAL(B_WIDTH), VAL(B_WIDTH));
glTranslated(0, 0, VAL(HEIGHT));
setColor(leaf_color);
drawSphere(VAL(L_SIZE));
setColor(branch_color);
break;
// draw a segment of the main 'stem'
case '1':
glRotated(VAL(S_ANGLE), 0.0, 1.0, 1.0);
drawCylinder(VAL(HEIGHT), VAL(B_WIDTH), VAL(B_WIDTH));
glTranslated(0, 0, VAL(HEIGHT));
break;
// push and rotate
case '[':
glPushMatrix();
if(VAL(STOCH) && (rand() % 2) == 1)
{
glRotated(-VAL(B_BEND_ANGLE), 0.0, .33, 0.0);
glRotated(-VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
else
{
glRotated(VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
glRotated(VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
break;
// pop and rotate the opposite direction
case ']':
glPopMatrix();
if(!VAL(SYMMETRY) && !VAL(STOCH))
glRotated(VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
if(VAL(STOCH) && (rand() % 2) == 1)
{
glRotated(VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
glRotated(VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
else
{
glRotated(-VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
glRotated(-VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
break;
default: break;
}
}
glPopMatrix();
// draw the other alternate plant as well, if that's enabled
if (VAL(ALT_PLANT))
{
glPushMatrix();
glTranslated(VAL(XPOS)+2.0, VAL(YPOS), VAL(ZPOS)+2.0);
glScaled(0.5,0.5,0.5);
glRotated(VAL(ROTATE), 0.0, 1.0, 0.0);
glRotated(-90, 1.0, 0.0, 0.0);
setColor(4);
// we draw the plant model based on our grammar string
for(unsigned int i = 0; i < m_alt_rules.length(); i++)
{
switch(m_alt_rules[i])
{
// draw a branch with a leaf
case '0':
drawCylinder(VAL(HEIGHT), VAL(B_WIDTH), VAL(B_WIDTH));
glTranslated(0, 0, VAL(HEIGHT));
setColor(5);
drawSphere(VAL(L_SIZE));
setColor(4);
break;
// draw a segment of the main 'stem'
case '1':
glRotated(VAL(S_ANGLE), 0.0, 1.0, 1.0);
drawCylinder(VAL(HEIGHT), VAL(B_WIDTH), VAL(B_WIDTH));
glTranslated(0, 0, VAL(HEIGHT));
break;
// push and rotate
case '[':
glPushMatrix();
if(VAL(STOCH) && (rand() % 2) == 1)
{
glRotated(-VAL(B_BEND_ANGLE), 0.0, .33, 0.0);
glRotated(-VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
else
{
glRotated(VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
glRotated(VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
break;
// pop and rotate the opposite direction
case ']':
glPopMatrix();
if(!VAL(SYMMETRY) && !VAL(STOCH))
glRotated(VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
if(VAL(STOCH) && (rand() % 2) == 1)
{
glRotated(VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
glRotated(VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
else
{
glRotated(-VAL(B_BEND_ANGLE), 0.0, 1.0, 0.0);
glRotated(-VAL(B_ANGLE), 1.0, 0.0, 1.0);
}
break;
default: break;
}
}
glPopMatrix();
}
}
/** @brief generateGrammar - Generate the grammar for our L-system
*
* @param int r_depth - the depth of recursion
* @return string output - the rules for generating our plant
*
**/
std::string SampleModel::generateGrammar(int r_depth)
{
// our 'seed' value
std::string axiom = "0";
// our instructions on how to draw the shape
std::string output = axiom;
for(int i = 0; i < r_depth; i++)
{
output.clear();
for(unsigned int j = 0; j < axiom.length(); j++)
{
switch(axiom[j])
{
case '0': output.append("1[0]1[0]0"); break;
case '1': output.append("11"); break;
case '[': output.append("["); break;
case ']': output.append("]"); break;
default: break;
}
}
axiom = output;
}
return output;
}
/** @brief generateAltGrammar - Generate the alt grammar for our L-system
*
* @param int r_depth - the depth of recursion
* @return string output - the rules for generating our plant
*
**/
std::string SampleModel::generateAltGrammar(int r_depth)
{
// our 'seed' value
std::string axiom = "0";
// our instructions on how to draw the shape
std::string output = axiom;
for(int i = 0; i < r_depth; i++)
{
output.clear();
for(unsigned int j = 0; j < axiom.length(); j++)
{
switch(axiom[j])
{
case '0': output.append("1[0]0"); break;
case '1': output.append("111[10]"); break;
case '[': output.append("["); break;
case ']': output.append("]"); break;
default: break;
}
}
axiom = output;
}
return output;
}
int main()
{
// Initialize the controls
// Constructor is ModelerControl(name, minimumvalue, maximumvalue,
// stepsize, defaultvalue)
ModelerControl controls[NUMCONTROLS];
// L-system controls
controls[XPOS] = ModelerControl("X Position", -5, 5, 0.1f, 0);
controls[YPOS] = ModelerControl("Y Position", 0, 5, 0.1f, 0);
controls[ZPOS] = ModelerControl("Z Position", -5, 5, 0.1f, 0);
controls[HEIGHT] = ModelerControl("Height", .05f, .5f, 0.01f, .2f);
controls[ROTATE] = ModelerControl("Rotate", -135, 135, 1, 0);
controls[R_DEPTH] = ModelerControl("Recursion Depth", 0, 5, 1, 4);
controls[B_ANGLE] = ModelerControl("Branch Angle", -100, 135, 0.1f, 45);
controls[B_BEND_ANGLE] = ModelerControl("Branch Bend Angle", -100, 135, 0.1f, -57);
controls[SYMMETRY] = ModelerControl("Branch Bend Symmetry", 0, 1, 1, 0);
controls[S_ANGLE] = ModelerControl("Stem Twist", -90, 90, 0.1f, 0);
controls[B_COLOR] = ModelerControl("Branch Color", 1, 16, 1, 9);
controls[L_COLOR] = ModelerControl("Leaf Color", 1, 16, 1, 12);
controls[B_WIDTH] = ModelerControl("Branch Width", .01f, 0.15f, .01f, .05f);
controls[L_SIZE] = ModelerControl("Leaf Size", .05f, 0.2f, 0.01f, 0.1f);
controls[STOCH] = ModelerControl("Stochastic", 0, 1, 1, 0);
// boids controls
controls[SHOW_DIR] = ModelerControl("View Boids Direction", 0, 1, 1, 0);
controls[PERCEPTION] = ModelerControl("Boids Perception Distance", .5f, 4.0f, .01f, 1.35f);
controls[FLOCK_D] = ModelerControl("Boids Min Distance", .2f, 3.5f, 0.1f, 0.6f);
controls[ADD_WIND] = ModelerControl("Enable Wind", 0, 1, 1, 0);
controls[CIRCLE_PLANT] = ModelerControl("Boids Circle Plant", 0, 1, 1, 0);
controls[BOID_COLOR] = ModelerControl("Boid Color", 1, 16, 1, 1);
controls[FLOCK_RANGE] = ModelerControl("Boid Boundaries", .5f, 5.0f, .1f, 4.5f);
controls[FLOCK_SPEED] = ModelerControl("Boid Velocity", .05f, .35f, .01f, .16f);
controls[CAN_PERCH] = ModelerControl("Enable Perching", 0, 1, 1, 0);
controls[FRAMERATE] = ModelerControl("Low-FPS Mode", 0, 1, 1, 0);
controls[ALT_PLANT] = ModelerControl("Generate Alt Plant", 0, 1, 1, 0);
ModelerApplication::Instance()->Init(&createSampleModel, controls, NUMCONTROLS);
return ModelerApplication::Instance()->Run();
}