ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.20
Committed: Sun Oct 3 09:18:23 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.19: +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.1 void entity_base::update_bbox ()
12     {
13     if (parent)
14     parent->update_bbox ();
15     }
16    
17     void entity_base::hide ()
18     {
19     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
20     (*i)->remove (this);
21    
22     o.clear ();
23     }
24    
25     void entity_filter::update_bbox ()
26     {
27     bbox = e->bbox;
28     entity_base::update_bbox ();
29     }
30    
31     void entity_filter::show (const sector &sec)
32     {
33 root 1.8 world.add (sec, this);
34    
35 root 1.1 e->show (sec);
36     }
37    
38 root 1.13 void entity_filter::draw (draw_context &ctx)
39 root 1.1 {
40 root 1.13 e->try_draw (ctx);
41 root 1.1 }
42    
43     entity_filter::~entity_filter ()
44     {
45     delete e;
46     }
47    
48     void entity::show (const sector &sec)
49     {
50 root 1.2 show ();
51     }
52    
53     void entity::show ()
54     {
55 root 1.1 e->show (this->sec);
56     }
57    
58 root 1.13 void entity::draw (draw_context &ctx)
59 root 1.1 {
60 root 1.2 glPushMatrix ();
61 root 1.4 glTranslated (-sec.x, -sec.y, -sec.z);
62 root 1.13 e->try_draw (ctx);
63 root 1.2 glPopMatrix ();
64 root 1.1 }
65    
66     void entity_container::update_bbox ()
67     {
68     bbox.reset ();
69    
70     for (iterator i = end (); i-- != begin (); )
71     {
72     (*i)->update_bbox ();
73     bbox.add ((*i)->bbox);
74     }
75     }
76    
77     void entity_container::show (const sector &sec)
78     {
79 root 1.8 world.add (sec, this);
80    
81 root 1.1 for (iterator i = end (); i-- != begin (); )
82     (*i)->show (sec);
83     }
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.13 (*i)->try_draw (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.12 template class entity_opengl1d<GL_POINTS>;
102     template class entity_opengl1d<GL_LINES>;
103     template class entity_opengl1d<GL_LINE_STRIP>;
104     template class entity_opengl1d<GL_LINE_LOOP>;
105     template class entity_opengl2d<GL_TRIANGLES>;
106     template class entity_opengl2d<GL_TRIANGLE_STRIP>;
107     template class entity_opengl2d<GL_TRIANGLE_FAN>;
108     template class entity_opengl2d<GL_QUADS>;
109     template class entity_opengl2d<GL_QUAD_STRIP>;
110     template class entity_opengl2d<GL_POLYGON>;
111 root 1.2
112     template<GLenum type>
113     void entity_opengl1d<type>::update_bbox ()
114     {
115 root 1.6 bbox.reset ();
116    
117     for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
118     bbox.add (i->p);
119    
120 root 1.2 entity_base::update_bbox ();
121     }
122    
123     template<GLenum type>
124     void entity_opengl2d<type>::update_bbox ()
125     {
126 root 1.7 for (vector<vertex2d>::iterator i = end (); i-- != begin (); )
127 root 1.6 bbox.add (i->p);
128    
129 root 1.2 entity_base::update_bbox ();
130     }
131    
132     template<GLenum type>
133 root 1.13 void entity_opengl1d<type>::draw (draw_context &ctx)
134 root 1.2 {
135     glBegin (type);
136    
137     for (iterator i = begin (); i < end (); ++i)
138     {
139 root 1.4 if (ctx.mode == draw_context::LIGHTED)
140     glColor3fv ((GLfloat *)&i->c);
141    
142 root 1.2 glVertex3fv ((GLfloat *)&i->p);
143     }
144    
145     glEnd ();
146     }
147    
148     template<GLenum type>
149 root 1.13 void entity_opengl2d<type>::draw (draw_context &ctx)
150 root 1.2 {
151 root 1.14 if (!list)
152     {
153 root 1.17 list = glGenLists (1);
154     glNewList (list, GL_COMPILE);
155 root 1.14
156 root 1.5 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
157     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
158     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
159 root 1.9 glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
160 root 1.15
161 root 1.20 #if 0
162 root 1.15 glBegin (type);
163 root 1.14
164     for (iterator i = begin (); i < end (); ++i)
165 root 1.4 {
166     glTexCoord2fv ((GLfloat *)&i->t);
167     glNormal3fv ((GLfloat *)&i->n);
168 root 1.14 glVertex3fv ((GLfloat *)&i->p);
169 root 1.4 }
170    
171 root 1.14 glEnd ();
172 root 1.19 #else
173     glEnableClientState (GL_VERTEX_ARRAY);
174     glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->p);
175     glEnableClientState (GL_NORMAL_ARRAY);
176     glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->n);
177     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
178     glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->t);
179    
180     glDrawArrays (type, 0, size ());
181     #endif
182 root 1.17 glEndList ();
183 root 1.19
184     clear ();
185 root 1.2 }
186    
187 root 1.17 glCallList (list);
188 root 1.2 }
189    
190     template<GLenum type>
191     void entity_opengl1d<type>::show (const sector &sec)
192     {
193 root 1.8 world.add (sec, this);
194 root 1.2 }
195    
196     template<GLenum type>
197     void entity_opengl2d<type>::show (const sector &sec)
198     {
199 root 1.8 world.add (sec, this);
200 root 1.1 }
201    
202