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

File Contents

# Content
1 #include <algorithm>
2 #include <cmath>
3
4 using namespace std;
5
6 #include "entity.h"
7 #include "oct.h"
8
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 void entity_filter::draw (const draw_context &ctx)
35 {
36 e->draw (ctx);
37 }
38
39 entity_filter::~entity_filter ()
40 {
41 delete e;
42 }
43
44 void entity::show (const sector &sec)
45 {
46 show ();
47 }
48
49 void entity::show ()
50 {
51 e->show (this->sec);
52 }
53
54 void entity::draw (const draw_context &ctx)
55 {
56 glPushMatrix ();
57 glTranslated (-sec.x, -sec.y, -sec.z);
58 e->draw (ctx);
59 glPopMatrix ();
60 }
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 void entity_container::draw (const draw_context &ctx)
80 {
81 for (iterator i = end (); i-- != begin (); )
82 (*i)->draw (ctx);
83 }
84
85 entity_container::~entity_container ()
86 {
87 hide ();
88
89 for (iterator i = end (); i-- != begin (); )
90 delete *i;
91
92 clear ();
93 }
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 bbox.reset ();
110
111 for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
112 bbox.add (i->p);
113
114 entity_base::update_bbox ();
115 }
116
117 template<GLenum type>
118 void entity_opengl2d<type>::update_bbox ()
119 {
120 for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
121 bbox.add (i->p);
122
123 entity_base::update_bbox ();
124 }
125
126 template<GLenum type>
127 void entity_opengl1d<type>::draw (const draw_context &ctx)
128 {
129 glBegin (type);
130
131 for (iterator i = begin (); i < end (); ++i)
132 {
133 if (ctx.mode == draw_context::LIGHTED)
134 glColor3fv ((GLfloat *)&i->c);
135
136 glVertex3fv ((GLfloat *)&i->p);
137 }
138
139 glEnd ();
140 }
141
142 template<GLenum type>
143 void entity_opengl2d<type>::draw (const draw_context &ctx)
144 {
145 glBegin (type);
146
147 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 for (iterator i = begin (); i < end (); ++i)
156 {
157 if (ctx.mode == draw_context::LIGHTED)
158 {
159 glTexCoord2fv ((GLfloat *)&i->t);
160 glNormal3fv ((GLfloat *)&i->n);
161 }
162
163 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 void view::draw (const draw_context &ctx)
180 {
181 glViewport (0, 0, w, h);
182
183 glMatrixMode (GL_PROJECTION);
184 glLoadIdentity ();
185
186 double aspect = (double)w/h;
187 double zNear = 0.0001;
188 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 world.draw (ctx);
218 }
219
220