| 1 |
root |
1.1 |
#include <algorithm> |
| 2 |
|
|
|
| 3 |
|
|
using namespace std; |
| 4 |
|
|
|
| 5 |
root |
1.28 |
#include "util.h" |
| 6 |
root |
1.1 |
#include "entity.h" |
| 7 |
root |
1.4 |
#include "oct.h" |
| 8 |
root |
1.13 |
#include "view.h" |
| 9 |
root |
1.1 |
|
| 10 |
root |
1.21 |
entity_base::entity_base () |
| 11 |
root |
1.26 |
: parent(0) |
| 12 |
root |
1.21 |
{ |
| 13 |
|
|
} |
| 14 |
|
|
|
| 15 |
|
|
entity_base::~entity_base () |
| 16 |
|
|
{ |
| 17 |
|
|
hide (); |
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
root |
1.28 |
void entity_base::update () |
| 21 |
root |
1.1 |
{ |
| 22 |
|
|
if (parent) |
| 23 |
root |
1.28 |
parent->update (); |
| 24 |
root |
1.1 |
} |
| 25 |
|
|
|
| 26 |
|
|
void entity_base::hide () |
| 27 |
|
|
{ |
| 28 |
|
|
for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); ) |
| 29 |
|
|
(*i)->remove (this); |
| 30 |
|
|
|
| 31 |
|
|
o.clear (); |
| 32 |
|
|
} |
| 33 |
|
|
|
| 34 |
root |
1.21 |
void entity_base::display (draw_context &ctx) |
| 35 |
|
|
{ |
| 36 |
|
|
if (ctx.may_draw (this)) |
| 37 |
root |
1.22 |
draw (ctx); |
| 38 |
root |
1.21 |
} |
| 39 |
|
|
|
| 40 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 41 |
|
|
|
| 42 |
root |
1.28 |
void entity_filter::update () |
| 43 |
root |
1.1 |
{ |
| 44 |
root |
1.22 |
bbox = translate (e->bbox, e->orig, orig); |
| 45 |
|
|
|
| 46 |
root |
1.28 |
entity_base::update (); |
| 47 |
root |
1.1 |
} |
| 48 |
|
|
|
| 49 |
root |
1.21 |
void entity_filter::show () |
| 50 |
root |
1.1 |
{ |
| 51 |
root |
1.22 |
entity_base::show (); |
| 52 |
root |
1.8 |
|
| 53 |
root |
1.21 |
e->show (); |
| 54 |
root |
1.1 |
} |
| 55 |
|
|
|
| 56 |
root |
1.13 |
void entity_filter::draw (draw_context &ctx) |
| 57 |
root |
1.1 |
{ |
| 58 |
root |
1.21 |
e->display (ctx); |
| 59 |
root |
1.1 |
} |
| 60 |
|
|
|
| 61 |
|
|
entity_filter::~entity_filter () |
| 62 |
|
|
{ |
| 63 |
|
|
delete e; |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
root |
1.21 |
///////////////////////////////////////////////////////////////////////////// |
| 67 |
root |
1.1 |
|
| 68 |
root |
1.28 |
void entity_container::update () |
| 69 |
root |
1.1 |
{ |
| 70 |
|
|
bbox.reset (); |
| 71 |
|
|
|
| 72 |
|
|
for (iterator i = end (); i-- != begin (); ) |
| 73 |
root |
1.26 |
bbox.add ((*i)->bbox); |
| 74 |
|
|
|
| 75 |
root |
1.28 |
entity_base::update (); |
| 76 |
root |
1.1 |
} |
| 77 |
|
|
|
| 78 |
root |
1.21 |
void entity_container::show () |
| 79 |
root |
1.1 |
{ |
| 80 |
root |
1.22 |
entity_base::show (); |
| 81 |
root |
1.8 |
|
| 82 |
root |
1.1 |
for (iterator i = end (); i-- != begin (); ) |
| 83 |
root |
1.21 |
(*i)->show (); |
| 84 |
root |
1.1 |
} |
| 85 |
|
|
|
| 86 |
root |
1.13 |
void entity_container::draw (draw_context &ctx) |
| 87 |
root |
1.1 |
{ |
| 88 |
|
|
for (iterator i = end (); i-- != begin (); ) |
| 89 |
root |
1.21 |
(*i)->display (ctx); |
| 90 |
root |
1.1 |
} |
| 91 |
|
|
|
| 92 |
|
|
entity_container::~entity_container () |
| 93 |
|
|
{ |
| 94 |
|
|
hide (); |
| 95 |
|
|
|
| 96 |
|
|
for (iterator i = end (); i-- != begin (); ) |
| 97 |
|
|
delete *i; |
| 98 |
|
|
|
| 99 |
|
|
clear (); |
| 100 |
root |
1.2 |
} |
| 101 |
|
|
|
| 102 |
root |
1.21 |
///////////////////////////////////////////////////////////////////////////// |
| 103 |
|
|
|
| 104 |
root |
1.24 |
entity_opengl::entity_opengl () |
| 105 |
|
|
{ |
| 106 |
|
|
list = glGenLists (1); |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
entity_opengl::~entity_opengl () |
| 110 |
|
|
{ |
| 111 |
|
|
glDeleteLists (list, 1); |
| 112 |
|
|
} |
| 113 |
|
|
|
| 114 |
root |
1.12 |
template class entity_opengl1d<GL_POINTS>; |
| 115 |
|
|
template class entity_opengl1d<GL_LINES>; |
| 116 |
|
|
template class entity_opengl1d<GL_LINE_STRIP>; |
| 117 |
|
|
template class entity_opengl1d<GL_LINE_LOOP>; |
| 118 |
|
|
template class entity_opengl2d<GL_TRIANGLES>; |
| 119 |
|
|
template class entity_opengl2d<GL_TRIANGLE_STRIP>; |
| 120 |
|
|
template class entity_opengl2d<GL_TRIANGLE_FAN>; |
| 121 |
|
|
template class entity_opengl2d<GL_QUADS>; |
| 122 |
|
|
template class entity_opengl2d<GL_QUAD_STRIP>; |
| 123 |
|
|
template class entity_opengl2d<GL_POLYGON>; |
| 124 |
root |
1.2 |
|
| 125 |
|
|
template<GLenum type> |
| 126 |
root |
1.28 |
void entity_opengl1d<type>::update () |
| 127 |
root |
1.2 |
{ |
| 128 |
root |
1.6 |
bbox.reset (); |
| 129 |
|
|
|
| 130 |
|
|
for (vector<vertex1d>::iterator i = end (); i-- != begin (); ) |
| 131 |
|
|
bbox.add (i->p); |
| 132 |
|
|
|
| 133 |
root |
1.28 |
entity_base::update (); |
| 134 |
root |
1.2 |
} |
| 135 |
|
|
|
| 136 |
|
|
template<GLenum type> |
| 137 |
root |
1.13 |
void entity_opengl1d<type>::draw (draw_context &ctx) |
| 138 |
root |
1.2 |
{ |
| 139 |
|
|
glBegin (type); |
| 140 |
|
|
|
| 141 |
|
|
for (iterator i = begin (); i < end (); ++i) |
| 142 |
|
|
{ |
| 143 |
root |
1.22 |
glColor3fv ((GLfloat *)&i->c); |
| 144 |
root |
1.2 |
glVertex3fv ((GLfloat *)&i->p); |
| 145 |
|
|
} |
| 146 |
|
|
|
| 147 |
|
|
glEnd (); |
| 148 |
|
|
} |
| 149 |
|
|
|
| 150 |
|
|
template<GLenum type> |
| 151 |
root |
1.24 |
void entity_opengl2d<type>::set (const vector<vertex2d> &v) |
| 152 |
root |
1.2 |
{ |
| 153 |
root |
1.24 |
bbox.reset (); |
| 154 |
|
|
|
| 155 |
|
|
for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); ) |
| 156 |
|
|
bbox.add (i->p); |
| 157 |
root |
1.26 |
|
| 158 |
root |
1.28 |
update (); |
| 159 |
root |
1.24 |
|
| 160 |
|
|
glNewList (list, GL_COMPILE); |
| 161 |
root |
1.14 |
|
| 162 |
root |
1.24 |
glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse); |
| 163 |
|
|
glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular); |
| 164 |
|
|
glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission); |
| 165 |
|
|
glMaterialf (GL_FRONT, GL_SHININESS, m.shininess); |
| 166 |
root |
1.15 |
|
| 167 |
root |
1.20 |
#if 0 |
| 168 |
root |
1.24 |
glBegin (type); |
| 169 |
|
|
|
| 170 |
root |
1.25 |
for (vector<vertex2d>::const_iterator i = v.begin (); i < v.end (); ++i) |
| 171 |
root |
1.24 |
{ |
| 172 |
|
|
glTexCoord2fv ((GLfloat *)&i->t); |
| 173 |
|
|
glNormal3fv ((GLfloat *)&i->n); |
| 174 |
|
|
glVertex3fv ((GLfloat *)&i->p); |
| 175 |
|
|
} |
| 176 |
root |
1.4 |
|
| 177 |
root |
1.24 |
glEnd (); |
| 178 |
root |
1.19 |
#else |
| 179 |
root |
1.24 |
glEnableClientState (GL_VERTEX_ARRAY); |
| 180 |
|
|
glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p); |
| 181 |
|
|
glEnableClientState (GL_NORMAL_ARRAY); |
| 182 |
|
|
glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n); |
| 183 |
|
|
glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
| 184 |
|
|
glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t); |
| 185 |
root |
1.19 |
|
| 186 |
root |
1.24 |
glDrawArrays (type, 0, v.size ()); |
| 187 |
root |
1.19 |
#endif |
| 188 |
root |
1.22 |
|
| 189 |
root |
1.24 |
glEndList (); |
| 190 |
|
|
} |
| 191 |
root |
1.2 |
|
| 192 |
root |
1.24 |
template<GLenum type> |
| 193 |
|
|
void entity_opengl2d<type>::draw (draw_context &ctx) |
| 194 |
|
|
{ |
| 195 |
root |
1.30 |
const matrix save_mv = ctx.modelview; |
| 196 |
|
|
|
| 197 |
root |
1.22 |
const sector &corig = ctx.v.orig; |
| 198 |
root |
1.31 |
ctx.transform (matrix::translation (vec3 (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z))); |
| 199 |
root |
1.17 |
glCallList (list); |
| 200 |
root |
1.30 |
|
| 201 |
|
|
ctx.modelview = save_mv; |
| 202 |
root |
1.22 |
} |
| 203 |
|
|
|
| 204 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 205 |
|
|
|
| 206 |
|
|
entity_transform::entity_transform () |
| 207 |
|
|
{ |
| 208 |
|
|
m.identity (); |
| 209 |
|
|
} |
| 210 |
|
|
|
| 211 |
root |
1.28 |
void entity_transform::update () |
| 212 |
root |
1.22 |
{ |
| 213 |
root |
1.27 |
box sub = translate (e->bbox, e->orig, orig); |
| 214 |
|
|
|
| 215 |
|
|
bbox.reset (); |
| 216 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z)); |
| 217 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z)); |
| 218 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z)); |
| 219 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z)); |
| 220 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z)); |
| 221 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z)); |
| 222 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z)); |
| 223 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z)); |
| 224 |
root |
1.22 |
|
| 225 |
root |
1.28 |
entity_base::update (); |
| 226 |
|
|
} |
| 227 |
|
|
|
| 228 |
|
|
void entity_transform::renormalize () |
| 229 |
|
|
{ |
| 230 |
|
|
#if 0 |
| 231 |
|
|
point trans(m(0,0), m(1,0), m(2,0)); |
| 232 |
|
|
::renormalize (orig, trans); |
| 233 |
|
|
m(0,0) = trans.x; m(1,0) = trans.y; m(2,0) = trans.z; |
| 234 |
|
|
#endif |
| 235 |
|
|
} |
| 236 |
|
|
|
| 237 |
root |
1.30 |
void entity_transform::update (const matrix &xfrm) |
| 238 |
root |
1.28 |
{ |
| 239 |
|
|
m = m * xfrm; |
| 240 |
|
|
|
| 241 |
|
|
renormalize (); |
| 242 |
|
|
update (); |
| 243 |
|
|
} |
| 244 |
|
|
|
| 245 |
root |
1.30 |
void entity_transform::set_matrix (const matrix &xfrm) |
| 246 |
root |
1.28 |
{ |
| 247 |
|
|
m = xfrm; |
| 248 |
|
|
|
| 249 |
|
|
renormalize (); |
| 250 |
|
|
update (); |
| 251 |
root |
1.22 |
} |
| 252 |
|
|
|
| 253 |
|
|
void entity_transform::show () |
| 254 |
|
|
{ |
| 255 |
|
|
entity_base::show (); |
| 256 |
|
|
} |
| 257 |
|
|
|
| 258 |
|
|
void entity_transform::draw (draw_context &ctx) |
| 259 |
|
|
{ |
| 260 |
root |
1.30 |
const matrix save_mv = ctx.modelview; |
| 261 |
|
|
|
| 262 |
|
|
const sector &corig = ctx.v.orig; |
| 263 |
root |
1.31 |
ctx.transform (m); |
| 264 |
root |
1.28 |
#if 0 |
| 265 |
|
|
const sector &corig = ctx.v.orig; |
| 266 |
|
|
glTranslatef (orig.x - corig.x, orig.y - corig.y, orig.z - corig.z); |
| 267 |
|
|
#endif |
| 268 |
root |
1.22 |
e->draw (ctx); |
| 269 |
root |
1.30 |
|
| 270 |
|
|
ctx.modelview = save_mv; |
| 271 |
root |
1.2 |
} |
| 272 |
|
|
|
| 273 |
root |
1.23 |
void entity_anim::draw (draw_context &ctx) |
| 274 |
|
|
{ |
| 275 |
root |
1.30 |
matrix save_m = m; |
| 276 |
root |
1.23 |
|
| 277 |
root |
1.30 |
update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0)) |
| 278 |
|
|
* matrix::rotation (vy * timer.now, vec3 (0, 1, 0)) |
| 279 |
|
|
* matrix::rotation (vz * timer.now, vec3 (0, 0, 1))); |
| 280 |
root |
1.28 |
|
| 281 |
root |
1.23 |
entity_transform::draw (ctx); |
| 282 |
|
|
|
| 283 |
|
|
m = save_m; |
| 284 |
|
|
} |
| 285 |
root |
1.1 |
|
| 286 |
|
|
|