Added Mesh::Transform and bounding box calculation

master
Martin Felis 2017-02-26 14:26:12 +01:00
parent 6573a36c90
commit 29431abde7
2 changed files with 34 additions and 1 deletions

View File

@ -910,6 +910,28 @@ void Mesh::Update() {
mBgfxMesh = bgfxutils::createMeshFromStdVectors (mVertices, mNormals, mColors); 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) { void Mesh::Merge (const Mesh& other, const Matrix44f &transform) {
for (int i = 0; i < other.mVertices.size(); ++i) { for (int i = 0; i < other.mVertices.size(); ++i) {
mVertices.push_back (transform.transpose() * other.mVertices[i]); mVertices.push_back (transform.transpose() * other.mVertices[i]);
@ -928,6 +950,12 @@ void Mesh::Submit (const RenderState *state, const float* matrix) const {
matrix); 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* Mesh::sCreateCuboid (float width, float height, float depth) {
Mesh* result = new Mesh(); Mesh* result = new Mesh();

View File

@ -18,11 +18,16 @@ struct Mesh {
std::vector<Vector4f> mVertices; std::vector<Vector4f> mVertices;
std::vector<Vector3f> mNormals; std::vector<Vector3f> mNormals;
std::vector<Vector4f> mColors; std::vector<Vector4f> mColors;
Vector3f mBoundsMin = Vector3f(0.f, 0.f, 0.f);
Vector3f mBoundsMax = Vector3f(0.f, 0.f, 0.f);
~Mesh(); ~Mesh();
void Update(); 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 Submit (const RenderState *state, const float* matrix) const;
void Transform (const Matrix44f &mat);
static Mesh *sCreateCuboid (float width, float height, float depth); static Mesh *sCreateCuboid (float width, float height, float depth);
static Mesh *sCreateUVSphere (int rows, int segments, float radius = 1.0f); static Mesh *sCreateUVSphere (int rows, int segments, float radius = 1.0f);