… | |
… | |
301 | { |
301 | { |
302 | tex2 t; // texture |
302 | tex2 t; // texture |
303 | vec3 n; // normal |
303 | vec3 n; // normal |
304 | point p; // vertex |
304 | point p; // vertex |
305 | |
305 | |
306 | vertex_t2f_n3f_v3f () { }; |
306 | vertex_t2f_n3f_v3f () { } |
307 | vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { }; |
307 | vertex_t2f_n3f_v3f (point p, vec3 n, tex2 t = tex2()) : p(p), n(n), t(t) { } |
308 | |
308 | |
309 | GLenum gl_format () const { return GL_T2F_N3F_V3F; } |
309 | GLenum gl_format () const { return GL_T2F_N3F_V3F; } |
|
|
310 | }; |
|
|
311 | |
|
|
312 | struct index_ushort |
|
|
313 | { |
|
|
314 | GLushort i; |
|
|
315 | |
|
|
316 | index_ushort () { } |
|
|
317 | index_ushort (GLushort i) : i(i) { } |
|
|
318 | |
|
|
319 | GLenum gl_format () const { return GL_UNSIGNED_SHORT; } |
310 | }; |
320 | }; |
311 | |
321 | |
312 | struct matrix |
322 | struct matrix |
313 | { |
323 | { |
314 | GLfloat data[4][4]; |
324 | GLfloat data[4][4]; |
… | |
… | |
332 | }; |
342 | }; |
333 | |
343 | |
334 | const matrix operator *(const matrix &a, const matrix &b); |
344 | const matrix operator *(const matrix &a, const matrix &b); |
335 | const vec3 operator *(const matrix &a, const vec3 &v); |
345 | const vec3 operator *(const matrix &a, const vec3 &v); |
336 | |
346 | |
|
|
347 | template<GLenum target> |
337 | struct vertex_buffer_object { |
348 | struct vertex_buffer_object_base { |
338 | GLuint buffer; |
349 | GLuint buffer; |
339 | GLenum format; |
350 | GLenum format; |
340 | |
351 | |
341 | void alloc () |
352 | bool alloc () |
342 | { |
353 | { |
343 | if (!buffer) |
354 | if (buffer) |
|
|
355 | return false; |
|
|
356 | |
344 | glGenBuffersARB (1, &buffer); |
357 | glGenBuffersARB (1, &buffer); |
|
|
358 | return true; |
345 | } |
359 | } |
346 | |
360 | |
347 | void bind (GLenum target = GL_ARRAY_BUFFER_ARB) |
361 | void bind () |
348 | { |
362 | { |
349 | glBindBufferARB (target, buffer); |
363 | glBindBufferARB (target, buffer); |
|
|
364 | } |
|
|
365 | |
|
|
366 | template<class data> |
|
|
367 | void set (const data *d, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB) |
|
|
368 | { |
|
|
369 | alloc (); |
|
|
370 | format = d->gl_format (); |
|
|
371 | glBindBufferARB (target, buffer); |
|
|
372 | glBufferDataARB (target, count * sizeof (data), d, usage); |
|
|
373 | } |
|
|
374 | |
|
|
375 | template<class data> |
|
|
376 | void set (const vector<data> &d, GLenum usage = GL_STATIC_DRAW_ARB) |
|
|
377 | { |
|
|
378 | set (&d[0], d.size (), usage); |
|
|
379 | } |
|
|
380 | |
|
|
381 | vertex_buffer_object_base () |
|
|
382 | : buffer(0) |
|
|
383 | { |
|
|
384 | } |
|
|
385 | |
|
|
386 | ~vertex_buffer_object_base () |
|
|
387 | { |
|
|
388 | if (buffer) |
|
|
389 | glDeleteBuffersARB (1, &buffer); |
|
|
390 | } |
|
|
391 | |
|
|
392 | operator GLint () |
|
|
393 | { |
|
|
394 | return buffer; |
|
|
395 | } |
|
|
396 | }; |
|
|
397 | |
|
|
398 | struct vertex_buffer_object : vertex_buffer_object_base<GL_ARRAY_BUFFER_ARB> { |
|
|
399 | void bind () |
|
|
400 | { |
|
|
401 | vertex_buffer_object_base<GL_ARRAY_BUFFER_ARB>::bind (); |
350 | glInterleavedArrays (format, 0, 0); |
402 | glInterleavedArrays (format, 0, 0); |
351 | } |
403 | } |
352 | |
404 | |
353 | void draw (GLenum mode, GLint first, GLsizei count) |
405 | void draw (GLenum mode, GLint first, GLsizei count) |
354 | { |
406 | { |
355 | bind (); |
407 | bind (); |
356 | glDrawArrays (mode, first, count); |
408 | glDrawArrays (mode, first, count); |
357 | } |
409 | } |
|
|
410 | }; |
358 | |
411 | |
359 | template<class vertex> |
412 | struct index_buffer_object : vertex_buffer_object_base<GL_ELEMENT_ARRAY_BUFFER_ARB> { |
360 | void set (const vertex *v, GLsizei count, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB) |
413 | void draw (GLenum mode, GLint first, GLsizei count) |
361 | { |
|
|
362 | alloc (); |
|
|
363 | format = v->gl_format (); |
|
|
364 | glBindBufferARB (target, buffer); |
|
|
365 | glBufferDataARB (target, count * sizeof (vertex), v, usage); |
|
|
366 | } |
|
|
367 | |
|
|
368 | template<class vertex> |
|
|
369 | void set (const vector<vertex> &v, GLenum usage = GL_STATIC_DRAW_ARB, GLenum target = GL_ARRAY_BUFFER_ARB) |
|
|
370 | { |
|
|
371 | set (&v[0], v.size (), usage, target); |
|
|
372 | } |
|
|
373 | |
|
|
374 | vertex_buffer_object () |
|
|
375 | : buffer(0) |
|
|
376 | { |
|
|
377 | } |
|
|
378 | |
414 | { |
379 | ~vertex_buffer_object () |
415 | bind (); |
|
|
416 | glDrawElements (mode, count, format, (GLushort*)0 + first); // only SHORT supported :( // first //D//TODO |
380 | { |
417 | } |
381 | glDeleteBuffersARB (1, &buffer); |
|
|
382 | } |
|
|
383 | |
|
|
384 | operator GLint () |
|
|
385 | { |
|
|
386 | return buffer; |
|
|
387 | } |
|
|
388 | }; |
418 | }; |
389 | |
419 | |
390 | void draw_bbox (vertex_buffer_object &vb, const sector &a, const sector &b); |
420 | void draw_bbox (const sector &a, const sector &b); |
391 | } |
421 | } |
392 | |
422 | |
393 | GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord); |
423 | GLuint SDL_GL_LoadTexture (SDL_Surface *surface, GLfloat *texcoord); |
394 | |
424 | |
395 | #endif |
425 | #endif |