Debian Codegraph @

libs/picomodel/picomodel.c

raw symbols SCIP
1 /* -----------------------------------------------------------------------------
2
3 PicoModel Library
4
5 Copyright (c) 2002, Randy Reddig & seaw0lf
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
13
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
17
18 Neither the names of the copyright holders nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 ----------------------------------------------------------------------------- */
34
35 /* dependencies */
36 #include "picointernal.h"
37
38
39
40 /*
41 PicoInit()
42 initializes the picomodel library
43 */
44
45 int PicoInit( void ){
46 /* successfully initialized -sea */
47 return 1;
48 }
49
50
51
52 /*
53 PicoShutdown()
54 shuts the pico model library down
55 */
56
57 void PicoShutdown( void ){
58 /* do something interesting here in the future */
59 return;
60 }
61
62
63
64 /*
65 PicoError()
66 returns last picomodel error code (see PME_* defines)
67 */
68
69 int PicoError( void ){
70 /* todo: do something here */
71 return 0;
72 }
73
74
75
76 /*
77 PicoSetMallocFunc()
78 sets the ptr to the malloc function
79 */
80
81 void PicoSetMallocFunc( void *( *func )( size_t ) ){
82 if ( func != NULL ) {
83 _pico_ptr_malloc = func;
84 }
85 }
86
87
88
89 /*
90 PicoSetFreeFunc()
91 sets the ptr to the free function
92 */
93
94 void PicoSetFreeFunc( void ( *func )( void* ) ){
95 if ( func != NULL ) {
96 _pico_ptr_free = func;
97 }
98 }
99
100
101
102 /*
103 PicoSetLoadFileFunc()
104 sets the ptr to the file load function
105 */
106
107 void PicoSetLoadFileFunc( void ( *func )( const char*, unsigned char**, int* ) ){
108 if ( func != NULL ) {
109 _pico_ptr_load_file = func;
110 }
111 }
112
113
114
115 /*
116 PicoSetFreeFileFunc()
117 sets the ptr to the free function
118 */
119
120 void PicoSetFreeFileFunc( void ( *func )( void* ) ){
121 if ( func != NULL ) {
122 _pico_ptr_free_file = func;
123 }
124 }
125
126
127
128 /*
129 PicoSetSaveFileFunc()
130 sets the ptr to the file save function
131 */
132
133 void PicoSetSaveFileFunc( int ( *func )( const char*, const unsigned char*, int ) ){
134 if ( func != NULL ) {
135 _pico_ptr_save_file = func;
136 }
137 }
138
139
140
141 /*
142 PicoSetPrintFunc()
143 sets the ptr to the print function
144 */
145
146 void PicoSetPrintFunc( void ( *func )( int, const char* ) ){
147 if ( func != NULL ) {
148 _pico_ptr_print = func;
149 }
150 }
151
152
153
154 picoModel_t *PicoModuleLoadModel( const picoModule_t* pm, const char* fileName, picoByte_t* buffer, int bufSize, int frameNum ){
155 char *modelFileName, *remapFileName;
156
157 /* see whether this module can load the model file or not */
158 if ( pm->canload( fileName, buffer, bufSize ) == PICO_PMV_OK ) {
159 /* use loader provided by module to read the model data */
160 picoModel_t* model = pm->load( fileName, frameNum, buffer, bufSize );
161 if ( model == NULL ) {
162 _pico_free_file( buffer );
163 return NULL;
164 }
165
166 /* assign pointer to file format module */
167 model->module = pm;
168
169 /* get model file name */
170 modelFileName = PicoGetModelFileName( model );
171
172 /* apply model remappings from <model>.remap */
173 if ( strlen( modelFileName ) ) {
174 /* alloc copy of model file name */
175 remapFileName = _pico_alloc( strlen( modelFileName ) + 20 );
176 if ( remapFileName != NULL ) {
177 /* copy model file name and change extension */
178 strcpy( remapFileName, modelFileName );
179 _pico_setfext( remapFileName, "remap" );
180
181 /* try to remap model; we don't handle the result */
182 PicoRemapModel( model, remapFileName );
183
184 /* free the remap file name string */
185 _pico_free( remapFileName );
186 }
187 }
188
189 _pico_deduce_shadernames( model );
190
191 return model;
192 }
193
194 return NULL;
195 }
196
197 /*
198 PicoLoadModel()
199 the meat and potatoes function
200 */
201
202 picoModel_t *PicoLoadModel( const char *fileName, int frameNum ){
203 const picoModule_t **modules, *pm;
204 picoModel_t *model;
205 picoByte_t *buffer;
206 int bufSize;
207
208
209 /* init */
210 model = NULL;
211
212 /* make sure we've got a file name */
213 if ( fileName == NULL ) {
214 _pico_printf( PICO_ERROR, "PicoLoadModel: No filename given (fileName == NULL)" );
215 return NULL;
216 }
217
218 /* load file data (buffer is allocated by host app) */
219 _pico_load_file( fileName, &buffer, &bufSize );
220 if ( bufSize < 0 ) {
221 _pico_printf( PICO_ERROR, "PicoLoadModel: Failed loading model %s", fileName );
222 return NULL;
223 }
224
225 /* get ptr to list of supported modules */
226 modules = PicoModuleList( NULL );
227
228 /* run it through the various loader functions and try */
229 /* to find a loader that fits the given file data */
230 for ( ; *modules != NULL; modules++ )
231 {
232 /* get module */
233 pm = *modules;
234
235 /* sanity check */
236 if ( pm == NULL ) {
237 break;
238 }
239
240 /* module must be able to load */
241 if ( pm->canload == NULL || pm->load == NULL ) {
242 continue;
243 }
244
245 model = PicoModuleLoadModel( pm, fileName, buffer, bufSize, frameNum );
246 if ( model != NULL ) {
247 /* model was loaded, so break out of loop */
248 break;
249 }
250 }
251
252 /* free memory used by file buffer */
253 if ( buffer ) {
254 _pico_free_file( buffer );
255 }
256
257 /* return */
258 return model;
259 }
260
261 picoModel_t *PicoModuleLoadModelStream( const picoModule_t* module, void* inputStream, PicoInputStreamReadFunc inputStreamRead, size_t streamLength, int frameNum, const char *fileName ){
262 picoModel_t *model;
263 picoByte_t *buffer;
264 int bufSize;
265
266
267 /* init */
268 model = NULL;
269
270 if ( inputStream == NULL ) {
271 _pico_printf( PICO_ERROR, "PicoLoadModel: invalid input stream (inputStream == NULL)" );
272 return NULL;
273 }
274
275 if ( inputStreamRead == NULL ) {
276 _pico_printf( PICO_ERROR, "PicoLoadModel: invalid input stream (inputStreamRead == NULL)" );
277 return NULL;
278 }
279
280 buffer = _pico_alloc( streamLength + 1 );
281
282 bufSize = (int)inputStreamRead( inputStream, buffer, streamLength );
283 buffer[bufSize] = '\0';
284
285 model = PicoModuleLoadModel( module, fileName, buffer, bufSize, frameNum );
286
287 if ( model != 0 ) {
288 _pico_free( buffer );
289 }
290
291 /* return */
292 return model;
293 }
294
295
296 /* ----------------------------------------------------------------------------
297 models
298 ---------------------------------------------------------------------------- */
299
300 /*
301 PicoNewModel()
302 creates a new pico model
303 */
304
305 picoModel_t *PicoNewModel( void ){
306 picoModel_t *model;
307
308 /* allocate */
309 model = _pico_alloc( sizeof( picoModel_t ) );
310 if ( model == NULL ) {
311 return NULL;
312 }
313
314 /* clear */
315 memset( model,0,sizeof( picoModel_t ) );
316
317 /* model set up */
318 _pico_zero_bounds( model->mins,model->maxs );
319
320 /* set initial frame count to 1 -sea */
321 model->numFrames = 1;
322
323 /* return ptr to new model */
324 return model;
325 }
326
327
328
329 /*
330 PicoFreeModel()
331 frees a model and all associated data
332 */
333
334 void PicoFreeModel( picoModel_t *model ){
335 int i;
336
337
338 /* sanity check */
339 if ( model == NULL ) {
340 return;
341 }
342
343 /* free bits */
344 if ( model->name ) {
345 _pico_free( model->name );
346 }
347
348 if ( model->fileName ) {
349 _pico_free( model->fileName );
350 }
351
352 /* free shaders */
353 for ( i = 0; i < model->numShaders; i++ )
354 PicoFreeShader( model->shader[ i ] );
355 free( model->shader );
356
357 /* free surfaces */
358 for ( i = 0; i < model->numSurfaces; i++ )
359 PicoFreeSurface( model->surface[ i ] );
360 free( model->surface );
361
362 /* free the model */
363 _pico_free( model );
364 }
365
366
367
368 /*
369 PicoAdjustModel()
370 adjusts a models's memory allocations to handle the requested sizes.
371 will always grow, never shrink
372 */
373
374 int PicoAdjustModel( picoModel_t *model, int numShaders, int numSurfaces ){
375 /* dummy check */
376 if ( model == NULL ) {
377 return 0;
378 }
379
380 /* bare minimums */
381 /* sea: null surface/shader fix (1s=>0s) */
382 if ( numShaders < 0 ) {
383 numShaders = 0;
384 }
385 if ( numSurfaces < 0 ) {
386 numSurfaces = 0;
387 }
388
389 /* additional shaders? */
390 while ( numShaders > model->maxShaders )
391 {
392 model->maxShaders += PICO_GROW_SHADERS;
393 if ( !_pico_realloc( (void *) &model->shader, model->numShaders * sizeof( *model->shader ), model->maxShaders * sizeof( *model->shader ) ) ) {
394 return 0;
395 }
396 }
397
398 /* set shader count to higher */
399 if ( numShaders > model->numShaders ) {
400 model->numShaders = numShaders;
401 }
402
403 /* additional surfaces? */
404 while ( numSurfaces > model->maxSurfaces )
405 {
406 model->maxSurfaces += PICO_GROW_SURFACES;
407 if ( !_pico_realloc( (void *) &model->surface, model->numSurfaces * sizeof( *model->surface ), model->maxSurfaces * sizeof( *model->surface ) ) ) {
408 return 0;
409 }
410 }
411
412 /* set shader count to higher */
413 if ( numSurfaces > model->numSurfaces ) {
414 model->numSurfaces = numSurfaces;
415 }
416
417 /* return ok */
418 return 1;
419 }
420
421
422
423 /* ----------------------------------------------------------------------------
424 shaders
425 ---------------------------------------------------------------------------- */
426
427 /*
428 PicoNewShader()
429 creates a new pico shader and returns its index. -sea
430 */
431
432 picoShader_t *PicoNewShader( picoModel_t *model ){
433 picoShader_t *shader;
434
435
436 /* allocate and clear */
437 shader = _pico_alloc( sizeof( picoShader_t ) );
438 if ( shader == NULL ) {
439 return NULL;
440 }
441 memset( shader, 0, sizeof( picoShader_t ) );
442
443 /* attach it to the model */
444 if ( model != NULL ) {
445 /* adjust model */
446 if ( !PicoAdjustModel( model, model->numShaders + 1, 0 ) ) {
447 _pico_free( shader );
448 return NULL;
449 }
450
451 /* attach */
452 model->shader[ model->numShaders - 1 ] = shader;
453 shader->model = model;
454 }
455
456 /* setup default shader colors */
457 _pico_set_color( shader->ambientColor,0,0,0,0 );
458 _pico_set_color( shader->diffuseColor,255,255,255,1 );
459 _pico_set_color( shader->specularColor,0,0,0,0 );
460
461 /* no need to do this, but i do it anyway */
462 shader->transparency = 0;
463 shader->shininess = 0;
464
465 /* return the newly created shader */
466 return shader;
467 }
468
469
470
471 /*
472 PicoFreeShader()
473 frees a shader and all associated data -sea
474 */
475
476 void PicoFreeShader( picoShader_t *shader ){
477 /* dummy check */
478 if ( shader == NULL ) {
479 return;
480 }
481
482 /* free bits */
483 if ( shader->name ) {
484 _pico_free( shader->name );
485 }
486 if ( shader->mapName ) {
487 _pico_free( shader->mapName );
488 }
489
490 /* free the shader */
491 _pico_free( shader );
492 }
493
494
495
496 /*
497 PicoFindShader()
498 finds a named shader in a model
499 */
500
501 picoShader_t *PicoFindShader( picoModel_t *model, char *name, int caseSensitive ){
502 /* sanity checks */
503 if ( model == NULL || name == NULL ) { /* sea: null name fix */
504 return NULL;
505 }
506
507 /* walk list */
508 for ( int i = 0; i < model->numShaders; i++ )
509 {
510 /* skip null shaders or shaders with null names */
511 if ( model->shader[ i ] == NULL ||
512 model->shader[ i ]->name == NULL ) {
513 continue;
514 }
515
516 /* compare the shader name with name we're looking for */
517 if ( caseSensitive ) {
518 if ( !strcmp( name, model->shader[ i ]->name ) ) {
519 return model->shader[ i ];
520 }
521 }
522 else if ( !_pico_stricmp( name, model->shader[ i ]->name ) ) {
523 return model->shader[ i ];
524 }
525 }
526
527 /* named shader not found */
528 return NULL;
529 }
530
531
532
533 /* ----------------------------------------------------------------------------
534 surfaces
535 ---------------------------------------------------------------------------- */
536
537 /*
538 PicoNewSurface()
539 creates a new pico surface
540 */
541
542 picoSurface_t *PicoNewSurface( picoModel_t *model ){
543 picoSurface_t *surface;
544 char surfaceName[64];
545
546 /* allocate and clear */
547 surface = _pico_alloc( sizeof( *surface ) );
548 if ( surface == NULL ) {
549 return NULL;
550 }
551 memset( surface, 0, sizeof( *surface ) );
552
553 /* attach it to the model */
554 if ( model != NULL ) {
555 /* adjust model */
556 if ( !PicoAdjustModel( model, 0, model->numSurfaces + 1 ) ) {
557 _pico_free( surface );
558 return NULL;
559 }
560
561 /* attach */
562 model->surface[ model->numSurfaces - 1 ] = surface;
563 surface->model = model;
564
565 /* set default name */
566 sprintf( surfaceName, "Unnamed_%d", model->numSurfaces );
567 PicoSetSurfaceName( surface, surfaceName );
568 }
569
570 /* return */
571 return surface;
572 }
573
574
575
576 /*
577 PicoFreeSurface()
578 frees a surface and all associated data
579 */
580 void PicoFreeSurface( picoSurface_t *surface ){
581 int i;
582
583
584 /* dummy check */
585 if ( surface == NULL ) {
586 return;
587 }
588
589 /* free bits */
590 _pico_free( surface->xyz );
591 _pico_free( surface->normal );
592 _pico_free( surface->smoothingGroup );
593 _pico_free( surface->index );
594 _pico_free( surface->faceNormal );
595
596 if ( surface->name ) {
597 _pico_free( surface->name );
598 }
599
600 /* free arrays */
601 for ( i = 0; i < surface->numSTArrays; i++ )
602 _pico_free( surface->st[ i ] );
603 free( surface->st );
604 for ( i = 0; i < surface->numColorArrays; i++ )
605 _pico_free( surface->color[ i ] );
606 free( surface->color );
607
608 /* free the surface */
609 _pico_free( surface );
610 }
611
612
613
614 /*
615 PicoAdjustSurface()
616 adjusts a surface's memory allocations to handle the requested sizes.
617 will always grow, never shrink
618 */
619
620 int PicoAdjustSurface( picoSurface_t *surface, int numVertexes, int numSTArrays, int numColorArrays, int numIndexes, int numFaceNormals ){
621 int i;
622
623
624 /* dummy check */
625 if ( surface == NULL ) {
626 return 0;
627 }
628
629 /* bare minimums */
630 if ( numVertexes < 1 ) {
631 numVertexes = 1;
632 }
633 if ( numSTArrays < 1 ) {
634 numSTArrays = 1;
635 }
636 if ( numColorArrays < 1 ) {
637 numColorArrays = 1;
638 }
639 if ( numIndexes < 1 ) {
640 numIndexes = 1;
641 }
642
643 /* additional vertexes? */
644 while ( numVertexes > surface->maxVertexes ) /* fix */
645 {
646 surface->maxVertexes += PICO_GROW_VERTEXES;
647 if ( !_pico_realloc( (void *) &surface->xyz, surface->numVertexes * sizeof( *surface->xyz ), surface->maxVertexes * sizeof( *surface->xyz ) ) ) {
648 return 0;
649 }
650 if ( !_pico_realloc( (void *) &surface->normal, surface->numVertexes * sizeof( *surface->normal ), surface->maxVertexes * sizeof( *surface->normal ) ) ) {
651 return 0;
652 }
653 if ( !_pico_realloc( (void *) &surface->smoothingGroup, surface->numVertexes * sizeof( *surface->smoothingGroup ), surface->maxVertexes * sizeof( *surface->smoothingGroup ) ) ) {
654 return 0;
655 }
656 for ( i = 0; i < surface->numSTArrays; i++ )
657 if ( !_pico_realloc( (void*) &surface->st[ i ], surface->numVertexes * sizeof( *surface->st[ i ] ), surface->maxVertexes * sizeof( *surface->st[ i ] ) ) ) {
658 return 0;
659 }
660 for ( i = 0; i < surface->numColorArrays; i++ )
661 if ( !_pico_realloc( (void*) &surface->color[ i ], surface->numVertexes * sizeof( *surface->color[ i ] ), surface->maxVertexes * sizeof( *surface->color[ i ] ) ) ) {
662 return 0;
663 }
664 }
665
666 /* set vertex count to higher */
667 if ( numVertexes > surface->numVertexes ) {
668 surface->numVertexes = numVertexes;
669 }
670
671 /* additional st arrays? */
672 while ( numSTArrays > surface->maxSTArrays ) /* fix */
673 {
674 surface->maxSTArrays += PICO_GROW_ARRAYS;
675 if ( !_pico_realloc( (void*) &surface->st, surface->numSTArrays * sizeof( *surface->st ), surface->maxSTArrays * sizeof( *surface->st ) ) ) {
676 return 0;
677 }
678 while ( surface->numSTArrays < numSTArrays )
679 {
680 surface->st[ surface->numSTArrays ] = _pico_alloc( surface->maxVertexes * sizeof( *surface->st[ 0 ] ) );
681 memset( surface->st[ surface->numSTArrays ], 0, surface->maxVertexes * sizeof( *surface->st[ 0 ] ) );
682 surface->numSTArrays++;
683 }
684 }
685
686 /* additional color arrays? */
687 while ( numColorArrays > surface->maxColorArrays ) /* fix */
688 {
689 surface->maxColorArrays += PICO_GROW_ARRAYS;
690 if ( !_pico_realloc( (void*) &surface->color, surface->numColorArrays * sizeof( *surface->color ), surface->maxColorArrays * sizeof( *surface->color ) ) ) {
691 return 0;
692 }
693 while ( surface->numColorArrays < numColorArrays )
694 {
695 surface->color[ surface->numColorArrays ] = _pico_alloc( surface->maxVertexes * sizeof( *surface->color[ 0 ] ) );
696 memset( surface->color[ surface->numColorArrays ], 0, surface->maxVertexes * sizeof( *surface->color[ 0 ] ) );
697 surface->numColorArrays++;
698 }
699 }
700
701 /* additional indexes? */
702 while ( numIndexes > surface->maxIndexes ) /* fix */
703 {
704 surface->maxIndexes += PICO_GROW_INDEXES;
705 if ( !_pico_realloc( (void*) &surface->index, surface->numIndexes * sizeof( *surface->index ), surface->maxIndexes * sizeof( *surface->index ) ) ) {
706 return 0;
707 }
708 }
709
710 /* set index count to higher */
711 if ( numIndexes > surface->numIndexes ) {
712 surface->numIndexes = numIndexes;
713 }
714
715 /* additional face normals? */
716 while ( numFaceNormals > surface->maxFaceNormals ) /* fix */
717 {
718 surface->maxFaceNormals += PICO_GROW_FACES;
719 if ( !_pico_realloc( (void *) &surface->faceNormal, surface->numFaceNormals * sizeof( *surface->faceNormal ), surface->maxFaceNormals * sizeof( *surface->faceNormal ) ) ) {
720 return 0;
721 }
722 }
723
724 /* set face normal count to higher */
725 if ( numFaceNormals > surface->numFaceNormals ) {
726 surface->numFaceNormals = numFaceNormals;
727 }
728
729 /* return ok */
730 return 1;
731 }
732
733
734 /* PicoFindSurface:
735 * Finds first matching named surface in a model.
736 */
737 picoSurface_t *PicoFindSurface(
738 picoModel_t *model, char *name, int caseSensitive ){
739 int i;
740
741 /* sanity check */
742 if ( model == NULL || name == NULL ) {
743 return NULL;
744 }
745
746 /* walk list */
747 for ( i = 0; i < model->numSurfaces; i++ )
748 {
749 /* skip null surfaces or surfaces with null names */
750 if ( model->surface[ i ] == NULL ||
751 model->surface[ i ]->name == NULL ) {
752 continue;
753 }
754
755 /* compare the surface name with name we're looking for */
756 if ( caseSensitive ) {
757 if ( !strcmp( name,model->surface[ i ]->name ) ) {
758 return model->surface[ i ];
759 }
760 }
761 else {
762 if ( !_pico_stricmp( name,model->surface[ i ]->name ) ) {
763 return model->surface[ i ];
764 }
765 }
766 }
767 /* named surface not found */
768 return NULL;
769 }
770
771
772
773 /*----------------------------------------------------------------------------
774 PicoSet*() Setter Functions
775 ----------------------------------------------------------------------------*/
776
777 void PicoSetModelName( picoModel_t *model, const char *name ){
778 if ( model == NULL || name == NULL ) {
779 return;
780 }
781 if ( model->name != NULL ) {
782 _pico_free( model->name );
783 }
784
785 model->name = _pico_clone_alloc( name );
786 }
787
788
789
790 void PicoSetModelFileName( picoModel_t *model, const char *fileName ){
791 if ( model == NULL || fileName == NULL ) {
792 return;
793 }
794 if ( model->fileName != NULL ) {
795 _pico_free( model->fileName );
796 }
797
798 model->fileName = _pico_clone_alloc( fileName );
799 }
800
801
802
803 void PicoSetModelFrameNum( picoModel_t *model, int frameNum ){
804 if ( model == NULL ) {
805 return;
806 }
807 model->frameNum = frameNum;
808 }
809
810
811
812 void PicoSetModelNumFrames( picoModel_t *model, int numFrames ){
813 if ( model == NULL ) {
814 return;
815 }
816 model->numFrames = numFrames;
817 }
818
819
820
821 void PicoSetModelData( picoModel_t *model, void *data ){
822 if ( model == NULL ) {
823 return;
824 }
825 model->data = data;
826 }
827
828
829
830 void PicoSetShaderName( picoShader_t *shader, char *name ){
831 if ( shader == NULL || name == NULL ) {
832 return;
833 }
834 if ( shader->name != NULL ) {
835 _pico_free( shader->name );
836 }
837
838 shader->name = _pico_clone_alloc( name );
839 }
840
841
842
843 void PicoSetShaderMapName( picoShader_t *shader, char *mapName ){
844 if ( shader == NULL || mapName == NULL ) {
845 return;
846 }
847 if ( shader->mapName != NULL ) {
848 _pico_free( shader->mapName );
849 }
850
851 shader->mapName = _pico_clone_alloc( mapName );
852 }
853
854
855
856 void PicoSetShaderAmbientColor( picoShader_t *shader, picoColor_t color ){
857 if ( shader == NULL || color == NULL ) {
858 return;
859 }
860 shader->ambientColor[ 0 ] = color[ 0 ];
861 shader->ambientColor[ 1 ] = color[ 1 ];
862 shader->ambientColor[ 2 ] = color[ 2 ];
863 shader->ambientColor[ 3 ] = color[ 3 ];
864 }
865
866
867
868 void PicoSetShaderDiffuseColor( picoShader_t *shader, picoColor_t color ){
869 if ( shader == NULL || color == NULL ) {
870 return;
871 }
872 shader->diffuseColor[ 0 ] = color[ 0 ];
873 shader->diffuseColor[ 1 ] = color[ 1 ];
874 shader->diffuseColor[ 2 ] = color[ 2 ];
875 shader->diffuseColor[ 3 ] = color[ 3 ];
876 }
877
878
879
880 void PicoSetShaderSpecularColor( picoShader_t *shader, picoColor_t color ){
881 if ( shader == NULL || color == NULL ) {
882 return;
883 }
884 shader->specularColor[ 0 ] = color[ 0 ];
885 shader->specularColor[ 1 ] = color[ 1 ];
886 shader->specularColor[ 2 ] = color[ 2 ];
887 shader->specularColor[ 3 ] = color[ 3 ];
888 }
889
890
891
892 void PicoSetShaderTransparency( picoShader_t *shader, float value ){
893 if ( shader == NULL ) {
894 return;
895 }
896 shader->transparency = value;
897
898 /* cap to 0..1 range */
899 if ( shader->transparency < 0.0 ) {
900 shader->transparency = 0.0;
901 }
902 if ( shader->transparency > 1.0 ) {
903 shader->transparency = 1.0;
904 }
905 }
906
907
908
909 void PicoSetShaderShininess( picoShader_t *shader, float value ){
910 if ( shader == NULL ) {
911 return;
912 }
913 shader->shininess = value;
914
915 /* cap to 0..127 range */
916 if ( shader->shininess < 0.0 ) {
917 shader->shininess = 0.0;
918 }
919 if ( shader->shininess > 127.0 ) {
920 shader->shininess = 127.0;
921 }
922 }
923
924
925
926 void PicoSetSurfaceData( picoSurface_t *surface, void *data ){
927 if ( surface == NULL ) {
928 return;
929 }
930 surface->data = data;
931 }
932
933
934
935 void PicoSetSurfaceType( picoSurface_t *surface, picoSurfaceType_t type ){
936 if ( surface == NULL ) {
937 return;
938 }
939 surface->type = type;
940 }
941
942
943
944 void PicoSetSurfaceName( picoSurface_t *surface, const char *name ){
945 if ( surface == NULL || name == NULL ) {
946 return;
947 }
948 if ( surface->name != NULL ) {
949 _pico_free( surface->name );
950 }
951
952 surface->name = _pico_clone_alloc( name );
953 }
954
955
956
957 void PicoSetSurfaceShader( picoSurface_t *surface, picoShader_t *shader ){
958 if ( surface == NULL ) {
959 return;
960 }
961 surface->shader = shader;
962 }
963
964
965
966 void PicoSetSurfaceXYZ( picoSurface_t *surface, int num, picoVec3_t xyz ){
967 if ( surface == NULL || num < 0 || xyz == NULL ) {
968 return;
969 }
970 if ( !PicoAdjustSurface( surface, num + 1, 0, 0, 0, 0 ) ) {
971 return;
972 }
973 _pico_copy_vec( xyz, surface->xyz[ num ] );
974 if ( surface->model != NULL ) {
975 _pico_expand_bounds( xyz, surface->model->mins, surface->model->maxs );
976 }
977 }
978
979
980
981 void PicoSetSurfaceNormal( picoSurface_t *surface, int num, picoVec3_t normal ){
982 if ( surface == NULL || num < 0 || normal == NULL ) {
983 return;
984 }
985 if ( !PicoAdjustSurface( surface, num + 1, 0, 0, 0, 0 ) ) {
986 return;
987 }
988 _pico_copy_vec( normal, surface->normal[ num ] );
989 }
990
991
992
993 void PicoSetSurfaceST( picoSurface_t *surface, int array, int num, picoVec2_t st ){
994 if ( surface == NULL || num < 0 || st == NULL ) {
995 return;
996 }
997 if ( !PicoAdjustSurface( surface, num + 1, array + 1, 0, 0, 0 ) ) {
998 return;
999 }
1000 surface->st[ array ][ num ][ 0 ] = st[ 0 ];
1001 surface->st[ array ][ num ][ 1 ] = st[ 1 ];
1002 }
1003
1004
1005
1006 void PicoSetSurfaceColor( picoSurface_t *surface, int array, int num, const picoColor_t color ){
1007 if ( surface == NULL || num < 0 || color == NULL ) {
1008 return;
1009 }
1010 if ( !PicoAdjustSurface( surface, num + 1, 0, array + 1, 0, 0 ) ) {
1011 return;
1012 }
1013 surface->color[ array ][ num ][ 0 ] = color[ 0 ];
1014 surface->color[ array ][ num ][ 1 ] = color[ 1 ];
1015 surface->color[ array ][ num ][ 2 ] = color[ 2 ];
1016 surface->color[ array ][ num ][ 3 ] = color[ 3 ];
1017 }
1018
1019
1020
1021 void PicoSetSurfaceIndex( picoSurface_t *surface, int num, picoIndex_t index ){
1022 if ( surface == NULL || num < 0 ) {
1023 return;
1024 }
1025 if ( !PicoAdjustSurface( surface, 0, 0, 0, num + 1, 0 ) ) {
1026 return;
1027 }
1028 surface->index[ num ] = index;
1029 }
1030
1031
1032
1033 void PicoSetSurfaceIndexes( picoSurface_t *surface, int num, picoIndex_t *index, int count ){
1034 if ( num < 0 || index == NULL || count < 1 ) {
1035 return;
1036 }
1037 if ( !PicoAdjustSurface( surface, 0, 0, 0, num + count, 0 ) ) {
1038 return;
1039 }
1040 memcpy( &surface->index[ num ], index, count * sizeof( surface->index[ num ] ) );
1041 }
1042
1043
1044
1045 void PicoSetFaceNormal( picoSurface_t *surface, int num, picoVec3_t normal ){
1046 if ( surface == NULL || num < 0 || normal == NULL ) {
1047 return;
1048 }
1049 if ( !PicoAdjustSurface( surface, 0, 0, 0, 0, num + 1 ) ) {
1050 return;
1051 }
1052 _pico_copy_vec( normal, surface->faceNormal[ num ] );
1053 }
1054
1055
1056 void PicoSetSurfaceSmoothingGroup( picoSurface_t *surface, int num, picoIndex_t smoothingGroup ){
1057 if ( num < 0 ) {
1058 return;
1059 }
1060 if ( !PicoAdjustSurface( surface, num + 1, 0, 0, 0, 0 ) ) {
1061 return;
1062 }
1063 surface->smoothingGroup[ num ] = smoothingGroup;
1064 }
1065
1066
1067 void PicoSetSurfaceSpecial( picoSurface_t *surface, int num, int special ){
1068 if ( surface == NULL || num < 0 || num >= PICO_MAX_SPECIAL ) {
1069 return;
1070 }
1071 surface->special[ num ] = special;
1072 }
1073
1074
1075
1076 /*----------------------------------------------------------------------------
1077 PicoGet*() Getter Functions
1078 ----------------------------------------------------------------------------*/
1079
1080 char *PicoGetModelName( picoModel_t *model ){
1081 if ( model == NULL ) {
1082 return NULL;
1083 }
1084 if ( model->name == NULL ) {
1085 return (char*) "";
1086 }
1087 return model->name;
1088 }
1089
1090
1091
1092 char *PicoGetModelFileName( picoModel_t *model ){
1093 if ( model == NULL ) {
1094 return NULL;
1095 }
1096 if ( model->fileName == NULL ) {
1097 return (char*) "";
1098 }
1099 return model->fileName;
1100 }
1101
1102
1103
1104 int PicoGetModelFrameNum( picoModel_t *model ){
1105 if ( model == NULL ) {
1106 return 0;
1107 }
1108 return model->frameNum;
1109 }
1110
1111
1112
1113 int PicoGetModelNumFrames( picoModel_t *model ){
1114 if ( model == NULL ) {
1115 return 0;
1116 }
1117 return model->numFrames;
1118 }
1119
1120
1121
1122 void *PicoGetModelData( picoModel_t *model ){
1123 if ( model == NULL ) {
1124 return NULL;
1125 }
1126 return model->data;
1127 }
1128
1129
1130
1131 int PicoGetModelNumShaders( picoModel_t *model ){
1132 if ( model == NULL ) {
1133 return 0;
1134 }
1135 return model->numShaders;
1136 }
1137
1138
1139
1140 picoShader_t *PicoGetModelShader( picoModel_t *model, int num ){
1141 /* a few sanity checks */
1142 if ( model == NULL ) {
1143 return NULL;
1144 }
1145 if ( model->shader == NULL ) {
1146 return NULL;
1147 }
1148 if ( num < 0 || num >= model->numShaders ) {
1149 return NULL;
1150 }
1151
1152 /* return the shader */
1153 return model->shader[ num ];
1154 }
1155
1156
1157
1158 int PicoGetModelNumSurfaces( picoModel_t *model ){
1159 if ( model == NULL ) {
1160 return 0;
1161 }
1162 return model->numSurfaces;
1163 }
1164
1165
1166
1167 picoSurface_t *PicoGetModelSurface( picoModel_t *model, int num ){
1168 /* a few sanity checks */
1169 if ( model == NULL ) {
1170 return NULL;
1171 }
1172 if ( model->surface == NULL ) {
1173 return NULL;
1174 }
1175 if ( num < 0 || num >= model->numSurfaces ) {
1176 return NULL;
1177 }
1178
1179 /* return the surface */
1180 return model->surface[ num ];
1181 }
1182
1183
1184
1185 int PicoGetModelTotalVertexes( picoModel_t *model ){
1186 int i, count;
1187
1188
1189 if ( model == NULL ) {
1190 return 0;
1191 }
1192 if ( model->surface == NULL ) {
1193 return 0;
1194 }
1195
1196 count = 0;
1197 for ( i = 0; i < model->numSurfaces; i++ )
1198 count += PicoGetSurfaceNumVertexes( model->surface[ i ] );
1199
1200 return count;
1201 }
1202
1203
1204
1205 int PicoGetModelTotalIndexes( picoModel_t *model ){
1206 int i, count;
1207
1208
1209 if ( model == NULL ) {
1210 return 0;
1211 }
1212 if ( model->surface == NULL ) {
1213 return 0;
1214 }
1215
1216 count = 0;
1217 for ( i = 0; i < model->numSurfaces; i++ )
1218 count += PicoGetSurfaceNumIndexes( model->surface[ i ] );
1219
1220 return count;
1221 }
1222
1223
1224
1225 char *PicoGetShaderName( picoShader_t *shader ){
1226 if ( shader == NULL ) {
1227 return NULL;
1228 }
1229 if ( shader->name == NULL ) {
1230 return (char*) "";
1231 }
1232 return shader->name;
1233 }
1234
1235
1236
1237 char *PicoGetShaderMapName( picoShader_t *shader ){
1238 if ( shader == NULL ) {
1239 return NULL;
1240 }
1241 if ( shader->mapName == NULL ) {
1242 return (char*) "";
1243 }
1244 return shader->mapName;
1245 }
1246
1247
1248
1249 picoByte_t *PicoGetShaderAmbientColor( picoShader_t *shader ){
1250 if ( shader == NULL ) {
1251 return NULL;
1252 }
1253 return shader->ambientColor;
1254 }
1255
1256
1257
1258 picoByte_t *PicoGetShaderDiffuseColor( picoShader_t *shader ){
1259 if ( shader == NULL ) {
1260 return NULL;
1261 }
1262 return shader->diffuseColor;
1263 }
1264
1265
1266
1267 picoByte_t *PicoGetShaderSpecularColor( picoShader_t *shader ){
1268 if ( shader == NULL ) {
1269 return NULL;
1270 }
1271 return shader->specularColor;
1272 }
1273
1274
1275
1276 float PicoGetShaderTransparency( picoShader_t *shader ){
1277 if ( shader == NULL ) {
1278 return 0.0f;
1279 }
1280 return shader->transparency;
1281 }
1282
1283
1284
1285 float PicoGetShaderShininess( picoShader_t *shader ){
1286 if ( shader == NULL ) {
1287 return 0.0f;
1288 }
1289 return shader->shininess;
1290 }
1291
1292
1293
1294 void *PicoGetSurfaceData( picoSurface_t *surface ){
1295 if ( surface == NULL ) {
1296 return NULL;
1297 }
1298 return surface->data;
1299 }
1300
1301
1302
1303 picoSurfaceType_t PicoGetSurfaceType( picoSurface_t *surface ){
1304 if ( surface == NULL ) {
1305 return PICO_BAD;
1306 }
1307 return surface->type;
1308 }
1309
1310
1311
1312 char *PicoGetSurfaceName( picoSurface_t *surface ){
1313 if ( surface == NULL ) {
1314 return NULL;
1315 }
1316 if ( surface->name == NULL ) {
1317 return (char*) "";
1318 }
1319 return surface->name;
1320 }
1321
1322
1323
1324 picoShader_t *PicoGetSurfaceShader( picoSurface_t *surface ){
1325 if ( surface == NULL ) {
1326 return NULL;
1327 }
1328 return surface->shader;
1329 }
1330
1331
1332
1333 int PicoGetSurfaceNumVertexes( picoSurface_t *surface ){
1334 if ( surface == NULL ) {
1335 return 0;
1336 }
1337 return surface->numVertexes;
1338 }
1339
1340
1341
1342 picoVec_t *PicoGetSurfaceXYZ( picoSurface_t *surface, int num ){
1343 if ( surface == NULL || num < 0 || num > surface->numVertexes ) {
1344 return NULL;
1345 }
1346 return surface->xyz[ num ];
1347 }
1348
1349
1350
1351 picoVec_t *PicoGetSurfaceNormal( picoSurface_t *surface, int num ){
1352 if ( surface == NULL || num < 0 || num > surface->numVertexes ) {
1353 return NULL;
1354 }
1355 return surface->normal[ num ];
1356 }
1357
1358
1359
1360 picoVec_t *PicoGetSurfaceST( picoSurface_t *surface, int array, int num ){
1361 if ( surface == NULL || array < 0 || array > surface->numSTArrays || num < 0 || num > surface->numVertexes ) {
1362 return NULL;
1363 }
1364 return surface->st[ array ][ num ];
1365 }
1366
1367
1368
1369 picoByte_t *PicoGetSurfaceColor( picoSurface_t *surface, int array, int num ){
1370 if ( surface == NULL || array < 0 || array > surface->numColorArrays || num < 0 || num > surface->numVertexes ) {
1371 return NULL;
1372 }
1373 return surface->color[ array ][ num ];
1374 }
1375
1376
1377
1378 int PicoGetSurfaceNumIndexes( picoSurface_t *surface ){
1379 if ( surface == NULL ) {
1380 return 0;
1381 }
1382 return surface->numIndexes;
1383 }
1384
1385
1386
1387 picoIndex_t PicoGetSurfaceIndex( picoSurface_t *surface, int num ){
1388 if ( surface == NULL || num < 0 || num > surface->numIndexes ) {
1389 return 0;
1390 }
1391 return surface->index[ num ];
1392 }
1393
1394
1395
1396 picoIndex_t *PicoGetSurfaceIndexes( picoSurface_t *surface, int num ){
1397 if ( surface == NULL || num < 0 || num > surface->numIndexes ) {
1398 return NULL;
1399 }
1400 return &surface->index[ num ];
1401 }
1402
1403
1404 picoVec_t *PicoGetFaceNormal( picoSurface_t *surface, int num ){
1405 if ( surface == NULL || num < 0 || num > surface->numFaceNormals ) {
1406 return NULL;
1407 }
1408 return surface->faceNormal[ num ];
1409 }
1410
1411 picoIndex_t PicoGetSurfaceSmoothingGroup( picoSurface_t *surface, int num ){
1412 if ( surface == NULL || num < 0 || num > surface->numVertexes ) {
1413 return -1;
1414 }
1415 return surface->smoothingGroup[ num ];
1416 }
1417
1418
1419 int PicoGetSurfaceSpecial( picoSurface_t *surface, int num ){
1420 if ( surface == NULL || num < 0 || num >= PICO_MAX_SPECIAL ) {
1421 return 0;
1422 }
1423 return surface->special[ num ];
1424 }
1425
1426
1427
1428 /* ----------------------------------------------------------------------------
1429 hashtable related functions
1430 ---------------------------------------------------------------------------- */
1431
1432 /* hashtable code for faster vertex lookups */
1433 //#define HASHTABLE_SIZE 32768 // 2048 /* power of 2, use & */
1434 const int HASHTABLE_SIZE = 7919; // 32749 // 2039 /* prime, use % */
1435
1436 int PicoGetHashTableSize( void ){
1437 return HASHTABLE_SIZE;
1438 }
1439
1440 #define HASH_USE_EPSILON
1441
1442 #ifdef HASH_USE_EPSILON
1443 #define HASH_XYZ_EPSILON 0.01f
1444 #define HASH_XYZ_EPSILONSPACE_MULTIPLIER 1.f / HASH_XYZ_EPSILON
1445 #define HASH_ST_EPSILON 0.0001f
1446 #define HASH_NORMAL_EPSILON 0.02f
1447 #endif
1448
1449 unsigned int PicoVertexCoordGenerateHash( picoVec3_t xyz ){
1450 unsigned int hash = 0;
1451
1452 #ifndef HASH_USE_EPSILON
1453 hash += ~( *( (unsigned int*) &xyz[ 0 ] ) << 15 );
1454 hash ^= ( *( (unsigned int*) &xyz[ 0 ] ) >> 10 );
1455 hash += ( *( (unsigned int*) &xyz[ 1 ] ) << 3 );
1456 hash ^= ( *( (unsigned int*) &xyz[ 1 ] ) >> 6 );
1457 hash += ~( *( (unsigned int*) &xyz[ 2 ] ) << 11 );
1458 hash ^= ( *( (unsigned int*) &xyz[ 2 ] ) >> 16 );
1459 #else
1460 picoVec3_t xyz_epsilonspace;
1461
1462 _pico_scale_vec( xyz, HASH_XYZ_EPSILONSPACE_MULTIPLIER, xyz_epsilonspace );
1463 xyz_epsilonspace[ 0 ] = (float)floor( xyz_epsilonspace[ 0 ] );
1464 xyz_epsilonspace[ 1 ] = (float)floor( xyz_epsilonspace[ 1 ] );
1465 xyz_epsilonspace[ 2 ] = (float)floor( xyz_epsilonspace[ 2 ] );
1466
1467 hash += ~( *( (unsigned int*) &xyz_epsilonspace[ 0 ] ) << 15 );
1468 hash ^= ( *( (unsigned int*) &xyz_epsilonspace[ 0 ] ) >> 10 );
1469 hash += ( *( (unsigned int*) &xyz_epsilonspace[ 1 ] ) << 3 );
1470 hash ^= ( *( (unsigned int*) &xyz_epsilonspace[ 1 ] ) >> 6 );
1471 hash += ~( *( (unsigned int*) &xyz_epsilonspace[ 2 ] ) << 11 );
1472 hash ^= ( *( (unsigned int*) &xyz_epsilonspace[ 2 ] ) >> 16 );
1473 #endif
1474
1475 //hash = hash & (HASHTABLE_SIZE-1);
1476 hash = hash % ( HASHTABLE_SIZE );
1477 return hash;
1478 }
1479
1480 picoVertexCombinationHash_t **PicoNewVertexCombinationHashTable( void ){
1481 picoVertexCombinationHash_t **hashTable = _pico_alloc( HASHTABLE_SIZE * sizeof( picoVertexCombinationHash_t* ) );
1482
1483 memset( hashTable, 0, HASHTABLE_SIZE * sizeof( picoVertexCombinationHash_t* ) );
1484
1485 return hashTable;
1486 }
1487
1488 void PicoFreeVertexCombinationHashTable( picoVertexCombinationHash_t **hashTable ){
1489 int i;
1490 picoVertexCombinationHash_t *vertexCombinationHash;
1491 picoVertexCombinationHash_t *nextVertexCombinationHash;
1492
1493 /* dummy check */
1494 if ( hashTable == NULL ) {
1495 return;
1496 }
1497
1498 for ( i = 0; i < HASHTABLE_SIZE; i++ )
1499 {
1500 if ( hashTable[ i ] ) {
1501 nextVertexCombinationHash = NULL;
1502
1503 for ( vertexCombinationHash = hashTable[ i ]; vertexCombinationHash; vertexCombinationHash = nextVertexCombinationHash )
1504 {
1505 nextVertexCombinationHash = vertexCombinationHash->next;
1506 if ( vertexCombinationHash->data != NULL ) {
1507 _pico_free( vertexCombinationHash->data );
1508 }
1509 _pico_free( vertexCombinationHash );
1510 }
1511 }
1512 }
1513
1514 _pico_free( hashTable );
1515 }
1516
1517 picoVertexCombinationHash_t *PicoFindVertexCombinationInHashTable( picoVertexCombinationHash_t **hashTable, picoVec3_t xyz, picoVec3_t normal, picoVec3_t st, picoColor_t color ){
1518 unsigned int hash;
1519 picoVertexCombinationHash_t *vertexCombinationHash;
1520
1521 /* dumy check */
1522 if ( hashTable == NULL || xyz == NULL || normal == NULL || st == NULL || color == NULL ) {
1523 return NULL;
1524 }
1525
1526 hash = PicoVertexCoordGenerateHash( xyz );
1527
1528 for ( vertexCombinationHash = hashTable[ hash ]; vertexCombinationHash; vertexCombinationHash = vertexCombinationHash->next )
1529 {
1530 #ifndef HASH_USE_EPSILON
1531 /* check xyz */
1532 if ( ( vertexCombinationHash->vcd.xyz[ 0 ] != xyz[ 0 ] || vertexCombinationHash->vcd.xyz[ 1 ] != xyz[ 1 ] || vertexCombinationHash->vcd.xyz[ 2 ] != xyz[ 2 ] ) ) {
1533 continue;
1534 }
1535
1536 /* check normal */
1537 if ( ( vertexCombinationHash->vcd.normal[ 0 ] != normal[ 0 ] || vertexCombinationHash->vcd.normal[ 1 ] != normal[ 1 ] || vertexCombinationHash->vcd.normal[ 2 ] != normal[ 2 ] ) ) {
1538 continue;
1539 }
1540
1541 /* check st */
1542 if ( vertexCombinationHash->vcd.st[ 0 ] != st[ 0 ] || vertexCombinationHash->vcd.st[ 1 ] != st[ 1 ] ) {
1543 continue;
1544 }
1545 #else
1546 /* check xyz */
1547 if ( ( fabs( xyz[ 0 ] - vertexCombinationHash->vcd.xyz[ 0 ] ) ) > HASH_XYZ_EPSILON ||
1548 ( fabs( xyz[ 1 ] - vertexCombinationHash->vcd.xyz[ 1 ] ) ) > HASH_XYZ_EPSILON ||
1549 ( fabs( xyz[ 2 ] - vertexCombinationHash->vcd.xyz[ 2 ] ) ) > HASH_XYZ_EPSILON ) {
1550 continue;
1551 }
1552
1553 /* check normal */
1554 if ( ( fabs( normal[ 0 ] - vertexCombinationHash->vcd.normal[ 0 ] ) ) > HASH_NORMAL_EPSILON ||
1555 ( fabs( normal[ 1 ] - vertexCombinationHash->vcd.normal[ 1 ] ) ) > HASH_NORMAL_EPSILON ||
1556 ( fabs( normal[ 2 ] - vertexCombinationHash->vcd.normal[ 2 ] ) ) > HASH_NORMAL_EPSILON ) {
1557 continue;
1558 }
1559
1560 /* check st */
1561 if ( ( fabs( st[ 0 ] - vertexCombinationHash->vcd.st[ 0 ] ) ) > HASH_ST_EPSILON ||
1562 ( fabs( st[ 1 ] - vertexCombinationHash->vcd.st[ 1 ] ) ) > HASH_ST_EPSILON ) {
1563 continue;
1564 }
1565 #endif
1566
1567 /* check color */
1568 if ( *( (int*) vertexCombinationHash->vcd.color ) != *( (int*) color ) ) {
1569 continue;
1570 }
1571
1572 /* gotcha */
1573 return vertexCombinationHash;
1574 }
1575
1576 return NULL;
1577 }
1578
1579 picoVertexCombinationHash_t *PicoAddVertexCombinationToHashTable( picoVertexCombinationHash_t **hashTable, picoVec3_t xyz, picoVec3_t normal, picoVec3_t st, picoColor_t color, picoIndex_t index ){
1580 unsigned int hash;
1581 picoVertexCombinationHash_t *vertexCombinationHash;
1582
1583 /* dumy check */
1584 if ( hashTable == NULL || xyz == NULL || normal == NULL || st == NULL || color == NULL ) {
1585 return NULL;
1586 }
1587
1588 vertexCombinationHash = _pico_alloc( sizeof( picoVertexCombinationHash_t ) );
1589
1590 if ( !vertexCombinationHash ) {
1591 return NULL;
1592 }
1593
1594 hash = PicoVertexCoordGenerateHash( xyz );
1595
1596 _pico_copy_vec( xyz, vertexCombinationHash->vcd.xyz );
1597 _pico_copy_vec( normal, vertexCombinationHash->vcd.normal );
1598 _pico_copy_vec2( st, vertexCombinationHash->vcd.st );
1599 _pico_copy_color( color, vertexCombinationHash->vcd.color );
1600 vertexCombinationHash->index = index;
1601 vertexCombinationHash->data = NULL;
1602 vertexCombinationHash->next = hashTable[ hash ];
1603 hashTable[ hash ] = vertexCombinationHash;
1604
1605 return vertexCombinationHash;
1606 }
1607
1608 /* ----------------------------------------------------------------------------
1609 specialized routines
1610 ---------------------------------------------------------------------------- */
1611
1612 /*
1613 PicoFindSurfaceVertex()
1614 finds a vertex matching the set parameters
1615 fixme: needs non-naive algorithm
1616 */
1617
1618 int PicoFindSurfaceVertexNum( picoSurface_t *surface, picoVec3_t xyz, picoVec3_t normal, int numSTs, picoVec2_t *st, int numColors, const picoColor_t *color, picoIndex_t smoothingGroup ){
1619 int i, j;
1620
1621
1622 /* dummy check */
1623 if ( surface == NULL || surface->numVertexes <= 0 ) {
1624 return -1;
1625 }
1626
1627 /* walk vertex list */
1628 for ( i = 0; i < surface->numVertexes; i++ )
1629 {
1630 /* check xyz */
1631 if ( xyz != NULL && ( surface->xyz[ i ][ 0 ] != xyz[ 0 ] || surface->xyz[ i ][ 1 ] != xyz[ 1 ] || surface->xyz[ i ][ 2 ] != xyz[ 2 ] ) ) {
1632 continue;
1633 }
1634
1635 /* check normal */
1636 if ( normal != NULL && ( surface->normal[ i ][ 0 ] != normal[ 0 ] || surface->normal[ i ][ 1 ] != normal[ 1 ] || surface->normal[ i ][ 2 ] != normal[ 2 ] ) ) {
1637 continue;
1638 }
1639
1640 /* check normal */
1641 if ( surface->smoothingGroup[ i ] != smoothingGroup ) {
1642 continue;
1643 }
1644
1645 /* check st */
1646 if ( numSTs > 0 && st != NULL ) {
1647 for ( j = 0; j < numSTs; j++ )
1648 {
1649 if ( surface->st[ j ][ i ][ 0 ] != st[ j ][ 0 ] || surface->st[ j ][ i ][ 1 ] != st[ j ][ 1 ] ) {
1650 break;
1651 }
1652 }
1653 if ( j != numSTs ) {
1654 continue;
1655 }
1656 }
1657
1658 /* check color */
1659 if ( numColors > 0 && color != NULL ) {
1660 for ( j = 0; j < numSTs; j++ )
1661 {
1662 if ( *( (const int*) surface->color[ j ] ) != *( (const int*) color[ j ] ) ) {
1663 break;
1664 }
1665 }
1666 if ( j != numColors ) {
1667 continue;
1668 }
1669 }
1670
1671 /* vertex matches */
1672 return i;
1673 }
1674
1675 /* nada */
1676 return -1;
1677 }
1678
1679
1680
1681
1682 typedef struct _IndexArray IndexArray;
1683 struct _IndexArray
1684 {
1685 picoIndex_t* data;
1686 picoIndex_t* last;
1687 };
1688
1689 void indexarray_push_back( IndexArray* self, picoIndex_t value ){
1690 *self->last++ = value;
1691 }
1692
1693 size_t indexarray_size( IndexArray* self ){
1694 return self->last - self->data;
1695 }
1696
1697 void indexarray_reserve( IndexArray* self, size_t size ){
1698 self->data = self->last = _pico_calloc( size, sizeof( picoIndex_t ) );
1699 }
1700
1701 void indexarray_clear( IndexArray* self ){
1702 _pico_free( self->data );
1703 }
1704
1705 typedef struct _BinaryTreeNode BinaryTreeNode;
1706 struct _BinaryTreeNode
1707 {
1708 picoIndex_t left;
1709 picoIndex_t right;
1710 };
1711
1712 typedef struct _BinaryTree BinaryTree;
1713 struct _BinaryTree
1714 {
1715 BinaryTreeNode* data;
1716 BinaryTreeNode* last;
1717 };
1718
1719 void binarytree_extend( BinaryTree* self ){
1720 self->last->left = 0;
1721 self->last->right = 0;
1722 ++self->last;
1723 }
1724
1725 size_t binarytree_size( BinaryTree* self ){
1726 return self->last - self->data;
1727 }
1728
1729 void binarytree_reserve( BinaryTree* self, size_t size ){
1730 self->data = self->last = _pico_calloc( size, sizeof( BinaryTreeNode ) );
1731 }
1732
1733 void binarytree_clear( BinaryTree* self ){
1734 _pico_free( self->data );
1735 }
1736
1737 typedef int ( *LessFunc )( void*, picoIndex_t, picoIndex_t );
1738
1739 typedef struct _UniqueIndices UniqueIndices;
1740 struct _UniqueIndices
1741 {
1742 BinaryTree tree;
1743 IndexArray indices;
1744 LessFunc lessFunc;
1745 void* lessData;
1746 };
1747
1748 size_t UniqueIndices_size( UniqueIndices* self ){
1749 return binarytree_size( &self->tree );
1750 }
1751
1752 void UniqueIndices_reserve( UniqueIndices* self, size_t size ){
1753 binarytree_reserve( &self->tree, size );
1754 indexarray_reserve( &self->indices, size );
1755 }
1756
1757 void UniqueIndices_init( UniqueIndices* self, LessFunc lessFunc, void* lessData ){
1758 self->lessFunc = lessFunc;
1759 self->lessData = lessData;
1760 }
1761
1762 void UniqueIndices_destroy( UniqueIndices* self ){
1763 binarytree_clear( &self->tree );
1764 indexarray_clear( &self->indices );
1765 }
1766
1767
1768 picoIndex_t UniqueIndices_find_or_insert( UniqueIndices* self, picoIndex_t value ){
1769 picoIndex_t index = 0;
1770
1771 for (;; )
1772 {
1773 if ( self->lessFunc( self->lessData, value, self->indices.data[index] ) ) {
1774 BinaryTreeNode* node = self->tree.data + index;
1775 if ( node->left != 0 ) {
1776 index = node->left;
1777 continue;
1778 }
1779 else
1780 {
1781 node->left = (picoIndex_t)binarytree_size( &self->tree );
1782 binarytree_extend( &self->tree );
1783 indexarray_push_back( &self->indices, value );
1784 return node->left;
1785 }
1786 }
1787 if ( self->lessFunc( self->lessData, self->indices.data[index], value ) ) {
1788 BinaryTreeNode* node = self->tree.data + index;
1789 if ( node->right != 0 ) {
1790 index = node->right;
1791 continue;
1792 }
1793 else
1794 {
1795 node->right = (picoIndex_t)binarytree_size( &self->tree );
1796 binarytree_extend( &self->tree );
1797 indexarray_push_back( &self->indices, value );
1798 return node->right;
1799 }
1800 }
1801
1802 return index;
1803 }
1804 }
1805
1806 picoIndex_t UniqueIndices_insert( UniqueIndices* self, picoIndex_t value ){
1807 if ( self->tree.data == self->tree.last ) {
1808 binarytree_extend( &self->tree );
1809 indexarray_push_back( &self->indices, value );
1810 return 0;
1811 }
1812 else
1813 {
1814 return UniqueIndices_find_or_insert( self, value );
1815 }
1816 }
1817
1818 typedef struct picoSmoothVertices_s picoSmoothVertices_t;
1819 struct picoSmoothVertices_s
1820 {
1821 picoVec3_t* xyz;
1822 picoIndex_t* smoothingGroups;
1823 };
1824
1825 int lessSmoothVertex( void* data, picoIndex_t first, picoIndex_t second ){
1826 picoSmoothVertices_t* smoothVertices = data;
1827
1828 if ( smoothVertices->xyz[first][0] != smoothVertices->xyz[second][0] ) {
1829 return smoothVertices->xyz[first][0] < smoothVertices->xyz[second][0];
1830 }
1831 if ( smoothVertices->xyz[first][1] != smoothVertices->xyz[second][1] ) {
1832 return smoothVertices->xyz[first][1] < smoothVertices->xyz[second][1];
1833 }
1834 if ( smoothVertices->xyz[first][2] != smoothVertices->xyz[second][2] ) {
1835 return smoothVertices->xyz[first][2] < smoothVertices->xyz[second][2];
1836 }
1837 if ( smoothVertices->smoothingGroups[first] != smoothVertices->smoothingGroups[second] ) {
1838 return smoothVertices->smoothingGroups[first] < smoothVertices->smoothingGroups[second];
1839 }
1840 return 0;
1841 }
1842
1843 void _pico_vertices_combine_shared_normals( picoVec3_t* xyz, picoIndex_t* smoothingGroups, picoVec3_t* normals, picoIndex_t numVertices ){
1844 UniqueIndices vertices;
1845 IndexArray indices;
1846 picoSmoothVertices_t smoothVertices = { xyz, smoothingGroups };
1847 UniqueIndices_init( &vertices, lessSmoothVertex, &smoothVertices );
1848 UniqueIndices_reserve( &vertices, numVertices );
1849 indexarray_reserve( &indices, numVertices );
1850
1851
1852 {
1853 picoIndex_t i = 0;
1854 for (; i < numVertices; ++i )
1855 {
1856 size_t size = UniqueIndices_size( &vertices );
1857 picoIndex_t index = UniqueIndices_insert( &vertices, i );
1858 if ( (size_t)index != size ) {
1859 float* normal = normals[vertices.indices.data[index]];
1860 _pico_add_vec( normal, normals[i], normal );
1861 }
1862 indexarray_push_back( &indices, index );
1863 }
1864 }
1865
1866 {
1867 picoIndex_t maxIndex = 0;
1868 picoIndex_t* i = indices.data;
1869 for (; i != indices.last; ++i )
1870 {
1871 if ( *i <= maxIndex ) {
1872 _pico_copy_vec( normals[vertices.indices.data[*i]], normals[i - indices.data] );
1873 }
1874 else
1875 {
1876 maxIndex = *i;
1877 }
1878 }
1879 }
1880
1881 UniqueIndices_destroy( &vertices );
1882 indexarray_clear( &indices );
1883 }
1884
1885 typedef picoVec3_t* picoNormalIter_t;
1886 typedef picoIndex_t* picoIndexIter_t;
1887
1888 #define THE_CROSSPRODUCTS_OF_ANY_PAIR_OF_EDGES_OF_A_GIVEN_TRIANGLE_ARE_EQUAL 1
1889
1890 void _pico_triangles_generate_weighted_normals( picoIndexIter_t first, picoIndexIter_t end, picoVec3_t* xyz, picoVec3_t* normals ){
1891 for (; first != end; first += 3 )
1892 {
1893 #if ( THE_CROSSPRODUCTS_OF_ANY_PAIR_OF_EDGES_OF_A_GIVEN_TRIANGLE_ARE_EQUAL )
1894 picoVec3_t weightedNormal;
1895 {
1896 float* a = xyz[*( first + 0 )];
1897 float* b = xyz[*( first + 1 )];
1898 float* c = xyz[*( first + 2 )];
1899 picoVec3_t ba, ca;
1900 _pico_subtract_vec( b, a, ba );
1901 _pico_subtract_vec( c, a, ca );
1902 _pico_cross_vec( ca, ba, weightedNormal );
1903 }
1904 #endif
1905 {
1906 int j = 0;
1907 for (; j < 3; ++j )
1908 {
1909 float* normal = normals[*( first + j )];
1910 #if ( !THE_CROSSPRODUCTS_OF_ANY_PAIR_OF_EDGES_OF_A_GIVEN_TRIANGLE_ARE_EQUAL )
1911 picoVec3_t weightedNormal;
1912 {
1913 float* a = xyz[*( first + ( ( j + 0 ) % 3 ) )];
1914 float* b = xyz[*( first + ( ( j + 1 ) % 3 ) )];
1915 float* c = xyz[*( first + ( ( j + 2 ) % 3 ) )];
1916 picoVec3_t ba, ca;
1917 _pico_subtract_vec( b, a, ba );
1918 _pico_subtract_vec( c, a, ca );
1919 _pico_cross_vec( ca, ba, weightedNormal );
1920 }
1921 #endif
1922 _pico_add_vec( weightedNormal, normal, normal );
1923 }
1924 }
1925 }
1926 }
1927
1928 void _pico_normals_zero( picoNormalIter_t first, picoNormalIter_t last ){
1929 for (; first != last; ++first )
1930 {
1931 _pico_zero_vec( *first );
1932 }
1933 }
1934
1935 void _pico_normals_normalize( picoNormalIter_t first, picoNormalIter_t last ){
1936 for (; first != last; ++first )
1937 {
1938 _pico_normalize_vec( *first );
1939 }
1940 }
1941
1942 double _pico_length_vec( picoVec3_t vec ){
1943 return sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] + vec[ 2 ] * vec[ 2 ] );
1944 }
1945
1946 #define NORMAL_UNIT_LENGTH_EPSILON 0.01
1947 #define FLOAT_EQUAL_EPSILON( f, other, epsilon ) ( fabs( f - other ) < epsilon )
1948
1949 int _pico_normal_is_unit_length( picoVec3_t normal ){
1950 return FLOAT_EQUAL_EPSILON( _pico_length_vec( normal ), 1.0, NORMAL_UNIT_LENGTH_EPSILON );
1951 }
1952
1953 int _pico_normal_within_tolerance( picoVec3_t normal, picoVec3_t other ){
1954 return _pico_dot_vec( normal, other ) > 0.0f;
1955 }
1956
1957
1958 void _pico_normals_assign_generated_normals( picoNormalIter_t first, picoNormalIter_t last, picoNormalIter_t generated ){
1959 for (; first != last; ++first, ++generated )
1960 {
1961 if ( !_pico_normal_is_unit_length( *first ) || !_pico_normal_within_tolerance( *first, *generated ) ) {
1962 _pico_copy_vec( *generated, *first );
1963 }
1964 }
1965 }
1966
1967 void PicoFixSurfaceNormals( picoSurface_t* surface ){
1968 picoVec3_t* normals = (picoVec3_t*)_pico_calloc( surface->numVertexes, sizeof( picoVec3_t ) );
1969
1970 _pico_normals_zero( normals, normals + surface->numVertexes );
1971
1972 _pico_triangles_generate_weighted_normals( surface->index, surface->index + surface->numIndexes, surface->xyz, normals );
1973 _pico_vertices_combine_shared_normals( surface->xyz, surface->smoothingGroup, normals, surface->numVertexes );
1974
1975 _pico_normals_normalize( normals, normals + surface->numVertexes );
1976
1977 _pico_normals_assign_generated_normals( surface->normal, surface->normal + surface->numVertexes, normals );
1978
1979 _pico_free( normals );
1980 }
1981
1982
1983 /*
1984 PicoRemapModel() - sea
1985 remaps model material/etc. information using the remappings
1986 contained in the given 'remapFile' (full path to the ascii file to open)
1987 returns 1 on success or 0 on error
1988 */
1989
1990 #define _prm_error_return \
1991 { \
1992 _pico_free_parser( p ); \
1993 _pico_free_file( remapBuffer ); \
1994 return 0; \
1995 }
1996
1997 int PicoRemapModel( picoModel_t *model, char *remapFile ){
1998 picoParser_t *p;
1999 picoByte_t *remapBuffer;
2000 int remapBufSize;
2001
2002
2003 /* sanity checks */
2004 if ( model == NULL || remapFile == NULL ) {
2005 return 0;
2006 }
2007
2008 /* load remap file contents */
2009 _pico_load_file( remapFile,&remapBuffer,&remapBufSize );
2010
2011 /* check result */
2012 if ( remapBufSize == 0 ) {
2013 return 1; /* file is empty: no error */
2014 }
2015 if ( remapBufSize < 0 ) {
2016 return 0; /* load failed: error */
2017
2018 }
2019 /* create a new pico parser */
2020 p = _pico_new_parser( remapBuffer, remapBufSize );
2021 if ( p == NULL ) {
2022 /* ram is really cheap nowadays... */
2023 _prm_error_return;
2024 }
2025
2026 /* doo teh parse */
2027 while ( 1 )
2028 {
2029 /* get next token in remap file */
2030 if ( !_pico_parse( p,1 ) ) {
2031 break;
2032 }
2033
2034 /* skip over c++ style comment lines */
2035 if ( !_pico_stricmp( p->token,"//" ) ) {
2036 _pico_parse_skip_rest( p );
2037 continue;
2038 }
2039
2040 /* block for quick material shader name remapping */
2041 /* materials { "m" (=>|->|=) "s" } */
2042 if ( !_pico_stricmp( p->token, "materials" ) ) {
2043 int level = 1;
2044
2045 /* check bracket */
2046 if ( !_pico_parse_check( p,1,"{" ) ) {
2047 _prm_error_return;
2048 }
2049
2050 /* process assignments */
2051 while ( 1 )
2052 {
2053 picoShader_t *shader;
2054 char *materialName;
2055
2056
2057 /* get material name */
2058 if ( _pico_parse( p,1 ) == NULL ) {
2059 break;
2060 }
2061 if ( !strlen( p->token ) ) {
2062 continue;
2063 }
2064 materialName = _pico_clone_alloc( p->token );
2065 if ( materialName == NULL ) {
2066 _prm_error_return;
2067 }
2068
2069 /* handle levels */
2070 if ( p->token[0] == '{' ) {
2071 level++;
2072 }
2073 if ( p->token[0] == '}' ) {
2074 level--;
2075 }
2076 if ( !level ) {
2077 break;
2078 }
2079
2080 /* get next token (assignment token or shader name) */
2081 if ( !_pico_parse( p,0 ) ) {
2082 _pico_free( materialName );
2083 _prm_error_return;
2084 }
2085 /* skip assignment token (if present) */
2086 if ( !strcmp( p->token,"=>" ) ||
2087 !strcmp( p->token,"->" ) ||
2088 !strcmp( p->token,"=" ) ) {
2089 /* simply grab the next token */
2090 if ( !_pico_parse( p,0 ) ) {
2091 _pico_free( materialName );
2092 _prm_error_return;
2093 }
2094 }
2095 /* try to find material by name */
2096 shader = PicoFindShader( model,materialName,0 );
2097
2098 /* we've found a material matching the name */
2099 if ( shader != NULL ) {
2100 PicoSetShaderName( shader,p->token );
2101 }
2102 /* free memory used by material name */
2103 _pico_free( materialName );
2104
2105 /* skip rest */
2106 _pico_parse_skip_rest( p );
2107 }
2108 }
2109 /* block for detailed single material remappings */
2110 /* materials[ "m" ] { key data... } */
2111 else if ( !_pico_stricmp( p->token,"materials[" ) ) {
2112 picoShader_t *shader;
2113 char *tempMaterialName;
2114 int level = 1;
2115
2116 /* get material name */
2117 if ( !_pico_parse( p,0 ) ) {
2118 _prm_error_return;
2119 }
2120
2121 /* temporary copy of material name */
2122 tempMaterialName = _pico_clone_alloc( p->token );
2123 if ( tempMaterialName == NULL ) {
2124 _prm_error_return;
2125 }
2126
2127 /* check square closing bracket */
2128 if ( !_pico_parse_check( p,0,"]" ) ) {
2129 _prm_error_return;
2130 }
2131
2132 /* try to find material by name */
2133 shader = PicoFindShader( model,tempMaterialName,0 );
2134
2135 /* free memory used by temporary material name */
2136 _pico_free( tempMaterialName );
2137
2138 /* we haven't found a material matching the name */
2139 /* so we simply skip the braced section now and */
2140 /* continue parsing with the next main token */
2141 if ( shader == NULL ) {
2142 _pico_parse_skip_braced( p );
2143 continue;
2144 }
2145 /* check opening bracket */
2146 if ( !_pico_parse_check( p,1,"{" ) ) {
2147 _prm_error_return;
2148 }
2149
2150 /* process material info keys */
2151 while ( 1 )
2152 {
2153 /* get key name */
2154 if ( _pico_parse( p,1 ) == NULL ) {
2155 break;
2156 }
2157 if ( !strlen( p->token ) ) {
2158 continue;
2159 }
2160
2161 /* handle levels */
2162 if ( p->token[0] == '{' ) {
2163 level++;
2164 }
2165 if ( p->token[0] == '}' ) {
2166 level--;
2167 }
2168 if ( !level ) {
2169 break;
2170 }
2171
2172 /* remap shader name */
2173 if ( !_pico_stricmp( p->token,"shader" ) ) {
2174 if ( !_pico_parse( p,0 ) ) {
2175 _prm_error_return;
2176 }
2177 PicoSetShaderName( shader,p->token );
2178 }
2179 /* remap shader map name */
2180 else if ( !_pico_stricmp( p->token,"mapname" ) ) {
2181 if ( !_pico_parse( p,0 ) ) {
2182 _prm_error_return;
2183 }
2184 PicoSetShaderMapName( shader,p->token );
2185 }
2186 /* remap shader's ambient color */
2187 else if ( !_pico_stricmp( p->token,"ambient" ) ) {
2188 picoColor_t color;
2189 picoVec3_t v;
2190
2191 /* get vector from parser */
2192 if ( !_pico_parse_vec( p,v ) ) {
2193 _prm_error_return;
2194 }
2195
2196 /* store as color */
2197 color[ 0 ] = (picoByte_t)v[ 0 ];
2198 color[ 1 ] = (picoByte_t)v[ 1 ];
2199 color[ 2 ] = (picoByte_t)v[ 2 ];
2200 color[ 3 ] = 0;
2201
2202 /* set new ambient color */
2203 PicoSetShaderAmbientColor( shader,color );
2204 }
2205 /* remap shader's diffuse color */
2206 else if ( !_pico_stricmp( p->token,"diffuse" ) ) {
2207 picoColor_t color;
2208 picoVec3_t v;
2209
2210 /* get vector from parser */
2211 if ( !_pico_parse_vec( p,v ) ) {
2212 _prm_error_return;
2213 }
2214
2215 /* store as color */
2216 color[ 0 ] = (picoByte_t)v[ 0 ];
2217 color[ 1 ] = (picoByte_t)v[ 1 ];
2218 color[ 2 ] = (picoByte_t)v[ 2 ];
2219 color[ 3 ] = 0;
2220
2221 /* set new ambient color */
2222 PicoSetShaderDiffuseColor( shader,color );
2223 }
2224 /* remap shader's specular color */
2225 else if ( !_pico_stricmp( p->token,"specular" ) ) {
2226 picoColor_t color;
2227 picoVec3_t v;
2228
2229 /* get vector from parser */
2230 if ( !_pico_parse_vec( p,v ) ) {
2231 _prm_error_return;
2232 }
2233
2234 /* store as color */
2235 color[ 0 ] = (picoByte_t)v[ 0 ];
2236 color[ 1 ] = (picoByte_t)v[ 1 ];
2237 color[ 2 ] = (picoByte_t)v[ 2 ];
2238 color[ 3 ] = 0;
2239
2240 /* set new ambient color */
2241 PicoSetShaderSpecularColor( shader,color );
2242 }
2243 /* skip rest */
2244 _pico_parse_skip_rest( p );
2245 }
2246 }
2247 /* end 'materials[' */
2248 }
2249
2250 /* free both parser and file buffer */
2251 _pico_free_parser( p );
2252 _pico_free_file( remapBuffer );
2253
2254 /* return with success */
2255 return 1;
2256 }
2257
2258
2259 /*
2260 PicoAddTriangleToModel() - jhefty
2261 A nice way to add individual triangles to the model.
2262 Chooses an appropriate surface based on the shader, or adds a new surface if necessary
2263 */
2264
2265 void PicoAddTriangleToModel( picoModel_t *model, picoVec3_t** xyz, picoVec3_t** normals,
2266 int numSTs, picoVec2_t **st, int numColors, const picoColor_t **colors,
2267 picoShader_t* shader, const char *name, picoIndex_t* smoothingGroup ){
2268 int i,j;
2269 int vertDataIndex;
2270 picoSurface_t* workSurface = NULL;
2271
2272 /* see if a surface already has the shader */
2273 for ( i = 0 ; i < model->numSurfaces ; i++ )
2274 {
2275 workSurface = model->surface[i];
2276 if ( !name || !strcmp( workSurface->name, name ) ) {
2277 if ( workSurface->shader == shader ) {
2278 break;
2279 }
2280 }
2281 }
2282
2283 /* no surface uses this shader yet, so create a new surface */
2284 if ( !workSurface || i >= model->numSurfaces ) {
2285 /* create a new surface in the model for the unique shader */
2286 workSurface = PicoNewSurface( model );
2287 if ( !workSurface ) {
2288 _pico_printf( PICO_ERROR, "Could not allocate a new surface!\n" );
2289 return;
2290 }
2291
2292 /* do surface setup */
2293 PicoSetSurfaceType( workSurface, PICO_TRIANGLES );
2294 PicoSetSurfaceName( workSurface, name ? name : shader->name );
2295 PicoSetSurfaceShader( workSurface, shader );
2296 }
2297
2298 /* add the triangle data to the surface */
2299 for ( i = 0 ; i < 3 ; i++ )
2300 {
2301 /* get the next free spot in the index array */
2302 int newVertIndex = PicoGetSurfaceNumIndexes( workSurface );
2303
2304 /* get the index of the vertex that we're going to store at newVertIndex */
2305 vertDataIndex = PicoFindSurfaceVertexNum( workSurface, *xyz[i], *normals[i], numSTs, st[i], numColors, colors[i], smoothingGroup[i] );
2306
2307 /* the vertex wasn't found, so create a new vertex in the pool from the data we have */
2308 if ( vertDataIndex == -1 ) {
2309 /* find the next spot for a new vertex */
2310 vertDataIndex = PicoGetSurfaceNumVertexes( workSurface );
2311
2312 /* assign the data to it */
2313 PicoSetSurfaceXYZ( workSurface,vertDataIndex, *xyz[i] );
2314 PicoSetSurfaceNormal( workSurface, vertDataIndex, *normals[i] );
2315
2316 /* make sure to copy over all available ST's and colors for the vertex */
2317 for ( j = 0 ; j < numColors ; j++ )
2318 {
2319 PicoSetSurfaceColor( workSurface, j, vertDataIndex, colors[i][j] );
2320 }
2321 for ( j = 0 ; j < numSTs ; j++ )
2322 {
2323 PicoSetSurfaceST( workSurface, j, vertDataIndex, st[i][j] );
2324 }
2325
2326 PicoSetSurfaceSmoothingGroup( workSurface, vertDataIndex, smoothingGroup[i] );
2327 }
2328
2329 /* add this vertex to the triangle */
2330 PicoSetSurfaceIndex( workSurface, newVertIndex, vertDataIndex );
2331 }
2332 }