Skip to content

Commit

Permalink
Merge pull request #305 from KhronosGroup/dev-7
Browse files Browse the repository at this point in the history
Dev 7
  • Loading branch information
fabrobinet committed Jul 14, 2014
2 parents 8a3db4f + 584b8c8 commit ded2e56
Show file tree
Hide file tree
Showing 69 changed files with 63,416 additions and 49,151 deletions.
15 changes: 9 additions & 6 deletions converter/COLLADA2GLTF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ if (USE_OPEN3DGC)
include_directories(${COLLADA2GLTF_SOURCE_DIR}/dependencies/o3dgc/src/o3dgc_decode_lib/inc)
endif()

if (NOT WIN32)
find_package(PNG REQUIRED)
find_package(PNG)
if (PNG_FOUND)
include_directories(${PNG_INCLUDE_DIR})
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIR})
add_definitions(-DUSE_LIBPNG)
else()
message(WARNING "libpng or one of its dependencies couldn't be found. Transparency may not be correctly detected.")
endif()

link_directories(${COLLADA2GLTF_BINARY_DIR}/lib)
Expand Down Expand Up @@ -143,6 +145,8 @@ set(GLTF_SOURCES
helpers/geometryHelpers.cpp
helpers/mathHelpers.h
helpers/mathHelpers.cpp
helpers/encodingHelpers.h
helpers/encodingHelpers.cpp

convert/meshConverter.cpp
convert/meshConverter.h
Expand Down Expand Up @@ -180,9 +184,8 @@ if (WIN32)
set_target_properties(collada2gltfConvert PROPERTIES RUNTIME_OUTPUT_DIRECTORY "bin")
endif()


if (NOT WIN32)
LIST(APPEND TARGET_LIBS ${PNG_LIBRARY} ${ZLIB_LIBRARY})
if (PNG_FOUND)
LIST(APPEND TARGET_LIBS ${PNG_LIBRARY} ${ZLIB_LIBRARY})
endif()

if (USE_OPEN3DGC)
Expand Down
122 changes: 108 additions & 14 deletions converter/COLLADA2GLTF/COLLADA2GLTFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
#include "GLTFOpenCOLLADAUtils.h"
#include "GLTFExtraDataHandler.h"
#include "COLLADASaxFWLLoader.h"
#include "COLLADAFWFileInfo.h"
#include "GLTF-Open3DGC.h"
#include "profiles/webgl-1.0/GLTFWebGL_1_0_Profile.h"
#include "GitSHA1.h"
#include <algorithm>
#include "commonProfileShaders.h"

#include "helpers/encodingHelpers.h"
#include "COLLADAFWFileInfo.h"

#if __cplusplus <= 199711L
using namespace std::tr1;
Expand All @@ -51,7 +53,7 @@ namespace GLTF
*/
COLLADA2GLTFWriter::COLLADA2GLTFWriter(shared_ptr<GLTFAsset> asset):
_asset(asset),
_visualScene(0) {
_visualScene(0){
}

/*
Expand Down Expand Up @@ -115,6 +117,37 @@ namespace GLTF
assetObject->setString(kProfile, asset->profile()->id());
assetObject->setDouble(kVersion, glTFVersion);

if (globalAsset->getUpAxisType() == COLLADAFW::FileInfo::X_UP ||
globalAsset->getUpAxisType() == COLLADAFW::FileInfo::Z_UP)
{
COLLADABU::Math::Matrix4 matrix = COLLADABU::Math::Matrix4::IDENTITY;
if (globalAsset->getUpAxisType() == COLLADAFW::FileInfo::X_UP)
{
// Rotate -90 deg around Z
matrix.setElement(0, 0, 0.0f);
matrix.setElement(0, 1, 1.0f);
matrix.setElement(1, 0, -1.0f);
matrix.setElement(1, 1, 0.0f);
}
else // Z_UP
{
// Rotate 90 deg around X
matrix.setElement(1, 1, 0.0f);
matrix.setElement(1, 2, -1.0f);
matrix.setElement(2, 1, 1.0f);
matrix.setElement(2, 2, 0.0f);
}

_rootTransform = std::shared_ptr<GLTF::JSONObject>(new GLTF::JSONObject());
_rootTransform->setString(kName, "Y_UP_Transform");
_rootTransform->setValue("matrix", serializeOpenCOLLADAMatrix4(matrix));

shared_ptr <GLTF::JSONArray> childrenArray(new GLTF::JSONArray());
_rootTransform->setValue(kChildren, childrenArray);
}

asset->setDistanceScale(globalAsset->getUnit().getLinearUnitMeter());

return true;
}

Expand Down Expand Up @@ -310,6 +343,11 @@ namespace GLTF

if (shouldExportTRS) {
GLTF::decomposeMatrix(matrix, translation, rotation, scale);

// Scale distance units if we need to
translation[0] *= (float)_asset->getDistanceScale();
translation[1] *= (float)_asset->getDistanceScale();
translation[2] *= (float)_asset->getDistanceScale();

bool exportDefaultValues = CONFIG_BOOL(asset, "exportDefaultValues");
bool exportTranslation = !(!exportDefaultValues &&
Expand All @@ -326,9 +364,11 @@ namespace GLTF

} else {
//FIXME: OpenCOLLADA typo
matrix.scaleTrans(_asset->getDistanceScale());
bool exportMatrix = !((matrix.isIdentiy() && (CONFIG_BOOL(asset, "exportDefaultValues") == false) ));
if (exportMatrix)
if (exportMatrix) {
nodeObject->setValue("matrix", serializeOpenCOLLADAMatrix4(matrix));
}
}

const InstanceControllerPointerArray& instanceControllers = node->getInstanceControllers();
Expand Down Expand Up @@ -503,8 +543,28 @@ namespace GLTF
scenesObject->setValue("defaultScene", sceneObject); //FIXME: should use this id -> visualScene->getOriginalId()

//first pass to output children name of our root node
shared_ptr <GLTF::JSONArray> childrenArray(new GLTF::JSONArray());
sceneObject->setValue(kNodes, childrenArray);
shared_ptr <GLTF::JSONArray> childrenArray;
if (_rootTransform)
{
// Add the root transform to the nodes
std::string yUpNodeID = uniqueIdWithType(kNode, this->_loader.getUniqueId(COLLADAFW::COLLADA_TYPE::NODE));
nodesObject->setValue(yUpNodeID, _rootTransform);

// Create a children array for the scene and add the root transform to it
shared_ptr <GLTF::JSONArray> sceneChildrenArray(new GLTF::JSONArray());
sceneObject->setValue(kNodes, sceneChildrenArray);
shared_ptr <GLTF::JSONString> rootIDValue(new GLTF::JSONString(yUpNodeID));
sceneChildrenArray->appendValue(static_pointer_cast <GLTF::JSONValue> (rootIDValue));

// Set childrenArray to the root transform's children array so all root nodes will become its children
childrenArray = std::static_pointer_cast<GLTF::JSONArray>(_rootTransform->getValue(kChildren));
}
else
{
// No root transform so just add all root nodes to the scene's children array
childrenArray = std::shared_ptr<GLTF::JSONArray>(new GLTF::JSONArray());
sceneObject->setValue(kNodes, childrenArray);
}

for (size_t i = 0 ; i < nodeCount ; i++) {
std::string nodeUID = nodePointerArray[i]->getOriginalId();
Expand Down Expand Up @@ -956,8 +1016,8 @@ namespace GLTF
break;
}

projectionObject->setDouble("znear", camera->getNearClippingPlane().getValue());
projectionObject->setDouble("zfar", camera->getFarClippingPlane().getValue());
projectionObject->setDouble("znear", camera->getNearClippingPlane().getValue() * _asset->getDistanceScale());
projectionObject->setDouble("zfar", camera->getFarClippingPlane().getValue() * _asset->getDistanceScale());

return true;
}
Expand All @@ -974,13 +1034,47 @@ namespace GLTF

const COLLADABU::URI &imageURI = openCOLLADAImage->getImageURI();
std::string relPathFile = imageURI.getPathFile();

if (imageURI.getPathDir().substr(0,2) != "./") {
relPathFile = imageURI.getPathDir() + imageURI.getPathFile();
} else {
relPathFile = imageURI.getPathDir().substr(2) + imageURI.getPathFile();
}
image->setString("path", this->_asset->resourceOuputPathForPath(relPathFile));
if (imageURI.getPathDir().substr(0, 2) != "./") {
relPathFile = imageURI.getPathDir() + imageURI.getPathFile();
}
else {
relPathFile = imageURI.getPathDir().substr(2) + imageURI.getPathFile();
}

if (_asset->getEmbedResources())
{
COLLADABU::URI inputURI(_asset->getInputFilePath().c_str());
std::string imageFullPath = inputURI.getPathDir() + relPathFile;

std::ifstream fin(imageFullPath, ios::in | ios::binary);
if (fin.is_open())
{
std::ostringstream ss;
ss << fin.rdbuf();
COLLADABU::URI u(imageFullPath);
std::string ext = u.getPathExtension();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
std::string contentType = "application/octet-stream";
if (ext == "png")
{
contentType = "image/png";
}
else if (ext == "gif")
{
contentType = "image/gif";
}
else if (ext == "jpg" || ext == "jpeg")
{
contentType = "image/jpeg";
}

image->setString(kURI, create_dataUri(ss.str(), contentType));

return true;
}
}

image->setString(kURI, COLLADABU::URI::uriEncode(this->_asset->resourceOuputPathForPath(relPathFile)));

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions converter/COLLADA2GLTF/COLLADA2GLTFWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace GLTF
{
class GLTFObject;
class ExtraDataHandler;
class JSONObject;

// -- SceneFlattening

Expand Down Expand Up @@ -196,6 +197,7 @@ namespace GLTF
SceneFlatteningInfo _sceneFlatteningInfo;
GLTF::ExtraDataHandler *_extraDataHandler;
std::ofstream _compressedDataOutputStream;
std::shared_ptr<GLTF::JSONObject> _rootTransform;
};
}

Expand Down
4 changes: 4 additions & 0 deletions converter/COLLADA2GLTF/GLTF/GLTFAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,8 @@ namespace GLTF
return this->getUnsignedInt32(kType);
}

std::string GLTFAccessor::valueType() {
return "accessor";
}

}
3 changes: 3 additions & 0 deletions converter/COLLADA2GLTF/GLTF/GLTFAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ namespace GLTF

bool matchesLayout(GLTFAccessor* meshAttribute);
void exposeMinMax();

virtual std::string valueType();

private:
void _computeMinMaxIfNeeded();

Expand Down
5 changes: 5 additions & 0 deletions converter/COLLADA2GLTF/GLTF/GLTFAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,9 @@ namespace GLTF
free(rotations);
}
}

std::string GLTFAnimation::valueType() {
return "animation";
}

}
2 changes: 2 additions & 0 deletions converter/COLLADA2GLTF/GLTF/GLTFAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace GLTF

void writeAnimationForTargetID(const std::string &targetID, GLTFAsset* asset);

virtual std::string valueType();

private:
std::string _id;
std::string _originalID;
Expand Down
Loading

0 comments on commit ded2e56

Please sign in to comment.