Irrlicht 3D Engine
SSkinMeshBuffer.h
Go to the documentation of this file.
00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __I_SKIN_MESH_BUFFER_H_INCLUDED__
00006 #define __I_SKIN_MESH_BUFFER_H_INCLUDED__
00007 
00008 #include "IMeshBuffer.h"
00009 #include "S3DVertex.h"
00010 
00011 
00012 namespace irr
00013 {
00014 namespace scene
00015 {
00016 
00017 
00019 struct SSkinMeshBuffer : public IMeshBuffer
00020 {
00022     SSkinMeshBuffer(video::E_VERTEX_TYPE vt=video::EVT_STANDARD) :
00023         ChangedID_Vertex(1), ChangedID_Index(1), VertexType(vt),
00024         MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER),
00025         BoundingBoxNeedsRecalculated(true)
00026     {
00027         #ifdef _DEBUG
00028         setDebugName("SSkinMeshBuffer");
00029         #endif
00030     }
00031 
00033     virtual const video::SMaterial& getMaterial() const
00034     {
00035         return Material;
00036     }
00037 
00039     virtual video::SMaterial& getMaterial()
00040     {
00041         return Material;
00042     }
00043 
00045     virtual video::S3DVertex *getVertex(u32 index)
00046     {
00047         switch (VertexType)
00048         {
00049             case video::EVT_2TCOORDS:
00050                 return (video::S3DVertex*)&Vertices_2TCoords[index];
00051             case video::EVT_TANGENTS:
00052                 return (video::S3DVertex*)&Vertices_Tangents[index];
00053             default:
00054                 return &Vertices_Standard[index];
00055         }
00056     }
00057 
00059     virtual const void* getVertices() const
00060     {
00061         switch (VertexType)
00062         {
00063             case video::EVT_2TCOORDS:
00064                 return Vertices_2TCoords.const_pointer();
00065             case video::EVT_TANGENTS:
00066                 return Vertices_Tangents.const_pointer();
00067             default:
00068                 return Vertices_Standard.const_pointer();
00069         }
00070     }
00071 
00073     virtual void* getVertices()
00074     {
00075         switch (VertexType)
00076         {
00077             case video::EVT_2TCOORDS:
00078                 return Vertices_2TCoords.pointer();
00079             case video::EVT_TANGENTS:
00080                 return Vertices_Tangents.pointer();
00081             default:
00082                 return Vertices_Standard.pointer();
00083         }
00084     }
00085 
00087     virtual u32 getVertexCount() const
00088     {
00089         switch (VertexType)
00090         {
00091             case video::EVT_2TCOORDS:
00092                 return Vertices_2TCoords.size();
00093             case video::EVT_TANGENTS:
00094                 return Vertices_Tangents.size();
00095             default:
00096                 return Vertices_Standard.size();
00097         }
00098     }
00099 
00101 
00102     virtual video::E_INDEX_TYPE getIndexType() const
00103     {
00104         return video::EIT_16BIT;
00105     }
00106 
00108     virtual const u16* getIndices() const
00109     {
00110         return Indices.const_pointer();
00111     }
00112 
00114     virtual u16* getIndices()
00115     {
00116         return Indices.pointer();
00117     }
00118 
00120     virtual u32 getIndexCount() const
00121     {
00122         return Indices.size();
00123     }
00124 
00126     virtual const core::aabbox3d<f32>& getBoundingBox() const
00127     {
00128         return BoundingBox;
00129     }
00130 
00132     virtual void setBoundingBox( const core::aabbox3df& box)
00133     {
00134         BoundingBox = box;
00135     }
00136 
00138     virtual void recalculateBoundingBox()
00139     {
00140         if(!BoundingBoxNeedsRecalculated)
00141             return;
00142 
00143         BoundingBoxNeedsRecalculated = false;
00144 
00145         switch (VertexType)
00146         {
00147             case video::EVT_STANDARD:
00148             {
00149                 if (Vertices_Standard.empty())
00150                     BoundingBox.reset(0,0,0);
00151                 else
00152                 {
00153                     BoundingBox.reset(Vertices_Standard[0].Pos);
00154                     for (u32 i=1; i<Vertices_Standard.size(); ++i)
00155                         BoundingBox.addInternalPoint(Vertices_Standard[i].Pos);
00156                 }
00157                 break;
00158             }
00159             case video::EVT_2TCOORDS:
00160             {
00161                 if (Vertices_2TCoords.empty())
00162                     BoundingBox.reset(0,0,0);
00163                 else
00164                 {
00165                     BoundingBox.reset(Vertices_2TCoords[0].Pos);
00166                     for (u32 i=1; i<Vertices_2TCoords.size(); ++i)
00167                         BoundingBox.addInternalPoint(Vertices_2TCoords[i].Pos);
00168                 }
00169                 break;
00170             }
00171             case video::EVT_TANGENTS:
00172             {
00173                 if (Vertices_Tangents.empty())
00174                     BoundingBox.reset(0,0,0);
00175                 else
00176                 {
00177                     BoundingBox.reset(Vertices_Tangents[0].Pos);
00178                     for (u32 i=1; i<Vertices_Tangents.size(); ++i)
00179                         BoundingBox.addInternalPoint(Vertices_Tangents[i].Pos);
00180                 }
00181                 break;
00182             }
00183         }
00184     }
00185 
00187     virtual video::E_VERTEX_TYPE getVertexType() const
00188     {
00189         return VertexType;
00190     }
00191 
00193     virtual void convertTo2TCoords()
00194     {
00195         if (VertexType==video::EVT_STANDARD)
00196         {
00197             for(u32 n=0;n<Vertices_Standard.size();++n)
00198             {
00199                 video::S3DVertex2TCoords Vertex;
00200                 Vertex.Color=Vertices_Standard[n].Color;
00201                 Vertex.Pos=Vertices_Standard[n].Pos;
00202                 Vertex.Normal=Vertices_Standard[n].Normal;
00203                 Vertex.TCoords=Vertices_Standard[n].TCoords;
00204                 Vertices_2TCoords.push_back(Vertex);
00205             }
00206             Vertices_Standard.clear();
00207             VertexType=video::EVT_2TCOORDS;
00208         }
00209     }
00210 
00212     virtual void convertToTangents()
00213     {
00214         if (VertexType==video::EVT_STANDARD)
00215         {
00216             for(u32 n=0;n<Vertices_Standard.size();++n)
00217             {
00218                 video::S3DVertexTangents Vertex;
00219                 Vertex.Color=Vertices_Standard[n].Color;
00220                 Vertex.Pos=Vertices_Standard[n].Pos;
00221                 Vertex.Normal=Vertices_Standard[n].Normal;
00222                 Vertex.TCoords=Vertices_Standard[n].TCoords;
00223                 Vertices_Tangents.push_back(Vertex);
00224             }
00225             Vertices_Standard.clear();
00226             VertexType=video::EVT_TANGENTS;
00227         }
00228         else if (VertexType==video::EVT_2TCOORDS)
00229         {
00230             for(u32 n=0;n<Vertices_2TCoords.size();++n)
00231             {
00232                 video::S3DVertexTangents Vertex;
00233                 Vertex.Color=Vertices_2TCoords[n].Color;
00234                 Vertex.Pos=Vertices_2TCoords[n].Pos;
00235                 Vertex.Normal=Vertices_2TCoords[n].Normal;
00236                 Vertex.TCoords=Vertices_2TCoords[n].TCoords;
00237                 Vertices_Tangents.push_back(Vertex);
00238             }
00239             Vertices_2TCoords.clear();
00240             VertexType=video::EVT_TANGENTS;
00241         }
00242     }
00243 
00245     virtual const core::vector3df& getPosition(u32 i) const
00246     {
00247         switch (VertexType)
00248         {
00249             case video::EVT_2TCOORDS:
00250                 return Vertices_2TCoords[i].Pos;
00251             case video::EVT_TANGENTS:
00252                 return Vertices_Tangents[i].Pos;
00253             default:
00254                 return Vertices_Standard[i].Pos;
00255         }
00256     }
00257 
00259     virtual core::vector3df& getPosition(u32 i)
00260     {
00261         switch (VertexType)
00262         {
00263             case video::EVT_2TCOORDS:
00264                 return Vertices_2TCoords[i].Pos;
00265             case video::EVT_TANGENTS:
00266                 return Vertices_Tangents[i].Pos;
00267             default:
00268                 return Vertices_Standard[i].Pos;
00269         }
00270     }
00271 
00273     virtual const core::vector3df& getNormal(u32 i) const
00274     {
00275         switch (VertexType)
00276         {
00277             case video::EVT_2TCOORDS:
00278                 return Vertices_2TCoords[i].Normal;
00279             case video::EVT_TANGENTS:
00280                 return Vertices_Tangents[i].Normal;
00281             default:
00282                 return Vertices_Standard[i].Normal;
00283         }
00284     }
00285 
00287     virtual core::vector3df& getNormal(u32 i)
00288     {
00289         switch (VertexType)
00290         {
00291             case video::EVT_2TCOORDS:
00292                 return Vertices_2TCoords[i].Normal;
00293             case video::EVT_TANGENTS:
00294                 return Vertices_Tangents[i].Normal;
00295             default:
00296                 return Vertices_Standard[i].Normal;
00297         }
00298     }
00299 
00301     virtual const core::vector2df& getTCoords(u32 i) const
00302     {
00303         switch (VertexType)
00304         {
00305             case video::EVT_2TCOORDS:
00306                 return Vertices_2TCoords[i].TCoords;
00307             case video::EVT_TANGENTS:
00308                 return Vertices_Tangents[i].TCoords;
00309             default:
00310                 return Vertices_Standard[i].TCoords;
00311         }
00312     }
00313 
00315     virtual core::vector2df& getTCoords(u32 i)
00316     {
00317         switch (VertexType)
00318         {
00319             case video::EVT_2TCOORDS:
00320                 return Vertices_2TCoords[i].TCoords;
00321             case video::EVT_TANGENTS:
00322                 return Vertices_Tangents[i].TCoords;
00323             default:
00324                 return Vertices_Standard[i].TCoords;
00325         }
00326     }
00327 
00329     virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) {}
00330 
00332     virtual void append(const IMeshBuffer* const other) {}
00333 
00335     virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const
00336     {
00337         return MappingHint_Vertex;
00338     }
00339 
00341     virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const
00342     {
00343         return MappingHint_Index;
00344     }
00345 
00347     virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX )
00348     {
00349         if (Buffer==EBT_VERTEX)
00350             MappingHint_Vertex=NewMappingHint;
00351         else if (Buffer==EBT_INDEX)
00352             MappingHint_Index=NewMappingHint;
00353         else if (Buffer==EBT_VERTEX_AND_INDEX)
00354         {
00355             MappingHint_Vertex=NewMappingHint;
00356             MappingHint_Index=NewMappingHint;
00357         }
00358     }
00359 
00361     virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX)
00362     {
00363         if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
00364             ++ChangedID_Vertex;
00365         if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
00366             ++ChangedID_Index;
00367     }
00368 
00369     virtual u32 getChangedID_Vertex() const {return ChangedID_Vertex;}
00370 
00371     virtual u32 getChangedID_Index() const {return ChangedID_Index;}
00372 
00374     void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
00375 
00376     core::array<video::S3DVertexTangents> Vertices_Tangents;
00377     core::array<video::S3DVertex2TCoords> Vertices_2TCoords;
00378     core::array<video::S3DVertex> Vertices_Standard;
00379     core::array<u16> Indices;
00380 
00381     u32 ChangedID_Vertex;
00382     u32 ChangedID_Index;
00383 
00384     //ISkinnedMesh::SJoint *AttachedJoint;
00385     core::matrix4 Transformation;
00386 
00387     video::SMaterial Material;
00388     video::E_VERTEX_TYPE VertexType;
00389 
00390     core::aabbox3d<f32> BoundingBox;
00391 
00392     // hardware mapping hint
00393     E_HARDWARE_MAPPING MappingHint_Vertex:3;
00394     E_HARDWARE_MAPPING MappingHint_Index:3;
00395 
00396     bool BoundingBoxNeedsRecalculated:1;
00397 };
00398 
00399 
00400 } // end namespace scene
00401 } // end namespace irr
00402 
00403 #endif
00404