Last active
June 27, 2017 03:36
-
-
Save victorholt/5913777922c61549f9d9bce9487a750f to your computer and use it in GitHub Desktop.
CustomMesh Header Refactored
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Victor Holt on 3/18/2017. | |
// Reference: (rbnpontes) http://discourse.urho3d.io/t/a-mesh-generator/2361 | |
// | |
#pragma once | |
#include <gs-common/PreCompiledHeadersUrho.h> | |
using namespace Urho3D; | |
namespace Sencha | |
{ | |
enum class MeshUVType { | |
UV_XY = 0, | |
UV_XZ, | |
UV_YZ, | |
UV_ZY | |
}; | |
struct GAMESENCHA_API MeshVertex { | |
Vector3 vertex_; | |
Vector3 normal_; | |
Vector2 uv_; | |
Vector4 tangent_; | |
MeshUVType uvType_; | |
}; | |
struct GAMESENCHA_API MeshFace { | |
unsigned vertIndexStart_; | |
MeshVertex* v0_; | |
MeshVertex* v1_; | |
MeshVertex* v2_; | |
}; | |
struct GAMESENCHA_API MeshRawData { | |
uint32_t numVerts_ = 0; | |
uint32_t numIndices_ = 0; | |
PODVector<VertexElement> elements_; | |
uint8_t* vertexData_ = nullptr; | |
uint16_t* indexData_ = nullptr; | |
uint32_t* largeIndexData_ = nullptr; | |
BoundingBox boundingBox_; | |
MeshRawData& operator=(const MeshRawData& rawData) { | |
this->numVerts_ = rawData.numVerts_; | |
this->numIndices_ = rawData.numIndices_; | |
this->elements_ = rawData.elements_; | |
this->vertexData_ = rawData.vertexData_; | |
this->indexData_ = rawData.indexData_; | |
this->largeIndexData_ = rawData.largeIndexData_; | |
this->boundingBox_ = rawData.boundingBox_; | |
return *this; | |
} | |
}; | |
class GAMESENCHA_API CustomMesh : public Object | |
{ | |
URHO3D_OBJECT(CustomMesh, Object); | |
public: | |
//! Constructor. | |
//! \param context | |
//! \param isDynamic | |
CustomMesh(Context* context, bool isDynamic); | |
//! Destructor. | |
virtual ~CustomMesh(); | |
//! Adds a face to the mesh. | |
//! \param v0 | |
//! \param v1 | |
//! \param v2 | |
//! \param uvType | |
MeshFace* AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2, const MeshUVType& uvType = MeshUVType::UV_XZ); | |
//! Adds an index. | |
//! \param index | |
void AddIndex(uint16_t index); | |
//! Adds an index. | |
//! \param index | |
void AddLargeIndex(uint32_t index); | |
//! Sets the face of the mesh based on the given index. | |
//! \param i | |
//! \param face | |
void SetFace(unsigned int i, const MeshFace& face); | |
//! Returns the face of the mesh from a given index. | |
//! \param i | |
//! \return | |
MeshFace* GetFace(unsigned int i); | |
//! Commits changes to the mesh. | |
//! \param updateNormals | |
//! \param updateTangents | |
void Commit(bool updateNormals = false, bool updateTangents = false); | |
//! Calculates the mesh normals. | |
//! \param check | |
void CalculateNormals(bool check = true); | |
//! Calculates the mesh tangents. | |
//! \param check | |
void CalculateTangents(bool check = true); | |
//! Updates the texture coordinates. | |
void UpdateTexCoords(); | |
//! Updates the bounding box for the mesh. | |
void UpdateBoundingBox(); | |
//! Clears all vertex data of the mesh. | |
void Clear(); | |
//! Saves the mesh to a file. | |
//! \param path | |
void Save(const String& path); | |
//! Returns the mesh geometry. | |
//! \return | |
Geometry* GetGeometry(); | |
//! Returns the mesh model. | |
//! \return | |
Model* GetModel(); | |
//! Sets the scale of the mesh. | |
//! \param scale | |
inline void SetScale(float scale) { | |
scale_ = scale; | |
} | |
//! Returns the scale of the mesh. | |
//! \return | |
inline float GetScale() const { | |
return scale_; | |
} | |
//! Flag to use large indices. | |
//! \param useLargeIndices | |
inline void SetUseLargeIndices(bool useLargeIndices) { | |
useLargeIndices_ = useLargeIndices; | |
} | |
//! Sets the raw data. | |
//! \param rawData | |
//! \param rawIndexData | |
void SetRawData(const MeshRawData& rawData); | |
//! Returns the mesh vertex buffer. | |
//! \return | |
VertexBuffer* GetVertexBuffer(); | |
//! Returns the mesh index buffer. | |
//! \return | |
IndexBuffer* GetIndexBuffer(); | |
//! Rotates a mesh face. | |
//! \param face | |
//! \param rot | |
void RotateFace(MeshFace* face, const Quaternion& rot); | |
//! Rotates a mesh face. | |
//! \param faceIndex | |
//! \param rot | |
void RotateFaceByIndex(unsigned faceIndex, const Quaternion& rot); | |
//! Rotates the mesh by the given rotation. | |
//! \param rot | |
void Rotate(const Quaternion& rot); | |
//! Returns the bounding box. | |
//! \return | |
inline const BoundingBox& GetBoundingBox() const { return boundingBox_; } | |
protected: | |
//! Adds a vertex. | |
//! \param v | |
//! \param uvType | |
MeshVertex* AddVertex(const Vector3& v, const MeshUVType& uvType = MeshUVType::UV_XZ); | |
//! Builds the mesh. | |
//! \param updateNormals | |
//! \param updateTangents | |
void CreateMesh(bool updateNormals, bool updateTangents); | |
//! Generates the vertex data. | |
void GenerateVertexData(); | |
//! Updates the vertex data. | |
void UpdateVertexData(); | |
//! Creates a new face. | |
//! \param v0 | |
//! \param v1 | |
//! \param v2 | |
//! \return | |
MeshFace* CreateFace(MeshVertex* v0, MeshVertex* v1, MeshVertex* v2); | |
//! Updates the mesh. | |
//! \param updateNormals | |
//! \param updateTangents | |
void UpdateMesh(bool updateNormals, bool updateTangents); | |
protected: | |
/// The scale of the mesh. | |
float scale_; | |
/// Flag for if this is a dynamic mesh. | |
bool isDynamic_; | |
/// Flag to calculate the normals. | |
bool calcNormals_; | |
/// Flag to calculate the tangents. | |
bool calcTangents_; | |
/// Flag to use large indices. | |
bool useLargeIndices_; | |
/// Flag to use raw vertex/index data. | |
bool useRawDataOnly_; | |
/// Flag to manually add indices. | |
bool manualAddIndex_; | |
/// Raw mesh data. | |
MeshRawData rawData_; | |
/// Faces for the mesh. | |
PODVector<MeshFace*> faces_; | |
/// Geometry of the mesh. | |
SharedPtr<Geometry> geometry_; | |
/// Model of the mesh. | |
SharedPtr<Model> model_; | |
/// Vertex buffer of the mesh. | |
SharedPtr<VertexBuffer> vertexBuffer_; | |
/// Index buffer of the mesh. | |
SharedPtr<IndexBuffer> indexBuffer_; | |
/// Vertex data of the mesh. | |
PODVector<MeshVertex*> meshVertexList_; | |
/// Vertex data of the mesh. | |
PODVector<float> vertexData_; | |
/// Index data of the mesh. | |
PODVector<uint16_t> indexData_; | |
/// Large index data. | |
PODVector<uint32_t> largeIndexData_; | |
/// Reference to the bounding box. | |
BoundingBox boundingBox_; | |
/// Mutex for the mesh. | |
Mutex meshLock_; | |
/// Mutex for calculations. | |
Mutex calcMeshLock_; | |
/// Transform for the mesh. | |
Matrix3x4 transform_; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment