ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.37
Committed: Wed Oct 6 17:55:40 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +4 -0 lines
Log Message:
i

File Contents

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