Skip to content

Commit

Permalink
https://github.com/KhronosGroup/glTF/issues/96
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrobinet committed Sep 27, 2013
1 parent 37f2a4c commit 34dd00d
Show file tree
Hide file tree
Showing 16 changed files with 1,297 additions and 231 deletions.
5 changes: 5 additions & 0 deletions converter/COLLADA2GLTF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ set(GLTF_SOURCES main.cpp
GLTF/GLTFSkin.h
GLTF/GLTFExtraDataHandler.h
GLTF/GLTFExtraDataHandler.cpp
GLTF/GLTFProfile.h
GLTF/GLTFProfile.cpp
profiles/webgl-1.0/webgl-idl.h
profiles/webgl-1.0/GLTFWebGL_1_0_Profile.cpp
profiles/webgl-1.0/GLTFWebGL_1_0_Profile.h
shaders/commonProfileShaders.h
shaders/commonProfileShaders.cpp
helpers/geometryHelpers.h
Expand Down
97 changes: 52 additions & 45 deletions converter/COLLADA2GLTF/COLLADA2GLTFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include "COLLADA2GLTFWriter.h"
#include "GLTFExtraDataHandler.h"
#include "COLLADASaxFWLLoader.h"
#include "profiles/webgl-1.0/GLTFWebGL_1_0_Profile.h"
#include "GitSHA1.h"


using namespace std::tr1;
using namespace std;
using namespace COLLADAFW;
Expand Down Expand Up @@ -155,6 +157,8 @@ namespace GLTF
this->_genericOutputStream.open (outputGenericFilePath.c_str(), ios::out | ios::ate | ios::binary);
ouputStream.open (outputFilePath.c_str(), ios::out | ios::ate | ios::binary);

this->_converterContext.profile = shared_ptr <GLTFWebGL_1_0_Profile> (new GLTFWebGL_1_0_Profile());

this->_converterContext.root = shared_ptr <GLTF::JSONObject> (new GLTF::JSONObject());
this->_converterContext.root->setString("profile", "WebGL 1.0");
this->_converterContext.root->setValue("nodes", shared_ptr <GLTF::JSONObject> (new GLTF::JSONObject()));
Expand Down Expand Up @@ -324,12 +328,13 @@ namespace GLTF
if (shouldSkipMesh)
continue;

void *buffers[3];
buffers[0] = (void*)verticesBufferView.get();
buffers[1] = (void*)indicesBufferView.get();
buffers[2] = (void*)genericBufferView.get();
void *serializationContext[4];
serializationContext[0] = (void*)verticesBufferView.get();
serializationContext[1] = (void*)indicesBufferView.get();
serializationContext[2] = (void*)genericBufferView.get();
serializationContext[3] = (void*)&this->_converterContext;

shared_ptr <GLTF::JSONObject> meshObject = serializeMesh(mesh.get(), (void*)buffers);
shared_ptr <GLTF::JSONObject> meshObject = serializeMesh(mesh.get(), (void*)serializationContext);

//serialize attributes
vector <GLTF::Semantic> allSemantics = mesh->allSemantics();
Expand All @@ -345,7 +350,7 @@ namespace GLTF
//(*it).second; // the mapped value (of type T)
shared_ptr <GLTF::GLTFMeshAttribute> meshAttribute = (*meshAttributeIterator).second;

shared_ptr <GLTF::JSONObject> meshAttributeObject = serializeMeshAttribute(meshAttribute.get(), (void*)buffers);
shared_ptr <GLTF::JSONObject> meshAttributeObject = serializeMeshAttribute(meshAttribute.get(), (void*)serializationContext);

attributes->setValue(meshAttribute->getID(), meshAttributeObject);
}
Expand All @@ -358,7 +363,7 @@ namespace GLTF
shared_ptr<GLTF::GLTFPrimitive> primitive = primitives[i];
shared_ptr <GLTF::GLTFIndices> uniqueIndices = primitive->getUniqueIndices();

shared_ptr <GLTF::JSONObject> serializedIndices = serializeIndices(uniqueIndices.get(), (void*)buffers);
shared_ptr <GLTF::JSONObject> serializedIndices = serializeIndices(uniqueIndices.get(), (void*)serializationContext);
indices->setValue(uniqueIndices->getID(), serializedIndices);
}

Expand Down Expand Up @@ -451,7 +456,7 @@ namespace GLTF
}

if (animation->channels()->values().size() > 0) {
shared_ptr <JSONObject> animationObject = serializeAnimation(animation.get());
shared_ptr <JSONObject> animationObject = serializeAnimation(animation.get(), &this->_converterContext);

animationsObject->setValue(animation->getID(), animationObject);
}
Expand Down Expand Up @@ -1185,50 +1190,49 @@ namespace GLTF


//--------------------------------------------------------------------
//FIXME: should be different depending on profiles. now assuming WebGL
static std::string __GetGLWrapMode(COLLADAFW::Sampler::WrapMode wrapMode) {
unsigned int __GetGLWrapMode(COLLADAFW::Sampler::WrapMode wrapMode, GLTFProfile *profile) {
switch (wrapMode) {
case COLLADAFW::Sampler::WRAP_MODE_UNSPECIFIED:
case COLLADAFW::Sampler::WRAP_MODE_NONE:
case COLLADAFW::Sampler::WRAP_MODE_WRAP:
return "REPEAT";
return profile->getGLenumForString("REPEAT");
case COLLADAFW::Sampler::WRAP_MODE_MIRROR:
return "MIRRORED_REPEAT";
return profile->getGLenumForString("MIRRORED_REPEAT");
case COLLADAFW::Sampler::WRAP_MODE_CLAMP:
return "CLAMP_TO_EDGE";
return profile->getGLenumForString("CLAMP_TO_EDGE");
default:
break;
}
return "REPEAT";
return profile->getGLenumForString("REPEAT");
}

static std::string __GetFilterMode(COLLADAFW::Sampler::SamplerFilter wrapMode) {
static unsigned int __GetFilterMode(COLLADAFW::Sampler::SamplerFilter wrapMode, GLTFProfile *profile) {
switch (wrapMode) {
case COLLADAFW::Sampler::SAMPLER_FILTER_UNSPECIFIED:
case COLLADAFW::Sampler::SAMPLER_FILTER_NONE:
case COLLADAFW::Sampler::SAMPLER_FILTER_LINEAR:
return "LINEAR";
return profile->getGLenumForString("LINEAR");
case COLLADAFW::Sampler::SAMPLER_FILTER_NEAREST:
return "NEAREST";
return profile->getGLenumForString("NEAREST");
case COLLADAFW::Sampler::SAMPLER_FILTER_NEAREST_MIPMAP_NEAREST:
return "NEAREST_MIPMAP_NEAREST";
return profile->getGLenumForString("NEAREST_MIPMAP_NEAREST");
case COLLADAFW::Sampler::SAMPLER_FILTER_LINEAR_MIPMAP_NEAREST:
return "LINEAR_MIPMAP_NEAREST";
return profile->getGLenumForString("LINEAR_MIPMAP_NEAREST");
case COLLADAFW::Sampler::SAMPLER_FILTER_NEAREST_MIPMAP_LINEAR:
return "NEAREST_MIPMAP_LINEAR";
return profile->getGLenumForString("NEAREST_MIPMAP_LINEAR");
case COLLADAFW::Sampler::SAMPLER_FILTER_LINEAR_MIPMAP_LINEAR:
return "LINEAR_MIPMAP_LINEAR";
return profile->getGLenumForString("LINEAR_MIPMAP_LINEAR");
default:
break;
}
return "LINEAR";
return profile->getGLenumForString("LINEAR");
}

std::string COLLADA2GLTFWriter::getSamplerUIDForParameters(std::string wrapS,
std::string wrapT,
std::string minFilter,
std::string maxFilter) {
std::string samplerHash = wrapS+wrapT+minFilter+maxFilter;
std::string COLLADA2GLTFWriter::getSamplerUIDForParameters(unsigned int wrapS,
unsigned int wrapT,
unsigned int minFilter,
unsigned int maxFilter) {
std::string samplerHash = GLTFUtils::toString(wrapS)+GLTFUtils::toString(wrapT)+GLTFUtils::toString(minFilter)+GLTFUtils::toString(maxFilter);
bool addSampler = false;
size_t index = 0;

Expand All @@ -1244,10 +1248,10 @@ namespace GLTF
if (addSampler) {
shared_ptr <JSONObject> sampler2D(new JSONObject());

sampler2D->setString("wrapS", wrapS);
sampler2D->setString("wrapT", wrapT);
sampler2D->setString("minFilter", minFilter);
sampler2D->setString("magFilter", maxFilter);
sampler2D->setUnsignedInt32("wrapS", wrapS);
sampler2D->setUnsignedInt32("wrapT", wrapT);
sampler2D->setUnsignedInt32("minFilter", minFilter);
sampler2D->setUnsignedInt32("magFilter", maxFilter);

shared_ptr <GLTF::JSONObject> samplers = this->_converterContext.root->createObjectIfNeeded("samplers");
samplers->setValue(samplerUID, sampler2D);
Expand All @@ -1262,7 +1266,8 @@ namespace GLTF
shared_ptr <JSONObject> extras)
{
shared_ptr <JSONObject> values = cvtEffect->getValues();

GLTFProfile* profile = this->_converterContext.profile.get();

ColorOrTexture slot;

if (slotName == "diffuse")
Expand Down Expand Up @@ -1299,7 +1304,7 @@ namespace GLTF
}
shared_ptr <JSONObject> slotObject(new JSONObject());
slotObject->setValue("value", serializeVec4(red, green, blue, alpha));
slotObject->setString("type", "FLOAT_VEC4");
slotObject->setUnsignedInt32("type", profile->getGLenumForString("FLOAT_VEC4"));
values->setValue(slotName, slotObject);

} else if (slot.isTexture()) {
Expand All @@ -1314,13 +1319,14 @@ namespace GLTF
shared_ptr <JSONObject> slotObject(new JSONObject());

//do we need to export a new texture ? if yes compose a new unique ID
slotObject->setString("type", "SAMPLER_2D");
slotObject->setUnsignedInt32("type", profile->getGLenumForString("SAMPLER_2D"));

//do we need a new sampler ?
std::string samplerUID = this->getSamplerUIDForParameters(__GetGLWrapMode(sampler->getWrapS()),
__GetGLWrapMode(sampler->getWrapT()),
__GetFilterMode(sampler->getMinFilter()),
__GetFilterMode(sampler->getMagFilter()));
GLTFProfile* profile = this->_converterContext.profile.get();
std::string samplerUID = this->getSamplerUIDForParameters(__GetGLWrapMode(sampler->getWrapS(), profile),
__GetGLWrapMode(sampler->getWrapT(), profile),
__GetFilterMode(sampler->getMinFilter(), profile),
__GetFilterMode(sampler->getMagFilter(), profile));

std::string textureUID = "texture_" + imageUID;

Expand All @@ -1329,9 +1335,9 @@ namespace GLTF
shared_ptr <JSONObject> textureObject(new JSONObject());
textureObject->setString("source", imageUID);
textureObject->setString("sampler", samplerUID);
textureObject->setString("format", "RGBA");
textureObject->setString("internalFormat", "RGBA");
textureObject->setString("target", "TEXTURE_2D");
textureObject->setUnsignedInt32("format", profile->getGLenumForString("RGBA"));
textureObject->setUnsignedInt32("internalFormat", profile->getGLenumForString("RGBA"));
textureObject->setUnsignedInt32("target", profile->getGLenumForString("TEXTURE_2D"));
textures->setValue(textureUID, textureObject);
}

Expand All @@ -1344,6 +1350,7 @@ namespace GLTF

bool COLLADA2GLTFWriter::writeEffect( const COLLADAFW::Effect* effect )
{
GLTFProfile* profile = this->_converterContext.profile.get();
const COLLADAFW::CommonEffectPointerArray& commonEffects = effect->getCommonEffects();

if (commonEffects.getCount() > 0) {
Expand Down Expand Up @@ -1390,14 +1397,14 @@ namespace GLTF
if (this->_converterContext.alwaysExportFilterColor) {
shared_ptr <JSONObject> slotObject(new JSONObject());
slotObject->setValue("value", serializeVec4(1, 1, 1, 1));
slotObject->setString("type", "FLOAT_VEC4");
slotObject->setUnsignedInt32("type", profile->getGLenumForString("FLOAT_VEC4"));
values->setValue("filterColor", slotObject);
}

if (!isOpaque(effectCommon) || this->_converterContext.alwaysExportTransparency) {
shared_ptr <JSONObject> transparency(new JSONObject());
transparency->setDouble("value", this->getTransparency(effectCommon));
transparency->setString("type", "FLOAT");
transparency->setUnsignedInt32("type", profile->getGLenumForString("FLOAT"));
values->setValue("transparency", transparency);
}

Expand All @@ -1408,7 +1415,7 @@ namespace GLTF
shininess *= 128.0;
}
shared_ptr <JSONObject> shininessObject(new JSONObject());
shininessObject->setString("type", "FLOAT");
shininessObject->setUnsignedInt32("type", profile->getGLenumForString("FLOAT"));
shininessObject->setDouble("value", shininess);
values->setValue("shininess", shininessObject);
}
Expand Down Expand Up @@ -1692,7 +1699,7 @@ namespace GLTF
glTFSkin->setInverseBindMatrices(inverseBindMatricesView);

shared_ptr<JSONObject> inverseBindMatrices(new JSONObject());
inverseBindMatrices->setString("type", "FLOAT_MAT4");
inverseBindMatrices->setUnsignedInt32("type", this->_converterContext.profile->getGLenumForString("FLOAT_MAT4"));
inverseBindMatrices->setUnsignedInt32("count", skinControllerData->getJointsCount());
inverseBindMatrices->setUnsignedInt32("byteOffset", 0);
glTFSkin->extras()->setValue("inverseBindMatrices", inverseBindMatrices);
Expand Down
2 changes: 1 addition & 1 deletion converter/COLLADA2GLTF/COLLADA2GLTFWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace GLTF
shared_ptr <GLTFEffect> cvtEffect,
shared_ptr<JSONObject> extras);

std::string getSamplerUIDForParameters(std::string wrapS, std::string wrapT, std::string minFilter, std::string maxFilter);
std::string getSamplerUIDForParameters(unsigned int wrapS, unsigned int wrapT, unsigned int minFilter, unsigned int maxFilter);

private:
COLLADASaxFWL::Loader _loader;
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 @@ -54,6 +54,7 @@

// GLTF headers
#include "GLTFTypesAndConstants.h"
#include "GLTFProfile.h"
#include "JSONValue.h"
#include "JSONNumber.h"
#include "JSONString.h"
Expand Down
7 changes: 2 additions & 5 deletions converter/COLLADA2GLTF/GLTF/GLTFMeshAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,8 @@ namespace GLTF
(meshAttribute->getVertexAttributeByteLength() == this->getVertexAttributeByteLength()));
}

std::string GLTFMeshAttribute::getGLType() {
//FIXME: simplified version
//FIXME: at the moment our ComponentType does not support Mat. We will probably have to add it for Skinning
//OpenGL ES2.0 supports GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4, GL_FLOAT_MAT2, GL_FLOAT_MAT3, or GL_FLOAT_MAT4
return GLTFUtils::getStringForGLType(this->_componentType) + "_VEC" + GLTFUtils::toString(this->_componentsPerAttribute);
unsigned int GLTFMeshAttribute::getGLType(shared_ptr<GLTFProfile> profile) {
return profile->getGLTypeForComponentType(this->_componentType, this->getComponentsPerAttribute());
}

}
2 changes: 1 addition & 1 deletion converter/COLLADA2GLTF/GLTF/GLTFMeshAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace GLTF
ComponentType getComponentType();

//return a string that represents the GL Type,by taking into account componentType and componentsPerAttribute
std::string getGLType();
unsigned int getGLType(shared_ptr<GLTFProfile> profile);

void setByteOffset(size_t offset);
size_t getByteOffset();
Expand Down
39 changes: 39 additions & 0 deletions converter/COLLADA2GLTF/GLTF/GLTFProfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2013, Fabrice Robinet
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "GLTF.h"

namespace GLTF
{
GLTFProfile::~GLTFProfile() {}

void GLTFProfile::setGLenumForString(const std::string& str , unsigned int aGLEnum)
{
this->_glEnumForString[str] = aGLEnum;
}

unsigned int GLTFProfile::getGLenumForString(const std::string& str)
{
return this->_glEnumForString[str];
}
}
46 changes: 46 additions & 0 deletions converter/COLLADA2GLTF/GLTF/GLTFProfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2013, Fabrice Robinet
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __GLTF_PROFILE__
#define __GLTF_PROFILE__

typedef unordered_map<std::string ,unsigned int> GLEnumForString;

namespace GLTF
{
class GLTFProfile
{
public:
virtual ~GLTFProfile();
virtual unsigned int getGLTypeForComponentType(ComponentType componentType, size_t) = 0;
virtual std::string getGLSLTypeForGLType(unsigned int glType) = 0;
public:
void setGLenumForString(const std::string& , unsigned int);
unsigned int getGLenumForString(const std::string&);

private:
GLEnumForString _glEnumForString;
};
}

#endif
Loading

0 comments on commit 34dd00d

Please sign in to comment.