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
38 // default color table
39 Color
brown(0.5, 0.2, 0.0); _colorTable
.push_back(brown
);
40 Color
white(1.0, 1.0, 1.0); _colorTable
.push_back(white
);
41 Color
green(0.0, 0.5, 0.0); _colorTable
.push_back(green
);
55 glDeleteLists(_displayList
, 1);
62 * @param x1 x start point
63 * @param y1 y start point
64 * @param z1 z start point
65 * @param x2 x end point
66 * @param y2 y end point
67 * @param z2 z end point
69 void Model::segment(double x1
, double y1
, double z1
, double x2
, double y2
, double z2
)
71 // find rotation vector and angle
73 Vector
initial(0.0, 1.0, 0.0); // initial rotation of cylinder
74 Vector
desired(x2
-x1
, y2
-y1
, z2
-z1
); // desired rotation of cylinder
78 double rotationAngle
= initial
.getAngle(desired
);
81 Vector rotationVector
= initial
.getCrossProduct(desired
);
82 rotationVector
.normalize();
85 // render the cylinder
87 static GLUquadric
*quadric
= gluNewQuadric();
90 glTranslatef(x1
, y1
, z1
);
91 glRotatef(rotationAngle
, rotationVector
.getX(), rotationVector
.getY(), rotationVector
.getZ());
92 glRotatef(-90, 1, 0, 0);
94 gluCylinder(quadric
, _diameter
, _diameter
, 1.0, 6, 6);
102 * @param x x-coordinate
103 * @param y y-coordinate
104 * @param z z-coordinate
106 void Model::vertex(double x
, double y
, double z
)
115 * @param x x-coordinate
116 * @param y y-coordinate
117 * @param z z-coordinate
119 void Model::normal(double x
, double y
, double z
)
131 glCallList(_displayList
);
141 glDeleteLists(_displayList
, 1);
142 _displayList
= glGenLists(1);
147 _diameterFactor
= 0.8;
153 * Begin a modelling session
157 glNewList(_displayList
, GL_COMPILE
);
163 * End a modelling session
173 * Begin creation of a filled polygon
175 void Model::fillBegin()
177 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 1);
185 * End creation of a filled polygon
187 void Model::fillEnd()
191 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 0);
197 * Increment current index to color table
199 void Model::nextColor()
201 setColorIndex(_colorIndex
+ 1);
207 * Decrement current index to color table
209 void Model::prevColor()
211 setColorIndex(_colorIndex
- 1);
217 * Decrement diameter of segment
219 void Model::decrementDiameter()
221 _diameter
*= _diameterFactor
;
227 * Set current color index
228 * @param index the color index
230 void Model::setColorIndex(int index
)
236 _colorIndex
= _colorTable
.size() - 1;
238 else if (index
> (_colorTable
.size() - 1))
250 Color color
= _colorTable
[_colorIndex
];
251 double r
= color
.getR();
252 double g
= color
.getG();
253 double b
= color
.getB();
261 * Set current diameter of segment
262 * @param diameter the diameter
264 void Model::setDiameter(double diameter
)
266 _diameter
= diameter
;
272 * Set diameter factor
273 * @param diameter the diameter factor
275 void Model::setDiameterFactor(double diameterFactor
)
277 _diameterFactor
= diameterFactor
;
283 * Get current color index
284 * @return color index
286 int Model::getColorIndex()
294 * Get current diameter of segment
295 * @return diameter of segment
297 double Model::getDiameter()