VedaLib3DEngine/lwo2reader.h

Go to the documentation of this file.
00001 /*! \file 
00002     \author victorien ferry
00003     \brief This file applies the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 , read file COPYING.
00004         independant lightwave object file format reader.    
00005                 by krb (vic ferry), 02/11/2006, based on previous works.
00006                 features:
00007                     - read LWOB and LWO2 versions.
00008                     - apply the right VMAP/VMAD vertex info for uv texture and rgb, for each polygon (LWO2).
00009                     - manage creation of new vertexes for discontinuous mapping, the best way
00010                      (no extra creation of vertex for a same discontinuity).
00011                     - can give a triangle list with polygons sorted by surface, for GL rendering...
00012                 restrictions:
00013                     - all object polygons must be convex. n-gons with n>3 supported.
00014                     - patch spline surface unsuported ATM.
00015 */
00016 #ifndef LWO2READER_H_
00017 #define LWO2READER_H_
00018 #ifdef __cplusplus
00019 extern "C" {
00020 #endif
00021 /*!
00022     \brief  you must provide this allocator:
00023 */
00024 extern void *lw_CleanAlloc( unsigned int _bytesize );
00025 /*!
00026     \brief  you must provide this desallocator:
00027 */
00028 extern void lw_FreeAlloc( void  *_pAllocToClose );
00029 
00030 /*!
00031     \enum   lwo2_error 
00032     \brief  init error enum returned by ReadLwo()
00033 */
00034 typedef enum
00035 {
00036     lwoerr_Ok=0,
00037     lwoerr_AllocFailed,
00038     lwoerr_FileProblem
00039 } lwo2_error;
00040 
00041 typedef struct _LwoObject sLwoObject;
00042 typedef struct _LwoLayer sLwoLayer;
00043 
00044     //! flag bits for ReadLwo(). Affect the way the objects are read.
00045     typedef enum
00046     {
00047         //! if present, the per-poly discontiuous texture coords are set back on new vertexes. This is needed if your engine only use vertex texture coord,( like usual hardware renderers.)
00048         ReadLwoFlag_DiscontinuousUVToNewVertex=1,
00049         //! Sort polygons by texture used in the polygon list.
00050         ReadLwoFlag_SortTriangleBySurfaces=2
00051     } ReadLwoFlag;
00052 
00053     /*!
00054         \brief  Read a File, open all layer shapes in tables. CloseLwo() must be called
00055                 at destruction and will close all allocs.
00056                 You can specify ReadLwoFlag_... flags if you need a special
00057                 feature according to your engine, it will not open the same tables
00058                 in each cases. Then if lwoerr_Ok is returned, 
00059                 you can use LwoGetLayer() to get an object shape. 
00060                 You can then copy all informations before CloseLwo().
00061                 consider all informations as const.
00062         \param  _pLwoObject reference to a sLwoObject to init, to handle the file.
00063         \param  _pLwoFileChunk  pointer to the whole file chunk.
00064         \param  _lwoFileByteLength the length of _pLwoFileChunk in bytes.
00065         \param  _flags  look ReadLwoFlag enum up there. 
00066         \return lwoerr_Ok if no problem. else a lwo2_error enum.
00067     */
00068     lwo2_error  ReadLwo(sLwoObject *_pLwoObject, const unsigned char *_pLwoFileChunk, unsigned int _lwoFileByteLength, const unsigned int _flags );
00069     /*!
00070         \brief  Close all allocs from ReadLwo().
00071         \param  _pLwoObject reference to an object that manage the LWO.
00072     */
00073     void    CloseLwo(sLwoObject *_pLwoObject);
00074 
00075     /*!
00076         \brief  Get a layer shape from a struct inited with ReadLwo().
00077                 Each .lwo files can contains one or more layer shapes.
00078                 _layerIndex corrsponds to the layer index in the interface,
00079                 and is zero-based.
00080                 Then you can use 
00081         \param  _pLwoObject pointer to struct computed with ReadLwo().
00082         \param _layerIndex index of the layer in the file.
00083         \return a sLwoLayer layer shape description.
00084     */
00085     const sLwoLayer *LwoGetLayer(const sLwoObject *_pLwoObject, const unsigned int _layerIndex);
00086 
00087 
00088 
00089 //! from LW envelope.h
00090 
00091 #define SHAPE_TCB   0
00092 #define SHAPE_HERM  1
00093 #define SHAPE_BEZI  2
00094 #define SHAPE_LINE  3
00095 #define SHAPE_STEP  4
00096 #define SHAPE_BEZ2  5
00097 
00098 #define BEH_RESET      0
00099 #define BEH_CONSTANT   1
00100 #define BEH_REPEAT     2
00101 #define BEH_OSCILLATE  3
00102 #define BEH_OFFSET     4
00103 #define BEH_LINEAR     5
00104 
00105 typedef struct st_Key {
00106    struct st_Key *next;
00107    struct st_Key *prev;
00108    float  value;
00109    float  time;
00110    int    shape;
00111    float  tension;
00112    float  continuity;
00113    float  bias;
00114    float  param[ 4 ];
00115 } Key;
00116 
00117 typedef struct st_Envelope {
00118    Key   *key;
00119    int    nkeys;
00120    int    behavior[ 2 ];
00121 } Envelope;
00122 
00123 float evalEnvelope( Envelope *env, float time );
00124 
00125 /*---------------------------------- LWOB-LWO2 structs  ---------------*/
00126 struct  LwoMatrix{
00127 
00128     float   T1X;    /* pivot point (unused ?) */
00129     float   T1Y;
00130     float   T1Z;
00131 };
00132 /*!
00133     \struct sLwoVertex
00134     \brief Manages a Vertex read from a lwo file.
00135             as a position vector, a computed normal vector,
00136             Texture UV coordinates, etc
00137 */
00138 typedef struct _LwoVertex{
00139     //! Vertex position vector
00140     float   XI,YI,ZI;
00141     //! vertex color (0.0f,1.0f)
00142     float   r,g,b;
00143     //! vertex computed normal vector
00144     float   XNI,YNI,ZNI;
00145     //! vertex UV texture coordinates.
00146     float   U,V;
00147     //! private use: trace VMAD discontinuity
00148     struct  _LwoPolyToVertex     *FirstDiscontinuousUse;
00149 } sLwoVertex ;
00150 /*!
00151     \struct sLwoPolyToVertex
00152     \brief Describes the reference of a polygon point to a given vertex
00153             in the vertex base. Informations can be discontinuous to the vertex,
00154             in which case they are here, or on the vertex, in which case they are in sLwoVertex.
00155 */
00156 typedef struct _LwoPolyToVertex{
00157     //! zero-based Vertex index in the layer vertex list.
00158     int     m_VertexIndex;
00159     //! Discontinuous UV texture coordinates, relative to the polygon, values in [0,1] on image
00160     float   tU;   
00161     //! Discontinuous UV texture coordinates, relative to the polygon. values in [0,1] on image
00162     float   tV;
00163     //! private use: 0 if normal ,used if >0, val -1 please
00164     int     DiscontinuousMappingOffset;
00165     //! private use.
00166     struct  _LwoPolyToVertex     *NextDiscontinuousUse;
00167 } sLwoPolyToVertex;
00168 /*!
00169     \struct sLwoPolygon
00170     \brief  Describes a raw polygon as a N-gon, uner the form
00171             of a vertex reference list.
00172             that describe a convex polygon.
00173 */
00174 typedef struct  _LwoPolygon{
00175     //! number of vertex reference.
00176     unsigned int m_NumberOfVertex;
00177     //! reference table with length=m_NumberOfVertex
00178     sLwoPolyToVertex *m_VertexList;
00179     //! computed face normal vector:
00180     float   XN,YN,ZN;
00181     //! texture surface used for this polygo.
00182     struct  LwTexture *surface;
00183 } sLwoPolygon;
00184 /*----------------------------------------*/
00185 struct  LwoTriangle{
00186     //float   XN,YN,ZN;   /* face normal */
00187     // first vertex index
00188     unsigned int    v0;
00189     // vertex index
00190     unsigned int    v1;
00191     // vertex index
00192     unsigned int    v2;
00193 };
00194 /*
00195     \struct sLwoSortedTriangleSurface
00196     \brief  Define a triangle set for a layer and a texture,
00197             in the layer. Used if ReadLwoFlag_SortTriangleBySurfaces
00198             was set.
00199 */
00200 typedef struct  _LwoSortedTriangleSurface {
00201     //! corresponding index in sLwoObject 's surface table.
00202     unsigned int    m_LwoFileSurfaceIndex;
00203     //! First 0-based triangle index in the layer triangle list.
00204     unsigned int    m_FirstTriangleIndex;
00205     //! number of triangle for this texture/layer set after m_FirstTriangleIndex.
00206     unsigned int    m_NumberOfTriangle;
00207 } sLwoSortedTriangleSurface;
00208 /*----------------------------------------*/
00209 struct  LwoMorphVertex{
00210         float   X,Y,Z;
00211 };
00212 
00213 struct MorphSet{
00214     struct MorphSet         *next;
00215     char                    name[88];
00216     struct  LwoMorphVertex  *vertex;
00217     int                     morf0spot1;
00218 };
00219 /*!
00220     \struct sLwoLayer
00221     \brief Describes an Object Layer, which means a given shape in the file.
00222             Then the geometry is described under it.
00223 */
00224 struct  _LwoLayer{
00225     //! file which manage this struct, private use.
00226     struct  _LwoObject  *m_LWOFile;
00227     //! index layer in LW's interface.
00228     int     m_LayerNumber;
00229     //! vertex table, used by m_LWO_Polygon and LWO_triangle lists. 
00230     sLwoVertex      *m_LWO_Vertex;
00231     //! number of vertex in table m_LWO_Vertex. polygons will index from 0 to m_NumberOfVertex-1.
00232     unsigned int    m_NumberOfVertex;
00233     //! the n-gon polygon tables, unsplitted and maybe not convex.
00234     sLwoPolygon *m_LWO_Polygon;
00235     //! Number of polygons in m_LWO_Polygon.
00236     unsigned int    m_MaxNbPolygon;
00237     //! this layer has a color-per-vertex map. (TODO)
00238     unsigned int    m_ColorFlag;
00239     // * these indented members are valid if ReadLwoFlag_SortTriangleBySurfaces was inited: *
00240         //! total number of triangle in m_LWO_Triangle, may be greater than m_MaxNbPolygon.
00241         int     m_MaxNbTriangle;
00242         //! triangle version of m_LWO_Polygon. active if ReadLwoFlag_SortTriangleBySurfaces.
00243         struct  LwoTriangle    *m_LWO_Triangle;
00244         //! number of surfaces (different rendering types)  used in m_LWO_Triangle, and length of m_pSurfaceSortedTriangleListIndex.
00245         unsigned int    m_NumberOfSurfaceUsed;
00246         //! point which surface use which part of the triangle list. Watch out, it is not the surface order surface are pointed by m_LwoFileSurfaceIndex.
00247         sLwoSortedTriangleSurface   *m_pSurfaceSortedTriangleListIndex;
00248     //! private use, algorythm to create discontinuous vertexes.(ReadLwoFlag_DiscontinuousUVToNewVertex)
00249     int     m_NumberOfDiscontinousVertexToAdd;
00250     //! object pivot matrix, not a pointer.
00251     struct LwoMatrix m_InitialObjectPos; 
00252     //! the * 16b flags, direct from lwo
00253     int     flags;    
00254     //! facultative parent layer (lwo)
00255     int     parent;  
00256     //! cube coordinates where the object is bound:
00257     float       m_boundMin[3]; 
00258     //! cube coordinates where the object is bound:
00259     float       m_boundMax[3]; 
00260     //! layer name
00261     char        *name;
00262     //! Number of element in m_MorphTable
00263     unsigned int m_NumberofMorph;
00264     //! Morph table
00265     struct MorphSet *m_MorphTable;
00266     //! facultative.exist if at least 1 morphset "morf" 
00267     struct  LwoMorphVertex  *baseMorph;
00268     //! hash to resolve mapping layer
00269     struct RememberVMAPChunk    *m_pFirstVMAP;
00270     //! hash to resolve mapping layer
00271     struct RememberVMAPChunk    *m_pFirstVMAD;
00272 
00273 };
00274 /*-----------------------------------*/
00275 // for LwTexture.typebits
00276 // for LwTexture.typebits
00277 typedef enum
00278 {
00279     TBit_MIPMap=1,
00280     TBit_ClampWidth=2,
00281     TBit_ClampHeight=4,
00282     TBit_NoSmooth=8,
00283     //TBit_Environment=16, now given by reflectimage.
00284 } e_typebits;
00285     /*----- texture object -------*/
00286 struct  LwTexture{
00287     unsigned int    IndexInFile;
00288     float            RGBAcolor[4];
00289     char            *name;       //used to bluit it.
00290     char            *txuv_used; // valid VMAP/VMAD name for this texture layer
00291     char            *mapimage;      // NULL if no image, or clip name.
00292     char            *reflectimage;  // NULL if no image, or clip name.
00293     char            *bumpimage; // NULL if no image, or clip name.
00294     int             typebits; //what type of rendering ? enum e_typebits.
00295     float             additiveRate; //if additive layer.
00296     float            transparencyRate;
00297 //re    struct  LwsMatrixValue  transparency;
00298 };
00299 /*-----------------------------------*/
00300 struct  LwoClip{
00301     int                 LWClipindex;
00302     //struct  LwoClip     *nextInStaticChain; // we
00303     char                *imagename;
00304 
00305 };
00306 /*-----------------------------------*/
00307 struct  LwoIndexedEnvelope{
00308     struct KTable *lwEnvlope;
00309     int         index;
00310 };
00311 
00312 
00313 /*!
00314     \struct sLwoObject
00315     \brief  Describe the whole lwo file To be used with ReadLwo()
00316 */
00317 struct _LwoObject 
00318 {
00319     //! Number of layers in m_LWO_Layer
00320     unsigned int m_NumberOfLayers;
00321     //! table of layers. Beware, layers index are m_LWO_Layer->m_LayerNumber, use LwoGetLayer() to get it.
00322     sLwoLayer    *m_LWO_Layer;
00323     //! number of surfaces (rendering types) in the whole file, common to all layers:
00324     struct LwTexture   *m_pSurfaces;
00325     unsigned int         nbsurf;
00326     struct LwoIndexedEnvelope   *envlp;
00327     unsigned int         nbenvlp;
00328     unsigned int        nbclip;
00329 //    int        *clipindextable; // used to link clipimages to textures.
00330     struct LwoClip *clipimagetable;
00331 
00332     //! if !=0, this is a LWO2 file, else a LWOB.
00333     int     LWO2_true;
00334     //! private use, link to binary while reading
00335     const unsigned char *m_pMainChunk;
00336     //! private use, main binary size
00337     unsigned int         m_mainsize;
00338 
00339 } ;
00340 /*----------------------------------*/
00341 
00342 #ifdef __cplusplus
00343 };
00344 #endif
00345 #endif

      /\/\        4         N         k         !         N         D
                      _______  _ __ ___  _____            ___ _ _  ____
     ___________  __//___   /________  |/    / ___________\_______/    \
    /   _   _   \/   _     /    _   /      _/_/____/    _       __     /
   /    /   /       /     /    /    \      \/     /    /    \   \     /
  \\___/___/___/    ¯    _____/_____/       ______\___/_____/\________\\
               \________/_ ___ __ l____\      /elD!  
                 http://www.m4nkind.com \____/