--- libgender/entity.h 2004/10/02 15:54:43 1.1 +++ libgender/entity.h 2005/08/09 23:58:43 1.35 @@ -1,146 +1,240 @@ #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; -typedef unsigned int soffs; // 32 bit -const soffs soffs_max = 1UL << 31; +struct geometry +{ + geometry *parent; + box bbox; -#define GLFLOAT_MAX 1e30 -#define GLFLOAT_MIN -1e30 + virtual void update () + { + if (parent) + parent->update (); + } -struct sector { - soffs x, y, z; + virtual void draw (view &ctx) = 0; + geometry (geometry *parent = 0) : parent(parent) { }; + virtual ~geometry (); }; -struct point { - GLfloat x, y, z; +struct geometry_opengl : geometry +{ + GLuint list; // TODO: dynamic caching + + geometry_opengl (); + ~geometry_opengl (); }; -struct colour { - GLfloat r, g, b; +template +struct geometry_opengl1d : geometry_opengl, vector +{ + void set (const vector &v); + void draw (view &ctx); }; -struct vect { - GLfloat x, y, z; +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 texc { - GLfloat u, v; +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 box { - point a, b; +struct geometry_indexed_2d : geometry +{ + material *m; + + GLenum type; + + vertex_buffer vb; + index_buffer ib; - void reset () + void draw (view &ctx); + + template + geometry_indexed_2d (material *m, GLenum type, const vector &v, const vector &i) + : m(m), type(type) { - a.x = a.y = a.z = GLFLOAT_MAX; - b.x = b.y = b.z = GLFLOAT_MIN; + vb.set (v); + ib.set (i); + + bbox.reset (); + for (typename vector::const_iterator i = v.begin () ; i != v.end (); ++i) + bbox.add (i->v); } - void add (const box &o); }; -struct entity_base { - struct entity_base *parent; - vector o; - box bbox; +///////////////////////////////////////////////////////////////////////////// - virtual void update_bbox (); - virtual void show (const sector &sec) { }; - void hide (); - virtual void draw () = 0; - virtual ~entity_base () - { - hide (); - }; -}; +struct geometry_filter : geometry +{ +protected: + geometry *g; +public: + + void set (geometry *g); + + geometry *content () { return g; }; -struct entity_container : entity_base, protected vector { - void update_bbox (); - void show (const sector &sec); - void draw (); - ~entity_container (); + void update (); + void show (); + 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 set (entity_base *e) - { - this->e = e; - e->parent = this; - } + void show (); + void draw (view &ctx); + void set_matrix (const matrix &xfrm); + void update (const matrix &xfrm); - void remove () - { - this->e->parent = 0; - this->e = 0; - } - - entity_base *content () { return e; }; + geometry_transform (geometry *g) : geometry_filter(g), m(1) { }; + geometry_transform () : geometry_filter(0), m(1) { }; +}; - void update_bbox (); - void show (const sector &sec); - void draw (); - ~entity_filter (); +struct geometry_anim : geometry_transform +{ + GLfloat vx, vy, vz; + void draw (view &ctx); }; -struct entity : entity_filter { - sector sec; +struct geometry_container : geometry, protected vector +{ + void update (); - void show (const sector &sec); - void draw (); + void add (geometry *g); + void draw (view &ctx); + ~geometry_container (); }; -struct entity_affine : entity_filter { - //matrix m; +///////////////////////////////////////////////////////////////////////////// + +struct entity : geometry_filter +{ + sector orig; + point p; + sector a, b; // bounding box corners + + vector o; + + void update (); + void draw (view &ctx); + + void move (const vec3 &v); + + virtual void show (); + virtual void hide (); + + entity (geometry *g = 0); + ~entity (); }; -struct vertex1d { - point v; // vertex - colour c; // colour +struct entity_moveable : entity +{ + vec3 v; + entity_moveable (geometry *g = 0) : entity (g) { } + + void perform_step (double t); }; -struct vertex2d { - point v; // vertex - colour c; // colour - vect n; // normal - texc t; // texture +struct entity_light : entity +{ + light *lview; }; -template -struct entity_opengl1d : entity_base, vector { - void update_bbox (); - void show (const sector &sec); - void draw (); +///////////////////////////////////////////////////////////////////////////// +// not the final API(!) + +struct skybox +{ + texture *tex[6]; + + skybox ( + const char *left, + const char *front, + const char *right, + const char *back, + const char *top, + const char *bottom + ); + + ~skybox (); + + void draw (view &ctx); }; -template -struct entity_opengl2d : entity_base, vector { - void update_bbox (); - void show (const sector &sec); - void draw (); -}; - -typedef entity_opengl1d entity_points; -typedef entity_opengl1d entity_lines; -typedef entity_opengl1d entity_linestrip; -typedef entity_opengl1d entity_lineloop; -typedef entity_opengl2d entity_triangles; -typedef entity_opengl2d entity_trianglestrip; -typedef entity_opengl2d entity_trianglefan; -typedef entity_opengl2d entity_quads; -typedef entity_opengl2d entity_quad_strip; -typedef entity_opengl2d entity_polygon; +///////////////////////////////////////////////////////////////////////////// +// +// VERY EXPERIMENTAL HEIGHTMAP + +struct geometry_heightfield : geometry +{ + struct node; + + node *tree; + + GLfloat sx, sy, sm; + + void update (); + void draw (view &ctx); + geometry_heightfield (GLfloat sx, GLfloat sy); +}; #endif