Polyphase Game Engine
Loading...
Searching...
No Matches
StaticMesh.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "Assets/Material.h"
6#include "Asset.h"
7#include "Vertex.h"
8
10
11#define CREATE_CONVEX_COLLISION_MESH (PLATFORM_WINDOWS || PLATFORM_LINUX)
12
13#if EDITOR
14#include <assimp/scene.h>
15#endif
16
18{
19public:
20
22
23 StaticMesh();
25
26 void CreateRaw(
27 uint32_t numVertices,
28 Vertex* vertices,
29 uint32_t numIndices,
30 IndexType* indices);
31
32 void CreateRawColor(
33 uint32_t numVertices,
34 VertexColor* vertices,
35 uint32_t numIndices,
36 IndexType* indices);
37
38 StaticMeshResource* GetResource();
39
40 // Asset Interface
41 virtual void LoadStream(Stream& stream, Platform platform) override;
42 virtual void SaveStream(Stream& stream, Platform platform) override;
43 virtual void Create() override;
44 virtual void Destroy() override;
45 virtual bool Import(const std::string& path, ImportOptions* options) override;
46 virtual void GatherProperties(std::vector<Property>& outProps) override;
47 virtual glm::vec4 GetTypeColor() override;
48 virtual const char* GetTypeName() override;
49 virtual const char* GetTypeImportExt() override;
50
51 class Material* GetMaterial();
52 void SetMaterial(class Material* newMaterial);
53
54 uint32_t GetNumIndices() const;
55 uint32_t GetNumFaces() const;
56 uint32_t GetNumVertices() const;
57 bool HasVertexColor() const;
58
59 Vertex* GetVertices();
60 VertexColor* GetColorVertices();
61 IndexType* GetIndices();
62
63 Bounds GetBounds() const;
64 AABB GetAABB() const;
65
66 btBvhTriangleMeshShape* GetTriangleCollisionShape();
67 btCollisionShape* GetCollisionShape();
68 void SetCollisionShape(btCollisionShape* shape);
69 void SetCollisionShapes(uint32_t numCollisionShapes, btCollisionShape** collisionShapes, btTransform* transforms, bool compound);
70
71 void SetGenerateTriangleCollisionMesh(bool generate);
72 bool IsTriangleCollisionMeshEnabled() const;
73 uint32_t GetVertexSize() const;
74
75 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
76
77private:
78
79 bool ShouldGenerateTriangleCollision() const;
80
81 void CreateTriangleCollisionShape();
82 void DestroyTriangleCollisionShape();
83
84 // Tear down the GPU resource and BVH on an already-loaded mesh and clear
85 // Asset::mLoaded so CreateRaw / CreateRawColor can rebuild via Create().
86 // Does NOT call Asset::Destroy (which would erase AssetRefs and break
87 // scene references to this mesh).
88 void PrepareForRebuild();
89
90 void ResizeVertexArray(uint32_t newSize);
91 void ResizeIndexArray(uint32_t newSize);
92
93 void ComputeBounds();
94
95 MaterialRef mMaterial;
96 uint32_t mNumVertices;
97 uint32_t mNumIndices;
98 uint32_t mNumUvMaps;
99
100 void* mVertices;
101 IndexType* mIndices;
102
103 Bounds mBounds;
104
105 // Derived from vertex positions in ComputeBounds(). Intentionally NOT
106 // serialized, because Create() recomputes bounds on every load -- the
107 // serialized mBounds is already dead data. If a future cook path ever
108 // strips vertex data, this has to become a versioned field.
109 AABB mAABB;
110
111 btCollisionShape* mCollisionShape;
112 btBvhTriangleMeshShape* mTriangleCollisionShape;
113 btTriangleIndexVertexArray* mTriangleIndexVertexArray;
114 btTriangleInfoMap* mTriangleInfoMap;
115 bool mGenerateTriangleCollisionMesh;
116 bool mHasVertexColor;
117
118 // Graphics Resource
119 StaticMeshResource mResource;
120
121#if EDITOR
122public:
123 void Create(
124 const aiScene* scene,
125 const aiMesh& meshData,
126 uint32_t numCollisionMeshes,
127 const aiMesh** collisionMeshes);
128 void CreateCombined(
129 const aiScene* scene,
130 const std::vector<const aiMesh*>& renderMeshes,
131 uint32_t numCollisionMeshes,
132 const aiMesh** collisionMeshes);
133protected:
134 std::vector<uint32_t> mPureVertexColors;
135#endif
136
137#if CREATE_CONVEX_COLLISION_MESH
138public:
139 void CreateCollisionMesh(btCollisionShape* collisionShape);
140 std::vector<StaticMesh*> mCollisionMeshes;
141#endif // EDITOR
142};
Platform
Definition EngineTypes.h:32
uint16_t IndexType
Definition GraphicsTypes.h:122
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Bounds ComputeBounds(const std::vector< T > &vertices)
Definition Utilities.h:153
Definition AssetRef.h:18
Definition Asset.h:120
virtual bool Import(const std::string &path, ImportOptions *options=nullptr)
Definition Asset.cpp:342
virtual void Create()
Definition Asset.cpp:78
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:334
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:352
virtual const char * GetTypeImportExt()
Definition Asset.cpp:362
virtual const char * GetTypeName()
Definition Asset.cpp:357
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:347
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:320
virtual void Destroy()
Definition Asset.cpp:93
Definition Datum.h:169
Definition Asset.h:109
Definition Material.h:48
Definition StaticMesh.h:18
DECLARE_ASSET(StaticMesh, Asset)
Definition Stream.h:21
Definition EngineTypes.h:210
Definition EngineTypes.h:201
Definition GraphicsTypes.h:172
Definition Vertex.h:28
Definition Vertex.h:20