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