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