5ef84a5afb0e91efc8591401406399ab579a19d8
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"
37 * @param model create this model
39 Turtle::Turtle(Model
*model
)
43 // initial position and orientation
62 * Reset to default state
66 // put turtle at (0,0,0), head upwards, back towards camera
68 _position
.setXYZ(0.0, 0.0, 0.0);
70 _heading
.setXYZ(0.0, 1.0, 0.0);
71 _left
.setXYZ(1.0, 0.0, 0.0);
72 _up
.setXYZ(0.0, 0.0, 1.0);
76 _positionStack
= stack
<Coordinate
>();
77 _headingStack
= stack
<Vector
>();
78 _leftStack
= stack
<Vector
>();
79 _upStack
= stack
<Vector
>();
80 _diameterStack
= stack
<double>();
81 _colorIndexStack
= stack
<int>();
89 void Turtle::turnLeft()
91 // rotate around "up vector"
92 _heading
.rotate(-_angle
, _up
);
93 _left
.rotate(-_angle
, _up
);
101 void Turtle::turnRight()
103 // rotate around "up vector"
104 _heading
.rotate(_angle
, _up
);
105 _left
.rotate(_angle
, _up
);
113 void Turtle::pitchDown()
115 // rotate around "left vector"
116 _heading
.rotate(-_angle
, _left
);
117 _up
.rotate(-_angle
, _left
);
125 void Turtle::pitchUp()
127 // rotate around "left vector"
128 _heading
.rotate(_angle
, _left
);
129 _up
.rotate(_angle
, _left
);
137 void Turtle::rollLeft()
139 // rotate around "heading vector"
140 _left
.rotate(_angle
, _heading
);
141 _up
.rotate(_angle
, _heading
);
149 void Turtle::rollRight()
151 // rotate around "heading vector"
152 _left
.rotate(-_angle
, _heading
);
153 _up
.rotate(-_angle
, _heading
);
161 void Turtle::turnAround()
163 // rotate 180 degrees around "up vector"
164 _heading
.rotate(M_PIl
, _up
);
165 _left
.rotate(M_PIl
, _up
);
176 double startX
= _position
.getX();
177 double startY
= _position
.getY();
178 double startZ
= _position
.getZ();
181 double endX
= startX
+ _heading
.getX();
182 double endY
= startY
+ _heading
.getY();
183 double endZ
= startZ
+ _heading
.getZ();
186 // move turtle to new position
187 _position
.setXYZ(endX
, endY
, endZ
);
189 // connect start and end point
190 _model
->segment(startX
, startY
, startZ
,
197 * Walk forward, creating a filled surface
198 * TODO: merge with walk() ?
200 void Turtle::fillWalk()
203 double destinationX
= _position
.getX() + _heading
.getX();
204 double destinationY
= _position
.getY() + _heading
.getY();
205 double destinationZ
= _position
.getZ() + _heading
.getZ();
207 _model
->normal(_up
.getX(), _up
.getY(), _up
.getZ());
209 // move turtle to new position
210 _position
.setXYZ(destinationX
, destinationY
, destinationZ
);
212 // part of a filled polygon
213 _model
->vertex(destinationX
, destinationY
, destinationZ
);
219 * Save current state to stack
224 _positionStack
.push(_position
);
227 _headingStack
.push(_heading
);
228 _leftStack
.push(_left
);
232 double diameter
= _model
->getDiameter();
233 _diameterStack
.push(diameter
);
236 int colorIndex
= _model
->getColorIndex();
237 _colorIndexStack
.push(colorIndex
);
243 * Restore old state from stack
248 _position
= _positionStack
.top(); _positionStack
.pop();
251 _heading
= _headingStack
.top(); _headingStack
.pop();
252 _left
= _leftStack
.top(); _leftStack
.pop();
253 _up
= _upStack
.top(); _upStack
.pop();
256 double diameter
= _diameterStack
.top(); _diameterStack
.pop();
257 _model
->setDiameter(diameter
);
260 int colorIndex
= _colorIndexStack
.top(); _colorIndexStack
.pop();
261 _model
->setColorIndex(colorIndex
);
267 * Set turn/pitch/roll angle
268 * @param angle the angle, in radians
270 void Turtle::setAngle(double radians
)
278 * Get turn/pitch/roll angle
279 * @return the angle, in radians
281 double Turtle::getAngle()