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
40 // default color table
41 Color
brown(0.5, 0.2, 0.0); _colorTable
.push_back(brown
);
42 Color
white(1.0, 1.0, 1.0); _colorTable
.push_back(white
);
43 Color
green(0.0, 0.5, 0.0); _colorTable
.push_back(green
);
57 glDeleteLists(_displayList
, 1);
64 * @param x1 x start point
65 * @param y1 y start point
66 * @param z1 z start point
67 * @param x2 x end point
68 * @param y2 y end point
69 * @param z2 z end point
71 void Model::segment(double x1
, double y1
, double z1
, double x2
, double y2
, double z2
)
73 // find rotation vector and angle
75 Vector
initial(0.0, 1.0, 0.0); // initial rotation of cylinder
76 Vector
desired(x2
-x1
, y2
-y1
, z2
-z1
); // desired rotation of cylinder
80 double rotationAngle
= initial
.getAngle(desired
);
83 Vector rotationVector
= initial
.getCrossProduct(desired
);
84 rotationVector
.normalize();
87 // render the cylinder
89 static GLUquadric
*quadric
= gluNewQuadric();
92 glTranslatef(x1
, y1
, z1
);
93 glRotatef(rotationAngle
, rotationVector
.getX(), rotationVector
.getY(), rotationVector
.getZ());
94 glRotatef(-90, 1, 0, 0);
96 gluCylinder(quadric
, _diameter
, _diameter
, 1.0, 6, 6);
104 * @param x x-coordinate
105 * @param y y-coordinate
106 * @param z z-coordinate
108 void Model::vertex(double x
, double y
, double z
)
117 * @param x x-coordinate
118 * @param y y-coordinate
119 * @param z z-coordinate
121 void Model::normal(double x
, double y
, double z
)
133 glCallList(_displayList
);
143 glDeleteLists(_displayList
, 1);
144 _displayList
= glGenLists(1);
149 _diameterFactor
= 0.8;
155 * Begin a modelling session
159 glNewList(_displayList
, GL_COMPILE
);
165 * End a modelling session
175 * Begin creation of a filled polygon
177 void Model::fillBegin()
179 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 1);
187 * End creation of a filled polygon
189 void Model::fillEnd()
193 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 0);
199 * Increment current index to color table
201 void Model::nextColor()
203 setColorIndex(_colorIndex
+ 1);
209 * Decrement current index to color table
211 void Model::prevColor()
213 setColorIndex(_colorIndex
- 1);
219 * Decrement diameter of segment
221 void Model::decrementDiameter()
223 _diameter
*= _diameterFactor
;
229 * Set current color index
230 * @param index the color index
232 void Model::setColorIndex(int index
)
238 _colorIndex
= _colorTable
.size() - 1;
240 else if (index
> (_colorTable
.size() - 1))
252 Color color
= _colorTable
[_colorIndex
];
253 double r
= color
.getR();
254 double g
= color
.getG();
255 double b
= color
.getB();
263 * Set current diameter of segment
264 * @param diameter the diameter
266 void Model::setDiameter(double diameter
)
268 _diameter
= diameter
;
274 * Set diameter factor
275 * @param diameter the diameter factor
277 void Model::setDiameterFactor(double diameterFactor
)
279 _diameterFactor
= diameterFactor
;
285 * Get current color index
286 * @return color index
288 int Model::getColorIndex()
296 * Get current diameter of segment
297 * @return diameter of segment
299 double Model::getDiameter()