1 | #include <algorithm> |
1 | #include <algorithm> |
|
|
2 | #include <cmath> |
2 | |
3 | |
3 | using namespace std; |
4 | using namespace std; |
4 | |
5 | |
5 | #include "entity.h" |
6 | #include "entity.h" |
|
|
7 | |
|
|
8 | const vec3 cross (const vec3 &a, const vec3 &b) |
|
|
9 | { |
|
|
10 | return vec3 ( |
|
|
11 | a.y * b.z - a.z * b.y, |
|
|
12 | a.z * b.x - a.x * b.z, |
|
|
13 | a.x * b.y - a.y * b.x |
|
|
14 | ); |
|
|
15 | } |
|
|
16 | |
|
|
17 | GLfloat dot (const vec3 &a, const vec3 &b) |
|
|
18 | { |
|
|
19 | return a.x * b.x + a.y * b.y + a.z * b.z; |
|
|
20 | } |
6 | |
21 | |
7 | void box::add (const box &o) |
22 | void box::add (const box &o) |
8 | { |
23 | { |
9 | a.x = min (a.x, o.a.x); |
24 | a.x = min (a.x, o.a.x); |
10 | a.y = min (a.y, o.a.y); |
25 | a.y = min (a.y, o.a.y); |
… | |
… | |
49 | delete e; |
64 | delete e; |
50 | } |
65 | } |
51 | |
66 | |
52 | void entity::show (const sector &sec) |
67 | void entity::show (const sector &sec) |
53 | { |
68 | { |
|
|
69 | show (); |
|
|
70 | } |
|
|
71 | |
|
|
72 | void entity::show () |
|
|
73 | { |
54 | e->show (this->sec); |
74 | e->show (this->sec); |
55 | } |
75 | } |
56 | |
76 | |
57 | void entity::draw () |
77 | void entity::draw () |
58 | { |
78 | { |
|
|
79 | glPushMatrix (); |
59 | //TODO setup matrix etc. |
80 | //TODO setup matrix etc. |
60 | e->draw (); |
81 | e->draw (); |
|
|
82 | glPopMatrix (); |
61 | } |
83 | } |
62 | |
84 | |
63 | void entity_container::update_bbox () |
85 | void entity_container::update_bbox () |
64 | { |
86 | { |
65 | bbox.reset (); |
87 | bbox.reset (); |
… | |
… | |
91 | delete *i; |
113 | delete *i; |
92 | |
114 | |
93 | clear (); |
115 | clear (); |
94 | } |
116 | } |
95 | |
117 | |
|
|
118 | template entity_opengl1d<GL_POINTS>; |
|
|
119 | template entity_opengl1d<GL_LINES>; |
|
|
120 | template entity_opengl1d<GL_LINE_STRIP>; |
|
|
121 | template entity_opengl1d<GL_LINE_LOOP>; |
|
|
122 | template entity_opengl2d<GL_TRIANGLES>; |
|
|
123 | template entity_opengl2d<GL_TRIANGLE_STRIP>; |
|
|
124 | template entity_opengl2d<GL_TRIANGLE_FAN>; |
|
|
125 | template entity_opengl2d<GL_QUADS>; |
|
|
126 | template entity_opengl2d<GL_QUAD_STRIP>; |
|
|
127 | template entity_opengl2d<GL_POLYGON>; |
96 | |
128 | |
|
|
129 | template<GLenum type> |
|
|
130 | void entity_opengl1d<type>::update_bbox () |
|
|
131 | { |
|
|
132 | entity_base::update_bbox (); |
|
|
133 | } |
|
|
134 | |
|
|
135 | template<GLenum type> |
|
|
136 | void entity_opengl2d<type>::update_bbox () |
|
|
137 | { |
|
|
138 | entity_base::update_bbox (); |
|
|
139 | } |
|
|
140 | |
|
|
141 | template<GLenum type> |
|
|
142 | void entity_opengl1d<type>::draw () |
|
|
143 | { |
|
|
144 | glBegin (type); |
|
|
145 | |
|
|
146 | for (iterator i = begin (); i < end (); ++i) |
|
|
147 | { |
|
|
148 | glColor3fv ((GLfloat *)&i->c); |
|
|
149 | glVertex3fv ((GLfloat *)&i->p); |
|
|
150 | } |
|
|
151 | |
|
|
152 | glEnd (); |
|
|
153 | } |
|
|
154 | |
|
|
155 | template<GLenum type> |
|
|
156 | void entity_opengl2d<type>::draw () |
|
|
157 | { |
|
|
158 | glBegin (type); |
|
|
159 | |
|
|
160 | for (iterator i = begin (); i < end (); ++i) |
|
|
161 | { |
|
|
162 | glColor3fv ((GLfloat *)&i->c); |
|
|
163 | glNormal3fv ((GLfloat *)&i->n); |
|
|
164 | glTexCoord2fv ((GLfloat *)&i->t); |
|
|
165 | glVertex3fv ((GLfloat *)&i->p); |
|
|
166 | } |
|
|
167 | |
|
|
168 | glEnd (); |
|
|
169 | } |
|
|
170 | |
|
|
171 | template<GLenum type> |
|
|
172 | void entity_opengl1d<type>::show (const sector &sec) |
|
|
173 | { |
|
|
174 | } |
|
|
175 | |
|
|
176 | template<GLenum type> |
|
|
177 | void entity_opengl2d<type>::show (const sector &sec) |
|
|
178 | { |
|
|
179 | } |
|
|
180 | |
|
|
181 | void view::draw (const draw_context &c) |
|
|
182 | { |
|
|
183 | glViewport (0, 0, w, h); |
|
|
184 | |
|
|
185 | glMatrixMode (GL_PROJECTION); |
|
|
186 | glLoadIdentity (); |
|
|
187 | |
|
|
188 | double aspect = (double)w/h; |
|
|
189 | double zNear = 5; |
|
|
190 | double zFar = 1000.; |
|
|
191 | |
|
|
192 | { |
|
|
193 | GLdouble xmin, xmax, ymin, ymax; |
|
|
194 | |
|
|
195 | ymax = zNear * tan (fov * (M_PI / 360.0)); |
|
|
196 | ymin = -ymax; |
|
|
197 | xmin = ymin * aspect; |
|
|
198 | xmax = ymax * aspect; |
|
|
199 | |
|
|
200 | glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); |
|
|
201 | } |
|
|
202 | |
|
|
203 | vec3 rz = -d; |
|
|
204 | vec3 rx = cross (u, rz); |
|
|
205 | vec3 ry = cross (rz, rx); |
|
|
206 | |
|
|
207 | GLfloat m[4][4]; |
|
|
208 | m[0][0] = rx.x; m[0][1] = rx.y; m[0][2] = rx.z; m[0][3] = 0; |
|
|
209 | m[1][0] = ry.x; m[1][1] = ry.y; m[1][2] = ry.z; m[1][3] = 0; |
|
|
210 | m[2][0] = rz.x; m[2][1] = rz.y; m[2][2] = rz.z; m[2][3] = 0; |
|
|
211 | m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1; |
|
|
212 | glMultMatrixf ((GLfloat *)m); |
|
|
213 | |
|
|
214 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
215 | |
|
|
216 | glMatrixMode (GL_MODELVIEW); |
|
|
217 | glLoadIdentity (); |
|
|
218 | |
|
|
219 | entity *e = new entity; |
|
|
220 | e->sec.x = e->sec.y = e->sec.z = 0; |
|
|
221 | |
|
|
222 | entity_triangles *tri = new entity_triangles; |
|
|
223 | tri->push_back (vertex2d (colour (1, 0, 0), point (-9, 0, -9), vec3 (0, 0, 1))); |
|
|
224 | tri->push_back (vertex2d (colour (0, 1, 1), point ( 0, 9, -9), vec3 (0, 0, 1))); |
|
|
225 | tri->push_back (vertex2d (colour (1, 0, 1), point ( 9, 0, -9), vec3 (0, 0, 1))); |
|
|
226 | e->set (tri); |
|
|
227 | |
|
|
228 | e->show (); |
|
|
229 | e->draw (); |
|
|
230 | |
|
|
231 | delete e; |
|
|
232 | } |
|
|
233 | |
|
|
234 | |