mirror of https://github.com/mosra/magnum.git
6 changed files with 193 additions and 0 deletions
@ -0,0 +1,38 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "FlipNormals.h" |
||||
|
||||
using namespace std; |
||||
using namespace Corrade::Utility; |
||||
|
||||
namespace Magnum { namespace MeshTools { |
||||
|
||||
void flipFaceWinding(vector<unsigned int>& indices) { |
||||
if(indices.size()%3 != 0) { |
||||
Error() << "MeshTools::flipNormals(): index count is not divisible by 3!"; |
||||
return; |
||||
} |
||||
|
||||
for(size_t i = 0; i != indices.size(); i += 3) |
||||
swap(indices[i+1], indices[i+2]); |
||||
} |
||||
|
||||
void flipNormals(vector<Vector3>& normals) { |
||||
for(Vector3& normal: normals) |
||||
normal = -normal; |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,61 @@
|
||||
#ifndef Magnum_MeshTools_FlipNormals_h |
||||
#define Magnum_MeshTools_FlipNormals_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Function Magnum::MeshTools::flipNormals() |
||||
*/ |
||||
|
||||
#include "Magnum.h" |
||||
|
||||
namespace Magnum { namespace MeshTools { |
||||
|
||||
/**
|
||||
@brief Flip face winding |
||||
|
||||
The same as flipNormals(std::vector<unsigned int>&, std::vector<Vector3>&), |
||||
but flips only face winding. |
||||
*/ |
||||
void flipFaceWinding(std::vector<unsigned int>& indices); |
||||
|
||||
/**
|
||||
@brief Flip mesh normals |
||||
|
||||
The same as flipNormals(std::vector<unsigned int>&, std::vector<Vector3>&), |
||||
but flips only normals, not face winding. |
||||
*/ |
||||
void flipNormals(std::vector<Vector3>& normals); |
||||
|
||||
/**
|
||||
@brief Flip mesh normals and face winding |
||||
@param indices Index array to operate on |
||||
@param normals Normal array to operate on |
||||
|
||||
Flips normal vectors and face winding in index array for face culling to work |
||||
properly too. See also flipNormals(std::vector<Vector3>&) and |
||||
flipFaceWinding(), which flip normals or face winding only. |
||||
|
||||
@attention The function requires the mesh to have triangle faces, thus index |
||||
count must be divisible by 3. |
||||
*/ |
||||
inline void flipNormals(std::vector<unsigned int>& indices, std::vector<Vector3>& normals) { |
||||
flipFaceWinding(indices); |
||||
flipNormals(normals); |
||||
} |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "FlipNormalsTest.h" |
||||
|
||||
#include <sstream> |
||||
#include <QtTest/QTest> |
||||
#include "MeshTools/FlipNormals.h" |
||||
|
||||
QTEST_APPLESS_MAIN(Magnum::MeshTools::Test::FlipNormalsTest) |
||||
|
||||
using namespace std; |
||||
using namespace Corrade::Utility; |
||||
|
||||
namespace Magnum { namespace MeshTools { namespace Test { |
||||
|
||||
void FlipNormalsTest::wrongIndexCount() { |
||||
stringstream ss; |
||||
Error::setOutput(&ss); |
||||
|
||||
vector<unsigned int> indices{0, 1}; |
||||
MeshTools::flipFaceWinding(indices); |
||||
|
||||
QVERIFY(ss.str() == "MeshTools::flipNormals(): index count is not divisible by 3!\n"); |
||||
} |
||||
|
||||
void FlipNormalsTest::flipFaceWinding() { |
||||
vector<unsigned int> indices{0, 1, 2, |
||||
3, 4, 5}; |
||||
MeshTools::flipFaceWinding(indices); |
||||
|
||||
QVERIFY((indices == vector<unsigned int>{0, 2, 1, |
||||
3, 5, 4})); |
||||
} |
||||
|
||||
void FlipNormalsTest::flipNormals() { |
||||
vector<Vector3> normals{Vector3::xAxis(), |
||||
Vector3::yAxis(), |
||||
Vector3::zAxis()}; |
||||
MeshTools::flipNormals(normals); |
||||
|
||||
QVERIFY((normals == vector<Vector3>{-Vector3::xAxis(), |
||||
-Vector3::yAxis(), |
||||
-Vector3::zAxis()})); |
||||
} |
||||
|
||||
}}} |
||||
@ -0,0 +1,33 @@
|
||||
#ifndef Magnum_MeshTools_Test_FlipNormalsTest_h |
||||
#define Magnum_MeshTools_Test_FlipNormalsTest_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include <QtCore/QObject> |
||||
|
||||
namespace Magnum { namespace MeshTools { namespace Test { |
||||
|
||||
class FlipNormalsTest: public QObject { |
||||
Q_OBJECT |
||||
|
||||
private slots: |
||||
void wrongIndexCount(); |
||||
void flipFaceWinding(); |
||||
void flipNormals(); |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue