1 |
#include <algorithm> |
2 |
|
3 |
using namespace std; |
4 |
|
5 |
#include "entity.h" |
6 |
#include "oct.h" |
7 |
#include "view.h" |
8 |
|
9 |
#include <GL/glext.h> |
10 |
|
11 |
entity_base::entity_base () |
12 |
{ |
13 |
orig.x = orig.y = orig.z = 0; |
14 |
} |
15 |
|
16 |
entity_base::~entity_base () |
17 |
{ |
18 |
hide (); |
19 |
} |
20 |
|
21 |
void entity_base::update_bbox () |
22 |
{ |
23 |
if (parent) |
24 |
parent->update_bbox (); |
25 |
} |
26 |
|
27 |
void entity_base::hide () |
28 |
{ |
29 |
for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); ) |
30 |
(*i)->remove (this); |
31 |
|
32 |
o.clear (); |
33 |
} |
34 |
|
35 |
void entity_base::display (draw_context &ctx) |
36 |
{ |
37 |
if (ctx.may_draw (this)) |
38 |
{ |
39 |
glPushMatrix (); |
40 |
glTranslated (-orig.x, -orig.y, -orig.z); |
41 |
draw (ctx); |
42 |
glPopMatrix (); |
43 |
} |
44 |
} |
45 |
|
46 |
///////////////////////////////////////////////////////////////////////////// |
47 |
|
48 |
void entity_filter::update_bbox () |
49 |
{ |
50 |
bbox = e->bbox; |
51 |
entity_base::update_bbox (); |
52 |
} |
53 |
|
54 |
void entity_filter::show () |
55 |
{ |
56 |
world.add (orig, this); |
57 |
|
58 |
e->show (); |
59 |
} |
60 |
|
61 |
void entity_filter::draw (draw_context &ctx) |
62 |
{ |
63 |
e->display (ctx); |
64 |
} |
65 |
|
66 |
entity_filter::~entity_filter () |
67 |
{ |
68 |
delete e; |
69 |
} |
70 |
|
71 |
///////////////////////////////////////////////////////////////////////////// |
72 |
|
73 |
void entity_container::update_bbox () |
74 |
{ |
75 |
bbox.reset (); |
76 |
|
77 |
for (iterator i = end (); i-- != begin (); ) |
78 |
{ |
79 |
(*i)->update_bbox (); |
80 |
bbox.add ((*i)->bbox); |
81 |
} |
82 |
} |
83 |
|
84 |
void entity_container::show () |
85 |
{ |
86 |
world.add (orig, this); |
87 |
|
88 |
for (iterator i = end (); i-- != begin (); ) |
89 |
(*i)->show (); |
90 |
} |
91 |
|
92 |
void entity_container::draw (draw_context &ctx) |
93 |
{ |
94 |
for (iterator i = end (); i-- != begin (); ) |
95 |
(*i)->display (ctx); |
96 |
} |
97 |
|
98 |
entity_container::~entity_container () |
99 |
{ |
100 |
hide (); |
101 |
|
102 |
for (iterator i = end (); i-- != begin (); ) |
103 |
delete *i; |
104 |
|
105 |
clear (); |
106 |
} |
107 |
|
108 |
///////////////////////////////////////////////////////////////////////////// |
109 |
|
110 |
template class entity_opengl1d<GL_POINTS>; |
111 |
template class entity_opengl1d<GL_LINES>; |
112 |
template class entity_opengl1d<GL_LINE_STRIP>; |
113 |
template class entity_opengl1d<GL_LINE_LOOP>; |
114 |
template class entity_opengl2d<GL_TRIANGLES>; |
115 |
template class entity_opengl2d<GL_TRIANGLE_STRIP>; |
116 |
template class entity_opengl2d<GL_TRIANGLE_FAN>; |
117 |
template class entity_opengl2d<GL_QUADS>; |
118 |
template class entity_opengl2d<GL_QUAD_STRIP>; |
119 |
template class entity_opengl2d<GL_POLYGON>; |
120 |
|
121 |
template<GLenum type> |
122 |
void entity_opengl1d<type>::update_bbox () |
123 |
{ |
124 |
bbox.reset (); |
125 |
|
126 |
for (vector<vertex1d>::iterator i = end (); i-- != begin (); ) |
127 |
bbox.add (i->p); |
128 |
|
129 |
entity_base::update_bbox (); |
130 |
} |
131 |
|
132 |
template<GLenum type> |
133 |
void entity_opengl2d<type>::update_bbox () |
134 |
{ |
135 |
for (vector<vertex2d>::iterator i = end (); i-- != begin (); ) |
136 |
bbox.add (i->p); |
137 |
|
138 |
entity_base::update_bbox (); |
139 |
} |
140 |
|
141 |
template<GLenum type> |
142 |
void entity_opengl1d<type>::draw (draw_context &ctx) |
143 |
{ |
144 |
glBegin (type); |
145 |
|
146 |
for (iterator i = begin (); i < end (); ++i) |
147 |
{ |
148 |
if (ctx.mode == draw_context::LIGHTED) |
149 |
glColor3fv ((GLfloat *)&i->c); |
150 |
|
151 |
glVertex3fv ((GLfloat *)&i->p); |
152 |
} |
153 |
|
154 |
glEnd (); |
155 |
} |
156 |
|
157 |
template<GLenum type> |
158 |
void entity_opengl2d<type>::draw (draw_context &ctx) |
159 |
{ |
160 |
if (!list) |
161 |
{ |
162 |
list = glGenLists (1); |
163 |
glNewList (list, GL_COMPILE); |
164 |
|
165 |
glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse); |
166 |
glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular); |
167 |
glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission); |
168 |
glMaterialf (GL_FRONT, GL_SHININESS, m.shininess); |
169 |
|
170 |
#if 0 |
171 |
glBegin (type); |
172 |
|
173 |
for (iterator i = begin (); i < end (); ++i) |
174 |
{ |
175 |
glTexCoord2fv ((GLfloat *)&i->t); |
176 |
glNormal3fv ((GLfloat *)&i->n); |
177 |
glVertex3fv ((GLfloat *)&i->p); |
178 |
} |
179 |
|
180 |
glEnd (); |
181 |
#else |
182 |
glEnableClientState (GL_VERTEX_ARRAY); |
183 |
glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->p); |
184 |
glEnableClientState (GL_NORMAL_ARRAY); |
185 |
glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->n); |
186 |
glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
187 |
glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->t); |
188 |
|
189 |
glDrawArrays (type, 0, size ()); |
190 |
#endif |
191 |
glEndList (); |
192 |
|
193 |
clear (); |
194 |
} |
195 |
|
196 |
glCallList (list); |
197 |
} |
198 |
|
199 |
|
200 |
|