1 //-----------------------------------------------------------------------------
2 // This is free and unencumbered software released into the public domain.
3 // For the full text of the Unlicense, see the file "docs/unlicense.html".
4 // Additional Unlicense information may be found at http://unlicense.org.
5 //-----------------------------------------------------------------------------
6 #ifndef __IRR_IRRB_MESH_WRITER_H_INCLUDED__
7 #define __IRR_IRRB_MESH_WRITER_H_INCLUDED__
8 
9 #include "IMeshWriter.h"
10 #include "S3DVertex.h"
11 #include "IVideoDriver.h"
12 #include "IFileSystem.h"
13 
14 namespace irr
15 {
16     namespace io
17     {
18         class IXMLWriter;
19     }
20     namespace scene
21     {
22 
23         /* v 1.6
24 
25         [header]
26         [animated mesh]
27         [mesh]
28            [mesh info]
29                [buffer count]
30                [verts]        (all mesh buffers)
31                [indices]      (all mesh buffers)
32            [/mesh info]
33 
34            [mesh buffer]
35               [meshbuffer info] (vertex/index offsets/counts)
36               [material]
37                  [common mat attributes]
38                  [layer count]
39                     [layer]
40                        [texture name string chunk]
41                        [layer attributes]
42                     [/layer]
43            [/mesh buffer]
44 
45            [mesh buffer]
46               ...
47            [/mesh buffer]
48         [/mesh]
49 
50         [mesh]
51         ...
52         [/mesh]
53 
54         */
55 
56         class IMeshBuffer;
57 
58         // byte-align structures
59         #if defined(_MSC_VER) ||  defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
60         #	pragma pack( push, packing )
61         #	pragma pack( 1 )
62         #	define PACK_STRUCT
63         #elif defined( __GNUC__ )
64         #	define PACK_STRUCT	__attribute__((packed))
65         #else
66         #	define PACK_STRUCT
67         #endif
68 
69         #define IRRB_VERSION    IRRLICHT_VERSION_MAJOR << 8 & IRRLICHT_VERSION_MINOR  // coincides with Irrlicht version
70 
71         #define CHUNK_ID(c0, c1, c2, c3) \
72 		((u32)(u8)(c0) | ((u32)(u8)(c1) << 8) | \
73 		((u32)(u8)(c2) << 16) | ((u32)(u8)(c3) << 24 ))
74 
75         #define INFO_ANIMATION_SKINNED  0x0001
76         #define INFO_ANIMATION_VERTEX   0x0002
77 
78         #define CID_MESH     CHUNK_ID('m','e','s','h')
79         #define CID_MATERIAL CHUNK_ID('m','a','t',' ')
80         #define CID_MESHBUF  CHUNK_ID('m','b','u','f')
81         #define CID_VBUFFER  CHUNK_ID('v','b','u','f')
82         #define CID_IBUFFER  CHUNK_ID('i','b','u','f')
83         #define CID_TEXTURE  CHUNK_ID('t','e','x',' ')
84         #define CID_SKEL     CHUNK_ID('s','k','e','l')
85         #define CID_MORPH    CHUNK_ID('m','r','p','h')
86         #define CID_STRING   CHUNK_ID('s','t','r',' ')
87 
88         // irrb header
89         struct IrrbHeader
90         {
91             c8      hSig[12];   // 'irrb vh.vl' eof
92             u32     hSigCheck;
93             u16     hVersion;
94             u16     hFill1;
95             u32     hFlags;     // compression/encryption/endianess/int bits
96             c8      hCreator[32];
97             u32     hMeshCount;
98             u32     hMeshBufferCount;
99             u32     hCRC;
100         } PACK_STRUCT;
101 
102         struct IrrbChunkInfo
103         {
104             u32     iId;
105             u32     iSize;
106         } PACK_STRUCT;
107 
108         struct Irrb3f
109         {
110             f32     x;
111             f32     y;
112             f32     z;
113         } PACK_STRUCT;
114 
115         struct Irrb2f
116         {
117             f32     x;
118             f32     y;
119         } PACK_STRUCT;
120 
121         struct IrrbMeshInfo
122         {
123             u32     iMeshBufferCount;
124             u32     iVertexCount;
125             u32     iIndexCount;
126             u32     iMaterialCount;
127             Irrb3f  ibbMin;
128             Irrb3f  ibbMax;
129         } PACK_STRUCT;
130 
131         struct IrrbVertex
132         {
133             struct Irrb3f   vPos;
134             struct Irrb3f   vNormal;
135             u32             vColor;
136             struct Irrb2f   vUV1;
137             struct Irrb2f   vUV2;
138             struct Irrb3f   vTangent;
139             struct Irrb3f   vBiNormal;
140         } PACK_STRUCT;
141 
142         struct IrrbMaterialLayer
143         {
144             u8      mTextureWrapU:4;
145             u8      mTextureWrapV:4;
146             f32     mMatrix[16];
147             u8      mAnisotropicFilter;
148             u8      mLODBias;
149             bool    mBilinearFilter:1;
150             bool    mTrilinearFilter:1;
151         } PACK_STRUCT;
152 
153         struct IrrbMaterial
154         {
155             u32     mType;
156             u32     mAmbient;
157             u32     mDiffuse;
158             u32     mEmissive;
159             u32     mSpecular;
160             f32     mShininess;
161             f32     mParm1;
162             f32     mParm2;
163             f32     mThickness;
164             u8      mZBuffer;
165             u8      mAntiAliasing;
166             u8      mColorMask;
167             u8      mColorMaterial;
168 		    u8      mBlendOperation;
169 		    u8      mPolygonOffsetFactor;
170     		u8      mPolygonOffsetDirection;
171             u8      mLayerCount;
172             bool    mWireframe:1;
173             bool    mPointCloud:1;
174             bool    mGrouraudShading:1;
175             bool    mLighting:1;
176             bool    mZWriteEnabled:1;
177             bool    mBackfaceCulling:1;
178             bool    mFrontfaceCulling:1;
179             bool    mFogEnable:1;
180             bool    mNormalizeNormals:1;
181             bool    mUseMipMaps:1;
182         } PACK_STRUCT;
183 
184         struct IrrbMeshBufInfo
185         {
186             u32     iVertexType;
187             u32     iVertCount;
188             u32     iVertStart;
189             u32     iIndexCount;
190             u32     iIndexStart;
191             u32     iFaceCount;
192             u32     iMaterialIndex;
193             char    iMaterialName[64];
194             Irrb3f  ibbMin;
195             Irrb3f  ibbMax;
196         } PACK_STRUCT;
197 
198         struct SCustomMaterial
199         {
200             core::stringc Name;
201             video::E_MATERIAL_TYPE  Type;
202             video::IMaterialRenderer* Renderer;
203         };
204 
205         // Default alignment
206 #if defined(_MSC_VER) ||  defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
207 #	pragma pack( pop, packing )
208 #endif
209 
210 #undef PACK_STRUCT
211 
212 
213         //! class to write meshes, implementing a IrrMesh (.irrmesh, .xml) writer
214         /** This writer implementation has been originally developed for irrEdit and then
215         merged out to the Irrlicht Engine */
216         class CIrrBMeshWriter : public IMeshWriter
217         {
218         public:
219 
220             CIrrBMeshWriter(video::IVideoDriver* driver, io::IFileSystem* fs, core::array<SCustomMaterial>* customMaterials=0);
221             virtual ~CIrrBMeshWriter();
222 
223             //! Returns the type of the mesh writer
224             virtual EMESH_WRITER_TYPE getType() const;
225 
226             //! writes a mesh
227             virtual bool writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 flags=EMWF_NONE);
228 
setVersion(u16 value)229             void setVersion(u16 value)
230             {
231                 Version = value;
232                 VMajor = (Version & 0xFF00) >> 8;
233                 VMinor = Version & 0x00FF;
234             }
setCreator(irr::core::stringc value)235             void setCreator(irr::core::stringc value) {Creator = value;}
236 
setRelativeBase(irr::core::stringc value)237             void setRelativeBase(irr::core::stringc value) {RelativeBase = value;}
238 
239         protected:
240 
241             void writeHeader(const scene::IMesh* mesh);
242 
243             void updateMaterial(const video::SMaterial& material,struct IrrbMaterial& mat);
244             void updateMaterialLayer(const video::SMaterial& material,u8 layerNumber, irr::core::stringc& textureName, struct IrrbMaterialLayer& layer);
245 
246             bool _writeMesh(const scene::IMesh* mesh);
247 
248             u32 _writeChunkInfo(u32 id, u32 size);
249             void _writeStringChunk(irr::core::stringc value);
250             void _updateChunkSize(u32 id, u32 offset);
251             void updateBuffers(const scene::IMesh* mesh, struct IrrbVertex* vbuffer, u32* ibuffer);
252 
253             bool addMaterial(irr::video::SMaterial& material);
254             u32 getMaterialIndex(irr::video::SMaterial& material);
255             irr::core::stringc getMaterialName(irr::video::SMaterial& material);
256 
257             // member variables:
258             irr::core::array<irr::video::SMaterial> Materials;
259             struct IrrbVertex*   VBuffer;
260             u32*    IBuffer;
261             io::IFileSystem* FileSystem;
262             video::IVideoDriver* VideoDriver;
263             core::array<SCustomMaterial>* CustomMaterials;
264             io::IWriteFile* Writer;
265             u16 Version, VMajor, VMinor;
266             irr::core::stringc Creator;
267             irr::core::stringc RelativeBase;
268         };
269 
270     int get_endianess(void);  // 1-big, 0-lil
271 
272     } // end namespace
273 } // end namespace
274 
275 #endif
276