Adding patch_seam q3map2 regression test. Probably not fixable, but good to
[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
47
48 /* version */
49 #define PICOMODEL_VERSION               "0.8.20"
50
51
52 /* constants */
53 #define PICO_GROW_SHADERS               16
54 #define PICO_GROW_SURFACES              16
55 #define PICO_GROW_VERTEXES              1024
56 #define PICO_GROW_INDEXES               1024
57 #define PICO_GROW_ARRAYS                8
58 #define PICO_GROW_FACES                 256
59 #define PICO_MAX_SPECIAL                8
60 #define PICO_MAX_DEFAULT_EXTS   4               /* max default extensions per module */
61
62
63 /* types */
64 typedef unsigned char                   picoByte_t;
65 typedef float                                   picoVec_t;
66 typedef float                                   picoVec2_t[ 2 ];
67 typedef float                                   picoVec3_t[ 3 ];
68 typedef float                                   picoVec4_t[ 4 ];
69 typedef picoByte_t                      picoColor_t[ 4 ];
70 typedef int                                             picoIndex_t;
71
72 typedef enum
73 {
74         PICO_BAD,
75         PICO_TRIANGLES,
76         PICO_PATCH
77 }
78 picoSurfaceType_t;
79
80 typedef enum
81 {
82         PICO_NORMAL,
83         PICO_VERBOSE,
84         PICO_WARNING,
85         PICO_ERROR,
86         PICO_FATAL
87 }
88 picoPrintLevel_t;
89
90 typedef struct picoSurface_s    picoSurface_t;
91 typedef struct picoShader_s             picoShader_t;
92 typedef struct picoModel_s              picoModel_t;
93 typedef struct picoModule_s             picoModule_t;
94
95 struct picoSurface_s
96 {
97         void                                            *data;
98         
99         picoModel_t                                     *model;         /* owner model */
100         
101         picoSurfaceType_t                       type;
102         char                                            *name;          /* sea: surface name */
103         picoShader_t                            *shader;        /* ydnar: changed to ptr */
104         
105         int                                                     numVertexes, maxVertexes;
106         picoVec3_t                                      *xyz;
107         picoVec3_t                                      *normal;
108         
109         int                                                     numSTArrays, maxSTArrays;
110         picoVec2_t                                      **st;
111         
112         int                                                     numColorArrays, maxColorArrays;
113         picoColor_t                                     **color;
114         
115         int                                                     numIndexes, maxIndexes;
116         picoIndex_t                                     *index;
117
118         int                                                     numFaceNormals, maxFaceNormals;
119         picoVec3_t                                      *faceNormal;
120
121         int                                                     special[ PICO_MAX_SPECIAL ];
122 };
123
124
125 /* seaw0lf */
126 struct picoShader_s
127 {
128         picoModel_t                                     *model;                 /* owner model */
129
130         char                                            *name;                  /* shader name */
131         char                                            *mapName;               /* shader file name (name of diffuse texturemap) */
132         picoColor_t                                     ambientColor;   /* ambient color of mesh (rgba) */
133         picoColor_t                                     diffuseColor;   /* diffuse color of mesh (rgba) */
134         picoColor_t                                     specularColor;  /* specular color of mesh (rgba) */
135         float                                           transparency;   /* transparency (0..1; 1 = 100% transparent) */
136         float                                           shininess;              /* shininess (0..128; 128 = 100% shiny) */
137 };
138
139 struct picoModel_s
140 {
141         void                                            *data;
142         char                                            *name;                  /* model name */
143         char                                            *fileName;              /* sea: model file name */
144         int                                                     frameNum;               /* sea: renamed to frameNum */
145         int                                                     numFrames;              /* sea: number of frames */
146         picoVec3_t                                      mins;
147         picoVec3_t                                      maxs;
148
149         int                                                     numShaders, maxShaders;
150         picoShader_t                            **shader;
151         
152         int                                                     numSurfaces, maxSurfaces;
153         picoSurface_t                           **surface;
154         
155         const picoModule_t                      *module;                /* sea */
156 };
157
158
159 /* seaw0lf */
160 /* return codes used by the validation callbacks; pmv is short */
161 /* for 'pico module validation'. everything >PICO_PMV_OK means */
162 /* that there was an error. */
163 enum
164 {
165         PICO_PMV_OK,                    /* file valid */
166         PICO_PMV_ERROR,                 /* file not valid */
167         PICO_PMV_ERROR_IDENT,   /* unknown file magic (aka ident) */
168         PICO_PMV_ERROR_VERSION, /* unsupported file version */
169         PICO_PMV_ERROR_SIZE,    /* file size error */
170         PICO_PMV_ERROR_MEMORY,  /* out of memory error */
171 };
172
173 /* convenience (makes it easy to add new params to the callbacks) */
174 #define PM_PARAMS_CANLOAD \
175         char *fileName, const void *buffer, int bufSize
176
177 #define PM_PARAMS_LOAD \
178         char *fileName, int frameNum, const void *buffer, int bufSize
179
180 #define PM_PARAMS_CANSAVE \
181         void
182
183 #define PM_PARAMS_SAVE \
184         char *fileName, picoModel_t *model
185
186 /* pico file format module structure */
187 struct picoModule_s
188 {
189         char                                    *version;                                                               /* internal module version (e.g. '1.5-b2') */
190
191         char                                    *displayName;                                                   /* string used to display in guis, etc. */
192         char                                    *authorName;                                                    /* author name (eg. 'My Real Name') */
193         char                                    *copyright;                                                             /* copyright year and holder (eg. '2002 My Company') */
194
195         char                                    *defaultExts[ PICO_MAX_DEFAULT_EXTS ];  /* default file extensions used by this file type */
196         int                                             (*canload)( PM_PARAMS_CANLOAD );                /* checks whether module can load given file (returns PMVR_*) */
197         picoModel_t                             *(*load)( PM_PARAMS_LOAD );                             /* parses model file data */
198         int                                             (*cansave)( PM_PARAMS_CANSAVE );                /* checks whether module can save (returns 1 or 0 and might spit out a message) */
199         int                                             (*save)( PM_PARAMS_SAVE );                              /* saves a pico model in module's native model format */
200 };
201
202
203
204 /* general functions */
205 int                                                     PicoInit( void );
206 void                                            PicoShutdown( void );
207 int                                                     PicoError( void );
208
209 void                                            PicoSetMallocFunc( void *(*func)( size_t ) );
210 void                                            PicoSetFreeFunc( void (*func)( void* ) );
211 void                                            PicoSetLoadFileFunc( void (*func)( char*, unsigned char**, int* ) );
212 void                                            PicoSetFreeFileFunc( void (*func)( void* ) );
213 void                                            PicoSetPrintFunc( void (*func)( int, const char* ) );
214
215 const picoModule_t                      **PicoModuleList( int *numModules );
216
217 picoModel_t                                     *PicoLoadModel( char *name, int frameNum );
218
219
220 /* model functions */
221 picoModel_t                                     *PicoNewModel( void );
222 void                                            PicoFreeModel( picoModel_t *model );
223 int                                                     PicoAdjustModel( picoModel_t *model, int numShaders, int numSurfaces );
224
225
226 /* shader functions */
227 picoShader_t                            *PicoNewShader( picoModel_t *model );
228 void                                            PicoFreeShader( picoShader_t *shader );
229 picoShader_t                            *PicoFindShader( picoModel_t *model, char *name, int caseSensitive );
230
231
232 /* surface functions */
233 picoSurface_t                           *PicoNewSurface( picoModel_t *model );
234 void                                            PicoFreeSurface( picoSurface_t *surface );
235 picoSurface_t                           *PicoFindSurface( picoModel_t *model, char *name, int caseSensitive );
236 int                                                     PicoAdjustSurface( picoSurface_t *surface, int numVertexes, int numSTArrays, int numColorArrays, int numIndexes, int numFaceNormals );
237
238
239 /* setter functions */
240 void                                            PicoSetModelName( picoModel_t *model, char *name );
241 void                                            PicoSetModelFileName( picoModel_t *model, char *fileName );
242 void                                            PicoSetModelFrameNum( picoModel_t *model, int frameNum );
243 void                                            PicoSetModelNumFrames( picoModel_t *model, int numFrames );
244 void                                            PicoSetModelData( picoModel_t *model, void *data );
245
246 void                                            PicoSetShaderName( picoShader_t *shader, char *name );
247 void                                            PicoSetShaderMapName( picoShader_t *shader, char *mapName );
248 void                                            PicoSetShaderAmbientColor( picoShader_t *shader, picoColor_t color );
249 void                                            PicoSetShaderDiffuseColor( picoShader_t *shader, picoColor_t color );
250 void                                            PicoSetShaderSpecularColor( picoShader_t *shader, picoColor_t color );
251 void                                            PicoSetShaderTransparency( picoShader_t *shader, float value );
252 void                                            PicoSetShaderShininess( picoShader_t *shader, float value );
253
254 void                                            PicoSetSurfaceData( picoSurface_t *surface, void *data );
255 void                                            PicoSetSurfaceType( picoSurface_t *surface, picoSurfaceType_t type );
256 void                                            PicoSetSurfaceName( picoSurface_t *surface, char *name );
257 void                                            PicoSetSurfaceShader( picoSurface_t *surface, picoShader_t *shader );
258 void                                            PicoSetSurfaceXYZ( picoSurface_t *surface, int num, picoVec3_t xyz );
259 void                                            PicoSetSurfaceNormal( picoSurface_t *surface, int num, picoVec3_t normal );
260 void                                            PicoSetSurfaceST( picoSurface_t *surface, int array, int num, picoVec2_t st );
261 void                                            PicoSetSurfaceColor( picoSurface_t *surface, int array, int num, picoColor_t color );
262 void                                            PicoSetSurfaceIndex( picoSurface_t *surface, int num, picoIndex_t index );
263 void                                            PicoSetSurfaceIndexes( picoSurface_t *surface, int num, picoIndex_t *index, int count );
264 void                                            PicoSetFaceNormal( picoSurface_t *surface, int num, picoVec3_t normal );
265 void                                            PicoSetSurfaceSpecial( picoSurface_t *surface, int num, int special );
266
267
268 /* getter functions */
269 char                                            *PicoGetModelName( picoModel_t *model );
270 char                                            *PicoGetModelFileName( picoModel_t *model );
271 int                                                     PicoGetModelFrameNum( picoModel_t *model );
272 int                                                     PicoGetModelNumFrames( picoModel_t *model );
273 void                                            *PicoGetModelData( picoModel_t *model );
274 int                                             PicoGetModelNumShaders( picoModel_t *model );
275 picoShader_t                            *PicoGetModelShader( picoModel_t *model, int num );     /* sea */
276 int                                                     PicoGetModelNumSurfaces( picoModel_t *model );
277 picoSurface_t                           *PicoGetModelSurface( picoModel_t *model, int num );
278 int                                             PicoGetModelTotalVertexes( picoModel_t *model );
279 int                                             PicoGetModelTotalIndexes( picoModel_t *model );
280
281 char                                            *PicoGetShaderName( picoShader_t *shader );
282 char                                            *PicoGetShaderMapName( picoShader_t *shader );
283 picoByte_t                                      *PicoGetShaderAmbientColor( picoShader_t *shader );
284 picoByte_t                                      *PicoGetShaderDiffuseColor( picoShader_t *shader );
285 picoByte_t                                      *PicoGetShaderSpecularColor( picoShader_t *shader );
286 float                                           PicoGetShaderTransparency( picoShader_t *shader );
287 float                                           PicoGetShaderShininess( picoShader_t *shader );
288
289 void                                            *PicoGetSurfaceData( picoSurface_t *surface );
290 char                                            *PicoGetSurfaceName( picoSurface_t *surface );          /* sea */
291 picoSurfaceType_t                       PicoGetSurfaceType( picoSurface_t *surface );
292 char                                            *PicoGetSurfaceName( picoSurface_t *surface );
293 picoShader_t                            *PicoGetSurfaceShader( picoSurface_t *surface );        /* sea */
294
295 int                                                     PicoGetSurfaceNumVertexes( picoSurface_t *surface );
296 picoVec_t                                       *PicoGetSurfaceXYZ( picoSurface_t *surface, int num );
297 picoVec_t                                       *PicoGetSurfaceNormal( picoSurface_t *surface, int num );
298 picoVec_t                                       *PicoGetSurfaceST( picoSurface_t *surface, int array, int num );
299 picoByte_t                                      *PicoGetSurfaceColor( picoSurface_t *surface, int array, int num );
300 int                                                     PicoGetSurfaceNumIndexes( picoSurface_t *surface );
301 picoIndex_t                                     PicoGetSurfaceIndex( picoSurface_t *surface, int num );
302 picoIndex_t                                     *PicoGetSurfaceIndexes( picoSurface_t *surface, int num );
303 picoVec_t                                       *PicoGetFaceNormal( picoSurface_t *surface, int num );
304 int                                                     PicoGetSurfaceSpecial( picoSurface_t *surface, int num );
305
306
307 /* hashtable related functions */
308 typedef struct picoVertexCombinationData_s
309 {
310         picoVec3_t      xyz, normal;
311         picoVec2_t      st;
312         picoColor_t     color;
313 } picoVertexCombinationData_t;
314
315 typedef struct picoVertexCombinationHash_s
316 {
317         picoVertexCombinationData_t     vcd;
318         picoIndex_t                                     index;
319
320         void                                            *data;
321
322         struct picoVertexCombinationHash_s      *next;
323 } picoVertexCombinationHash_t;
324
325 int                                                     PicoGetHashTableSize( void );
326 unsigned int                            PicoVertexCoordGenerateHash( picoVec3_t xyz );
327 picoVertexCombinationHash_t     **PicoNewVertexCombinationHashTable( void );
328 void                                            PicoFreeVertexCombinationHashTable( picoVertexCombinationHash_t **hashTable );
329 picoVertexCombinationHash_t     *PicoFindVertexCombinationInHashTable( picoVertexCombinationHash_t **hashTable, picoVec3_t xyz, picoVec3_t normal, picoVec3_t st, picoColor_t color );
330 picoVertexCombinationHash_t *PicoAddVertexCombinationToHashTable( picoVertexCombinationHash_t **hashTable, picoVec3_t xyz, picoVec3_t normal, picoVec3_t st, picoColor_t color, picoIndex_t index );
331
332 /* specialized functions */
333 int                                                     PicoFindSurfaceVertexNum( picoSurface_t *surface, picoVec3_t xyz, picoVec3_t normal, int numSTs, picoVec2_t *st, int numColors, picoColor_t *color );
334 void                                            PicoFixSurfaceNormals( picoSurface_t *surface );
335 int                                                     PicoRemapModel( picoModel_t *model, char *remapFile );
336
337
338 void PicoAddTriangleToModel( picoModel_t *model, picoVec3_t** xyz, picoVec3_t** normals, int numSTs, picoVec2_t **st, int numColors, picoColor_t **colors, picoShader_t* shader );
339
340 /* end marker */
341 #ifdef __cplusplus
342 }
343 #endif
344
345 #endif