00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_HWBUFFER_MESHBUFFER_H__
00009 #define __SP_HWBUFFER_MESHBUFFER_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spInputOutputString.hpp"
00014 #include "Base/spGeometryStructures.hpp"
00015 #include "Base/spMaterialStates.hpp"
00016 #include "Base/spVertexFormat.hpp"
00017 #include "Base/spIndexFormat.hpp"
00018 #include "Base/spMathTriangleCutter.hpp"
00019 #include "RenderSystem/spTextureLayer.hpp"
00020
00021 #include <vector>
00022
00023
00024 namespace sp
00025 {
00026 namespace video
00027 {
00028
00029
00030 class TextureLayer;
00031
00036 class SP_EXPORT MeshBuffer
00037 {
00038
00039 public:
00040
00041 MeshBuffer(const video::VertexFormat* VertexFormat = 0, ERendererDataTypes IndexFormat = DATATYPE_UNSIGNED_INT);
00042 MeshBuffer(const MeshBuffer &Other, bool isCreateMeshBuffer = true);
00043 virtual ~MeshBuffer();
00044
00045
00046
00048 virtual io::stringc getIdentifier() const;
00049
00051 MeshBuffer* getReference();
00053 const MeshBuffer* getReference() const;
00054
00064 bool sortCompare(const MeshBuffer &Other) const;
00065
00071 bool compare(const MeshBuffer &Other) const;
00072
00081 void setVertexFormat(const VertexFormat* Format);
00082
00090 void setIndexFormat(ERendererDataTypes Format);
00091
00093 void saveBackup();
00095 void loadBackup();
00097 void clearBackup();
00098
00100 void createVertexBuffer();
00102 void createIndexBuffer();
00104 void createMeshBuffer();
00105
00107 void deleteVertexBuffer();
00109 void deleteIndexBuffer();
00111 void deleteMeshBuffer();
00112
00114 void updateVertexBuffer();
00116 void updateIndexBuffer();
00118 void updateMeshBuffer();
00119
00121 void updateVertexBufferElement(u32 Index);
00123 void updateIndexBufferElement(u32 Index);
00124
00129 void setPrimitiveType(const ERenderPrimitives Type);
00130
00136 static s32 getPrimitiveSize(const ERenderPrimitives Type);
00137
00139 s32 getPrimitiveSize() const;
00140
00147 bool renderable() const;
00148
00149
00150
00155 u32 addVertex();
00156
00158 void addVertices(const u32 Count);
00159
00166 u32 addVertex(
00167 const dim::vector3df &Position,
00168 const dim::vector3df &TexCoord = 0,
00169 const video::color &Color = 255
00170 );
00171
00176 u32 addVertex(
00177 const dim::vector3df &Position,
00178 const dim::vector3df &Normal,
00179 const dim::vector3df &TexCoord,
00180 const video::color &Color = 255,
00181 const f32 Fog = 0.0f
00182 );
00183
00185 u32 addVertex(
00186 const dim::vector3df &Position,
00187 const dim::vector3df &Normal,
00188 const std::vector<dim::vector3df> &TexCoordList,
00189 const video::color &Color = 255,
00190 const f32 Fog = 0.0f
00191 );
00192
00194 u32 addVertex(const scene::SMeshVertex3D &VertexData);
00195
00207 u32 addVertex(u32 IndexA, u32 IndexB, u32 IndexC, const dim::vector3df &BarycentricCoord);
00208
00215 bool removeVertex(const u32 Index);
00216
00221 u32 addTriangle();
00222
00224 void addTriangles(const u32 Count);
00225
00234 u32 addTriangle(u32 VertexA, u32 VertexB, u32 VertexC);
00235
00237 u32 addTriangle(const u32 Indices[3]);
00238
00248 u32 addQuadrangle(u32 VertexA, u32 VertexB, u32 VertexC, u32 VertexD);
00249
00251 u32 addQuadrangle(const u32 Indices[4]);
00252
00257 u32 addPrimitiveIndex(u32 Index);
00259 void addIndices(const u32 Count);
00260
00268 bool removePrimitive(const u32 Index);
00269
00271 void clearVertices();
00273 void clearIndices();
00274
00283 bool insertMeshBuffer(const MeshBuffer &Other);
00284
00290 void setTriangleIndices(const u32 Index, const u32 (&Indices)[3]);
00291
00298 void getTriangleIndices(const u32 Index, u32 (&Indices)[3]) const;
00299
00305 void setPrimitiveIndex(const u32 Index, const u32 VertexIndex);
00306
00313 u32 getPrimitiveIndex(const u32 Index) const;
00314
00316 scene::SMeshVertex3D getVertex(const u32 Index) const;
00318 scene::SMeshTriangle3D getTriangle(const u32 Index) const;
00319
00321 dim::triangle3df getTriangleCoords(const u32 Index) const;
00326 dim::ptriangle3df getTriangleReference(const u32 Index) const;
00327
00329 void flipTriangles();
00330
00332 bool cutTriangle(const u32 Index, const dim::plane3df &ClipPlane);
00333
00338 u32 getIndexCount() const;
00343 u32 getTriangleCount() const;
00344
00345
00346
00358 void setVertexAttribute(const u32 Index, const SVertexAttribute &Attrib, const void* AttribData, u32 Size);
00359
00367 void getVertexAttribute(const u32 Index, const SVertexAttribute &Attrib, void* AttribData, u32 Size);
00368
00374 void setVertexCoord(const u32 Index, const dim::vector3df &Coord);
00375
00377 dim::vector3df getVertexCoord(const u32 Index) const;
00378
00386 void setVertexNormal(const u32 Index, const dim::vector3df &Normal);
00387
00389 dim::vector3df getVertexNormal(const u32 Index) const;
00390
00396 void setVertexTangent(const u32 Index, const dim::vector3df &Tangent);
00397
00399 dim::vector3df getVertexTangent(const u32 Index) const;
00400
00406 void setVertexBinormal(const u32 Index, const dim::vector3df &Binormal);
00407
00409 dim::vector3df getVertexBinormal(const u32 Index) const;
00410
00416 void setVertexColor(const u32 Index, const color &Color);
00417
00419 color getVertexColor(const u32 Index) const;
00420
00427 void setVertexTexCoord(const u32 Index, const dim::vector3df &TexCoord, const u8 Layer = TEXTURE_IGNORE);
00428
00430 dim::vector3df getVertexTexCoord(const u32 Index, const u8 Layer = 0) const;
00431
00437 void setVertexFog(const u32 Index, const f32 FogCoord);
00438
00440 f32 getVertexFog(const u32 Index) const;
00441
00442
00443
00445 virtual void updateNormals(const EShadingTypes Shading = SHADING_GOURAUD);
00446
00455 void updateTangentSpace(
00456 const u8 TangentLayer = TEXTURE_IGNORE, const u8 BinormalLayer = TEXTURE_IGNORE, bool UpdateNormals = true
00457 );
00458
00460 void meshTranslate(const dim::vector3df &Direction);
00462 void meshTransform(const dim::vector3df &Size);
00464 void meshTransform(const dim::matrix4f &Matrix);
00466 void meshTurn(const dim::vector3df &Rotation);
00467
00469 void meshFlip();
00471 void meshFlip(bool isXAxis, bool isYAxis, bool isZAxis);
00472
00477 void seperateTriangles();
00478
00484 void paint(const video::color &Color, bool CombineColors = false);
00485
00486
00487
00497 TextureLayer* addTexture(
00498 Texture* Tex = 0, const u8 Layer = TEXLAYER_LAST, const ETextureLayerTypes LayerType = TEXLAYER_DEFAULT
00499 );
00500
00510 template <class T> T* addTexture(Texture* Tex = 0, const u8 Layer = TEXLAYER_LAST)
00511 {
00512 T* NewTexLayer = new T();
00513 addTextureLayer(NewTexLayer, Tex, Layer);
00514 return NewTexLayer;
00515 }
00516
00525 bool removeTexture(const u8 Layer = TEXLAYER_LAST, bool RemoveLayer = true);
00526
00535 u32 removeTexture(Texture* Tex, bool RemoveLayers = true);
00536
00538 void clearTextureLayers();
00539
00550 TextureLayer* getTextureLayer(const u8 Layer, bool SearchLayerIndex = false) const;
00551
00557 void textureTranslate(const u8 Layer, const dim::vector3df &Direction);
00558
00564 void textureTransform(const u8 Layer, const dim::vector3df &Size);
00565
00572 void textureTurn(const u8 Layer, const f32 Rotation);
00573
00579 void setTexture(const u8 Layer, Texture* Tex);
00580
00582 Texture* getTexture(const u8 Layer = 0) const;
00583
00591 void setTextureMatrix(const u8 Layer, const dim::matrix4f &Matrix);
00592
00594 dim::matrix4f getTextureMatrix(const u8 Layer) const;
00595
00601 void setTextureEnv(const u8 Layer, const ETextureEnvTypes Type);
00602
00604 ETextureEnvTypes getTextureEnv(const u8 Layer) const;
00605
00615 void setMappingGen(const u8 Layer, const EMappingGenTypes Type);
00616
00618 EMappingGenTypes getMappingGen(const u8 Layer) const;
00619
00630 void setMappingGenCoords(const u8 Layer, s32 Coords);
00631
00633 s32 getMappingGenCoords(const u8 Layer) const;
00634
00636 std::vector<Texture*> getTextureList() const;
00637
00645 void setTexturesReference(MeshBuffer* Reference);
00646
00653 void setTexturesReference(TextureLayerListType* Reference);
00654
00655
00656
00658 inline void setReference(MeshBuffer* ReferenceSurface)
00659 {
00660 Reference_ = ReferenceSurface;
00661 }
00662
00668 inline void setHardwareInstancing(s32 InstanceCount)
00669 {
00670 InstanceCount_ = InstanceCount;
00671 }
00673 inline s32 getHardwareInstancing() const
00674 {
00675 return InstanceCount_;
00676 }
00677
00679 inline void setName(const io::stringc &Name)
00680 {
00681 Name_ = Name;
00682 }
00684 inline const io::stringc& getName() const
00685 {
00686 return Name_;
00687 }
00688
00690 inline void* getVertexBufferID() const
00691 {
00692 return VertexBuffer_.Reference;
00693 }
00695 inline void* getIndexBufferID() const
00696 {
00697 return IndexBuffer_.Reference;
00698 }
00699
00701 inline const dim::UniversalBuffer& getVertexBuffer() const
00702 {
00703 return VertexBuffer_.RawBuffer;
00704 }
00706 inline const dim::UniversalBuffer& getIndexBuffer() const
00707 {
00708 return IndexBuffer_.RawBuffer;
00709 }
00710
00712 inline const VertexFormat* getVertexFormat() const
00713 {
00714 return VertexFormat_;
00715 }
00717 inline const IndexFormat* getIndexFormat() const
00718 {
00719 return &IndexFormat_;
00720 }
00721
00723 inline u32 getVertexCount() const
00724 {
00725 return VertexBuffer_.RawBuffer.getCount();
00726 }
00727
00729 inline void setMeshBufferUsage(const EHWBufferUsage Usage)
00730 {
00731 setVertexBufferUsage(Usage);
00732 setIndexBufferUsage(Usage);
00733 }
00734
00741 inline void setVertexBufferUsage(const EHWBufferUsage Usage)
00742 {
00743 VertexBuffer_.Usage = Usage;
00744 }
00746 inline EHWBufferUsage getVertexBufferUsage() const
00747 {
00748 return VertexBuffer_.Usage;
00749 }
00750
00752 inline void setIndexBufferUsage(const EHWBufferUsage Usage)
00753 {
00754 IndexBuffer_.Usage = Usage;
00755 }
00757 inline EHWBufferUsage getIndexBufferUsage() const
00758 {
00759 return IndexBuffer_.Usage;
00760 }
00761
00763 inline void setTextureLayerList(const TextureLayerListType &TextureLayers)
00764 {
00765 *TextureLayers_ = TextureLayers;
00766 }
00768 inline const TextureLayerListType& getTextureLayerList() const
00769 {
00770 return *TextureLayers_;
00771 }
00772
00774 inline u32 getTextureCount() const
00775 {
00776 return TextureLayers_->size();
00777 }
00778
00780 inline bool hasTexturesReference() const
00781 {
00782 return (&OrigTextureLayers_) != TextureLayers_;
00783 }
00784
00786 inline void setIndexOffset(u32 Offset)
00787 {
00788 IndexOffset_ = Offset;
00789 }
00791 inline void addIndexOffset(u32 Offset)
00792 {
00793 IndexOffset_ += Offset;
00794 }
00795
00803 inline void setIndexBufferEnable(bool Enable)
00804 {
00805 UseIndexBuffer_ = Enable;
00806 }
00808 inline bool getIndexBufferEnable() const
00809 {
00810 return UseIndexBuffer_;
00811 }
00812
00819 inline void setUpdateImmediate(bool Enable)
00820 {
00821 UpdateImmediate_ = Enable;
00822 }
00824 inline bool getUpdateImmediate() const
00825 {
00826 return UpdateImmediate_;
00827 }
00828
00830 inline ERenderPrimitives getPrimitiveType() const
00831 {
00832 return PrimitiveType_;
00833 }
00834
00835 protected:
00836
00837
00838
00839 struct SMeshBufferBackup
00840 {
00841 SMeshBufferBackup() :
00842 BUVertexFormat(0)
00843 {
00844 }
00845 ~SMeshBufferBackup()
00846 {
00847 }
00848
00849
00850 dim::UniversalBuffer BUVertexBuffer;
00851 dim::UniversalBuffer BUIndexBuffer;
00852
00853 const VertexFormat* BUVertexFormat;
00854 IndexFormat BUIndexFormat;
00855 };
00856
00857 struct SBuffer
00858 {
00859 SBuffer() :
00860 Reference (0 ),
00861 Validated (false ),
00862 Usage (HWBUFFER_STATIC)
00863 {
00864 }
00865 SBuffer(const SBuffer &Other) :
00866 Reference (0 ),
00867 RawBuffer (Other.RawBuffer),
00868 Validated (false ),
00869 Usage (Other.Usage )
00870 {
00871 }
00872 ~SBuffer()
00873 {
00874 }
00875
00876
00877 void* Reference;
00878 dim::UniversalBuffer RawBuffer;
00879 bool Validated;
00880 EHWBufferUsage Usage;
00881 };
00882
00883
00884
00885 void convertVertexAttribute(
00886 const dim::UniversalBuffer &OldBuffer, u32 Index, const SVertexAttribute &OldAttrib,
00887 const SVertexAttribute &NewAttrib, bool isClamp = true
00888 );
00889 void fillVertexAttribute(u32 Index, const SVertexAttribute &Attrib);
00890
00891 void setVertexAttributeBarycentric(
00892 u32 IndexResult, u32 IndexA, u32 IndexB, u32 IndexC,
00893 const SVertexAttribute &Attrib, const dim::vector3df &BarycentricCoord
00894 );
00895
00896 virtual void updateNormalsFlat();
00897 virtual void updateNormalsGouraud();
00898
00899 void checkIndexFormat(ERendererDataTypes &Format);
00900
00901
00902
00903 template <typename T, typename D> inline void setDefaultVertexAttribute(
00904 const ERendererDataTypes Type, s32 MaxSize, u32 Index, const SVertexAttribute &Attrib, const T &Data)
00905 {
00906 if (Attrib.Type == Type)
00907 VertexBuffer_.RawBuffer.setBuffer(Index, Attrib.Offset, (const void*)&Data, sizeof(D) * math::Min(Attrib.Size, MaxSize));
00908 }
00909 template <typename T, typename D> inline T getDefaultVertexAttribute(
00910 const ERendererDataTypes Type, s32 MaxSize, u32 Index, const SVertexAttribute &Attrib) const
00911 {
00912 T Data;
00913
00914 if (Attrib.Type == Type)
00915 VertexBuffer_.RawBuffer.getBuffer(Index, Attrib.Offset, (void*)&Data, sizeof(D) * math::Min(Attrib.Size, MaxSize));
00916
00917 return Data;
00918 }
00919
00920 template <typename T> inline void addTriangleIndices(const u32 VertexA, const u32 VertexB, const u32 VertexC)
00921 {
00922 IndexBuffer_.RawBuffer.add<T>((T)VertexA);
00923 IndexBuffer_.RawBuffer.add<T>((T)VertexB);
00924 IndexBuffer_.RawBuffer.add<T>((T)VertexC);
00925 }
00926 template <typename T> inline void addQuadrangleIndices(const u32 VertexA, const u32 VertexB, const u32 VertexC, const u32 VertexD)
00927 {
00928 IndexBuffer_.RawBuffer.add<T>((T)VertexA);
00929 IndexBuffer_.RawBuffer.add<T>((T)VertexB);
00930 IndexBuffer_.RawBuffer.add<T>((T)VertexC);
00931 IndexBuffer_.RawBuffer.add<T>((T)VertexD);
00932 }
00933
00934
00935
00936 io::stringc Name_;
00937
00938 SBuffer VertexBuffer_;
00939 SBuffer IndexBuffer_;
00940
00941 const VertexFormat* VertexFormat_;
00942 IndexFormat IndexFormat_;
00943
00944 MeshBuffer* Reference_;
00945
00946 TextureLayerListType OrigTextureLayers_;
00947 TextureLayerListType* TextureLayers_;
00948
00949 u32 IndexOffset_;
00950 s32 InstanceCount_;
00951
00952 ERenderPrimitives PrimitiveType_;
00953 bool UseIndexBuffer_;
00954 bool UpdateImmediate_;
00955
00956 SMeshBufferBackup* Backup_;
00957
00958 private:
00959
00960
00961
00962 void setupDefaultBuffers();
00963
00964 void addTextureLayer(TextureLayer* TexLayer, Texture* Tex = 0, const u8 Layer = TEXLAYER_LAST);
00965 void removeTextureFromLayer(TextureLayerListType::iterator &it, bool RemoveLayer);
00966 void sortTextureLayers();
00967
00968 };
00969
00970
00971 }
00972
00973 }
00974
00975
00976 #endif
00977
00978
00979
00980