ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.25
Committed: Mon Oct 4 02:32:20 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.24: +1 -1 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.18 #include <GL/glext.h>
10    
11 root 1.21 entity_base::entity_base ()
12     {
13     }
14    
15     entity_base::~entity_base ()
16     {
17     hide ();
18     }
19    
20 root 1.1 void entity_base::update_bbox ()
21     {
22     if (parent)
23     parent->update_bbox ();
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 root 1.21 void entity_base::display (draw_context &ctx)
35     {
36     if (ctx.may_draw (this))
37 root 1.22 draw (ctx);
38 root 1.21 }
39    
40     /////////////////////////////////////////////////////////////////////////////
41    
42 root 1.1 void entity_filter::update_bbox ()
43     {
44 root 1.22 bbox = translate (e->bbox, e->orig, orig);
45    
46 root 1.1 entity_base::update_bbox ();
47     }
48    
49 root 1.21 void entity_filter::show ()
50 root 1.1 {
51 root 1.22 entity_base::show ();
52 root 1.8
53 root 1.21 e->show ();
54 root 1.1 }
55    
56 root 1.13 void entity_filter::draw (draw_context &ctx)
57 root 1.1 {
58 root 1.21 e->display (ctx);
59 root 1.1 }
60    
61     entity_filter::~entity_filter ()
62     {
63     delete e;
64     }
65    
66 root 1.21 /////////////////////////////////////////////////////////////////////////////
67 root 1.1
68     void entity_container::update_bbox ()
69     {
70     bbox.reset ();
71    
72     for (iterator i = end (); i-- != begin (); )
73     {
74     (*i)->update_bbox ();
75     bbox.add ((*i)->bbox);
76     }
77     }
78    
79 root 1.21 void entity_container::show ()
80 root 1.1 {
81 root 1.22 entity_base::show ();
82 root 1.8
83 root 1.1 for (iterator i = end (); i-- != begin (); )
84 root 1.21 (*i)->show ();
85 root 1.1 }
86    
87 root 1.13 void entity_container::draw (draw_context &ctx)
88 root 1.1 {
89     for (iterator i = end (); i-- != begin (); )
90 root 1.21 (*i)->display (ctx);
91 root 1.1 }
92    
93     entity_container::~entity_container ()
94     {
95     hide ();
96    
97     for (iterator i = end (); i-- != begin (); )
98     delete *i;
99    
100     clear ();
101 root 1.2 }
102    
103 root 1.21 /////////////////////////////////////////////////////////////////////////////
104    
105 root 1.24 entity_opengl::entity_opengl ()
106     {
107     list = glGenLists (1);
108     }
109    
110     entity_opengl::~entity_opengl ()
111     {
112     glDeleteLists (list, 1);
113     }
114    
115 root 1.12 template class entity_opengl1d<GL_POINTS>;
116     template class entity_opengl1d<GL_LINES>;
117     template class entity_opengl1d<GL_LINE_STRIP>;
118     template class entity_opengl1d<GL_LINE_LOOP>;
119     template class entity_opengl2d<GL_TRIANGLES>;
120     template class entity_opengl2d<GL_TRIANGLE_STRIP>;
121     template class entity_opengl2d<GL_TRIANGLE_FAN>;
122     template class entity_opengl2d<GL_QUADS>;
123     template class entity_opengl2d<GL_QUAD_STRIP>;
124     template class entity_opengl2d<GL_POLYGON>;
125 root 1.2
126     template<GLenum type>
127     void entity_opengl1d<type>::update_bbox ()
128     {
129 root 1.6 bbox.reset ();
130    
131     for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
132     bbox.add (i->p);
133    
134 root 1.2 entity_base::update_bbox ();
135     }
136    
137     template<GLenum type>
138 root 1.13 void entity_opengl1d<type>::draw (draw_context &ctx)
139 root 1.2 {
140     glBegin (type);
141    
142     for (iterator i = begin (); i < end (); ++i)
143     {
144 root 1.22 glColor3fv ((GLfloat *)&i->c);
145 root 1.2 glVertex3fv ((GLfloat *)&i->p);
146     }
147    
148     glEnd ();
149     }
150    
151     template<GLenum type>
152 root 1.24 void entity_opengl2d<type>::set (const vector<vertex2d> &v)
153 root 1.2 {
154 root 1.24 bbox.reset ();
155    
156     for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); )
157     bbox.add (i->p);
158    
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