--- libgender/entity.h 2004/10/05 03:39:47 1.17 +++ libgender/entity.h 2005/08/11 19:28:45 1.36 @@ -1,139 +1,260 @@ #ifndef ENTITY_H #define ENTITY_H -#include - #include +#include +#include "opengl.h" + #include "util.h" #include "oct.h" #include "view.h" +#include "material.h" using namespace std; +using namespace gl; -struct entity_base { - struct entity_base *parent; - sector orig; - vector o; +struct geometry +{ + geometry *parent; box bbox; - virtual void update (); - virtual void show () { world.add (this); }; - void hide (); - void display (draw_context &ctx); - virtual void draw (draw_context &ctx) = 0; + virtual void update () + { + if (parent) + parent->update (); + } + + virtual void draw (view &ctx) = 0; + geometry (geometry *parent = 0) : parent(parent) { }; + virtual ~geometry (); +}; + +struct geometry_opengl : geometry +{ + GLuint list; // TODO: dynamic caching - entity_base (); - virtual ~entity_base (); + geometry_opengl (); + ~geometry_opengl (); }; -struct entity_container : entity_base, protected vector { - void add (entity_base *e) +template +struct geometry_opengl1d : geometry_opengl, vector +{ + void set (const vector &v); + void draw (view &ctx); +}; + +template +struct geometry_opengl2d : geometry_opengl +{ + material *m; + + geometry_opengl2d () : m(0) { }; + + void set (const vector &v); + void draw (view &ctx); +}; + +typedef geometry_opengl1d geometry_points; +typedef geometry_opengl1d geometry_lines; +typedef geometry_opengl1d geometry_line_strip; +typedef geometry_opengl1d geometry_line_loop; +typedef geometry_opengl2d geometry_triangles; +typedef geometry_opengl2d geometry_triangle_strip; +typedef geometry_opengl2d geometry_triangle_fan; +typedef geometry_opengl2d geometry_quads; +typedef geometry_opengl2d geometry_quad_strip; +typedef geometry_opengl2d geometry_polygon; + +struct geometry_nurbs : geometry, vector +{ + GLUnurbsObj *nurb; + GLUtesselator *tess; + GLfloat ctlpoints[4][4][3]; + + geometry_nurbs() : tess(0), nurb(0) { } + virtual void draw (view &ctx); + void set (); +}; + +struct geometry_sphere : geometry +{ + material *m; + GLfloat radius; + + void update (); + void draw (view &ctx); + geometry_sphere (material *m, GLfloat radius) : m(m), radius(radius) { update (); }; +}; + +struct geometry_indexed_2d : geometry +{ + material *m; + + GLenum type; + + vertex_buffer vb; + index_buffer ib; + + void draw (view &ctx); + + template + geometry_indexed_2d (material *m, GLenum type, const vector &v, const vector &i) + : m(m), type(type) { - push_back (e); - e->parent = this; - update (); + vb.set (v); + ib.set (i); + + bbox.reset (); + for (typename vector::const_iterator i = v.begin () ; i != v.end (); ++i) + bbox.add (i->v); } +}; + +///////////////////////////////////////////////////////////////////////////// + +struct geometry_filter : geometry +{ +protected: + geometry *g; +public: + + void set (geometry *g); + + geometry *content () { return g; }; + void update (); void show (); - void draw (draw_context &ctx); - ~entity_container (); + void draw (view &ctx); + geometry_filter (geometry *g = 0) { set (g); } + ~geometry_filter (); }; -struct entity_filter : entity_base { +struct geometry_transform : geometry_filter +{ protected: - entity_base *e; + matrix m; public: + void update (); + + void show (); + void draw (view &ctx); + void set_matrix (const matrix &xfrm); + void update (const matrix &xfrm); - void set (entity_base *e) + geometry_transform (geometry *g) : geometry_filter(g), m(1) { }; + geometry_transform () : geometry_filter(0), m(1) { }; +}; + +struct geometry_anim : geometry_transform +{ + GLfloat vx, vy, vz; + void draw (view &ctx); +}; + +struct geometry_container : geometry, protected vector +{ + void update (); + + void add (geometry *g); + void draw (view &ctx); + ~geometry_container (); +}; + +///////////////////////////////////////////////////////////////////////////// + +struct entity_visibility : visibility_base { + entity *e; + double next; // time of next check + int occ_res; + + void clear () { - this->e = e; - e->parent = this; - update (); + next = 0.; + occ_res = -1; } - void remove () + entity_visibility (entity &e) + : e(&e) { - this->e->parent = 0; - this->e = 0; + clear (); } +}; - entity_base *content () { return e; }; +struct entity : geometry_filter, visible +{ + sector orig; + point p; + sector a, b; // bounding box corners + + vector o; void update (); - void show (); - void draw (draw_context &ctx); - ~entity_filter (); -}; + void draw (view &ctx); -struct entity : entity_filter { -}; + void move (const vec3 &v); -struct vertex1d { - colour c; // colour - point p; // vertex -}; + virtual void show (); + virtual void hide (); -struct vertex2d { - point p; // vertex - vec3 n; // normal - texc t; // texture + visibility_base *new_visibility (); + void clear_visibility (visibility_base *vs); - vertex2d () { }; - vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; + entity (geometry *g = 0); + ~entity (); }; -struct entity_opengl : entity_base { - GLuint list; +struct entity_moveable : entity +{ + vec3 v; + entity_moveable (geometry *g = 0) : entity (g) { } - entity_opengl (); - ~entity_opengl (); + void perform_step (double t); }; -template -struct entity_opengl1d : entity_opengl, vector { - void update (); - void draw (draw_context &ctx); +struct entity_light : entity +{ + light *lview; }; -template -struct entity_opengl2d : entity_opengl { - material m; +///////////////////////////////////////////////////////////////////////////// +// not the final API(!) + +struct skybox +{ + texture *tex[6]; - void set (const vector &v); - void draw (draw_context &ctx); + skybox ( + const char *left, + const char *front, + const char *right, + const char *back, + const char *top, + const char *bottom + ); + + ~skybox (); + + void draw (view &ctx); }; -typedef entity_opengl1d entity_points; -typedef entity_opengl1d entity_lines; -typedef entity_opengl1d entity_line_strip; -typedef entity_opengl1d entity_line_loop; -typedef entity_opengl2d entity_triangles; -typedef entity_opengl2d entity_triangle_strip; -typedef entity_opengl2d entity_triangle_fan; -typedef entity_opengl2d entity_quads; -typedef entity_opengl2d entity_quad_strip; -typedef entity_opengl2d entity_polygon; +///////////////////////////////////////////////////////////////////////////// +// +// VERY EXPERIMENTAL HEIGHTMAP -struct entity_transform : entity_filter { -protected: - matrix m; - void renormalize (); -public: +struct geometry_heightfield : geometry +{ + struct node; - entity_transform (); + node *tree; - void update (); - void show (); - void draw (draw_context &ctx); - void set_matrix (const matrix &xfrm); - void update (const matrix &xfrm); -}; + GLfloat sx, sy, sm; -struct entity_anim : entity_transform { - GLfloat vx, vy, vz; - void draw (draw_context &ctx); + void update (); + void draw (view &ctx); + geometry_heightfield (GLfloat sx, GLfloat sy); }; #endif