ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.26
Committed: Mon Oct 4 07:04:58 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +6 -6 lines
Log Message:
*** empty log message ***

File Contents

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