dd73b028 |
1 | // Copyright (C) 2006 Erik Dahlberg |
2 | // |
3 | // This file is part of LSystem3d. |
4 | // |
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. |
9 | // |
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. |
14 | // |
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 |
18 | |
19 | |
20 | |
21 | |
22 | #include "GL/gl.h" |
23 | #include "GL/glu.h" |
24 | |
25 | #include "model.h" |
26 | #include "vector.h" |
27 | |
28 | |
29 | |
30 | /** |
31 | * Constructor |
32 | */ |
33 | Model::Model() |
34 | { |
35 | _displayList = glGenLists(1); |
36 | } |
37 | |
38 | |
39 | |
40 | /** |
41 | * Destructor |
42 | */ |
43 | Model::~Model() |
44 | { |
45 | glDeleteLists(_displayList, 1); |
46 | } |
47 | |
48 | |
49 | |
50 | /** |
51 | * Connect two points |
52 | * @param diameter diameter of segment |
53 | * @param x1 x start point |
54 | * @param y1 y start point |
55 | * @param z1 z start point |
56 | * @param x2 x end point |
57 | * @param y2 y end point |
58 | * @param z2 z end point |
59 | */ |
60 | void Model::segment(double diameter, double x1, double y1, double z1, double x2, double y2, double z2) |
61 | { |
62 | // wireframe |
63 | // |
64 | // glColor3f(1,1,1); |
65 | // glBegin(GL_LINES); |
66 | // glVertex3d(x1, y1, z1); |
67 | // glVertex3d(x2, y2, z2); |
68 | // glEnd(); |
69 | |
70 | |
71 | |
72 | // solid |
73 | |
74 | // find rotation vector and angle of cylinder |
75 | |
76 | Vector initial(0.0, 1.0, 0.0); // initial rotation of cylinder |
77 | Vector desired(x2-x1, y2-y1, z2-z1); // desired rotation of cylinder |
78 | desired.normalize(); |
79 | |
80 | // rotation angle |
81 | double angle = initial.getAngle(desired); |
82 | |
83 | // rotation vector |
84 | Vector crossProduct = initial.getCrossProduct(desired); |
85 | crossProduct.normalize(); |
86 | |
87 | |
88 | // render the cylinder |
89 | |
90 | GLUquadric *quadric = gluNewQuadric(); |
91 | |
92 | glColor3f(1.0, 0.5, 0.0); |
93 | |
94 | glPushMatrix(); |
95 | glTranslatef(x1, y1, z1); |
96 | glRotatef(angle, crossProduct.getX(), crossProduct.getY(), crossProduct.getZ()); |
97 | glRotatef(-90, 1, 0, 0); |
98 | |
99 | gluCylinder(quadric, diameter, diameter, 1.0, 6, 6); |
100 | glPopMatrix(); |
101 | } |
102 | |
103 | |
104 | |
105 | /** |
106 | * Draw model to screen |
107 | */ |
108 | void Model::draw() |
109 | { |
110 | glCallList(_displayList); |
111 | } |
112 | |
113 | |
114 | |
115 | /** |
116 | * Clear the model |
117 | */ |
118 | void Model::clear() |
119 | { |
120 | // TODO: not needed? |
121 | } |
122 | |
123 | |
124 | |
125 | /** |
126 | * Start recording of drawing operations |
127 | */ |
128 | void Model::begin() |
129 | { |
130 | glNewList(_displayList, GL_COMPILE); |
131 | } |
132 | |
133 | |
134 | |
135 | /** |
136 | * Stop recording of drawing operations |
137 | */ |
138 | void Model::end() |
139 | { |
140 | glEndList(); |
141 | } |
142 | |
143 | |
144 | |
145 | /** |
146 | * Begin creation of a filled surface |
147 | */ |
148 | void Model::fillBegin() |
149 | { |
150 | glColor3f(0.0, 0.5, 0.0); |
151 | glBegin(GL_POLYGON); |
152 | } |
153 | |
154 | |
155 | |
156 | /** |
157 | * End creation of a filled surface |
158 | */ |
159 | void Model::fillEnd() |
160 | { |
161 | glEnd(); |
162 | } |
163 | |
164 | |
165 | |
166 | /** |
167 | * Create one vertex in the filled surface |
168 | * @param x x-coordinate |
169 | * @param y y-coordinate |
170 | * @param z z-coordinate |
171 | */ |
172 | void Model::point(double x, double y, double z) |
173 | { |
174 | glVertex3d(x, y, z); |
175 | } |