ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.32
Committed: Tue Oct 5 07:09:17 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: knowngood
Changes since 1.31: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <algorithm>
2
3 using namespace std;
4
5 #include "util.h"
6 #include "entity.h"
7 #include "oct.h"
8 #include "view.h"
9
10 entity_base::entity_base ()
11 : parent(0)
12 {
13 }
14
15 entity_base::~entity_base ()
16 {
17 hide ();
18 }
19
20 void entity_base::update ()
21 {
22 if (parent)
23 parent->update ();
24 }
25
26 void entity_base::hide ()
27 {
28 for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
29 (*i)->remove (this);
30
31 o.clear ();
32 }
33
34 void entity_base::display (draw_context &ctx)
35 {
36 if (ctx.may_draw (this))
37 draw (ctx);
38 }
39
40 /////////////////////////////////////////////////////////////////////////////
41
42 void entity_filter::update ()
43 {
44 bbox = translate (e->bbox, e->orig, orig);
45
46 entity_base::update ();
47 }
48
49 void entity_filter::show ()
50 {
51 entity_base::show ();
52
53 e->show ();
54 }
55
56 void entity_filter::draw (draw_context &ctx)
57 {
58 e->display (ctx);
59 }
60
61 entity_filter::~entity_filter ()
62 {
63 delete e;
64 }
65
66 /////////////////////////////////////////////////////////////////////////////
67
68 void entity_container::update ()
69 {
70 bbox.reset ();
71
72 for (iterator i = end (); i-- != begin (); )
73 bbox.add ((*i)->bbox);
74
75 entity_base::update ();
76 }
77
78 void entity_container::show ()
79 {
80 entity_base::show ();
81
82 for (iterator i = end (); i-- != begin (); )
83 (*i)->show ();
84 }
85
86 void entity_container::draw (draw_context &ctx)
87 {
88 for (iterator i = end (); i-- != begin (); )
89 (*i)->display (ctx);
90 }
91
92 entity_container::~entity_container ()
93 {
94 hide ();
95
96 for (iterator i = end (); i-- != begin (); )
97 delete *i;
98
99 clear ();
100 }
101
102 /////////////////////////////////////////////////////////////////////////////
103
104 entity_opengl::entity_opengl ()
105 {
106 list = glGenLists (1);
107 }
108
109 entity_opengl::~entity_opengl ()
110 {
111 glDeleteLists (list, 1);
112 }
113
114 template class entity_opengl1d<GL_POINTS>;
115 template class entity_opengl1d<GL_LINES>;
116 template class entity_opengl1d<GL_LINE_STRIP>;
117 template class entity_opengl1d<GL_LINE_LOOP>;
118 template class entity_opengl2d<GL_TRIANGLES>;
119 template class entity_opengl2d<GL_TRIANGLE_STRIP>;
120 template class entity_opengl2d<GL_TRIANGLE_FAN>;
121 template class entity_opengl2d<GL_QUADS>;
122 template class entity_opengl2d<GL_QUAD_STRIP>;
123 template class entity_opengl2d<GL_POLYGON>;
124
125 template<GLenum type>
126 void entity_opengl1d<type>::update ()
127 {
128 bbox.reset ();
129
130 for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
131 bbox.add (i->p);
132
133 entity_base::update ();
134 }
135
136 template<GLenum type>
137 void entity_opengl1d<type>::draw (draw_context &ctx)
138 {
139 glBegin (type);
140
141 for (iterator i = begin (); i < end (); ++i)
142 {
143 glColor3fv ((GLfloat *)&i->c);
144 glVertex3fv ((GLfloat *)&i->p);
145 }
146
147 glEnd ();
148 }
149
150 template<GLenum type>
151 void entity_opengl2d<type>::set (const vector<vertex2d> &v)
152 {
153 bbox.reset ();
154
155 for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); )
156 bbox.add (i->p);
157
158 update ();
159
160 glNewList (list, GL_COMPILE);
161
162 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
163 glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
164 glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
165 glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
166
167 #if 0
168 glBegin (type);
169
170 for (vector<vertex2d>::const_iterator i = v.begin (); i < v.end (); ++i)
171 {
172 glTexCoord2fv ((GLfloat *)&i->t);
173 glNormal3fv ((GLfloat *)&i->n);
174 glVertex3fv ((GLfloat *)&i->p);
175 }
176
177 glEnd ();
178 #else
179 glEnableClientState (GL_VERTEX_ARRAY);
180 glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p);
181 glEnableClientState (GL_NORMAL_ARRAY);
182 glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n);
183 glEnableClientState (GL_TEXTURE_COORD_ARRAY);
184 glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t);
185
186 glDrawArrays (type, 0, v.size ());
187 #endif
188
189 glEndList ();
190 }
191
192 template<GLenum type>
193 void entity_opengl2d<type>::draw (draw_context &ctx)
194 {
195 const matrix save_mv = ctx.modelview;
196
197 const sector &corig = ctx.v.orig;
198 ctx.transform (matrix::translation (vec3 (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z)));
199 glCallList (list);
200
201 ctx.modelview = save_mv;
202 }
203
204 /////////////////////////////////////////////////////////////////////////////
205
206 entity_transform::entity_transform ()
207 {
208 m.identity ();
209 }
210
211 void entity_transform::update ()
212 {
213 box sub = translate (e->bbox, e->orig, orig);
214
215 bbox.reset ();
216 bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z));
217 bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z));
218 bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z));
219 bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z));
220 bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z));
221 bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z));
222 bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z));
223 bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z));
224
225 entity_base::update ();
226 }
227
228 void entity_transform::renormalize ()
229 {
230 #if 0
231 point trans(m(0,0), m(1,0), m(2,0));
232 ::renormalize (orig, trans);
233 m(0,0) = trans.x; m(1,0) = trans.y; m(2,0) = trans.z;
234 #endif
235 }
236
237 void entity_transform::update (const matrix &xfrm)
238 {
239 m = m * xfrm;
240
241 renormalize ();
242 update ();
243 }
244
245 void entity_transform::set_matrix (const matrix &xfrm)
246 {
247 m = xfrm;
248
249 renormalize ();
250 update ();
251 }
252
253 void entity_transform::show ()
254 {
255 entity_base::show ();
256 }
257
258 void entity_transform::draw (draw_context &ctx)
259 {
260 const matrix save_mv = ctx.modelview;
261
262 const sector &corig = ctx.v.orig;
263 #if 0
264 glTranslatef (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z);
265 ctx.transform (matrix::translation (vec3 (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z)));
266 #endif
267 ctx.transform (m);
268 e->draw (ctx);
269
270 ctx.modelview = save_mv;
271 }
272
273 void entity_anim::draw (draw_context &ctx)
274 {
275 matrix save_m = m;
276
277 update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0))
278 * matrix::rotation (vy * timer.now, vec3 (0, 1, 0))
279 * matrix::rotation (vz * timer.now, vec3 (0, 0, 1)));
280
281 entity_transform::draw (ctx);
282
283 m = save_m;
284 }
285
286