1 /* -----------------------------------------------------------------------------
5 Copyright (c) 2002, Randy Reddig & seaw0lf
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
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.
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.
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.
33 ----------------------------------------------------------------------------- */
51 #define PICOMODEL_VERSION "0.8.20"
55 #define PICO_GROW_SHADERS 16
56 #define PICO_GROW_SURFACES 16
57 #define PICO_GROW_VERTEXES 1024
58 #define PICO_GROW_INDEXES 1024
59 #define PICO_GROW_ARRAYS 8
60 #define PICO_GROW_FACES 256
61 #define PICO_MAX_SPECIAL 8
62 #define PICO_MAX_DEFAULT_EXTS 4 /* max default extensions per module */
66 typedef unsigned char picoByte_t;
67 typedef float picoVec_t;
68 typedef float picoVec2_t[ 2 ];
69 typedef float picoVec3_t[ 3 ];
70 typedef float picoVec4_t[ 4 ];
71 typedef picoByte_t picoColor_t[ 4 ];
72 typedef int picoIndex_t;
92 typedef struct picoSurface_s picoSurface_t;
93 typedef struct picoShader_s picoShader_t;
94 typedef struct picoModel_s picoModel_t;
95 typedef struct picoModule_s picoModule_t;
101 picoModel_t *model; /* owner model */
103 picoSurfaceType_t type;
104 char *name; /* sea: surface name */
105 picoShader_t *shader; /* ydnar: changed to ptr */
107 int numVertexes, maxVertexes;
110 picoIndex_t *smoothingGroup;
112 int numSTArrays, maxSTArrays;
115 int numColorArrays, maxColorArrays;
118 int numIndexes, maxIndexes;
121 int numFaceNormals, maxFaceNormals;
122 picoVec3_t *faceNormal;
124 int special[ PICO_MAX_SPECIAL ];
131 picoModel_t *model; /* owner model */
133 char *name; /* shader name */
134 char *mapName; /* shader file name (name of diffuse texturemap) */
135 picoColor_t ambientColor; /* ambient color of mesh (rgba) */
136 picoColor_t diffuseColor; /* diffuse color of mesh (rgba) */
137 picoColor_t specularColor; /* specular color of mesh (rgba) */
138 float transparency; /* transparency (0..1; 1 = 100% transparent) */
139 float shininess; /* shininess (0..128; 128 = 100% shiny) */
145 char *name; /* model name */
146 char *fileName; /* sea: model file name */
147 int frameNum; /* sea: renamed to frameNum */
148 int numFrames; /* sea: number of frames */
152 int numShaders, maxShaders;
153 picoShader_t **shader;
155 int numSurfaces, maxSurfaces;
156 picoSurface_t **surface;
158 const picoModule_t *module; /* sea */
163 /* return codes used by the validation callbacks; pmv is short */
164 /* for 'pico module validation'. everything >PICO_PMV_OK means */
165 /* that there was an error. */
168 PICO_PMV_OK, /* file valid */
169 PICO_PMV_ERROR, /* file not valid */
170 PICO_PMV_ERROR_IDENT, /* unknown file magic (aka ident) */
171 PICO_PMV_ERROR_VERSION, /* unsupported file version */
172 PICO_PMV_ERROR_SIZE, /* file size error */
173 PICO_PMV_ERROR_MEMORY, /* out of memory error */
176 /* convenience (makes it easy to add new params to the callbacks) */
177 #define PM_PARAMS_CANLOAD \
178 const char *fileName, const void *buffer, int bufSize
180 #define PM_PARAMS_LOAD \
181 const char *fileName, int frameNum, const void *buffer, int bufSize
183 #define PM_PARAMS_CANSAVE \
186 #define PM_PARAMS_SAVE \
187 const char *fileName, picoModel_t * model
189 /* pico file format module structure */
192 char *version; /* internal module version (e.g. '1.5-b2') */
194 char *displayName; /* string used to display in guis, etc. */
195 char *authorName; /* author name (eg. 'My Real Name') */
196 char *copyright; /* copyright year and holder (eg. '2002 My Company') */
198 char *defaultExts[ PICO_MAX_DEFAULT_EXTS ]; /* default file extensions used by this file type */
199 int ( *canload )( PM_PARAMS_CANLOAD ); /* checks whether module can load given file (returns PMVR_*) */
200 picoModel_t *( *load )( PM_PARAMS_LOAD ); /* parses model file data */
201 int ( *cansave )( PM_PARAMS_CANSAVE ); /* checks whether module can save (returns 1 or 0 and might spit out a message) */
202 int ( *save )( PM_PARAMS_SAVE ); /* saves a pico model in module's native model format */
207 /* general functions */
208 int PicoInit( void );
209 void PicoShutdown( void );
210 int PicoError( void );
212 void PicoSetMallocFunc( void *( *func )( size_t ) );
213 void PicoSetFreeFunc( void ( *func )( void* ) );
214 void PicoSetLoadFileFunc( void ( *func )( const char*, unsigned char**, int* ) );
215 void PicoSetFreeFileFunc( void ( *func )( void* ) );
216 void PicoSetPrintFunc( void ( *func )( int, const char* ) );
218 const picoModule_t **PicoModuleList( int *numModules );
220 picoModel_t *PicoLoadModel( const char *name, int frameNum );
222 typedef size_t ( *PicoInputStreamReadFunc )( void* inputStream, unsigned char* buffer, size_t length );
223 picoModel_t* PicoModuleLoadModelStream( const picoModule_t* module, void* inputStream, PicoInputStreamReadFunc inputStreamRead, size_t streamLength, int frameNum, const char *fileName );
225 /* model functions */
226 picoModel_t *PicoNewModel( void );
227 void PicoFreeModel( picoModel_t *model );
228 int PicoAdjustModel( picoModel_t *model, int numShaders, int numSurfaces );
231 /* shader functions */
232 picoShader_t *PicoNewShader( picoModel_t *model );
233 void PicoFreeShader( picoShader_t *shader );
234 picoShader_t *PicoFindShader( picoModel_t *model, char *name, int caseSensitive );
237 /* surface functions */
238 picoSurface_t *PicoNewSurface( picoModel_t *model );
239 void PicoFreeSurface( picoSurface_t *surface );
240 picoSurface_t *PicoFindSurface( picoModel_t *model, char *name, int caseSensitive );
241 int PicoAdjustSurface( picoSurface_t *surface, int numVertexes, int numSTArrays, int numColorArrays, int numIndexes, int numFaceNormals );
244 /* setter functions */
245 void PicoSetModelName( picoModel_t *model, const char *name );
246 void PicoSetModelFileName( picoModel_t *model, const char *fileName );
247 void PicoSetModelFrameNum( picoModel_t *model, int frameNum );
248 void PicoSetModelNumFrames( picoModel_t *model, int numFrames );
249 void PicoSetModelData( picoModel_t *model, void *data );
251 void PicoSetShaderName( picoShader_t *shader, char *name );
252 void PicoSetShaderMapName( picoShader_t *shader, char *mapName );
253 void PicoSetShaderAmbientColor( picoShader_t *shader, picoColor_t color );
254 void PicoSetShaderDiffuseColor( picoShader_t *shader, picoColor_t color );
255 void PicoSetShaderSpecularColor( picoShader_t *shader, picoColor_t color );
256 void PicoSetShaderTransparency( picoShader_t *shader, float value );
257 void PicoSetShaderShininess( picoShader_t *shader, float value );
259 void PicoSetSurfaceData( picoSurface_t *surface, void *data );
260 void PicoSetSurfaceType( picoSurface_t *surface, picoSurfaceType_t type );
261 void PicoSetSurfaceName( picoSurface_t *surface, const char *name );
262 void PicoSetSurfaceShader( picoSurface_t *surface, picoShader_t *shader );
263 void PicoSetSurfaceXYZ( picoSurface_t *surface, int num, picoVec3_t xyz );
264 void PicoSetSurfaceNormal( picoSurface_t *surface, int num, picoVec3_t normal );
265 void PicoSetSurfaceST( picoSurface_t *surface, int array, int num, picoVec2_t st );
266 void PicoSetSurfaceColor( picoSurface_t *surface, int array, int num, const picoColor_t color );
267 void PicoSetSurfaceIndex( picoSurface_t *surface, int num, picoIndex_t index );
268 void PicoSetSurfaceIndexes( picoSurface_t *surface, int num, picoIndex_t *index, int count );
269 void PicoSetFaceNormal( picoSurface_t *surface, int num, picoVec3_t normal );
270 void PicoSetSurfaceSpecial( picoSurface_t *surface, int num, int special );
271 void PicoSetSurfaceSmoothingGroup( picoSurface_t *surface, int num, picoIndex_t smoothingGroup );
274 /* getter functions */
275 char *PicoGetModelName( picoModel_t *model );
276 char *PicoGetModelFileName( picoModel_t *model );
277 int PicoGetModelFrameNum( picoModel_t *model );
278 int PicoGetModelNumFrames( picoModel_t *model );
279 void *PicoGetModelData( picoModel_t *model );
280 int PicoGetModelNumShaders( picoModel_t *model );
281 picoShader_t *PicoGetModelShader( picoModel_t *model, int num ); /* sea */
282 int PicoGetModelNumSurfaces( picoModel_t *model );
283 picoSurface_t *PicoGetModelSurface( picoModel_t *model, int num );
284 int PicoGetModelTotalVertexes( picoModel_t *model );
285 int PicoGetModelTotalIndexes( picoModel_t *model );
287 char *PicoGetShaderName( picoShader_t *shader );
288 char *PicoGetShaderMapName( picoShader_t *shader );
289 picoByte_t *PicoGetShaderAmbientColor( picoShader_t *shader );
290 picoByte_t *PicoGetShaderDiffuseColor( picoShader_t *shader );
291 picoByte_t *PicoGetShaderSpecularColor( picoShader_t *shader );
292 float PicoGetShaderTransparency( picoShader_t *shader );
293 float PicoGetShaderShininess( picoShader_t *shader );
295 void *PicoGetSurfaceData( picoSurface_t *surface );
296 char *PicoGetSurfaceName( picoSurface_t *surface ); /* sea */
297 picoSurfaceType_t PicoGetSurfaceType( picoSurface_t *surface );
298 char *PicoGetSurfaceName( picoSurface_t *surface );
299 picoShader_t *PicoGetSurfaceShader( picoSurface_t *surface ); /* sea */
301 int PicoGetSurfaceNumVertexes( picoSurface_t *surface );
302 picoVec_t *PicoGetSurfaceXYZ( picoSurface_t *surface, int num );
303 picoVec_t *PicoGetSurfaceNormal( picoSurface_t *surface, int num );
304 picoVec_t *PicoGetSurfaceST( picoSurface_t *surface, int array, int num );
305 picoByte_t *PicoGetSurfaceColor( picoSurface_t *surface, int array, int num );
306 int PicoGetSurfaceNumIndexes( picoSurface_t *surface );
307 picoIndex_t PicoGetSurfaceIndex( picoSurface_t *surface, int num );
308 picoIndex_t *PicoGetSurfaceIndexes( picoSurface_t *surface, int num );
309 picoVec_t *PicoGetFaceNormal( picoSurface_t *surface, int num );
310 int PicoGetSurfaceSpecial( picoSurface_t *surface, int num );
313 /* hashtable related functions */
314 typedef struct picoVertexCombinationData_s
316 picoVec3_t xyz, normal;
319 } picoVertexCombinationData_t;
321 typedef struct picoVertexCombinationHash_s
323 picoVertexCombinationData_t vcd;
328 struct picoVertexCombinationHash_s *next;
329 } picoVertexCombinationHash_t;
331 int PicoGetHashTableSize( void );
332 unsigned int PicoVertexCoordGenerateHash( picoVec3_t xyz );
333 picoVertexCombinationHash_t **PicoNewVertexCombinationHashTable( void );
334 void PicoFreeVertexCombinationHashTable( picoVertexCombinationHash_t **hashTable );
335 picoVertexCombinationHash_t *PicoFindVertexCombinationInHashTable( picoVertexCombinationHash_t **hashTable, picoVec3_t xyz, picoVec3_t normal, picoVec3_t st, picoColor_t color );
336 picoVertexCombinationHash_t *PicoAddVertexCombinationToHashTable( picoVertexCombinationHash_t **hashTable, picoVec3_t xyz, picoVec3_t normal, picoVec3_t st, picoColor_t color, picoIndex_t index );
338 /* specialized functions */
339 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 );
340 void PicoFixSurfaceNormals( picoSurface_t *surface );
341 int PicoRemapModel( picoModel_t *model, char *remapFile );
344 void PicoAddTriangleToModel( picoModel_t *model, picoVec3_t** xyz, picoVec3_t** normals, int numSTs, picoVec2_t **st, int numColors, const picoColor_t **colors, picoShader_t* shader, const char *name, picoIndex_t* smoothingGroup );