Fix MSYS2 issues
[xonotic/netradiant.git] / libs / picomodel.h
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
36
37 /* marker */
38 #ifndef PICOMODEL_H
39 #define PICOMODEL_H
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45
46 #include <stddef.h>
47
48
49
50 /* version */
51 #define PICOMODEL_VERSION       "0.8.20"
52
53
54 /* constants */
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 */
63
64
65 /* types */
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;
73
74 typedef enum
75 {
76         PICO_BAD,
77         PICO_TRIANGLES,
78         PICO_PATCH
79 }
80 picoSurfaceType_t;
81
82 typedef enum
83 {
84         PICO_NORMAL,
85         PICO_VERBOSE,
86         PICO_WARNING,
87         PICO_ERROR,
88         PICO_FATAL
89 }
90 picoPrintLevel_t;
91
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;
96
97 struct picoSurface_s
98 {
99         void                        *data;
100
101         picoModel_t                 *model;     /* owner model */
102
103         picoSurfaceType_t type;
104         char                        *name;      /* sea: surface name */
105         picoShader_t                *shader;    /* ydnar: changed to ptr */
106
107         int numVertexes, maxVertexes;
108         picoVec3_t                  *xyz;
109         picoVec3_t                  *normal;
110         picoIndex_t                 *smoothingGroup;
111
112         int numSTArrays, maxSTArrays;
113         picoVec2_t                  **st;
114
115         int numColorArrays, maxColorArrays;
116         picoColor_t                 **color;
117
118         int numIndexes, maxIndexes;
119         picoIndex_t                 *index;
120
121         int numFaceNormals, maxFaceNormals;
122         picoVec3_t                  *faceNormal;
123
124         int special[ PICO_MAX_SPECIAL ];
125 };
126
127
128 /* seaw0lf */
129 struct picoShader_s
130 {
131         picoModel_t                 *model;         /* owner model */
132
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) */
140 };
141
142 struct picoModel_s
143 {
144         void                        *data;
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 */
149         picoVec3_t mins;
150         picoVec3_t maxs;
151
152         int numShaders, maxShaders;
153         picoShader_t                **shader;
154
155         int numSurfaces, maxSurfaces;
156         picoSurface_t               **surface;
157
158         const picoModule_t          *module;        /* sea */
159 };
160
161
162 /* seaw0lf */
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. */
166 enum
167 {
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 */
174 };
175
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
179
180 #define PM_PARAMS_LOAD \
181         const char *fileName, int frameNum, const void *buffer, int bufSize
182
183 #define PM_PARAMS_CANSAVE \
184         void
185
186 #define PM_PARAMS_SAVE \
187         const char *fileName, picoModel_t * model
188
189 /* pico file format module structure */
190 struct picoModule_s
191 {
192         char                    *version;                               /* internal module version (e.g. '1.5-b2') */
193
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') */
197
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 */
203 };
204
205
206
207 /* general functions */
208 int                         PicoInit( void );
209 void                        PicoShutdown( void );
210 int                         PicoError( void );
211
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* ) );
217
218 const picoModule_t          **PicoModuleList( int *numModules );
219
220 picoModel_t                 *PicoLoadModel( const char *name, int frameNum );
221
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 );
224
225 /* model functions */
226 picoModel_t                 *PicoNewModel( void );
227 void                        PicoFreeModel( picoModel_t *model );
228 int                         PicoAdjustModel( picoModel_t *model, int numShaders, int numSurfaces );
229
230
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 );
235
236
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 );
242
243
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 );
250
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 );
258
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, 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 );
272
273
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 );
286
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 );
294
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 */
300
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 );
311
312
313 /* hashtable related functions */
314 typedef struct picoVertexCombinationData_s
315 {
316         picoVec3_t xyz, normal;
317         picoVec2_t st;
318         picoColor_t color;
319 } picoVertexCombinationData_t;
320
321 typedef struct picoVertexCombinationHash_s
322 {
323         picoVertexCombinationData_t vcd;
324         picoIndex_t index;
325
326         void                        *data;
327
328         struct picoVertexCombinationHash_s  *next;
329 } picoVertexCombinationHash_t;
330
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 );
337
338 /* specialized functions */
339 int                         PicoFindSurfaceVertexNum( picoSurface_t *surface, picoVec3_t xyz, picoVec3_t normal, int numSTs, picoVec2_t *st, int numColors, picoColor_t *color, picoIndex_t smoothingGroup );
340 void                        PicoFixSurfaceNormals( picoSurface_t *surface );
341 int                         PicoRemapModel( picoModel_t *model, char *remapFile );
342
343
344 void PicoAddTriangleToModel( picoModel_t *model, picoVec3_t** xyz, picoVec3_t** normals, int numSTs, picoVec2_t **st, int numColors, picoColor_t **colors, picoShader_t* shader, const char *name, picoIndex_t* smoothingGroup );
345
346 /* end marker */
347 #ifdef __cplusplus
348 }
349 #endif
350
351 #endif