ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.23
Committed: Sun Oct 3 23:59:30 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +11 -0 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     orig.x = orig.y = orig.z = 0;
14     }
15    
16     entity_base::~entity_base ()
17     {
18     hide ();
19     }
20    
21 root 1.1 void entity_base::update_bbox ()
22     {
23     if (parent)
24     parent->update_bbox ();
25     }
26    
27     void entity_base::hide ()
28     {
29     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
30     (*i)->remove (this);
31    
32     o.clear ();
33     }
34    
35 root 1.21 void entity_base::display (draw_context &ctx)
36     {
37     if (ctx.may_draw (this))
38 root 1.22 draw (ctx);
39 root 1.21 }
40    
41     /////////////////////////////////////////////////////////////////////////////
42    
43 root 1.1 void entity_filter::update_bbox ()
44     {
45 root 1.22 bbox = translate (e->bbox, e->orig, orig);
46    
47 root 1.1 entity_base::update_bbox ();
48     }
49    
50 root 1.21 void entity_filter::show ()
51 root 1.1 {
52 root 1.22 entity_base::show ();
53 root 1.8
54 root 1.21 e->show ();
55 root 1.1 }
56    
57 root 1.13 void entity_filter::draw (draw_context &ctx)
58 root 1.1 {
59 root 1.21 e->display (ctx);
60 root 1.1 }
61    
62     entity_filter::~entity_filter ()
63     {
64     delete e;
65     }
66    
67 root 1.21 /////////////////////////////////////////////////////////////////////////////
68 root 1.1
69     void entity_container::update_bbox ()
70     {
71     bbox.reset ();
72    
73     for (iterator i = end (); i-- != begin (); )
74     {
75     (*i)->update_bbox ();
76     bbox.add ((*i)->bbox);
77     }
78     }
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.13 void entity_container::draw (draw_context &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.12 template class entity_opengl1d<GL_POINTS>;
107     template class entity_opengl1d<GL_LINES>;
108     template class entity_opengl1d<GL_LINE_STRIP>;
109     template class entity_opengl1d<GL_LINE_LOOP>;
110     template class entity_opengl2d<GL_TRIANGLES>;
111     template class entity_opengl2d<GL_TRIANGLE_STRIP>;
112     template class entity_opengl2d<GL_TRIANGLE_FAN>;
113     template class entity_opengl2d<GL_QUADS>;
114     template class entity_opengl2d<GL_QUAD_STRIP>;
115     template class entity_opengl2d<GL_POLYGON>;
116 root 1.2
117     template<GLenum type>
118     void entity_opengl1d<type>::update_bbox ()
119     {
120 root 1.6 bbox.reset ();
121    
122     for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
123     bbox.add (i->p);
124    
125 root 1.2 entity_base::update_bbox ();
126     }
127    
128     template<GLenum type>
129     void entity_opengl2d<type>::update_bbox ()
130     {
131 root 1.7 for (vector<vertex2d>::iterator i = end (); i-- != begin (); )
132 root 1.6 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.13 void entity_opengl2d<type>::draw (draw_context &ctx)
153 root 1.2 {
154 root 1.14 if (!list)
155     {
156 root 1.17 list = glGenLists (1);
157     glNewList (list, GL_COMPILE);
158 root 1.14
159 root 1.5 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
160     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
161     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
162 root 1.9 glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
163 root 1.15
164 root 1.20 #if 0
165 root 1.15 glBegin (type);
166 root 1.14
167     for (iterator i = begin (); i < end (); ++i)
168 root 1.4 {
169     glTexCoord2fv ((GLfloat *)&i->t);
170     glNormal3fv ((GLfloat *)&i->n);
171 root 1.14 glVertex3fv ((GLfloat *)&i->p);
172 root 1.4 }
173    
174 root 1.14 glEnd ();
175 root 1.19 #else
176     glEnableClientState (GL_VERTEX_ARRAY);
177     glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->p);
178     glEnableClientState (GL_NORMAL_ARRAY);
179     glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->n);
180     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
181     glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->t);
182    
183     glDrawArrays (type, 0, size ());
184     #endif
185 root 1.22
186 root 1.17 glEndList ();
187 root 1.19
188     clear ();
189 root 1.2 }
190    
191 root 1.22 glPushMatrix ();
192     const sector &corig = ctx.v.orig;
193     glTranslatef (corig.x - orig.x, corig.y - orig.y, corig.z - orig.z);
194 root 1.17 glCallList (list);
195 root 1.22 glPopMatrix ();
196     }
197    
198     /////////////////////////////////////////////////////////////////////////////
199    
200     entity_transform::entity_transform ()
201     {
202     m.identity ();
203     }
204    
205     void entity_transform::update_bbox ()
206     {
207     bbox = translate (e->bbox, e->orig, orig);
208    
209     entity_base::update_bbox ();
210     }
211    
212     void entity_transform::show ()
213     {
214     entity_base::show ();
215     }
216    
217     void entity_transform::draw (draw_context &ctx)
218     {
219     glPushMatrix ();
220     glMultMatrixf ((GLfloat *)&m);
221     e->draw (ctx);
222     glPopMatrix ();
223 root 1.2 }
224    
225 root 1.23 void entity_anim::draw (draw_context &ctx)
226     {
227     gl_matrix save_m = m;
228    
229     m.rotate (vx * timer.now, vec3 (1, 0, 0));
230     m.rotate (vy * timer.now, vec3 (0, 1, 0));
231     m.rotate (vz * timer.now, vec3 (0, 0, 1));
232     entity_transform::draw (ctx);
233    
234     m = save_m;
235     }
236 root 1.1
237