0
|
1 /* ============================================================================
|
|
2 * Freetype GL - A C OpenGL Freetype engine
|
|
3 * Platform: Any
|
|
4 * WWW: http://code.google.com/p/freetype-gl/
|
|
5 * ----------------------------------------------------------------------------
|
|
6 * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions are met:
|
|
10 *
|
|
11 * 1. Redistributions of source code must retain the above copyright notice,
|
|
12 * this list of conditions and the following disclaimer.
|
|
13 *
|
|
14 * 2. Redistributions in binary form must reproduce the above copyright
|
|
15 * notice, this list of conditions and the following disclaimer in the
|
|
16 * documentation and/or other materials provided with the distribution.
|
|
17 *
|
|
18 * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
|
|
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
21 * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28 *
|
|
29 * The views and conclusions contained in the software and documentation are
|
|
30 * those of the authors and should not be interpreted as representing official
|
|
31 * policies, either expressed or implied, of Nicolas P. Rougier.
|
|
32 * ============================================================================
|
|
33 */
|
|
34 #include <assert.h>
|
|
35 #include <string.h>
|
|
36 #include <stdlib.h>
|
|
37 #include <stdio.h>
|
|
38 #include "vec234.h"
|
|
39 #include "platform.h"
|
|
40 #include "vertex-buffer.h"
|
|
41
|
|
42
|
|
43 /**
|
|
44 * Buffer status
|
|
45 */
|
|
46 #define CLEAN (0)
|
|
47 #define DIRTY (1)
|
|
48 #define FROZEN (2)
|
|
49
|
|
50
|
|
51 // ----------------------------------------------------------------------------
|
|
52 vertex_buffer_t *
|
|
53 vertex_buffer_new( const char *format )
|
|
54 {
|
|
55 size_t i, index = 0, stride = 0;
|
|
56 const char *start = 0, *end = 0;
|
|
57 GLchar *pointer = 0;
|
|
58
|
|
59 vertex_buffer_t *self = (vertex_buffer_t *) malloc (sizeof(vertex_buffer_t));
|
|
60 if( !self )
|
|
61 {
|
|
62 return NULL;
|
|
63 }
|
|
64
|
|
65 self->format = strdup( format );
|
|
66
|
|
67 for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i )
|
|
68 {
|
|
69 self->attributes[i] = 0;
|
|
70 }
|
|
71
|
|
72 start = format;
|
|
73 do
|
|
74 {
|
|
75 char *desc = 0;
|
|
76 vertex_attribute_t *attribute;
|
|
77 GLuint attribute_size = 0;
|
|
78 end = (char *) (strchr(start+1, ','));
|
|
79
|
|
80 if (end == NULL)
|
|
81 {
|
|
82 desc = strdup( start );
|
|
83 }
|
|
84 else
|
|
85 {
|
|
86 desc = strndup( start, end-start );
|
|
87 }
|
|
88 attribute = vertex_attribute_parse( desc );
|
|
89 start = end+1;
|
|
90 free(desc);
|
|
91 attribute->pointer = pointer;
|
|
92
|
|
93 switch( attribute->type )
|
|
94 {
|
|
95 case GL_BOOL: attribute_size = sizeof(GLboolean); break;
|
|
96 case GL_BYTE: attribute_size = sizeof(GLbyte); break;
|
|
97 case GL_UNSIGNED_BYTE: attribute_size = sizeof(GLubyte); break;
|
|
98 case GL_SHORT: attribute_size = sizeof(GLshort); break;
|
|
99 case GL_UNSIGNED_SHORT: attribute_size = sizeof(GLushort); break;
|
|
100 case GL_INT: attribute_size = sizeof(GLint); break;
|
|
101 case GL_UNSIGNED_INT: attribute_size = sizeof(GLuint); break;
|
|
102 case GL_FLOAT: attribute_size = sizeof(GLfloat); break;
|
|
103 default: attribute_size = 0;
|
|
104 }
|
|
105 stride += attribute->size*attribute_size;
|
|
106 pointer += attribute->size*attribute_size;
|
|
107 self->attributes[index] = attribute;
|
|
108 index++;
|
|
109 } while ( end && (index < MAX_VERTEX_ATTRIBUTE) );
|
|
110
|
|
111 for( i=0; i<index; ++i )
|
|
112 {
|
|
113 self->attributes[i]->stride = stride;
|
|
114 }
|
|
115
|
|
116 self->vertices = vector_new( stride );
|
|
117 self->vertices_id = 0;
|
|
118 self->GPU_vsize = 0;
|
|
119
|
|
120 self->indices = vector_new( sizeof(GLuint) );
|
|
121 self->indices_id = 0;
|
|
122 self->GPU_isize = 0;
|
|
123
|
|
124 self->items = vector_new( sizeof(ivec4) );
|
|
125 self->state = DIRTY;
|
|
126 self->mode = GL_TRIANGLES;
|
|
127 return self;
|
|
128 }
|
|
129
|
|
130
|
|
131
|
|
132 // ----------------------------------------------------------------------------
|
|
133 void
|
|
134 vertex_buffer_delete( vertex_buffer_t *self )
|
|
135 {
|
|
136 size_t i;
|
|
137
|
|
138 assert( self );
|
|
139
|
|
140
|
|
141 for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i )
|
|
142 {
|
|
143 if( self->attributes[i] )
|
|
144 {
|
|
145 vertex_attribute_delete( self->attributes[i] );
|
|
146 }
|
|
147 }
|
|
148
|
|
149
|
|
150 vector_delete( self->vertices );
|
|
151 self->vertices = 0;
|
|
152 if( self->vertices_id )
|
|
153 {
|
|
154 glDeleteBuffers( 1, &self->vertices_id );
|
|
155 }
|
|
156 self->vertices_id = 0;
|
|
157
|
|
158 vector_delete( self->indices );
|
|
159 self->indices = 0;
|
|
160 if( self->indices_id )
|
|
161 {
|
|
162 glDeleteBuffers( 1, &self->indices_id );
|
|
163 }
|
|
164 self->indices_id = 0;
|
|
165
|
|
166 vector_delete( self->items );
|
|
167
|
|
168 if( self->format )
|
|
169 {
|
|
170 free( self->format );
|
|
171 }
|
|
172 self->format = 0;
|
|
173 self->state = 0;
|
|
174 free( self );
|
|
175 }
|
|
176
|
|
177
|
|
178 // ----------------------------------------------------------------------------
|
|
179 const char *
|
|
180 vertex_buffer_format( const vertex_buffer_t *self )
|
|
181 {
|
|
182 assert( self );
|
|
183
|
|
184 return self->format;
|
|
185 }
|
|
186
|
|
187
|
|
188 // ----------------------------------------------------------------------------
|
|
189 size_t
|
|
190 vertex_buffer_size( const vertex_buffer_t *self )
|
|
191 {
|
|
192 assert( self );
|
|
193
|
|
194 return vector_size( self->items );
|
|
195 }
|
|
196
|
|
197
|
|
198 // ----------------------------------------------------------------------------
|
|
199 void
|
|
200 vertex_buffer_print( vertex_buffer_t * self )
|
|
201 {
|
|
202 int i = 0;
|
|
203 static const char *gltypes[9] = {
|
|
204 "GL_BOOL",
|
|
205 "GL_BYTE",
|
|
206 "GL_UNSIGNED_BYTE",
|
|
207 "GL_SHORT",
|
|
208 "GL_UNSIGNED_SHORT",
|
|
209 "GL_INT",
|
|
210 "GL_UNSIGNED_INT",
|
|
211 "GL_FLOAT",
|
|
212 "GL_VOID"
|
|
213 };
|
|
214
|
|
215 assert(self);
|
|
216
|
|
217 fprintf( stderr, "%ld vertices, %ld indices\n",
|
|
218 vector_size( self->vertices ), vector_size( self->indices ) );
|
|
219 while( self->attributes[i] )
|
|
220 {
|
|
221 int j = 8;
|
|
222 switch( self->attributes[i]->type )
|
|
223 {
|
|
224 case GL_BOOL: j=0; break;
|
|
225 case GL_BYTE: j=1; break;
|
|
226 case GL_UNSIGNED_BYTE: j=2; break;
|
|
227 case GL_SHORT: j=3; break;
|
|
228 case GL_UNSIGNED_SHORT: j=4; break;
|
|
229 case GL_INT: j=5; break;
|
|
230 case GL_UNSIGNED_INT: j=6; break;
|
|
231 case GL_FLOAT: j=7; break;
|
|
232 default: j=8; break;
|
|
233 }
|
|
234 fprintf(stderr, "%s : %dx%s (+%p)\n",
|
|
235 self->attributes[i]->name,
|
|
236 self->attributes[i]->size,
|
|
237 gltypes[j],
|
|
238 self->attributes[i]->pointer);
|
|
239
|
|
240 i += 1;
|
|
241 }
|
|
242 }
|
|
243
|
|
244
|
|
245 // ----------------------------------------------------------------------------
|
|
246 void
|
|
247 vertex_buffer_upload ( vertex_buffer_t *self )
|
|
248 {
|
|
249 size_t vsize, isize;
|
|
250
|
|
251 if( self->state == FROZEN )
|
|
252 {
|
|
253 return;
|
|
254 }
|
|
255
|
|
256 if( !self->vertices_id )
|
|
257 {
|
|
258 glGenBuffers( 1, &self->vertices_id );
|
|
259 }
|
|
260 if( !self->indices_id )
|
|
261 {
|
|
262 glGenBuffers( 1, &self->indices_id );
|
|
263 }
|
|
264
|
|
265 vsize = self->vertices->size*self->vertices->item_size;
|
|
266 isize = self->indices->size*self->indices->item_size;
|
|
267
|
|
268
|
|
269 // Always upload vertices first such that indices do not point to non
|
|
270 // existing data (if we get interrupted in between for example).
|
|
271
|
|
272 // Upload vertices
|
|
273 glBindBuffer( GL_ARRAY_BUFFER, self->vertices_id );
|
|
274 if( vsize != self->GPU_vsize )
|
|
275 {
|
|
276 glBufferData( GL_ARRAY_BUFFER,
|
|
277 vsize, self->vertices->items, GL_DYNAMIC_DRAW );
|
|
278 self->GPU_vsize = vsize;
|
|
279 }
|
|
280 else
|
|
281 {
|
|
282 glBufferSubData( GL_ARRAY_BUFFER,
|
|
283 0, vsize, self->vertices->items );
|
|
284 }
|
|
285 glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
|
286
|
|
287 // Upload indices
|
|
288 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id );
|
|
289 if( isize != self->GPU_isize )
|
|
290 {
|
|
291 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
|
|
292 isize, self->indices->items, GL_DYNAMIC_DRAW );
|
|
293 self->GPU_isize = isize;
|
|
294 }
|
|
295 else
|
|
296 {
|
|
297 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER,
|
|
298 0, isize, self->indices->items );
|
|
299 }
|
|
300 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
|
|
301 }
|
|
302
|
|
303
|
|
304
|
|
305 // ----------------------------------------------------------------------------
|
|
306 void
|
|
307 vertex_buffer_clear( vertex_buffer_t *self )
|
|
308 {
|
|
309 assert( self );
|
|
310
|
|
311 self->state = FROZEN;
|
|
312 vector_clear( self->indices );
|
|
313 vector_clear( self->vertices );
|
|
314 vector_clear( self->items );
|
|
315 self->state = DIRTY;
|
|
316 }
|
|
317
|
|
318
|
|
319
|
|
320 // ----------------------------------------------------------------------------
|
|
321 void
|
|
322 vertex_buffer_render_setup ( vertex_buffer_t *self, GLenum mode )
|
|
323 {
|
|
324 size_t i;
|
|
325
|
|
326 if( self->state != CLEAN )
|
|
327 {
|
|
328 vertex_buffer_upload( self );
|
|
329 self->state = CLEAN;
|
|
330 }
|
|
331
|
|
332 glBindBuffer( GL_ARRAY_BUFFER, self->vertices_id );
|
|
333
|
|
334 for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i )
|
|
335 {
|
|
336 vertex_attribute_t *attribute = self->attributes[i];
|
|
337 if ( attribute == 0 )
|
|
338 {
|
|
339 continue;
|
|
340 }
|
|
341 else
|
|
342 {
|
|
343 vertex_attribute_enable( attribute );
|
|
344 }
|
|
345 }
|
|
346
|
|
347 if( self->indices->size )
|
|
348 {
|
|
349 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id );
|
|
350 }
|
|
351 self->mode = mode;
|
|
352 }
|
|
353
|
|
354 // ----------------------------------------------------------------------------
|
|
355 void
|
|
356 vertex_buffer_render_finish ( vertex_buffer_t *self )
|
|
357 {
|
|
358 glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
|
359 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
|
|
360 }
|
|
361
|
|
362
|
|
363 // ----------------------------------------------------------------------------
|
|
364 void
|
|
365 vertex_buffer_render_item ( vertex_buffer_t *self,
|
|
366 size_t index )
|
|
367 {
|
|
368 ivec4 * item = (ivec4 *) vector_get( self->items, index );
|
|
369 assert( self );
|
|
370 assert( index < vector_size( self->items ) );
|
|
371
|
|
372
|
|
373 if( self->indices->size )
|
|
374 {
|
|
375 size_t start = item->istart;
|
|
376 size_t count = item->icount;
|
|
377 glDrawElements( self->mode, count, GL_UNSIGNED_INT, (void *)(start*sizeof(GLuint)) );
|
|
378 }
|
|
379 else if( self->vertices->size )
|
|
380 {
|
|
381 size_t start = item->vstart;
|
|
382 size_t count = item->vcount;
|
|
383 glDrawArrays( self->mode, start*self->vertices->item_size, count);
|
|
384 }
|
|
385 }
|
|
386
|
|
387
|
|
388 // ----------------------------------------------------------------------------
|
|
389 void
|
|
390 vertex_buffer_render ( vertex_buffer_t *self, GLenum mode )
|
|
391 {
|
|
392 size_t vcount = self->vertices->size;
|
|
393 size_t icount = self->indices->size;
|
|
394
|
|
395 vertex_buffer_render_setup( self, mode );
|
|
396 if( icount )
|
|
397 {
|
|
398 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id );
|
|
399 glDrawElements( mode, icount, GL_UNSIGNED_INT, 0 );
|
|
400 }
|
|
401 else
|
|
402 {
|
|
403 glDrawArrays( mode, 0, vcount );
|
|
404 }
|
|
405 vertex_buffer_render_finish( self );
|
|
406 }
|
|
407
|
|
408
|
|
409
|
|
410 // ----------------------------------------------------------------------------
|
|
411 void
|
|
412 vertex_buffer_push_back_indices ( vertex_buffer_t * self,
|
|
413 const GLuint * indices,
|
|
414 const size_t icount )
|
|
415 {
|
|
416 assert( self );
|
|
417
|
|
418 self->state |= DIRTY;
|
|
419 vector_push_back_data( self->indices, indices, icount );
|
|
420 }
|
|
421
|
|
422
|
|
423
|
|
424 // ----------------------------------------------------------------------------
|
|
425 void
|
|
426 vertex_buffer_push_back_vertices ( vertex_buffer_t * self,
|
|
427 const void * vertices,
|
|
428 const size_t vcount )
|
|
429 {
|
|
430 assert( self );
|
|
431
|
|
432 self->state |= DIRTY;
|
|
433 vector_push_back_data( self->vertices, vertices, vcount );
|
|
434 }
|
|
435
|
|
436
|
|
437
|
|
438 // ----------------------------------------------------------------------------
|
|
439 void
|
|
440 vertex_buffer_insert_indices ( vertex_buffer_t *self,
|
|
441 const size_t index,
|
|
442 const GLuint *indices,
|
|
443 const size_t count )
|
|
444 {
|
|
445 assert( self );
|
|
446 assert( self->indices );
|
|
447 assert( index < self->indices->size+1 );
|
|
448
|
|
449 self->state |= DIRTY;
|
|
450 vector_insert_data( self->indices, index, indices, count );
|
|
451 }
|
|
452
|
|
453
|
|
454
|
|
455 // ----------------------------------------------------------------------------
|
|
456 void
|
|
457 vertex_buffer_insert_vertices( vertex_buffer_t *self,
|
|
458 const size_t index,
|
|
459 const void *vertices,
|
|
460 const size_t vcount )
|
|
461 {
|
|
462 size_t i;
|
|
463 assert( self );
|
|
464 assert( self->vertices );
|
|
465 assert( index < self->vertices->size+1 );
|
|
466
|
|
467 self->state |= DIRTY;
|
|
468
|
|
469 for( i=0; i<self->indices->size; ++i )
|
|
470 {
|
|
471 if( *(GLuint *)(vector_get( self->indices, i )) > index )
|
|
472 {
|
|
473 *(GLuint *)(vector_get( self->indices, i )) += index;
|
|
474 }
|
|
475 }
|
|
476
|
|
477 vector_insert_data( self->vertices, index, vertices, vcount );
|
|
478 }
|
|
479
|
|
480
|
|
481
|
|
482 // ----------------------------------------------------------------------------
|
|
483 void
|
|
484 vertex_buffer_erase_indices( vertex_buffer_t *self,
|
|
485 const size_t first,
|
|
486 const size_t last )
|
|
487 {
|
|
488 assert( self );
|
|
489 assert( self->indices );
|
|
490 assert( first < self->indices->size );
|
|
491 assert( (last) <= self->indices->size );
|
|
492
|
|
493 self->state |= DIRTY;
|
|
494 vector_erase_range( self->indices, first, last );
|
|
495 }
|
|
496
|
|
497
|
|
498
|
|
499 // ----------------------------------------------------------------------------
|
|
500 void
|
|
501 vertex_buffer_erase_vertices( vertex_buffer_t *self,
|
|
502 const size_t first,
|
|
503 const size_t last )
|
|
504 {
|
|
505 size_t i;
|
|
506 assert( self );
|
|
507 assert( self->vertices );
|
|
508 assert( first < self->vertices->size );
|
|
509 assert( (first+last) <= self->vertices->size );
|
|
510 assert( last > first );
|
|
511
|
|
512 self->state |= DIRTY;
|
|
513 for( i=0; i<self->indices->size; ++i )
|
|
514 {
|
|
515 if( *(GLuint *)(vector_get( self->indices, i )) > first )
|
|
516 {
|
|
517 *(GLuint *)(vector_get( self->indices, i )) -= (last-first);
|
|
518 }
|
|
519 }
|
|
520 vector_erase_range( self->vertices, first, last );
|
|
521 }
|
|
522
|
|
523
|
|
524
|
|
525 // ----------------------------------------------------------------------------
|
|
526 size_t
|
|
527 vertex_buffer_push_back( vertex_buffer_t * self,
|
|
528 const void * vertices, const size_t vcount,
|
|
529 const GLuint * indices, const size_t icount )
|
|
530 {
|
|
531 return vertex_buffer_insert( self, vector_size( self->items ),
|
|
532 vertices, vcount, indices, icount );
|
|
533 }
|
|
534
|
|
535 // ----------------------------------------------------------------------------
|
|
536 size_t
|
|
537 vertex_buffer_insert( vertex_buffer_t * self, const size_t index,
|
|
538 const void * vertices, const size_t vcount,
|
|
539 const GLuint * indices, const size_t icount )
|
|
540 {
|
|
541 size_t vstart, istart, i;
|
|
542 ivec4 item;
|
|
543 assert( self );
|
|
544 assert( vertices );
|
|
545 assert( indices );
|
|
546
|
|
547 self->state = FROZEN;
|
|
548
|
|
549 // Push back vertices
|
|
550 vstart = vector_size( self->vertices );
|
|
551 vertex_buffer_push_back_vertices( self, vertices, vcount );
|
|
552
|
|
553 // Push back indices
|
|
554 istart = vector_size( self->indices );
|
|
555 vertex_buffer_push_back_indices( self, indices, icount );
|
|
556
|
|
557 // Update indices within the vertex buffer
|
|
558 for( i=0; i<icount; ++i )
|
|
559 {
|
|
560 *(GLuint *)(vector_get( self->indices, istart+i )) += vstart;
|
|
561 }
|
|
562
|
|
563 // Insert item
|
|
564 item.x = vstart;
|
|
565 item.y = vcount;
|
|
566 item.z = istart;
|
|
567 item.w = icount;
|
|
568 vector_insert( self->items, index, &item );
|
|
569
|
|
570 self->state = DIRTY;
|
|
571 return index;
|
|
572 }
|
|
573
|
|
574 // ----------------------------------------------------------------------------
|
|
575 void
|
|
576 vertex_buffer_erase( vertex_buffer_t * self,
|
|
577 const size_t index )
|
|
578 {
|
|
579 ivec4 * item;
|
|
580 size_t vstart, vcount, istart, icount, i;
|
|
581
|
|
582 assert( self );
|
|
583 assert( index < vector_size( self->items ) );
|
|
584
|
|
585 item = (ivec4 *) vector_get( self->items, index );
|
|
586 vstart = item->vstart;
|
|
587 vcount = item->vcount;
|
|
588 istart = item->istart;
|
|
589 icount = item->icount;
|
|
590
|
|
591 // Update items
|
|
592 for( i=0; i<vector_size(self->items); ++i )
|
|
593 {
|
|
594 ivec4 * item = (ivec4 *) vector_get( self->items, i );
|
|
595 if( item->vstart > vstart)
|
|
596 {
|
|
597 item->vstart -= vcount;
|
|
598 item->istart -= icount;
|
|
599 }
|
|
600 }
|
|
601
|
|
602 self->state = FROZEN;
|
|
603 vertex_buffer_erase_indices( self, istart, istart+icount );
|
|
604 vertex_buffer_erase_vertices( self, vstart, vstart+vcount );
|
|
605 vector_erase( self->items, index );
|
|
606 self->state = DIRTY;
|
|
607 }
|