1 // Copyright (C) 2006 Erik Dahlberg
3 // This file is part of LSystem3d.
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // LSystem3D is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "coordinate.h"
35 * @param model render this model
37 Turtle::Turtle(Model
*model
)
39 // initial position and orientation
62 void Turtle::turnLeft()
64 // rotate around "up vector"
65 _heading
.rotate(-_angle
, _up
);
66 _left
.rotate(-_angle
, _up
);
74 void Turtle::turnRight()
76 // rotate around "up vector"
77 _heading
.rotate(_angle
, _up
);
78 _left
.rotate(_angle
, _up
);
86 void Turtle::pitchDown()
88 // rotate around "left vector"
89 _heading
.rotate(-_angle
, _left
);
90 _up
.rotate(-_angle
, _left
);
98 void Turtle::pitchUp()
100 // rotate around "left vector"
101 _heading
.rotate(_angle
, _left
);
102 _up
.rotate(_angle
, _left
);
110 void Turtle::rollLeft()
112 // rotate around "heading vector"
113 _left
.rotate(_angle
, _heading
);
114 _up
.rotate(_angle
, _heading
);
122 void Turtle::rollRight()
124 // rotate around "heading vector"
125 _left
.rotate(-_angle
, _heading
);
126 _up
.rotate(-_angle
, _heading
);
134 void Turtle::turnAround()
136 // rotate 180 degrees around "up vector"
137 _heading
.rotate(M_PIl
, _up
);
138 _left
.rotate(M_PIl
, _up
);
149 double startX
= _position
.getX();
150 double startY
= _position
.getY();
151 double startZ
= _position
.getZ();
154 double endX
= startX
+ _heading
.getX();
155 double endY
= startY
+ _heading
.getY();
156 double endZ
= startZ
+ _heading
.getZ();
159 // move turtle to new position
160 _position
.setXYZ(endX
, endY
, endZ
);
164 // connect the points
165 _model
->segment(_diameter
,
166 startX
, startY
, startZ
,
171 // part of a filled surface
172 _model
->point(endX
, endY
, endZ
);
179 * Save current state to stack
184 _positionStack
.push(_position
);
187 _headingStack
.push(_heading
);
188 _leftStack
.push(_left
);
192 _diameterStack
.push(_diameter
);
198 * Restore old state from stack
203 _position
= _positionStack
.top(); _positionStack
.pop();
206 _heading
= _headingStack
.top(); _headingStack
.pop();
207 _left
= _leftStack
.top(); _leftStack
.pop();
208 _up
= _upStack
.top(); _upStack
.pop();
211 _diameter
= _diameterStack
.top(); _diameterStack
.pop();
217 * Reset to default state
226 // put turtle at (0,0,0), head upwards, back towards camera
228 _position
.setXYZ(0.0, 0.0, 0.0);
230 _heading
.setXYZ(0.0, 1.0, 0.0);
231 _left
.setXYZ(1.0, 0.0, 0.0);
232 _up
.setXYZ(0.0, 0.0, 1.0);
238 * Decrement diameter of segment
240 void Turtle::decrementDiameter()
242 // TODO: dynamic value...
249 * Begin creation of a filled surface
251 void Turtle::fenceInBegin()
256 _model
->point(_position
.getX(), _position
.getY(), _position
.getZ());
262 * End creation of a filled surface
264 void Turtle::fenceInEnd()
274 * Set turn/pitch/roll angle
275 * @param angle the angle, in radians
277 void Turtle::setAngle(double radians
)
285 * Get turn/pitch/roll angle
286 * @return the angle, in radians
288 double Turtle::getAngle()