ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.5
Committed: Sun Oct 3 03:17:09 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +8 -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.4 //for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
110 root 1.2 entity_base::update_bbox ();
111     }
112    
113     template<GLenum type>
114     void entity_opengl2d<type>::update_bbox ()
115     {
116     entity_base::update_bbox ();
117     }
118    
119     template<GLenum type>
120 root 1.4 void entity_opengl1d<type>::draw (const draw_context &ctx)
121 root 1.2 {
122     glBegin (type);
123    
124     for (iterator i = begin (); i < end (); ++i)
125     {
126 root 1.4 if (ctx.mode == draw_context::LIGHTED)
127     glColor3fv ((GLfloat *)&i->c);
128    
129 root 1.2 glVertex3fv ((GLfloat *)&i->p);
130     }
131    
132     glEnd ();
133     }
134    
135     template<GLenum type>
136 root 1.4 void entity_opengl2d<type>::draw (const draw_context &ctx)
137 root 1.2 {
138     glBegin (type);
139    
140 root 1.5 if (ctx.mode == draw_context::LIGHTED)
141     {
142     glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
143     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
144     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
145     glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
146     }
147    
148 root 1.2 for (iterator i = begin (); i < end (); ++i)
149     {
150 root 1.4 if (ctx.mode == draw_context::LIGHTED)
151     {
152     glTexCoord2fv ((GLfloat *)&i->t);
153     glNormal3fv ((GLfloat *)&i->n);
154     }
155    
156 root 1.2 glVertex3fv ((GLfloat *)&i->p);
157     }
158    
159     glEnd ();
160     }
161    
162     template<GLenum type>
163     void entity_opengl1d<type>::show (const sector &sec)
164     {
165     }
166    
167     template<GLenum type>
168     void entity_opengl2d<type>::show (const sector &sec)
169     {
170     }
171    
172 root 1.4 void view::draw (const draw_context &ctx)
173 root 1.2 {
174     glViewport (0, 0, w, h);
175    
176     glMatrixMode (GL_PROJECTION);
177     glLoadIdentity ();
178    
179     double aspect = (double)w/h;
180 root 1.4 double zNear = 0.0001;
181 root 1.2 double zFar = 1000.;
182    
183     {
184     GLdouble xmin, xmax, ymin, ymax;
185    
186     ymax = zNear * tan (fov * (M_PI / 360.0));
187     ymin = -ymax;
188     xmin = ymin * aspect;
189     xmax = ymax * aspect;
190    
191     glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
192     }
193    
194     vec3 rz = -d;
195     vec3 rx = cross (u, rz);
196     vec3 ry = cross (rz, rx);
197    
198     GLfloat m[4][4];
199     m[0][0] = rx.x; m[0][1] = rx.y; m[0][2] = rx.z; m[0][3] = 0;
200     m[1][0] = ry.x; m[1][1] = ry.y; m[1][2] = ry.z; m[1][3] = 0;
201     m[2][0] = rz.x; m[2][1] = rz.y; m[2][2] = rz.z; m[2][3] = 0;
202     m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
203     glMultMatrixf ((GLfloat *)m);
204    
205     glTranslatef (-p.x, -p.y, -p.z);
206    
207     glMatrixMode (GL_MODELVIEW);
208     glLoadIdentity ();
209    
210 root 1.4 world.draw (ctx);
211 root 1.1 }
212    
213