ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.7
Committed: Sun Oct 3 03:20:22 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <algorithm>
2 root 1.2 #include <cmath>
3 root 1.1
4     using namespace std;
5    
6     #include "entity.h"
7 root 1.4 #include "oct.h"
8 root 1.1
9     void entity_base::update_bbox ()
10     {
11     if (parent)
12     parent->update_bbox ();
13     }
14    
15     void entity_base::hide ()
16     {
17     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
18     (*i)->remove (this);
19    
20     o.clear ();
21     }
22    
23     void entity_filter::update_bbox ()
24     {
25     bbox = e->bbox;
26     entity_base::update_bbox ();
27     }
28    
29     void entity_filter::show (const sector &sec)
30     {
31     e->show (sec);
32     }
33    
34 root 1.4 void entity_filter::draw (const draw_context &ctx)
35 root 1.1 {
36 root 1.4 e->draw (ctx);
37 root 1.1 }
38    
39     entity_filter::~entity_filter ()
40     {
41     delete e;
42     }
43    
44     void entity::show (const sector &sec)
45     {
46 root 1.2 show ();
47     }
48    
49     void entity::show ()
50     {
51 root 1.1 e->show (this->sec);
52     }
53    
54 root 1.4 void entity::draw (const draw_context &ctx)
55 root 1.1 {
56 root 1.2 glPushMatrix ();
57 root 1.4 glTranslated (-sec.x, -sec.y, -sec.z);
58     e->draw (ctx);
59 root 1.2 glPopMatrix ();
60 root 1.1 }
61    
62     void entity_container::update_bbox ()
63     {
64     bbox.reset ();
65    
66     for (iterator i = end (); i-- != begin (); )
67     {
68     (*i)->update_bbox ();
69     bbox.add ((*i)->bbox);
70     }
71     }
72    
73     void entity_container::show (const sector &sec)
74     {
75     for (iterator i = end (); i-- != begin (); )
76     (*i)->show (sec);
77     }
78    
79 root 1.4 void entity_container::draw (const draw_context &ctx)
80 root 1.1 {
81     for (iterator i = end (); i-- != begin (); )
82 root 1.4 (*i)->draw (ctx);
83 root 1.1 }
84    
85     entity_container::~entity_container ()
86     {
87     hide ();
88    
89     for (iterator i = end (); i-- != begin (); )
90     delete *i;
91    
92     clear ();
93 root 1.2 }
94    
95     template entity_opengl1d<GL_POINTS>;
96     template entity_opengl1d<GL_LINES>;
97     template entity_opengl1d<GL_LINE_STRIP>;
98     template entity_opengl1d<GL_LINE_LOOP>;
99     template entity_opengl2d<GL_TRIANGLES>;
100     template entity_opengl2d<GL_TRIANGLE_STRIP>;
101     template entity_opengl2d<GL_TRIANGLE_FAN>;
102     template entity_opengl2d<GL_QUADS>;
103     template entity_opengl2d<GL_QUAD_STRIP>;
104     template entity_opengl2d<GL_POLYGON>;
105    
106     template<GLenum type>
107     void entity_opengl1d<type>::update_bbox ()
108     {
109 root 1.6 bbox.reset ();
110    
111     for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
112     bbox.add (i->p);
113    
114 root 1.2 entity_base::update_bbox ();
115     }
116    
117     template<GLenum type>
118     void entity_opengl2d<type>::update_bbox ()
119     {
120 root 1.7 for (vector<vertex2d>::iterator i = end (); i-- != begin (); )
121 root 1.6 bbox.add (i->p);
122    
123 root 1.2 entity_base::update_bbox ();
124     }
125    
126     template<GLenum type>
127 root 1.4 void entity_opengl1d<type>::draw (const draw_context &ctx)
128 root 1.2 {
129     glBegin (type);
130    
131     for (iterator i = begin (); i < end (); ++i)
132     {
133 root 1.4 if (ctx.mode == draw_context::LIGHTED)
134     glColor3fv ((GLfloat *)&i->c);
135    
136 root 1.2 glVertex3fv ((GLfloat *)&i->p);
137     }
138    
139     glEnd ();
140     }
141    
142     template<GLenum type>
143 root 1.4 void entity_opengl2d<type>::draw (const draw_context &ctx)
144 root 1.2 {
145     glBegin (type);
146    
147 root 1.5 if (ctx.mode == draw_context::LIGHTED)
148     {
149     glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
150     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
151     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
152     glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
153     }
154    
155 root 1.2 for (iterator i = begin (); i < end (); ++i)
156     {
157 root 1.4 if (ctx.mode == draw_context::LIGHTED)
158     {
159     glTexCoord2fv ((GLfloat *)&i->t);
160     glNormal3fv ((GLfloat *)&i->n);
161     }
162    
163 root 1.2 glVertex3fv ((GLfloat *)&i->p);
164     }
165    
166     glEnd ();
167     }
168    
169     template<GLenum type>
170     void entity_opengl1d<type>::show (const sector &sec)
171     {
172     }
173    
174     template<GLenum type>
175     void entity_opengl2d<type>::show (const sector &sec)
176     {
177     }
178    
179 root 1.4 void view::draw (const draw_context &ctx)
180 root 1.2 {
181     glViewport (0, 0, w, h);
182    
183     glMatrixMode (GL_PROJECTION);
184     glLoadIdentity ();
185    
186     double aspect = (double)w/h;
187 root 1.4 double zNear = 0.0001;
188 root 1.2 double zFar = 1000.;
189    
190     {
191     GLdouble xmin, xmax, ymin, ymax;
192    
193     ymax = zNear * tan (fov * (M_PI / 360.0));
194     ymin = -ymax;
195     xmin = ymin * aspect;
196     xmax = ymax * aspect;
197    
198     glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
199     }
200    
201     vec3 rz = -d;
202     vec3 rx = cross (u, rz);
203     vec3 ry = cross (rz, rx);
204    
205     GLfloat m[4][4];
206     m[0][0] = rx.x; m[0][1] = rx.y; m[0][2] = rx.z; m[0][3] = 0;
207     m[1][0] = ry.x; m[1][1] = ry.y; m[1][2] = ry.z; m[1][3] = 0;
208     m[2][0] = rz.x; m[2][1] = rz.y; m[2][2] = rz.z; m[2][3] = 0;
209     m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
210     glMultMatrixf ((GLfloat *)m);
211    
212     glTranslatef (-p.x, -p.y, -p.z);
213    
214     glMatrixMode (GL_MODELVIEW);
215     glLoadIdentity ();
216    
217 root 1.4 world.draw (ctx);
218 root 1.1 }
219    
220