Skip to content

Instantly share code, notes, and snippets.

@victorholt
Last active May 14, 2018 03:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorholt/2dac4b295623afd66641e629c8b16dad to your computer and use it in GitHub Desktop.
Save victorholt/2dac4b295623afd66641e629c8b16dad to your computer and use it in GitHub Desktop.
Urho3D CustomMesh Header
//
// 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,
UV_XZ,
UV_YZ,
UV_ZY
};
struct GAMESENCHA_API MeshFace {
Vector3 v0_;
Vector3 v1_;
Vector3 v2_;
Vector3 n0_;
Vector3 n1_;
Vector3 n2_;
Vector2 uv0_;
Vector2 uv1_;
Vector2 uv2_;
Vector4 tan0_;
Vector4 tan1_;
Vector4 tan2_;
unsigned vertIndexStart_;
MeshUVType uvType_ = MeshUVType::UV_XY;
};
class GAMESENCHA_API CustomMesh : public Object
{
URHO3D_OBJECT(CustomMesh, Object);
public:
//! Constructor.
//! \param context
//! \param isDynamic
CustomMesh(Context* context, bool isDynamic);
//! Adds a face to the mesh.
//! \param v0
//! \param v1
//! \param v2
//! \param uvType
void AddFace(Vector3 v0, Vector3 v1, Vector3 v2, const MeshUVType& uvType = MeshUVType::UV_XZ);
//! Adds a face to the mesh.
//! \param face
void AddFace(MeshFace* face);
//! 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();
//! Returns the mesh vertex buffer.
//! \return
VertexBuffer* GetVertexBuffer();
//! Returns the mesh index buffer.
//! \return
IndexBuffer* GetIndexBuffer();
//! Returns the bounding box.
//! \return
inline const BoundingBox& GetBoundingBox() const { return boundingBox_; }
protected:
//! Builds the mesh.
//! \param updateNormals
//! \param updateTangents
void CreateMesh(bool updateNormals, bool updateTangents);
//! Updates the vertex data from the face data.
void UpdateVertexData();
//! Updates the vertex data from a new mesh face.
//! \param face
void UpdateVertexData(MeshFace*& newMeshFace);
//! Updates the mesh.
//! \param updateNormals
//! \param updateTangents
void UpdateMesh(bool updateNormals, bool updateTangents);
protected:
/// Flag for if this is a dynamic mesh.
bool isDynamic_;
/// Flag to calculate the normals.
bool calcNormals_;
/// Flag to calculate the tangents.
bool calcTangents_;
/// Faces for the mesh.
Vector<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.
Vector<float> vertexData_;
/// Index data of the mesh.
Vector<unsigned short> indexData_;
/// Reference to the bounding box.
BoundingBox boundingBox_;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment