1 |
#ifndef ENTITY_H |
2 |
#define ENTITY_H |
3 |
|
4 |
#include <GL/gl.h> |
5 |
#include <GL/glu.h> |
6 |
|
7 |
#include <vector> |
8 |
|
9 |
#include "util.h" |
10 |
#include "oct.h" |
11 |
#include "view.h" |
12 |
|
13 |
using namespace std; |
14 |
|
15 |
struct geometry { |
16 |
geometry *parent; |
17 |
box bbox; |
18 |
|
19 |
virtual void update () |
20 |
{ |
21 |
if (parent) |
22 |
parent->update (); |
23 |
} |
24 |
|
25 |
virtual void draw (view &ctx) = 0; |
26 |
geometry (geometry *parent = 0) : parent(parent) { }; |
27 |
virtual ~geometry (); |
28 |
}; |
29 |
|
30 |
struct vertex1d { |
31 |
colour c; // colour |
32 |
point p; // vertex |
33 |
}; |
34 |
|
35 |
struct vertex2d { |
36 |
point p; // vertex |
37 |
vec3 n; // normal |
38 |
texc t; // texture |
39 |
|
40 |
vertex2d () { }; |
41 |
vertex2d (point p, vec3 n, texc t = texc()) : p(p), n(n), t(t) { }; |
42 |
}; |
43 |
|
44 |
struct geometry_opengl : geometry { |
45 |
GLuint list; // TODO: dynamic caching |
46 |
|
47 |
geometry_opengl (); |
48 |
~geometry_opengl (); |
49 |
}; |
50 |
|
51 |
template<GLenum type> |
52 |
struct geometry_opengl1d : geometry_opengl, vector<vertex1d> { |
53 |
void set (const vector<vertex1d> &v); |
54 |
void draw (view &ctx); |
55 |
}; |
56 |
|
57 |
template<GLenum type> |
58 |
struct geometry_opengl2d : geometry_opengl { |
59 |
material m; |
60 |
|
61 |
void set (const vector<vertex2d> &v); |
62 |
void draw (view &ctx); |
63 |
}; |
64 |
|
65 |
typedef geometry_opengl1d<GL_POINTS> geometry_points; |
66 |
typedef geometry_opengl1d<GL_LINES> geometry_lines; |
67 |
typedef geometry_opengl1d<GL_LINE_STRIP> geometry_line_strip; |
68 |
typedef geometry_opengl1d<GL_LINE_LOOP> geometry_line_loop; |
69 |
typedef geometry_opengl2d<GL_TRIANGLES> geometry_triangles; |
70 |
typedef geometry_opengl2d<GL_TRIANGLE_STRIP> geometry_triangle_strip; |
71 |
typedef geometry_opengl2d<GL_TRIANGLE_FAN> geometry_triangle_fan; |
72 |
typedef geometry_opengl2d<GL_QUADS> geometry_quads; |
73 |
typedef geometry_opengl2d<GL_QUAD_STRIP> geometry_quad_strip; |
74 |
typedef geometry_opengl2d<GL_POLYGON> geometry_polygon; |
75 |
|
76 |
struct geometry_nurbs : geometry, vector<vertex2d> { |
77 |
GLUnurbsObj *nurb; |
78 |
GLUtesselator *tess; |
79 |
GLfloat ctlpoints[4][4][3]; |
80 |
|
81 |
geometry_nurbs() : tess(0), nurb(0) { } |
82 |
virtual void draw (view &ctx); |
83 |
void set (); |
84 |
}; |
85 |
|
86 |
struct geometry_filter : geometry { |
87 |
protected: |
88 |
geometry *g; |
89 |
public: |
90 |
|
91 |
void set (geometry *g); |
92 |
|
93 |
geometry *content () { return g; }; |
94 |
|
95 |
void update (); |
96 |
void show (); |
97 |
void draw (view &ctx); |
98 |
geometry_filter (geometry *g = 0) { set (g); } |
99 |
~geometry_filter (); |
100 |
}; |
101 |
|
102 |
struct geometry_transform : geometry_filter { |
103 |
protected: |
104 |
matrix m; |
105 |
public: |
106 |
void update (); |
107 |
|
108 |
void show (); |
109 |
void draw (view &ctx); |
110 |
void set_matrix (const matrix &xfrm); |
111 |
void update (const matrix &xfrm); |
112 |
|
113 |
geometry_transform (geometry *g) : geometry_filter(g), m(1) { }; |
114 |
geometry_transform () : geometry_filter(0), m(1) { }; |
115 |
}; |
116 |
|
117 |
struct geometry_anim : geometry_transform { |
118 |
GLfloat vx, vy, vz; |
119 |
void draw (view &ctx); |
120 |
}; |
121 |
|
122 |
struct geometry_container : geometry, protected vector<geometry *> { |
123 |
void update (); |
124 |
|
125 |
void add (geometry *g); |
126 |
void draw (view &ctx); |
127 |
~geometry_container (); |
128 |
}; |
129 |
|
130 |
///////////////////////////////////////////////////////////////////////////// |
131 |
|
132 |
struct entity : geometry_filter { |
133 |
sector orig; |
134 |
point p; |
135 |
sector a, b; // bounding box corners |
136 |
|
137 |
vector<octant *> o; |
138 |
|
139 |
void update (); |
140 |
void draw (view &ctx); |
141 |
|
142 |
void move (const vec3 &v); |
143 |
|
144 |
virtual void show () { if (!o.size ()) world.add (this); }; |
145 |
void hide (); |
146 |
void display (view &ctx); |
147 |
|
148 |
entity (geometry *g = 0); |
149 |
~entity (); |
150 |
}; |
151 |
#endif |
152 |
|
153 |
|
154 |
|