diff --git a/code/ColladaExporter.cpp b/code/ColladaExporter.cpp index 078ac0a56..bcd4518e4 100644 --- a/code/ColladaExporter.cpp +++ b/code/ColladaExporter.cpp @@ -51,7 +51,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "XMLTools.h" #include "../include/assimp/IOSystem.hpp" #include "../include/assimp/Exporter.hpp" -#include "../include/assimp/scene.h" +#include "../include/assimp/scene.h" #include "Exceptional.h" @@ -127,6 +127,8 @@ void ColladaExporter::WriteFile() WriteTextures(); WriteHeader(); + WriteCamerasLibrary(); + WriteLightsLibrary(); WriteMaterials(); WriteGeometryLibrary(); @@ -286,6 +288,201 @@ void ColladaExporter::WriteTextures() { } } +// ------------------------------------------------------------------------------------------------ +// Write the embedded textures +void ColladaExporter::WriteCamerasLibrary() { + if(mScene->HasCameras()) { + + mOutput << startstr << "" << endstr; + PushTag(); + + for( size_t a = 0; a < mScene->mNumCameras; ++a) + WriteCamera( a); + + PopTag(); + mOutput << startstr << "" << endstr; + + } +} + +void ColladaExporter::WriteCamera(size_t pIndex){ + + const aiCamera *cam = mScene->mCameras[pIndex]; + const std::string idstrEscaped = XMLEscape(cam->mName.C_Str()); + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + //assimp doesn't support the import of orthographic cameras! se we write + //always perspective + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << ""<< + AI_RAD_TO_DEG(cam->mHorizontalFOV) + <<"" << endstr; + mOutput << startstr << "" + << cam->mAspect + << "" << endstr; + mOutput << startstr << "" + << cam->mClipPlaneNear + << "" << endstr; + mOutput << startstr << "" + << cam->mClipPlaneFar + << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + +} + + +// ------------------------------------------------------------------------------------------------ +// Write the embedded textures +void ColladaExporter::WriteLightsLibrary() { + if(mScene->HasLights()) { + + mOutput << startstr << "" << endstr; + PushTag(); + + for( size_t a = 0; a < mScene->mNumLights; ++a) + WriteLight( a); + + PopTag(); + mOutput << startstr << "" << endstr; + + } +} + +void ColladaExporter::WriteLight(size_t pIndex){ + + const aiLight *light = mScene->mLights[pIndex]; + const std::string idstrEscaped = XMLEscape(light->mName.C_Str()); + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + switch(light->mType){ + case aiLightSource_AMBIENT: + WriteAmbienttLight(light); + break; + case aiLightSource_DIRECTIONAL: + WriteDirectionalLight(light); + break; + case aiLightSource_POINT: + WritePointLight(light); + break; + case aiLightSource_SPOT: + WriteSpotLight(light); + break; + case aiLightSource_UNDEFINED: + case _aiLightSource_Force32Bit: + break; + } + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + +} + +void ColladaExporter::WritePointLight(const aiLight *const light){ + const aiColor3D &color= light->mColorDiffuse; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + mOutput << startstr << "" + << light->mAttenuationConstant + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationLinear + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationQuadratic + <<"" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + +} +void ColladaExporter::WriteDirectionalLight(const aiLight *const light){ + const aiColor3D &color= light->mColorDiffuse; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + +} +void ColladaExporter::WriteSpotLight(const aiLight *const light){ + + const aiColor3D &color= light->mColorDiffuse; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + mOutput << startstr << "" + << light->mAttenuationConstant + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationLinear + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationQuadratic + <<"" << endstr; + /* + out->mAngleOuterCone = AI_DEG_TO_RAD (std::acos(std::pow(0.1f,1.f/srcLight->mFalloffExponent))+ + srcLight->mFalloffAngle); + */ + + const float fallOffAngle = AI_RAD_TO_DEG(light->mAngleInnerCone); + mOutput << startstr <<"" + << fallOffAngle + <<"" << endstr; + double temp = light->mAngleOuterCone-light->mAngleInnerCone; + + temp = std::cos(temp); + temp = std::log(temp)/std::log(0.1); + temp = 1/temp; + mOutput << startstr << "" + << temp + <<"" << endstr; + + + PopTag(); + mOutput << startstr << "" << endstr; + +} + +void ColladaExporter::WriteAmbienttLight(const aiLight *const light){ + + const aiColor3D &color= light->mColorAmbient; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; +} + // ------------------------------------------------------------------------------------------------ // Reads a single surface entry from the given material keys void ColladaExporter::ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex) @@ -847,6 +1044,23 @@ void ColladaExporter::WriteNode(aiNode* pNode) mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4; mOutput << "" << endstr; + if(pNode->mNumMeshes==0){ + //check if it is a camera node + for(size_t i=0; imNumCameras; i++){ + if(mScene->mCameras[i]->mName == pNode->mName){ + mOutput << startstr <<"" << endstr; + break; + } + } + //check if it is a light node + for(size_t i=0; imNumLights; i++){ + if(mScene->mLights[i]->mName == pNode->mName){ + mOutput << startstr <<"" << endstr; + break; + } + } + + }else // instance every geometry for( size_t a = 0; a < pNode->mNumMeshes; ++a ) { @@ -854,9 +1068,8 @@ void ColladaExporter::WriteNode(aiNode* pNode) // do not instanciate mesh if empty. I wonder how this could happen if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 ) continue; - - mOutput << startstr << "mMeshes[a])) << "\">" << endstr; - PushTag(); + mOutput << startstr << "mMeshes[a])) << "\">" << endstr; + PushTag(); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; diff --git a/code/ColladaExporter.h b/code/ColladaExporter.h index 7ba32f9ea..1312ab9ce 100644 --- a/code/ColladaExporter.h +++ b/code/ColladaExporter.h @@ -47,6 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../include/assimp/ai_assert.h" #include "../include/assimp/material.h" #include "../include/assimp/mesh.h" +#include "../include/assimp/light.h" #include "../include/assimp/Exporter.hpp" #include #include @@ -83,6 +84,22 @@ protected: /// Writes the material setup void WriteMaterials(); + /// Writes the cameras library + void WriteCamerasLibrary(); + + // Write a camera entry + void WriteCamera(size_t pIndex); + + /// Writes the cameras library + void WriteLightsLibrary(); + + // Write a camera entry + void WriteLight(size_t pIndex); + void WritePointLight(const aiLight *const light); + void WriteDirectionalLight(const aiLight *const light); + void WriteSpotLight(const aiLight *const light); + void WriteAmbienttLight(const aiLight *const light); + /// Writes the geometry library void WriteGeometryLibrary(); diff --git a/code/ColladaLoader.cpp b/code/ColladaLoader.cpp index b2d4d4869..833e428ac 100644 --- a/code/ColladaLoader.cpp +++ b/code/ColladaLoader.cpp @@ -349,8 +349,8 @@ void ColladaLoader::BuildLightsForNode( const ColladaParser& pParser, const Coll { // Need to rely on falloff_exponent. I don't know how to interpret it, so I need to guess .... // epsilon chosen to be 0.1 - out->mAngleOuterCone = AI_DEG_TO_RAD (std::acos(std::pow(0.1f,1.f/srcLight->mFalloffExponent))+ - srcLight->mFalloffAngle); + out->mAngleOuterCone = std::acos(std::pow(0.1f,1.f/srcLight->mFalloffExponent))+ + out->mAngleInnerCone; } else { out->mAngleOuterCone = out->mAngleInnerCone + AI_DEG_TO_RAD( srcLight->mPenumbraAngle ); diff --git a/include/assimp/defs.h b/include/assimp/defs.h index 6b8ac3416..c21b3cf2b 100644 --- a/include/assimp/defs.h +++ b/include/assimp/defs.h @@ -209,7 +209,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #if (defined(__BORLANDC__) || defined (__BCPLUSPLUS__)) #error Currently, Borland is unsupported. Feel free to port Assimp. -// "W8059 Packgröße der Struktur geändert" +// "W8059 Packgr��e der Struktur ge�ndert" #endif ////////////////////////////////////////////////////////////////////////// @@ -257,8 +257,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define AI_MATH_HALF_PI_F (AI_MATH_PI_F * 0.5f) /* Tiny macro to convert from radians to degrees and back */ -#define AI_DEG_TO_RAD(x) (x*0.0174532925f) -#define AI_RAD_TO_DEG(x) (x*57.2957795f) +#define AI_DEG_TO_RAD(x) ((x)*0.0174532925f) +#define AI_RAD_TO_DEG(x) ((x)*57.2957795f) /* Support for big-endian builds */ #if defined(__BYTE_ORDER__) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f4b67bad6..287f51698 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -40,6 +40,8 @@ SET( TEST_SRCS unit/utTriangulate.cpp unit/utVertexTriangleAdjacency.cpp unit/utNoBoostTest.cpp + unit/utColladaExportCamera.cpp + unit/utColladaExportLight.cpp ) SOURCE_GROUP( tests FILES ${TEST_SRCS} ) diff --git a/test/models/Collada/cameras.dae b/test/models/Collada/cameras.dae new file mode 100644 index 000000000..d514b0f3d --- /dev/null +++ b/test/models/Collada/cameras.dae @@ -0,0 +1,93 @@ + + + + + Blender User + Blender 2.74.0 commit date:2015-03-31, commit time:13:39, hash:000dfc0 + + 2015-05-17T19:24:51 + 2015-05-17T19:24:51 + + Z_UP + + + + + + + 49.13434 + 1.777778 + 0.1 + 100 + + + + + + 0 + 0 + 0 + + + + + + + + 3 + 1.777778 + 0.1 + 100 + + + + + + 0 + 0 + 0 + + + + + + + + 29.86284 + 1.777778 + 0.1 + 50 + + + + + + 0 + 0 + 0 + + + + + + + + + + 7.54979e-8 0 1 10 0 1 0 0 -1 0 7.54979e-8 0 0 0 0 1 + + + + 7.54979e-8 0 -1 -10 0 1 0 0 1 0 7.54979e-8 0 0 0 0 1 + + + + 3.09086e-8 -1 1.58933e-8 0 -3.09086e-8 1.58933e-8 1 5 -1 -3.09086e-8 -3.09086e-8 0 0 0 0 1 + + + + + + + + diff --git a/test/models/Collada/lights.dae b/test/models/Collada/lights.dae new file mode 100644 index 000000000..9e040aac6 --- /dev/null +++ b/test/models/Collada/lights.dae @@ -0,0 +1,380 @@ + + + + + Blender User + Blender 2.74.0 commit date:2015-03-31, commit time:13:39, hash:000dfc0 + + 2015-05-17T21:55:44 + 2015-05-17T21:55:44 + + Z_UP + + + + + + 1 1 1 + 1 + 0 + 0.00111109 + + + + + 0.000999987 + 1 + 0.1 + 0.1 + 1 + 1 + 1 + 2 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 2880 + 2 + 30.002 + 1.000799 + 0.04999995 + 29.99998 + 1 + 2 + 0 + 0 + 1 + 1 + 1 + 1 + 8192 + 1 + 1 + 0 + 1 + 1 + 1 + 3 + 0 + 0 + 0 + 0 + 0 + 1 + 1 + 1 + 3 + 0.15 + 75 + 1 + 1 + 0 + 1 + 1 + 0 + + + + + + + 1 1 1 + + + + + 0.000999987 + 0 + 0.1 + 0.1 + 0.1 + 1 + 1 + 2 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 512 + 2 + 40 + 0.5 + 0.04999995 + 25 + 1 + 2 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 3 + 0 + 0 + 0 + 0 + 2 + 1 + 1 + 1 + 3 + 0.15 + 45 + 1 + 1 + 0 + 1 + 1 + 1 + + + + + + + 1 1 1 + 1 + 0 + 0.001599967 + 45 + 0.15 + + + + + 0.000999987 + 0 + 0.1 + 0.1 + 0.1 + 1 + 1 + 2 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 512 + 2 + 40 + 0.5 + 0.04999995 + 25 + 1 + 2 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 3 + 0 + 0 + 0 + 0 + 2 + 1 + 1 + 1 + 3 + 0.15 + 45 + 1 + 1 + 0 + 1 + 1 + 2 + + + + + + + 1 1 1 + + + + + 0.000999987 + 0 + 0.1 + 0.1 + 0.1 + 1 + 1 + 2 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 512 + 2 + 40 + 0.5 + 0.04999995 + 25 + 1 + 2 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 3 + 0 + 0 + 0 + 0 + 2 + 1 + 1 + 1 + 3 + 0.15 + 45 + 1 + 1 + 0 + 1 + 1 + 3 + + + + + + + 1 1 1 + 1 + 0 + 0.001599967 + + + + + 0.000999987 + 0 + 0.1 + 0.1 + 0.1 + 1 + 1 + 2 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 512 + 2 + 40 + 0.5 + 0.04999995 + 25 + 1 + 2 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 3 + 0 + 0 + 0 + 0 + 2 + 1 + 1 + 1 + 3 + 0.15 + 45 + 1 + 1 + 0 + 1 + 1 + 4 + + + + + + + + + + 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 + + + + 1 0 0 7.076701 0 1 0 -5.572294 0 0 1 5.147222 0 0 0 1 + + + + 1 0 0 8.888217 0 1 0 -5.016863 0 0 1 5.336025 0 0 0 1 + + + + 1 0 0 7.326984 0 1 0 -4.602942 0 0 1 5.554852 0 0 0 1 + + + + 1 0 0 8.063721 0 1 0 -4.19857 0 0 1 5.273283 0 0 0 1 + + + + + + + + \ No newline at end of file diff --git a/test/unit/utColladaExportCamera.cpp b/test/unit/utColladaExportCamera.cpp new file mode 100644 index 000000000..c647684b6 --- /dev/null +++ b/test/unit/utColladaExportCamera.cpp @@ -0,0 +1,80 @@ +/* + * ColladaCameraExporter.cpp + * + * Created on: May 17, 2015 + * Author: wise + */ + + +#include "UnitTestPCH.h" + +#include +#include +#include +#include + +#ifndef ASSIMP_BUILD_NO_EXPORT + +class ColladaExportCamera : public ::testing::Test { +public: + + virtual void SetUp() + { + ex = new Assimp::Exporter(); + im = new Assimp::Importer(); + + } + + virtual void TearDown() + { + delete ex; + delete im; + } + +protected: + + + Assimp::Exporter* ex; + Assimp::Importer* im; +}; + +// ------------------------------------------------------------------------------------------------ +TEST_F(ColladaExportCamera, testExportCamera) +{ + const char* file = "cameraExp.dae"; + + const aiScene* pTest = im->ReadFile("../test/models/Collada/cameras.dae",0); + ASSERT_TRUE(pTest!=NULL); + ASSERT_TRUE(pTest->HasCameras()); + + + EXPECT_EQ(AI_SUCCESS,ex->Export(pTest,"collada",file)); + + const aiScene* imported = im->ReadFile(file,0); + + ASSERT_TRUE(imported!=NULL); + + EXPECT_TRUE(imported->HasCameras()); + EXPECT_EQ(pTest->mNumCameras,imported->mNumCameras); + + for(size_t i=0; i< pTest->mNumCameras;i++){ + + const aiCamera *orig = pTest->mCameras[i]; + const aiCamera *read = imported->mCameras[i]; + + EXPECT_TRUE(orig->mName==read->mName); + EXPECT_FLOAT_EQ(orig->mHorizontalFOV,read->mHorizontalFOV); + EXPECT_FLOAT_EQ(orig->mClipPlaneNear,read->mClipPlaneNear); + EXPECT_FLOAT_EQ(orig->mClipPlaneFar,read->mClipPlaneFar); + + EXPECT_FLOAT_EQ(orig->mPosition.x,read->mPosition.x); + EXPECT_FLOAT_EQ(orig->mPosition.y,read->mPosition.y); + EXPECT_FLOAT_EQ(orig->mPosition.z,read->mPosition.z); + } + +} + + +#endif + + diff --git a/test/unit/utColladaExportLight.cpp b/test/unit/utColladaExportLight.cpp new file mode 100644 index 000000000..510be7ee2 --- /dev/null +++ b/test/unit/utColladaExportLight.cpp @@ -0,0 +1,92 @@ +/* + * ColladaCameraExporter.cpp + * + * Created on: May 17, 2015 + * Author: wise + */ + + +#include "UnitTestPCH.h" + +#include +#include +#include +#include + +#ifndef ASSIMP_BUILD_NO_EXPORT + +class ColladaExportLight : public ::testing::Test { +public: + + virtual void SetUp() + { + ex = new Assimp::Exporter(); + im = new Assimp::Importer(); + + } + + virtual void TearDown() + { + delete ex; + delete im; + } + +protected: + + + Assimp::Exporter* ex; + Assimp::Importer* im; +}; + +// ------------------------------------------------------------------------------------------------ +TEST_F(ColladaExportLight, testExportLight) +{ + const char* file = "cameraExp.dae"; + + const aiScene* pTest = im->ReadFile("../test/models/Collada/lights.dae",0); + ASSERT_TRUE(pTest!=NULL); + ASSERT_TRUE(pTest->HasLights()); + + + EXPECT_EQ(AI_SUCCESS,ex->Export(pTest,"collada",file)); + EXPECT_EQ(AI_SUCCESS,ex->Export(pTest,"collada","/home/wise/lightsExp.dae")); + + const aiScene* imported = im->ReadFile(file,0); + + ASSERT_TRUE(imported!=NULL); + + EXPECT_TRUE(imported->HasLights()); + EXPECT_EQ(pTest->mNumLights,imported->mNumLights); + + for(size_t i=0; i< pTest->mNumLights;i++){ + + const aiLight *orig = pTest->mLights[i]; + const aiLight *read = imported->mLights[i]; + + EXPECT_TRUE(orig->mName==read->mName); + EXPECT_EQ(orig->mType,read->mType); + EXPECT_FLOAT_EQ(orig->mAttenuationConstant,read->mAttenuationConstant); + EXPECT_FLOAT_EQ(orig->mAttenuationLinear,read->mAttenuationLinear); + EXPECT_FLOAT_EQ(orig->mAttenuationQuadratic,read->mAttenuationQuadratic); + + EXPECT_FLOAT_EQ(orig->mColorAmbient.r,read->mColorAmbient.r); + EXPECT_FLOAT_EQ(orig->mColorAmbient.g,read->mColorAmbient.g); + EXPECT_FLOAT_EQ(orig->mColorAmbient.b,read->mColorAmbient.b); + + EXPECT_FLOAT_EQ(orig->mColorDiffuse.r,read->mColorDiffuse.r); + EXPECT_FLOAT_EQ(orig->mColorDiffuse.g,read->mColorDiffuse.g); + EXPECT_FLOAT_EQ(orig->mColorDiffuse.b,read->mColorDiffuse.b); + + EXPECT_FLOAT_EQ(orig->mColorSpecular.r,read->mColorSpecular.r); + EXPECT_FLOAT_EQ(orig->mColorSpecular.g,read->mColorSpecular.g); + EXPECT_FLOAT_EQ(orig->mColorSpecular.b,read->mColorSpecular.b); + + EXPECT_NEAR(orig->mAngleInnerCone,read->mAngleInnerCone,0.001); + EXPECT_NEAR(orig->mAngleOuterCone,read->mAngleOuterCone,0.001); + } +} + + +#endif + +