| 1 |
root |
1.1 |
#include <algorithm> |
| 2 |
root |
1.37 |
#include <GL/gl.h> |
| 3 |
root |
1.1 |
|
| 4 |
|
|
using namespace std; |
| 5 |
root |
1.39 |
static GLenum gl_error; |
| 6 |
root |
1.1 |
|
| 7 |
root |
1.28 |
#include "util.h" |
| 8 |
root |
1.1 |
#include "entity.h" |
| 9 |
root |
1.4 |
#include "oct.h" |
| 10 |
root |
1.13 |
#include "view.h" |
| 11 |
root |
1.1 |
|
| 12 |
root |
1.21 |
///////////////////////////////////////////////////////////////////////////// |
| 13 |
|
|
|
| 14 |
root |
1.38 |
geometry::~geometry () |
| 15 |
root |
1.1 |
{ |
| 16 |
root |
1.2 |
} |
| 17 |
|
|
|
| 18 |
root |
1.38 |
template class geometry_opengl1d<GL_POINTS>; |
| 19 |
|
|
template class geometry_opengl1d<GL_LINES>; |
| 20 |
|
|
template class geometry_opengl1d<GL_LINE_STRIP>; |
| 21 |
|
|
template class geometry_opengl1d<GL_LINE_LOOP>; |
| 22 |
|
|
template class geometry_opengl2d<GL_TRIANGLES>; |
| 23 |
|
|
template class geometry_opengl2d<GL_TRIANGLE_STRIP>; |
| 24 |
|
|
template class geometry_opengl2d<GL_TRIANGLE_FAN>; |
| 25 |
|
|
template class geometry_opengl2d<GL_QUADS>; |
| 26 |
|
|
template class geometry_opengl2d<GL_QUAD_STRIP>; |
| 27 |
|
|
template class geometry_opengl2d<GL_POLYGON>; |
| 28 |
root |
1.21 |
|
| 29 |
root |
1.38 |
geometry_opengl::geometry_opengl () |
| 30 |
root |
1.24 |
{ |
| 31 |
|
|
list = glGenLists (1); |
| 32 |
|
|
} |
| 33 |
|
|
|
| 34 |
root |
1.38 |
geometry_opengl::~geometry_opengl () |
| 35 |
root |
1.24 |
{ |
| 36 |
|
|
glDeleteLists (list, 1); |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
root |
1.2 |
template<GLenum type> |
| 40 |
root |
1.38 |
void geometry_opengl1d<type>::set (const vector<vertex1d> &v) |
| 41 |
root |
1.2 |
{ |
| 42 |
root |
1.38 |
clear (); |
| 43 |
|
|
insert (end (), v.begin (), v.end ()); |
| 44 |
|
|
|
| 45 |
root |
1.6 |
bbox.reset (); |
| 46 |
|
|
|
| 47 |
root |
1.38 |
for (const_iterator i = end (); i-- != begin (); ) |
| 48 |
root |
1.6 |
bbox.add (i->p); |
| 49 |
|
|
|
| 50 |
root |
1.38 |
update (); |
| 51 |
root |
1.2 |
} |
| 52 |
|
|
|
| 53 |
|
|
template<GLenum type> |
| 54 |
root |
1.38 |
void geometry_opengl1d<type>::draw (view &ctx) |
| 55 |
root |
1.2 |
{ |
| 56 |
|
|
glBegin (type); |
| 57 |
|
|
|
| 58 |
root |
1.39 |
GL_LG_DEBUG; |
| 59 |
root |
1.2 |
for (iterator i = begin (); i < end (); ++i) |
| 60 |
|
|
{ |
| 61 |
root |
1.22 |
glColor3fv ((GLfloat *)&i->c); |
| 62 |
root |
1.2 |
glVertex3fv ((GLfloat *)&i->p); |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
root |
1.39 |
GL_LG_DEBUG; |
| 66 |
|
|
|
| 67 |
root |
1.2 |
glEnd (); |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
template<GLenum type> |
| 71 |
root |
1.38 |
void geometry_opengl2d<type>::set (const vector<vertex2d> &v) |
| 72 |
root |
1.2 |
{ |
| 73 |
root |
1.24 |
bbox.reset (); |
| 74 |
|
|
|
| 75 |
|
|
for (vector<vertex2d>::const_iterator i = v.end (); i-- != v.begin (); ) |
| 76 |
|
|
bbox.add (i->p); |
| 77 |
root |
1.26 |
|
| 78 |
root |
1.28 |
update (); |
| 79 |
root |
1.24 |
|
| 80 |
|
|
glNewList (list, GL_COMPILE); |
| 81 |
root |
1.14 |
|
| 82 |
root |
1.24 |
glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse); |
| 83 |
|
|
glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular); |
| 84 |
|
|
glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission); |
| 85 |
|
|
glMaterialf (GL_FRONT, GL_SHININESS, m.shininess); |
| 86 |
root |
1.15 |
|
| 87 |
root |
1.39 |
GL_LG_DEBUG; |
| 88 |
root |
1.20 |
#if 0 |
| 89 |
root |
1.24 |
glBegin (type); |
| 90 |
|
|
|
| 91 |
root |
1.25 |
for (vector<vertex2d>::const_iterator i = v.begin (); i < v.end (); ++i) |
| 92 |
root |
1.24 |
{ |
| 93 |
|
|
glTexCoord2fv ((GLfloat *)&i->t); |
| 94 |
|
|
glNormal3fv ((GLfloat *)&i->n); |
| 95 |
|
|
glVertex3fv ((GLfloat *)&i->p); |
| 96 |
|
|
} |
| 97 |
root |
1.4 |
|
| 98 |
root |
1.24 |
glEnd (); |
| 99 |
root |
1.19 |
#else |
| 100 |
root |
1.24 |
glEnableClientState (GL_VERTEX_ARRAY); |
| 101 |
|
|
glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->p); |
| 102 |
|
|
glEnableClientState (GL_NORMAL_ARRAY); |
| 103 |
|
|
glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->n); |
| 104 |
|
|
glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
| 105 |
|
|
glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&v.begin ()->t); |
| 106 |
root |
1.19 |
|
| 107 |
root |
1.39 |
GL_LG_DEBUG; |
| 108 |
root |
1.24 |
glDrawArrays (type, 0, v.size ()); |
| 109 |
root |
1.19 |
#endif |
| 110 |
root |
1.22 |
|
| 111 |
root |
1.39 |
GL_LG_DEBUG; |
| 112 |
root |
1.24 |
glEndList (); |
| 113 |
|
|
} |
| 114 |
root |
1.2 |
|
| 115 |
root |
1.24 |
template<GLenum type> |
| 116 |
root |
1.38 |
void geometry_opengl2d<type>::draw (view &ctx) |
| 117 |
root |
1.24 |
{ |
| 118 |
root |
1.17 |
glCallList (list); |
| 119 |
root |
1.39 |
GL_LG_DEBUG; |
| 120 |
root |
1.22 |
} |
| 121 |
|
|
|
| 122 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 123 |
|
|
|
| 124 |
root |
1.38 |
void geometry_transform::update () |
| 125 |
root |
1.22 |
{ |
| 126 |
root |
1.38 |
const box &sub = g->bbox; |
| 127 |
root |
1.27 |
|
| 128 |
|
|
bbox.reset (); |
| 129 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.a.z)); |
| 130 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.a.z)); |
| 131 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.a.z)); |
| 132 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.a.z)); |
| 133 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.a.y, sub.b.z)); |
| 134 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.a.y, sub.b.z)); |
| 135 |
|
|
bbox.add (m * vec3 (sub.a.x, sub.b.y, sub.b.z)); |
| 136 |
|
|
bbox.add (m * vec3 (sub.b.x, sub.b.y, sub.b.z)); |
| 137 |
root |
1.22 |
|
| 138 |
root |
1.38 |
geometry::update (); |
| 139 |
root |
1.28 |
} |
| 140 |
|
|
|
| 141 |
root |
1.38 |
#if 0 |
| 142 |
|
|
void geometry_transform::renormalize () |
| 143 |
root |
1.28 |
{ |
| 144 |
root |
1.34 |
point trans(m(0,3), m(1,3), m(2,3)); |
| 145 |
|
|
::renormalize (e->orig, trans); |
| 146 |
|
|
m(0,3) = trans.x; m(1,3) = trans.y; m(2,3) = trans.z; |
| 147 |
root |
1.38 |
} |
| 148 |
root |
1.28 |
#endif |
| 149 |
|
|
|
| 150 |
root |
1.38 |
void geometry_transform::update (const matrix &xfrm) |
| 151 |
root |
1.28 |
{ |
| 152 |
|
|
m = m * xfrm; |
| 153 |
|
|
|
| 154 |
|
|
update (); |
| 155 |
root |
1.39 |
GL_LG_DEBUG; |
| 156 |
root |
1.28 |
} |
| 157 |
|
|
|
| 158 |
root |
1.38 |
void geometry_transform::set_matrix (const matrix &xfrm) |
| 159 |
root |
1.28 |
{ |
| 160 |
|
|
m = xfrm; |
| 161 |
|
|
|
| 162 |
|
|
update (); |
| 163 |
root |
1.39 |
GL_LG_DEBUG; |
| 164 |
root |
1.22 |
} |
| 165 |
|
|
|
| 166 |
root |
1.38 |
void geometry_transform::draw (view &ctx) |
| 167 |
root |
1.22 |
{ |
| 168 |
root |
1.39 |
GL_LG_DEBUG; |
| 169 |
root |
1.33 |
glPushMatrix (); |
| 170 |
|
|
glMultMatrixf ((GLfloat *)&m); |
| 171 |
root |
1.38 |
g->draw (ctx); |
| 172 |
root |
1.33 |
glPopMatrix (); |
| 173 |
root |
1.39 |
GL_LG_DEBUG; |
| 174 |
root |
1.2 |
} |
| 175 |
|
|
|
| 176 |
root |
1.38 |
void geometry_anim::draw (view &ctx) |
| 177 |
root |
1.23 |
{ |
| 178 |
root |
1.30 |
matrix save_m = m; |
| 179 |
root |
1.23 |
|
| 180 |
root |
1.30 |
update (matrix::rotation (vx * timer.now, vec3 (1, 0, 0)) |
| 181 |
|
|
* matrix::rotation (vy * timer.now, vec3 (0, 1, 0)) |
| 182 |
|
|
* matrix::rotation (vz * timer.now, vec3 (0, 0, 1))); |
| 183 |
root |
1.28 |
|
| 184 |
root |
1.38 |
geometry_transform::draw (ctx); |
| 185 |
root |
1.23 |
|
| 186 |
|
|
m = save_m; |
| 187 |
root |
1.38 |
} |
| 188 |
|
|
|
| 189 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 190 |
|
|
|
| 191 |
|
|
void geometry_filter::set (geometry *g) |
| 192 |
|
|
{ |
| 193 |
|
|
this->g = g; |
| 194 |
|
|
|
| 195 |
|
|
if (g) |
| 196 |
|
|
g->parent = this; |
| 197 |
|
|
|
| 198 |
|
|
update (); |
| 199 |
|
|
} |
| 200 |
|
|
|
| 201 |
|
|
void geometry_filter::update () |
| 202 |
|
|
{ |
| 203 |
|
|
if (g) |
| 204 |
|
|
{ |
| 205 |
|
|
bbox = g->bbox; |
| 206 |
|
|
geometry::update (); |
| 207 |
|
|
} |
| 208 |
|
|
} |
| 209 |
|
|
|
| 210 |
|
|
void geometry_filter::draw (view &ctx) |
| 211 |
|
|
{ |
| 212 |
|
|
g->draw (ctx); |
| 213 |
|
|
} |
| 214 |
|
|
|
| 215 |
|
|
geometry_filter::~geometry_filter () |
| 216 |
|
|
{ |
| 217 |
|
|
delete g; |
| 218 |
|
|
} |
| 219 |
|
|
|
| 220 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 221 |
|
|
|
| 222 |
|
|
void geometry_container::update () |
| 223 |
|
|
{ |
| 224 |
|
|
bbox.reset (); |
| 225 |
|
|
|
| 226 |
|
|
for (iterator i = end (); i-- != begin (); ) |
| 227 |
|
|
bbox.add ((*i)->bbox); |
| 228 |
|
|
|
| 229 |
|
|
geometry::update (); |
| 230 |
|
|
} |
| 231 |
|
|
|
| 232 |
|
|
void geometry_container::add (geometry *g) |
| 233 |
|
|
{ |
| 234 |
|
|
push_back (g); |
| 235 |
|
|
g->parent = this; |
| 236 |
|
|
|
| 237 |
|
|
update (); |
| 238 |
|
|
} |
| 239 |
|
|
|
| 240 |
|
|
void geometry_container::draw (view &ctx) |
| 241 |
|
|
{ |
| 242 |
|
|
for (iterator i = end (); i-- != begin (); ) |
| 243 |
|
|
(*i)->draw (ctx); |
| 244 |
|
|
} |
| 245 |
|
|
|
| 246 |
|
|
geometry_container::~geometry_container () |
| 247 |
|
|
{ |
| 248 |
|
|
for (iterator i = end (); i-- != begin (); ) |
| 249 |
|
|
delete *i; |
| 250 |
|
|
|
| 251 |
|
|
clear (); |
| 252 |
|
|
} |
| 253 |
|
|
|
| 254 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 255 |
|
|
|
| 256 |
|
|
entity::entity (geometry *g) |
| 257 |
|
|
: geometry_filter(g), p(0,0,0) |
| 258 |
|
|
{ |
| 259 |
|
|
update (); |
| 260 |
|
|
} |
| 261 |
|
|
|
| 262 |
|
|
entity::~entity () |
| 263 |
|
|
{ |
| 264 |
|
|
hide (); |
| 265 |
|
|
} |
| 266 |
|
|
|
| 267 |
|
|
void entity::move (const vec3 &v) |
| 268 |
|
|
{ |
| 269 |
|
|
p = p + v; |
| 270 |
|
|
|
| 271 |
|
|
//renormalize (orig, p); |
| 272 |
|
|
|
| 273 |
|
|
update (); |
| 274 |
|
|
} |
| 275 |
|
|
|
| 276 |
|
|
void entity::hide () |
| 277 |
|
|
{ |
| 278 |
|
|
for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); ) |
| 279 |
|
|
(*i)->remove (this); |
| 280 |
|
|
|
| 281 |
|
|
o.clear (); |
| 282 |
|
|
} |
| 283 |
|
|
|
| 284 |
|
|
void entity::update () |
| 285 |
|
|
{ |
| 286 |
|
|
if (!g) |
| 287 |
|
|
return; |
| 288 |
|
|
|
| 289 |
|
|
bbox = g->bbox; |
| 290 |
|
|
|
| 291 |
|
|
a.x = orig.x + (soffs)floorf (bbox.a.x + p.x); |
| 292 |
|
|
a.y = orig.y + (soffs)floorf (bbox.a.y + p.y); |
| 293 |
|
|
a.z = orig.z + (soffs)floorf (bbox.a.z + p.z); |
| 294 |
|
|
b.x = orig.x + (soffs)ceilf (bbox.b.x + p.x); |
| 295 |
|
|
b.y = orig.y + (soffs)ceilf (bbox.b.y + p.y); |
| 296 |
|
|
b.z = orig.z + (soffs)ceilf (bbox.b.z + p.z); |
| 297 |
|
|
|
| 298 |
|
|
if (o.size ()) |
| 299 |
|
|
{ |
| 300 |
|
|
hide (); |
| 301 |
|
|
show (); |
| 302 |
|
|
} |
| 303 |
|
|
} |
| 304 |
|
|
|
| 305 |
|
|
void entity::draw (view &ctx) |
| 306 |
|
|
{ |
| 307 |
|
|
sector diff = orig - ctx.orig; |
| 308 |
|
|
|
| 309 |
root |
1.39 |
GL_LG_DEBUG; |
| 310 |
root |
1.38 |
glPushMatrix (); |
| 311 |
|
|
glTranslatef (diff.x + p.x, diff.y + p.y, diff.z + p.z); |
| 312 |
root |
1.39 |
GL_LG_DEBUG; |
| 313 |
root |
1.38 |
g->draw (ctx); |
| 314 |
root |
1.39 |
GL_LG_DEBUG; |
| 315 |
root |
1.38 |
glPopMatrix (); |
| 316 |
root |
1.39 |
GL_LG_DEBUG; |
| 317 |
root |
1.38 |
} |
| 318 |
|
|
|
| 319 |
|
|
void entity::display (view &ctx) |
| 320 |
|
|
{ |
| 321 |
|
|
if (ctx.may_draw (this)) |
| 322 |
|
|
draw (ctx); |
| 323 |
root |
1.23 |
} |
| 324 |
root |
1.1 |
|
| 325 |
root |
1.39 |
/////////////////////////////////////////////////////////////////////////// |
| 326 |
|
|
static void nurbs_error (GLenum errorCode) |
| 327 |
|
|
{ |
| 328 |
|
|
const GLubyte *estring; |
| 329 |
|
|
estring = gluErrorString(errorCode); |
| 330 |
|
|
fprintf (stderr, "Nurbs error: %s\n", estring); |
| 331 |
|
|
} |
| 332 |
|
|
void errorCallback(GLenum errorCode) |
| 333 |
|
|
{ |
| 334 |
|
|
const GLubyte *estring; |
| 335 |
|
|
|
| 336 |
|
|
estring = gluErrorString(errorCode); |
| 337 |
|
|
fprintf (stderr, "Tessellation Error: %s\n", estring); |
| 338 |
|
|
exit (0); |
| 339 |
|
|
} |
| 340 |
|
|
void geometry_nurbs::set () |
| 341 |
|
|
{ |
| 342 |
|
|
// < XXX >: Testing CODE |
| 343 |
|
|
int u, v; |
| 344 |
|
|
for (u = 0; u < 4; u++) { |
| 345 |
|
|
for (v = 0; v < 4; v++) { |
| 346 |
|
|
ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5); |
| 347 |
|
|
ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5); |
| 348 |
|
|
|
| 349 |
|
|
if ( (u == 1 || u == 2) && (v == 1 || v == 2)) |
| 350 |
|
|
ctlpoints[u][v][2] = 3.0; |
| 351 |
|
|
else |
| 352 |
|
|
ctlpoints[u][v][2] = -3.0; |
| 353 |
|
|
} |
| 354 |
|
|
} |
| 355 |
|
|
// </ XXX > |
| 356 |
root |
1.1 |
|
| 357 |
root |
1.39 |
glEnable(GL_AUTO_NORMAL); |
| 358 |
|
|
nurb = gluNewNurbsRenderer (); |
| 359 |
|
|
gluNurbsProperty (nurb, GLU_SAMPLING_TOLERANCE, 300.0); |
| 360 |
|
|
gl_error = glGetError (); if (gl_error != GL_NO_ERROR) fprintf (stderr, "XXXX1XX OpenGL error: %d\n", gl_error); |
| 361 |
|
|
gluNurbsProperty (nurb, GLU_DISPLAY_MODE, GLU_NURBS_TESSELLATOR); |
| 362 |
|
|
// gluNurbsProperty (nurb, GLU_SAMPLING_METHOD, GLU_PARAMETRIC_ERROR); |
| 363 |
|
|
// gluNurbsProperty (nurb, GLU_AUTO_LOAD_MATRIX, GL_TRUE); |
| 364 |
|
|
gl_error = glGetError (); if (gl_error != GL_NO_ERROR) fprintf (stderr, "XXXX2XX OpenGL error: %d\n", gl_error); |
| 365 |
|
|
gluNurbsCallback (nurb, GLU_ERROR, (GLvoid (*)()) nurbs_error); |
| 366 |
|
|
gl_error = glGetError (); if (gl_error != GL_NO_ERROR) fprintf (stderr, "XXXX3XX OpenGL error: %d\n", gl_error); |
| 367 |
|
|
glDisable(GL_AUTO_NORMAL); |
| 368 |
|
|
} |
| 369 |
|
|
void geometry_nurbs::draw (view &ctx) |
| 370 |
|
|
{ |
| 371 |
|
|
GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0}; |
| 372 |
|
|
|
| 373 |
|
|
GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 }; |
| 374 |
|
|
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; |
| 375 |
|
|
GLfloat mat_shininess[] = { 100.0 }; |
| 376 |
|
|
|
| 377 |
|
|
glClearColor (0.0, 0.0, 0.0, 0.0); |
| 378 |
|
|
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); |
| 379 |
|
|
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); |
| 380 |
|
|
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); |
| 381 |
|
|
|
| 382 |
|
|
|
| 383 |
|
|
glEnable(GL_AUTO_NORMAL); |
| 384 |
|
|
gl_error = glGetError (); if (gl_error != GL_NO_ERROR) fprintf (stderr, "XXXXDDX OpenGL error: %d\n", gl_error); |
| 385 |
|
|
gluBeginSurface (nurb); |
| 386 |
|
|
gl_error = glGetError (); if (gl_error != GL_NO_ERROR) fprintf (stderr, "XXXX4XX OpenGL error: %d\n", gl_error); |
| 387 |
|
|
gluNurbsSurface (nurb, 8, knots, 8, knots, 4 * 3, 3, &ctlpoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3); |
| 388 |
|
|
gl_error = glGetError (); if (gl_error != GL_NO_ERROR) fprintf (stderr, "XXXX5XX OpenGL error: %d\n", gl_error); |
| 389 |
|
|
|
| 390 |
|
|
gluEndSurface (nurb); |
| 391 |
|
|
GL_LG_DEBUG; |
| 392 |
|
|
glDisable(GL_AUTO_NORMAL); |
| 393 |
|
|
} |