1 |
root |
1.1 |
#ifndef UTIL_H |
2 |
|
|
#define UTIL_H |
3 |
|
|
|
4 |
root |
1.7 |
#include <cmath> |
5 |
root |
1.27 |
#include <cfloat> |
6 |
root |
1.39 |
#include <cstdlib> |
7 |
root |
1.1 |
#include <vector> |
8 |
root |
1.22 |
#include <string> |
9 |
root |
1.1 |
|
10 |
root |
1.37 |
#include "opengl.h" |
11 |
root |
1.18 |
|
12 |
root |
1.37 |
#include <SDL/SDL_image.h> |
13 |
root |
1.30 |
|
14 |
root |
1.1 |
using namespace std; |
15 |
root |
1.18 |
|
16 |
root |
1.36 |
typedef long long soffs; // 32 bit |
17 |
|
|
typedef unsigned long long uoffs; |
18 |
root |
1.39 |
#define OFFS_BITS 63 |
19 |
root |
1.36 |
#define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2)) |
20 |
|
|
#define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2)) |
21 |
|
|
#define MAXEXTENT (1ULL << (OFFS_BITS - 1)) |
22 |
root |
1.1 |
|
23 |
root |
1.39 |
#define ABS(n) ((n) < 0 ? -(n) : (n)) |
24 |
|
|
|
25 |
root |
1.37 |
struct sector |
26 |
|
|
{ |
27 |
root |
1.32 |
soffs x, y, z; |
28 |
|
|
|
29 |
|
|
sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { }; |
30 |
|
|
sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { }; |
31 |
|
|
|
32 |
|
|
void offset (int subindex, uoffs extent) |
33 |
|
|
{ |
34 |
|
|
if (subindex & 1) x += extent; |
35 |
|
|
if (subindex & 2) y += extent; |
36 |
|
|
if (subindex & 4) z += extent; |
37 |
|
|
} |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
inline sector operator +(const sector &a, const sector &b) |
41 |
|
|
{ |
42 |
|
|
return sector (a.x + b.x, a.y + b.y, a.z + b.z); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
inline sector operator -(const sector &a, const sector &b) |
46 |
|
|
{ |
47 |
|
|
return sector (a.x - b.x, a.y - b.y, a.z - b.z); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
inline sector operator /(const sector &a, soffs b) |
51 |
|
|
{ |
52 |
|
|
return sector (a.x / b, a.y / b, a.z / b); |
53 |
|
|
} |
54 |
|
|
|
55 |
root |
1.40 |
inline sector operator >>(const sector &a, unsigned int b) |
56 |
|
|
{ |
57 |
|
|
return sector (a.x >> b, a.y >> b, a.z >> b); |
58 |
|
|
} |
59 |
|
|
|
60 |
root |
1.32 |
inline bool operator <=(const sector &a, const sector &b) |
61 |
|
|
{ |
62 |
|
|
return a.x <= b.x && a.y <= b.y && a.z <= b.z; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
inline soffs max (const sector &a) |
66 |
|
|
{ |
67 |
|
|
return max (a.x, max (a.y, a.z)); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
inline sector translate (const sector &p, const sector &src, const sector &dst) |
71 |
|
|
{ |
72 |
|
|
return p + (dst - src); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
inline sector abs (const sector &s) |
76 |
|
|
{ |
77 |
root |
1.39 |
return sector (ABS (s.x), ABS (s.y), ABS (s.z)); |
78 |
root |
1.32 |
} |
79 |
root |
1.1 |
|
80 |
root |
1.51 |
struct vec2 |
81 |
|
|
{ |
82 |
|
|
GLfloat x, y; |
83 |
|
|
vec2 () { }; |
84 |
|
|
vec2 (GLfloat s) : x(s), y(s) { }; |
85 |
|
|
vec2 (GLfloat x, GLfloat y) : x(x), y(y) { }; |
86 |
|
|
}; |
87 |
|
|
|
88 |
root |
1.57 |
inline const vec2 operator +(const vec2 &a, const vec2 &b) |
89 |
|
|
{ |
90 |
|
|
return vec2 (a.x + b.x, a.y + b.y); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
inline const vec2 operator -(const vec2 &a, const vec2 &b) |
94 |
|
|
{ |
95 |
|
|
return vec2 (a.x - b.x, a.y - b.y); |
96 |
|
|
} |
97 |
|
|
|
98 |
root |
1.37 |
struct vec3 |
99 |
|
|
{ |
100 |
root |
1.1 |
GLfloat x, y, z; |
101 |
|
|
vec3 () { }; |
102 |
root |
1.32 |
vec3 (GLfloat s) : x(s), y(s), z(s) { }; |
103 |
root |
1.1 |
vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { }; |
104 |
root |
1.32 |
vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { }; |
105 |
root |
1.1 |
|
106 |
|
|
const vec3 operator -() const |
107 |
|
|
{ return vec3 (-x, -y, -z); } |
108 |
|
|
}; |
109 |
|
|
|
110 |
|
|
const vec3 normalize (const vec3 &v); |
111 |
|
|
const vec3 cross (const vec3 &a, const vec3 &b); |
112 |
root |
1.6 |
|
113 |
|
|
inline const vec3 operator *(const vec3 &a, GLfloat s) |
114 |
|
|
{ |
115 |
|
|
return vec3 (a.x * s, a.y * s, a.z * s); |
116 |
|
|
} |
117 |
|
|
|
118 |
root |
1.27 |
inline const vec3 operator +(const vec3 &a, const vec3 &b) |
119 |
|
|
{ |
120 |
|
|
return vec3 (a.x + b.x, a.y + b.y, a.z + b.z); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
inline const vec3 operator -(const vec3 &a, const vec3 &b) |
124 |
|
|
{ |
125 |
|
|
return vec3 (a.x - b.x, a.y - b.y, a.z - b.z); |
126 |
|
|
} |
127 |
|
|
|
128 |
root |
1.6 |
inline GLfloat dot (const vec3 &a, const vec3 &b) |
129 |
|
|
{ |
130 |
|
|
return a.x * b.x + a.y * b.y + a.z * b.z; |
131 |
|
|
} |
132 |
|
|
|
133 |
root |
1.45 |
// squared length |
134 |
|
|
inline const GLfloat length2 (const vec3 &v) |
135 |
root |
1.6 |
{ |
136 |
root |
1.45 |
return dot (v, v); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
inline const GLfloat length (const vec3 &v) |
140 |
|
|
{ |
141 |
|
|
return sqrtf (length2 (v)); |
142 |
root |
1.6 |
} |
143 |
root |
1.1 |
|
144 |
root |
1.10 |
typedef vec3 point; |
145 |
root |
1.51 |
|
146 |
|
|
struct vec4 |
147 |
|
|
{ |
148 |
|
|
GLfloat x, y, z, w; |
149 |
|
|
vec4 () { }; |
150 |
|
|
vec4 (GLfloat s) : x(s), y(s), z(s), w(s) { }; |
151 |
|
|
vec4 (GLfloat x, GLfloat y, GLfloat z, GLfloat w) : x(x), y(y), z(z), w(w) { }; |
152 |
root |
1.53 |
vec4 (const vec3 &v, GLfloat w = 1.F) : x(v.x), y(v.y), z(v.z), w(w) { }; |
153 |
root |
1.51 |
}; |
154 |
root |
1.10 |
|
155 |
|
|
// a generic plane |
156 |
root |
1.37 |
struct plane |
157 |
|
|
{ |
158 |
root |
1.10 |
vec3 n; |
159 |
|
|
GLfloat d; |
160 |
|
|
|
161 |
|
|
plane () { }; |
162 |
|
|
plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d); |
163 |
|
|
}; |
164 |
root |
1.45 |
|
165 |
|
|
inline GLfloat distance (const plane &a, const point &p) |
166 |
|
|
{ |
167 |
|
|
return dot (a.n, p) + a.d; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
struct sphere { |
171 |
|
|
point p; |
172 |
|
|
GLfloat r; |
173 |
|
|
|
174 |
|
|
sphere () { }; |
175 |
|
|
sphere (const point &p, GLfloat r) : p(p), r(r) { }; |
176 |
|
|
}; |
177 |
|
|
|
178 |
|
|
inline bool overlap (const sphere &a, const sphere &b) |
179 |
|
|
{ |
180 |
|
|
GLfloat d = a.r + b.r; |
181 |
|
|
|
182 |
|
|
return length2 (a.p - b.p) <= d * d; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
struct cone { |
186 |
|
|
point p; |
187 |
|
|
vec3 a; // axis |
188 |
root |
1.47 |
GLfloat f, fs1, fc2; // angle |
189 |
root |
1.45 |
|
190 |
|
|
cone () { }; |
191 |
|
|
cone (const point &p, const vec3 &a, GLfloat f) |
192 |
|
|
: p(p), a(a), f(f) |
193 |
|
|
{ |
194 |
root |
1.46 |
fs1 = 1.F / sinf (f); |
195 |
root |
1.47 |
fc2 = cosf (f); fc2 *= fc2; |
196 |
root |
1.45 |
}; |
197 |
|
|
}; |
198 |
|
|
|
199 |
|
|
inline bool overlap (const cone &c, const sphere &s) |
200 |
|
|
{ |
201 |
root |
1.46 |
vec3 d = s.p - c.p + c.a * (s.r * c.fs1); |
202 |
root |
1.47 |
GLfloat dd = dot (c.a, d); |
203 |
|
|
return dd > 0.F && dd * dd >= length2 (d) * c.fc2; |
204 |
root |
1.45 |
} |
205 |
root |
1.10 |
|
206 |
|
|
void renormalize (sector &s, point &p); |
207 |
|
|
|
208 |
root |
1.37 |
struct colour |
209 |
|
|
{ |
210 |
root |
1.40 |
GLubyte r, g, b, a; |
211 |
|
|
colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) |
212 |
|
|
: r(GLubyte (r * 255.F + .5F)) |
213 |
|
|
, g(GLubyte (g * 255.F + .5F)) |
214 |
|
|
, b(GLubyte (b * 255.F + .5F)) |
215 |
|
|
, a(GLubyte (a * 255.F + .5F)) |
216 |
|
|
{ |
217 |
|
|
} |
218 |
root |
1.10 |
}; |
219 |
root |
1.5 |
|
220 |
root |
1.40 |
struct tex2 |
221 |
root |
1.37 |
{ |
222 |
root |
1.1 |
GLfloat s, t; |
223 |
root |
1.40 |
tex2 () { }; |
224 |
|
|
tex2 (GLfloat s, GLfloat t) : s(s), t(t) { }; |
225 |
root |
1.1 |
}; |
226 |
root |
1.57 |
|
227 |
|
|
inline const tex2 operator +(const tex2 &a, const tex2 &b) |
228 |
|
|
{ |
229 |
|
|
return tex2 (a.s + b.s, a.t + b.t); |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
inline const tex2 operator -(const tex2 &a, const tex2 &b) |
233 |
|
|
{ |
234 |
|
|
return tex2 (a.s - b.s, a.t - b.t); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
inline const tex2 operator *(const tex2 &a, GLfloat s) |
238 |
|
|
{ |
239 |
|
|
return tex2 (a.s * s, a.t * s); |
240 |
|
|
} |
241 |
root |
1.1 |
|
242 |
root |
1.52 |
template<typename vectype> class vector_traits { }; |
243 |
|
|
|
244 |
|
|
template<> |
245 |
|
|
struct vector_traits<vec2> |
246 |
|
|
{ |
247 |
|
|
typedef GLfloat basetype; |
248 |
|
|
static const int dimension () { return 2; } |
249 |
|
|
}; |
250 |
|
|
|
251 |
|
|
template<> |
252 |
|
|
struct vector_traits<vec3> |
253 |
|
|
{ |
254 |
|
|
typedef GLfloat basetype; |
255 |
|
|
static const int dimension () { return 3; } |
256 |
|
|
}; |
257 |
|
|
|
258 |
|
|
template<> |
259 |
|
|
struct vector_traits<vec4> |
260 |
|
|
{ |
261 |
|
|
typedef GLfloat basetype; |
262 |
|
|
static const int dimension () { return 4; } |
263 |
|
|
}; |
264 |
|
|
|
265 |
|
|
template<> |
266 |
|
|
struct vector_traits<tex2> |
267 |
|
|
{ |
268 |
|
|
typedef GLfloat basetype; |
269 |
|
|
static const int dimension () { return 2; } |
270 |
|
|
}; |
271 |
|
|
|
272 |
|
|
template<> |
273 |
|
|
struct vector_traits<colour> |
274 |
|
|
{ |
275 |
|
|
typedef GLubyte basetype; |
276 |
|
|
static const int dimension () { return 4; } |
277 |
|
|
}; |
278 |
|
|
|
279 |
root |
1.37 |
struct box |
280 |
|
|
{ |
281 |
root |
1.27 |
point a, b; |
282 |
root |
1.8 |
|
283 |
|
|
box() { }; |
284 |
root |
1.1 |
|
285 |
|
|
void reset () |
286 |
|
|
{ |
287 |
root |
1.27 |
a = point ( FLT_MAX, FLT_MAX, FLT_MAX); |
288 |
|
|
b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX); |
289 |
root |
1.1 |
} |
290 |
|
|
|
291 |
|
|
void add (const box &o); |
292 |
|
|
void add (const point &p); |
293 |
|
|
}; |
294 |
root |
1.5 |
|
295 |
root |
1.27 |
struct entity; |
296 |
|
|
struct geometry; |
297 |
root |
1.20 |
struct view; |
298 |
root |
1.32 |
struct octant; |
299 |
root |
1.7 |
|
300 |
root |
1.37 |
extern struct timer |
301 |
|
|
{ |
302 |
root |
1.7 |
static double now; |
303 |
|
|
static double diff; |
304 |
root |
1.44 |
static double fps; |
305 |
root |
1.7 |
|
306 |
|
|
static void frame (); |
307 |
|
|
timer (); |
308 |
|
|
} timer; |
309 |
root |
1.11 |
|
310 |
root |
1.13 |
/* |
311 |
root |
1.11 |
#define MAX_EVENT_TYPES 10 |
312 |
|
|
enum event_type { TIMER_EV }; |
313 |
root |
1.37 |
struct event |
314 |
|
|
{ |
315 |
root |
1.11 |
event_type type; |
316 |
|
|
}; |
317 |
|
|
|
318 |
|
|
typedef callback1<void, event&> event_cb; |
319 |
|
|
|
320 |
root |
1.37 |
class skedjuhlar |
321 |
|
|
{ |
322 |
root |
1.11 |
public: |
323 |
|
|
// only 10 types for now |
324 |
|
|
private: |
325 |
|
|
vector <list<event_cb> > event_lists; |
326 |
|
|
|
327 |
|
|
public: |
328 |
|
|
skedjuhlar () { |
329 |
|
|
event_lists.resize (MAX_EVENT_TYPES, list<event_cb>()); |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
void register_event_cb (const event_type &t, const event_cb &e) { |
333 |
|
|
event_lists[t].push_back (e); |
334 |
|
|
}; |
335 |
|
|
void send_event (event &e) { |
336 |
|
|
list<event_cb> &l = event_lists[e.type]; |
337 |
|
|
for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) { |
338 |
|
|
(*it)(e); |
339 |
|
|
} |
340 |
|
|
}; |
341 |
|
|
void check_events () { |
342 |
|
|
while (!events.empty ()) { |
343 |
|
|
event &e = events.pop_front (); |
344 |
|
|
list<event_cb> &l = event_lists[e->name]; |
345 |
|
|
for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) { |
346 |
|
|
(*it)(e); |
347 |
|
|
} |
348 |
|
|
delete e; // ugly slow? hai hai..... 183G |
349 |
|
|
} |
350 |
|
|
} |
351 |
|
|
}; |
352 |
|
|
|
353 |
|
|
extern skedjuhlar main_scheduler; |
354 |
root |
1.13 |
*/ |
355 |
root |
1.22 |
|
356 |
root |
1.37 |
namespace gl |
357 |
|
|
{ |
358 |
root |
1.41 |
#ifdef DEBUG |
359 |
|
|
void errchk (const char *name, const char *args, const char *file, int line); |
360 |
|
|
#endif |
361 |
|
|
|
362 |
root |
1.40 |
struct vertex_v3f |
363 |
|
|
{ |
364 |
root |
1.56 |
point v; // vertex |
365 |
root |
1.40 |
|
366 |
|
|
vertex_v3f () { }; |
367 |
root |
1.56 |
vertex_v3f (point v) : v(v) { }; |
368 |
root |
1.40 |
}; |
369 |
|
|
|
370 |
|
|
struct vertex_t2f_n3f_v3f |
371 |
|
|
{ |
372 |
|
|
tex2 t; // texture |
373 |
|
|
vec3 n; // normal |
374 |
root |
1.56 |
point v; // vertex |
375 |
root |
1.40 |
|
376 |
root |
1.48 |
vertex_t2f_n3f_v3f () { } |
377 |
root |
1.56 |
vertex_t2f_n3f_v3f (point v, vec3 n, tex2 t = tex2()) : v(v), n(n), t(t) { } |
378 |
|
|
}; |
379 |
root |
1.40 |
|
380 |
root |
1.56 |
template<typename unused> class type_traits { }; |
381 |
|
|
|
382 |
|
|
template<> struct type_traits<GLfloat > { static const GLenum glenum () { return GL_FLOAT; } }; |
383 |
|
|
template<> struct type_traits<GLdouble> { static const GLenum glenum () { return GL_DOUBLE; } }; |
384 |
|
|
template<> struct type_traits<GLint > { static const GLenum glenum () { return GL_INT; } }; |
385 |
|
|
template<> struct type_traits<GLuint > { static const GLenum glenum () { return GL_UNSIGNED_INT; } }; |
386 |
|
|
template<> struct type_traits<GLshort > { static const GLenum glenum () { return GL_SHORT; } }; |
387 |
|
|
template<> struct type_traits<GLushort> { static const GLenum glenum () { return GL_UNSIGNED_SHORT; } }; |
388 |
|
|
template<> struct type_traits<GLbyte > { static const GLenum glenum () { return GL_BYTE; } }; |
389 |
|
|
template<> struct type_traits<GLubyte > { static const GLenum glenum () { return GL_UNSIGNED_BYTE; } }; |
390 |
|
|
template<> struct type_traits<vertex_v3f > { static const GLenum glenum () { return GL_V3F; } }; |
391 |
|
|
template<> struct type_traits<vertex_t2f_n3f_v3f> { static const GLenum glenum () { return GL_T2F_N3F_V3F; } }; |
392 |
|
|
|
393 |
|
|
template<GLenum unused> class enum_traits { }; |
394 |
|
|
|
395 |
|
|
template<> struct enum_traits<GL_FLOAT > { typedef GLfloat type; }; |
396 |
|
|
template<> struct enum_traits<GL_DOUBLE > { typedef GLdouble type; }; |
397 |
|
|
template<> struct enum_traits<GL_INT > { typedef GLint type; }; |
398 |
|
|
template<> struct enum_traits<GL_UNSIGNED_INT > { typedef GLuint type; }; |
399 |
|
|
template<> struct enum_traits<GL_SHORT > { typedef GLshort type; }; |
400 |
|
|
template<> struct enum_traits<GL_UNSIGNED_SHORT> { typedef GLushort type; }; |
401 |
|
|
template<> struct enum_traits<GL_BYTE > { typedef GLbyte type; }; |
402 |
|
|
template<> struct enum_traits<GL_UNSIGNED_BYTE > { typedef GLubyte type; }; |
403 |
|
|
template<> struct enum_traits<GL_V3F > { typedef vertex_v3f type; }; |
404 |
|
|
template<> struct enum_traits<GL_T2F_N3F_V3F > { typedef vertex_t2f_n3f_v3f type; }; |
405 |
root |
1.34 |
|
406 |
root |
1.56 |
// deprecated: |
407 |
root |
1.49 |
GLsizei format_stride (GLenum format); |
408 |
|
|
GLsizei format_offset_p (GLenum format); |
409 |
|
|
GLsizei format_offset_n (GLenum format); |
410 |
|
|
GLsizei format_offset_t (GLenum format); |
411 |
|
|
|
412 |
root |
1.41 |
struct matrix |
413 |
|
|
{ |
414 |
|
|
GLfloat data[4][4]; |
415 |
|
|
|
416 |
|
|
const GLfloat operator ()(int i, int j) const { return data[j][i]; }; |
417 |
|
|
GLfloat &operator ()(int i, int j) { return data[j][i]; }; |
418 |
|
|
|
419 |
|
|
operator GLfloat *() { return &data[0][0]; } |
420 |
|
|
|
421 |
|
|
void diagonal (GLfloat v); |
422 |
|
|
void clear () { diagonal (0.F); }; |
423 |
|
|
void identity () { diagonal (1.F); }; |
424 |
|
|
|
425 |
|
|
void print (); // ugly |
426 |
|
|
|
427 |
root |
1.54 |
static matrix translation (const vec3 &v); |
428 |
|
|
static matrix rotation (GLfloat degrees, const vec3 &axis); |
429 |
|
|
static matrix scaling (GLfloat sx, GLfloat sy, GLfloat sz, GLfloat w = 1.F); |
430 |
root |
1.41 |
|
431 |
|
|
matrix () { }; |
432 |
|
|
matrix (GLfloat diag) { diagonal (diag); }; |
433 |
|
|
}; |
434 |
|
|
|
435 |
root |
1.54 |
matrix operator *(const matrix &a, const matrix &b); |
436 |
|
|
vec3 operator *(const matrix &a, const vec3 &v); |
437 |
root |
1.40 |
|
438 |
root |
1.48 |
template<GLenum target> |
439 |
root |
1.56 |
struct vertex_buffer_object |
440 |
|
|
{ |
441 |
root |
1.40 |
GLuint buffer; |
442 |
|
|
GLenum format; |
443 |
root |
1.52 |
GLsizei count; |
444 |
root |
1.56 |
GLsizei size; |
445 |
root |
1.40 |
|
446 |
root |
1.48 |
bool alloc () |
447 |
root |
1.40 |
{ |
448 |
root |
1.48 |
if (buffer) |
449 |
|
|
return false; |
450 |
|
|
|
451 |
|
|
glGenBuffersARB (1, &buffer); |
452 |
|
|
return true; |
453 |
root |
1.40 |
} |
454 |
|
|
|
455 |
root |
1.48 |
void bind () |
456 |
root |
1.40 |
{ |
457 |
|
|
glBindBufferARB (target, buffer); |
458 |
|
|
} |
459 |
|
|
|
460 |
root |
1.56 |
template<class basetype> |
461 |
|
|
void set (const basetype *d, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB) |
462 |
root |
1.40 |
{ |
463 |
|
|
alloc (); |
464 |
root |
1.56 |
format = type_traits<basetype>::glenum (); |
465 |
root |
1.52 |
this->count = count; |
466 |
root |
1.56 |
size = sizeof (basetype); |
467 |
root |
1.55 |
vertex_buffer_object::bind (); |
468 |
root |
1.56 |
glBufferDataARB (target, count * this->size, d, usage); |
469 |
root |
1.40 |
} |
470 |
|
|
|
471 |
root |
1.48 |
template<class data> |
472 |
|
|
void set (const vector<data> &d, GLenum usage = GL_STATIC_DRAW_ARB) |
473 |
root |
1.40 |
{ |
474 |
root |
1.48 |
set (&d[0], d.size (), usage); |
475 |
root |
1.40 |
} |
476 |
|
|
|
477 |
root |
1.56 |
template<typename basetype> |
478 |
|
|
void set (GLsizei count, GLenum usage = GL_DYNAMIC_DRAW_ARB) |
479 |
root |
1.55 |
{ |
480 |
|
|
alloc (); |
481 |
root |
1.56 |
format = type_traits<basetype>::glenum (); |
482 |
|
|
this->count = count; |
483 |
|
|
size = sizeof (basetype); |
484 |
root |
1.55 |
vertex_buffer_object::bind (); |
485 |
root |
1.56 |
glBufferDataARB (target, this->count * size, 0, usage); |
486 |
root |
1.55 |
} |
487 |
|
|
|
488 |
|
|
void *map (GLenum access = GL_WRITE_ONLY) |
489 |
|
|
{ |
490 |
|
|
vertex_buffer_object::bind (); |
491 |
|
|
return glMapBufferARB (target, access); |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
void unmap () |
495 |
|
|
{ |
496 |
|
|
vertex_buffer_object::bind (); |
497 |
|
|
glUnmapBuffer (target); |
498 |
|
|
} |
499 |
|
|
|
500 |
root |
1.54 |
vertex_buffer_object () |
501 |
root |
1.40 |
: buffer(0) |
502 |
|
|
{ |
503 |
|
|
} |
504 |
|
|
|
505 |
root |
1.54 |
~vertex_buffer_object () |
506 |
root |
1.40 |
{ |
507 |
root |
1.48 |
if (buffer) |
508 |
|
|
glDeleteBuffersARB (1, &buffer); |
509 |
root |
1.40 |
} |
510 |
|
|
|
511 |
|
|
operator GLint () |
512 |
|
|
{ |
513 |
|
|
return buffer; |
514 |
|
|
} |
515 |
|
|
}; |
516 |
|
|
|
517 |
root |
1.56 |
struct vertex_buffer : vertex_buffer_object<GL_ARRAY_BUFFER_ARB> |
518 |
|
|
{ |
519 |
root |
1.48 |
void bind () |
520 |
|
|
{ |
521 |
root |
1.54 |
vertex_buffer_object<GL_ARRAY_BUFFER_ARB>::bind (); |
522 |
root |
1.56 |
glInterleavedArrays (format, 0, 0); |
523 |
root |
1.48 |
} |
524 |
|
|
|
525 |
|
|
void draw (GLenum mode, GLint first, GLsizei count) |
526 |
|
|
{ |
527 |
|
|
bind (); |
528 |
|
|
glDrawArrays (mode, first, count); |
529 |
|
|
} |
530 |
|
|
}; |
531 |
|
|
|
532 |
root |
1.56 |
struct index_buffer : vertex_buffer_object<GL_ELEMENT_ARRAY_BUFFER_ARB> |
533 |
|
|
{ |
534 |
root |
1.48 |
void draw (GLenum mode, GLint first, GLsizei count) |
535 |
|
|
{ |
536 |
|
|
bind (); |
537 |
root |
1.56 |
glDrawElements (mode, count, format, (GLvoid *)((char *)0 + first * size)); // only SHORT supported :( // first //D//TODO |
538 |
root |
1.48 |
} |
539 |
|
|
}; |
540 |
|
|
|
541 |
|
|
void draw_bbox (const sector &a, const sector &b); |
542 |
root |
1.49 |
|
543 |
root |
1.31 |
} |
544 |
|
|
|
545 |
root |
1.40 |
GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord); |
546 |
root |
1.27 |
|
547 |
root |
1.1 |
#endif |
548 |
|
|
|