diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 79e5648..6558fa3 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -910,6 +910,28 @@ void Mesh::Update() { mBgfxMesh = bgfxutils::createMeshFromStdVectors (mVertices, mNormals, mColors); } +void Mesh::UpdateBounds() { + if (mVertices.size() == 0) { + mBoundsMin = Vector3f (0.f, 0.f, 0.f); + mBoundsMax = Vector3f (0.f, 0.f, 0.f); + + gLog ("Error: updating bounds for mesh with zero vertices"); + abort(); + } + + mBoundsMin = mVertices[0].block<3,1>(0,0); + mBoundsMax = mVertices[0].block<3,1>(0,0); + + for (int i = 0; i < mVertices.size(); i++) { + for (int j = 0; j < 3; j++) { + mBoundsMin[j] = mBoundsMin[j] < mVertices[i][j] + ? mBoundsMin[j] :mVertices[i][j]; + mBoundsMax[j] = mBoundsMax[j] > mVertices[i][j] + ? mBoundsMax[j] :mVertices[i][j]; + } + } +} + void Mesh::Merge (const Mesh& other, const Matrix44f &transform) { for (int i = 0; i < other.mVertices.size(); ++i) { mVertices.push_back (transform.transpose() * other.mVertices[i]); @@ -928,6 +950,12 @@ void Mesh::Submit (const RenderState *state, const float* matrix) const { matrix); } +void Mesh::Transform(const Matrix44f &transform) { + for (int i = 0; i < mVertices.size(); ++i) { + mVertices[i] = (transform.transpose() * mVertices[i]); + mNormals[i] = (Matrix33f(transform.block<3,3>(0,0)).transpose() * mNormals[i]); + } +} Mesh* Mesh::sCreateCuboid (float width, float height, float depth) { Mesh* result = new Mesh(); diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 04e567e..be7f088 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -18,11 +18,16 @@ struct Mesh { std::vector mVertices; std::vector mNormals; std::vector mColors; + Vector3f mBoundsMin = Vector3f(0.f, 0.f, 0.f); + Vector3f mBoundsMax = Vector3f(0.f, 0.f, 0.f); ~Mesh(); void Update(); - void Merge (const Mesh& other, const Matrix44f &mat = Matrix44f::Identity()); + void UpdateBounds (); + void Merge (const Mesh& other, + const Matrix44f &transform = Matrix44f::Identity()); void Submit (const RenderState *state, const float* matrix) const; + void Transform (const Matrix44f &mat); static Mesh *sCreateCuboid (float width, float height, float depth); static Mesh *sCreateUVSphere (int rows, int segments, float radius = 1.0f);