Skip to content

Commit

Permalink
Merge pull request #282 from KhronosGroup/v0.6-fixes
Browse files Browse the repository at this point in the history
V0.6 fixes
  • Loading branch information
fabrobinet committed Jun 6, 2014
2 parents c95a65f + 8207673 commit 484bc37
Show file tree
Hide file tree
Showing 24 changed files with 832 additions and 415 deletions.
2 changes: 2 additions & 0 deletions converter/COLLADA2GLTF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ set(GLTF_SOURCES
GLTF/GLTFAccessor.cpp
GLTF/GLTFBuffer.cpp
GLTF/GLTFEffect.cpp
GLTF/GLTFExtras.cpp
GLTF/GLTFMesh.cpp
GLTF/GLTFPrimitive.cpp
GLTF/GLTFUtils.cpp
Expand All @@ -114,6 +115,7 @@ set(GLTF_SOURCES
GLTF/GLTFAccessor.h
GLTF/GLTFBuffer.h
GLTF/GLTFEffect.h
GLTF/GLTFExtras.h
GLTF/GLTFMesh.h
GLTF/GLTFPrimitive.h
GLTF/GLTFUtils.h
Expand Down
186 changes: 101 additions & 85 deletions converter/COLLADA2GLTF/COLLADA2GLTFWriter.cpp

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions converter/COLLADA2GLTF/COLLADA2GLTFWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ namespace GLTF
virtual bool writeKinematicsScene( const COLLADAFW::KinematicsScene* kinematicsScene ){return true;};

private:
void _installTextureSlot(COLLADAFW::Sampler* sampler,
const std::string& slotName,
const std::string& texcoord,
std::shared_ptr <GLTF::GLTFAsset> asset,
std::shared_ptr<GLTF::GLTFEffect> cvtEffect);

void _storeMaterialBindingArray(const std::string& prefix,
const std::string& nodeUID,
const std::string& meshUID,
Expand Down
1 change: 1 addition & 0 deletions converter/COLLADA2GLTF/GLTF/GLTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@
#include "GLTFOutputStream.h"
#include "GLTFConfig.h"
#include "GLTFAssetModifier.h"
#include "GLTFExtras.h"

#endif
4 changes: 2 additions & 2 deletions converter/COLLADA2GLTF/GLTF/GLTFAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ namespace GLTF

shared_ptr<JSONArray> GLTFAccessor::min() {
this->_computeMinMaxIfNeeded();
return this->_min;
return this->createArrayIfNeeded("min");
}

shared_ptr<JSONArray> GLTFAccessor::max() {
this->_computeMinMaxIfNeeded();
return this->_max;
return this->createArrayIfNeeded("max");
}

typedef struct {
Expand Down
2 changes: 0 additions & 2 deletions converter/COLLADA2GLTF/GLTF/GLTFAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ namespace GLTF
ComponentType _componentType;
size_t _elementByteLength;
std::string _ID;
std::shared_ptr<JSONArray> _min;
std::shared_ptr<JSONArray> _max;
bool _minMaxDirty;
};
}
Expand Down
86 changes: 68 additions & 18 deletions converter/COLLADA2GLTF/GLTF/GLTFAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "GLTF-Open3DGC.h"
#include "GLTFFlipUVModifier.h"
#include "geometryHelpers.h"
#include "../shaders/commonProfileShaders.h"

#if __cplusplus <= 199711L
using namespace std::tr1;
Expand Down Expand Up @@ -462,9 +463,7 @@ namespace GLTF
}

unsigned int indexOfSet = 0;
if (semantic == GLTF::TEXCOORD) {
indexOfSet = primitive->getIndexOfSetAtIndex((unsigned int)j);
}
indexOfSet = primitive->getIndexOfSetAtIndex((unsigned int)j);

sets->appendValue(shared_ptr<JSONNumber> (new JSONNumber(indexOfSet)));
}
Expand All @@ -488,7 +487,7 @@ namespace GLTF
double red = 1, green = 1, blue = 1, alpha = 1;
shared_ptr <JSONObject> slotObject(new JSONObject());
slotObject->setValue("value", serializeVec4(red, green, blue, alpha));
slotObject->setUnsignedInt32("type", asset->profile()->getGLenumForString("FLOAT_VEC4"));
slotObject->setUnsignedInt32(kType, asset->profile()->getGLenumForString("FLOAT_VEC4"));
values->setValue("diffuse", slotObject);

shared_ptr<JSONObject> techniqueGenerator(new JSONObject());
Expand All @@ -504,7 +503,8 @@ namespace GLTF
return effect;
}

static std::string buildKeyForMaterialBindingMap(shared_ptr <MaterialBindingsPrimitiveMap> materialBindingPrimitiveMap) {
static std::string buildKeyForMaterialBindingMap(shared_ptr <MaterialBindingsPrimitiveMap> materialBindingPrimitiveMap,
shared_ptr<JSONObject> meshExtras) {
std::string materialBindingKey = "";
size_t size = materialBindingPrimitiveMap->size();

Expand All @@ -513,6 +513,15 @@ namespace GLTF
for (iterator = materialBindingPrimitiveMap->begin() ; iterator != materialBindingPrimitiveMap->end() ; iterator++) {
std::shared_ptr <COLLADAFW::MaterialBinding> materialBinding = iterator->second;
materialBindingKey += materialBinding->getReferencedMaterial().toAscii();
if (meshExtras != nullptr) {
if (meshExtras->contains("double_sided")) {
materialBindingKey += "doubleSided:1";
}
if (meshExtras->contains("jointsCount")) {
unsigned int jointsCount = meshExtras->getUnsignedInt32("jointsCount");
materialBindingKey += "jointsCount:" + GLTFUtils::toString(jointsCount);
}
}
}
}

Expand All @@ -526,7 +535,7 @@ namespace GLTF

std::string meshOriginalID = mesh->getID();
if (materialBindingsPrimitiveMap) {
std::string materialBindingKey = buildKeyForMaterialBindingMap(materialBindingsPrimitiveMap);
std::string materialBindingKey = buildKeyForMaterialBindingMap(materialBindingsPrimitiveMap, meshExtras);
if (this->_meshesForMaterialBindingKey->contains(meshOriginalID) == false) {
shared_ptr<JSONObject> meshesForBindingKey = _meshesForMaterialBindingKey->createObjectIfNeeded(meshOriginalID);
meshesForBindingKey->setValue(materialBindingKey, mesh);
Expand Down Expand Up @@ -602,18 +611,24 @@ namespace GLTF
}
}
}

unsigned int jointsCount = 0;
shared_ptr<JSONObject> techniqueExtras(new JSONObject());
if ((meshExtras != nullptr) && meshExtras->contains("double_sided")) {
techniqueExtras->setBool("double_sided", meshExtras->getBool("double_sided"));
if (meshExtras != nullptr) {
if (meshExtras->contains("double_sided")) {
techniqueExtras->setBool("double_sided", meshExtras->getBool("double_sided"));
}
if (meshExtras->contains("jointsCount")) {
jointsCount = meshExtras->getUnsignedInt32("jointsCount");
techniqueExtras->setUnsignedInt32("jointsCount", jointsCount);
}
}

if ((effectExtras != nullptr) && effectExtras->contains("double_sided")) {
techniqueExtras->setBool("double_sided", effectExtras->getBool("double_sided"));
}

//generate shaders if needed
shared_ptr <JSONObject> attributeSemantics = serializeAttributeSemanticsForPrimitiveAtIndex(mesh.get(), (unsigned int)j);

shared_ptr<JSONObject> techniqueGenerator(new JSONObject());

techniqueGenerator->setString("lightingModel", effect->getLightingModel());
Expand All @@ -622,6 +637,20 @@ namespace GLTF
techniqueGenerator->setValue("techniqueExtras", techniqueExtras);
techniqueGenerator->setValue("texcoordBindings", texcoordBindings);

if (effect->getTechniqueGenerator() != nullptr) {
//here we have the same material that shared by different meshes.
//some of these meshes have different number of bones
//so, we'll have to clone the effect
std::string techniqueKey = getTechniqueKey(techniqueGenerator, this);
if (getTechniqueKey(effect->getTechniqueGenerator(), this) != techniqueKey) {
shared_ptr<GLTFEffect> effectCopy = shared_ptr <GLTFEffect> (new GLTFEffect(*effect));
effectCopy->setID(effect->getID() + "-variant-" + GLTFUtils::toString(materials->getKeysCount()));
effect = effectCopy;
if (materials->contains(effect->getID()) == false) {
materials->setValue(effect->getID(), effect);
}
}
}
effect->setTechniqueGenerator(techniqueGenerator);
effect->setName(materialName);
primitive->setMaterialID(effect->getID());
Expand Down Expand Up @@ -660,6 +689,17 @@ namespace GLTF
if (materialBindings == nullptr)
return false;

size_t jointsCount = 0;
if (node->contains(kInstanceSkin)) {
shared_ptr <JSONObject> instanceSkin = node->getObject(kInstanceSkin);
if (instanceSkin->contains(kSkin)) {
std::string skinOriginalID = instanceSkin->getString(kSkin);
shared_ptr <JSONObject> skins = this->_root->createObjectIfNeeded(kSkins);
std::vector <std::string> skinUIDs = skins->getAllKeys();
shared_ptr <GLTFSkin> skin = static_pointer_cast<GLTFSkin>(skins->getObject(skinOriginalID) );
jointsCount = skin->getJointsCount();
}
}
shared_ptr <JSONArray> meshesArray = nullptr;

MaterialBindingsForMeshUID::const_iterator materialBindingsIterator;
Expand Down Expand Up @@ -691,11 +731,16 @@ namespace GLTF
}

assert(meshesInSkinning || meshesInNode);

shared_ptr <MaterialBindingsPrimitiveMap> materialBindingsPrimitiveMap = (*materialBindingsIterator).second;

shared_ptr<JSONObject> meshExtras = this->_extras->contains(meshUID) ? this->_extras->getObject(meshUID) : nullptr;

if (jointsCount > 0) {
if (meshExtras == nullptr)
meshExtras = shared_ptr <JSONObject> (new JSONObject());
meshExtras->setUnsignedInt32("jointsCount", jointsCount);
}

shared_ptr<GLTFMesh> mesh = static_pointer_cast<GLTFMesh>(this->getValueForUniqueId(meshUID));

this->_applyMaterialBindings(mesh, materialBindingsPrimitiveMap, meshesArray, meshExtras);
Expand Down Expand Up @@ -783,6 +828,17 @@ namespace GLTF
}
}

// ----
shared_ptr <GLTF::JSONObject> skins = this->_root->createObjectIfNeeded(kSkins);
std::vector <std::string> skinsUIDs = skins->getAllKeys();
for (size_t skinIndex = 0 ; skinIndex < skinsUIDs.size() ; skinIndex++) {
shared_ptr <GLTFSkin> skin = static_pointer_cast<GLTFSkin>(skins->getObject(skinsUIDs[skinIndex]));
skins->setValue(skin->getId(), skin);
skins->removeValue(skinsUIDs[skinIndex]);
}
//we change the keys...
skinsUIDs = skins->getAllKeys();

//Handle late binding of material in node
//So we go through all nodes and if a mesh got different bindings than the ones needed we clone the "reference" mesh and assign the binding
//we delay this operation to now, so that we get the reference mesh splitted.
Expand Down Expand Up @@ -932,11 +988,7 @@ namespace GLTF
}

// ----
shared_ptr <GLTF::JSONObject> skins = this->_root->createObjectIfNeeded("skins");
std::vector <std::string> skinsUIDs = skins->getAllKeys();

for (size_t skinIndex = 0 ; skinIndex < skinsUIDs.size() ; skinIndex++) {

shared_ptr <GLTFSkin> skin = static_pointer_cast<GLTFSkin>(skins->getObject(skinsUIDs[skinIndex]));
shared_ptr<JSONArray> joints = skin->getJointsIds();
shared_ptr<JSONArray> jointsWithOriginalSids(new JSONArray());
Expand All @@ -954,8 +1006,6 @@ namespace GLTF
shared_ptr <JSONObject> inverseBindMatrices = static_pointer_cast<JSONObject>(skin->extras()->getValue(kInverseBindMatrices));
inverseBindMatrices->setString(kBufferView, genericBufferView->getID());
skin->setValue(kInverseBindMatrices, inverseBindMatrices);
skins->setValue(skin->getId(), skin);
skins->removeValue(skinsUIDs[skinIndex]);
}

// ----
Expand Down
16 changes: 15 additions & 1 deletion converter/COLLADA2GLTF/GLTF/GLTFEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ using namespace std;
namespace GLTF
{
GLTFEffect::GLTFEffect(const std::string& ID): JSONObject(),
_ID(ID) {
_ID(ID), _techniqueGenerator(nullptr) {
}

GLTFEffect::GLTFEffect(const GLTFEffect &effect) : JSONObject() {
GLTFEffect* source = const_cast<GLTFEffect*>(&effect);
this->_ID = source->getID();
this->_techniqueGenerator = source->getTechniqueGenerator();
this->setName(source->getName());
this->setValues(source->getValues());
this->setLightingModel(source->getLightingModel());
this->_texcoordToSemantics = effect._texcoordToSemantics;
}

GLTFEffect::~GLTFEffect() {
Expand All @@ -46,6 +56,10 @@ namespace GLTF
return this->_ID;
}

void GLTFEffect::setID(const std::string& id) {
this->_ID = id;
}

void GLTFEffect::setTechniqueGenerator(shared_ptr <JSONObject> techniqueGenerator) {
this->_techniqueGenerator = techniqueGenerator;
}
Expand Down
5 changes: 4 additions & 1 deletion converter/COLLADA2GLTF/GLTF/GLTFEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ namespace GLTF
GLTFEffect(const std::string &ID);
virtual ~GLTFEffect();

const std::string& getID();
GLTFEffect(const GLTFEffect &effect);

const std::string& getID();
void setID(const std::string& id);

void setTechniqueGenerator(std::shared_ptr <JSONObject> techniqueGenerator);
std::shared_ptr <JSONObject> getTechniqueGenerator();
Expand Down
Loading

0 comments on commit 484bc37

Please sign in to comment.