8e3417b1656dc984e0c7ff8cd709cc98e9355dc7
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);
67 glDeleteLists(_displayList
, 1);
68 _displayList
= glGenLists(1);
73 _diameterFactor
= 0.8;
83 glCallList(_displayList
);
90 * @param x1 x start point
91 * @param y1 y start point
92 * @param z1 z start point
93 * @param x2 x end point
94 * @param y2 y end point
95 * @param z2 z end point
97 void Model::segment(double x1
, double y1
, double z1
, double x2
, double y2
, double z2
)
99 // find rotation vector and angle
101 Vector
initial(0.0, 1.0, 0.0); // initial rotation of cylinder
102 Vector
desired(x2
-x1
, y2
-y1
, z2
-z1
); // desired rotation of cylinder
106 double rotationAngle
= initial
.getAngle(desired
);
109 Vector rotationVector
= initial
.getCrossProduct(desired
);
110 rotationVector
.normalize();
113 // render the cylinder
115 static GLUquadric
*quadric
= gluNewQuadric();
118 glTranslatef(x1
, y1
, z1
);
119 glRotatef(rotationAngle
, rotationVector
.getX(), rotationVector
.getY(), rotationVector
.getZ());
120 glRotatef(-90, 1, 0, 0);
122 gluCylinder(quadric
, _diameter
, _diameter
, 1.0, 6, 6);
130 * @param x x-coordinate
131 * @param y y-coordinate
132 * @param z z-coordinate
134 void Model::vertex(double x
, double y
, double z
)
143 * @param x x-coordinate
144 * @param y y-coordinate
145 * @param z z-coordinate
147 void Model::normal(double x
, double y
, double z
)
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 segment diameter
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 segment diameter
264 * @param diameter the diameter
266 void Model::setDiameter(double diameter
)
268 _diameter
= diameter
;
274 * Set segment 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 segment diameter
297 * @return the diameter
299 double Model::getDiameter()
307 * Get segment diameter factor
308 * @return the diameter factor
310 double Model::getDiameterFactor()
312 return _diameterFactor
;