Skip to content

Commit

Permalink
https://github.com/KhronosGroup/glTF/issues/194
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrobinet committed Feb 2, 2014
1 parent 552c262 commit f2a761a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 69 deletions.
102 changes: 61 additions & 41 deletions converter/COLLADA2GLTF/COLLADA2GLTFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,6 @@ namespace GLTF
trTarget->setString("path", path);
cvtAnimation->channels()->appendValue(trChannel);
}

/*
static shared_ptr <GLTFEffect> defaultEffect() {
std::string uniqueId = "__glTF__defaultMaterial";
shared_ptr <GLTFEffect> cvtEffect(new GLTFEffect(uniqueId));
shared_ptr <JSONObject> values(new JSONObject());
cvtEffect->setValues(values);
cvtEffect->setLightingModel("Phong");
//retrieve the type, parameterName -> symbol -> type
double red = 1, green = 1, blue = 1, alpha = 1;
shared_ptr <JSONObject> slotObject(new JSONObject());
slotObject->setValue("value", serializeVec4(red, green, blue, alpha));
slotObject->setString("type", "FLOAT_VEC4");
values->setValue("diffuse", slotObject);
shared_ptr<JSONObject> techniqueGenerator(new JSONObject());
shared_ptr <JSONObject> attributeSemantics = serializeAttributeSemanticsForPrimitiveAtIndex(mesh.get(),j);
techniqueGenerator->setString("lightingModel", cvtEffect->getLightingModel());
techniqueGenerator->setValue("attributeSemantics", attributeSemantics);
techniqueGenerator->setValue("values", cvtEffect->getValues());
//techniqueGenerator->setValue("techniqueExtras", techniqueExtras);
//techniqueGenerator->setValue("texcoordBindings", texcoordBindings);
cvtEffect->setTechniqueGenerator(techniqueGenerator);
cvtEffect->setName(uniqueId);
return cvtEffect;
}
*/


bool COLLADA2GLTFWriter::write()
{
Expand All @@ -131,9 +97,6 @@ namespace GLTF

this->_converterContext.shaderIdToShaderString.clear();
this->_converterContext._uniqueIDToMeshes.clear();

//shared_ptr <GLTFEffect> aDefaultEffect = defaultEffect();
//this->_converterContext._uniqueIDToEffect[0] = aDefaultEffect;
/*
1. We output vertices and indices separatly in 2 different files
2. Then output them in a single file
Expand Down Expand Up @@ -415,6 +378,14 @@ namespace GLTF
materialsObject->setValue(effect->getID(), serializeEffect(effect.get(), &this->_converterContext));
}
}
for (UniqueIDToEffectIterator = this->_converterContext._uniqueIDToDefaultEffect.begin() ; UniqueIDToEffectIterator != this->_converterContext._uniqueIDToDefaultEffect.end() ; UniqueIDToEffectIterator++) {
//(*it).first; // the key value (of type Key)
//(*it).second; // the mapped value (of type T)
shared_ptr <GLTF::GLTFEffect> effect = (*UniqueIDToEffectIterator).second;
if (effect->getTechniqueGenerator()) {
materialsObject->setValue(effect->getID(), serializeEffect(effect.get(), &this->_converterContext));
}
}

// ----
shared_ptr <GLTF::JSONObject> skins = this->_converterContext.root->createObjectIfNeeded("skins");
Expand Down Expand Up @@ -772,6 +743,7 @@ namespace GLTF
}

shared_ptr<JSONObject> texcoordBindings(new JSONObject());
shared_ptr <GLTFEffect> effect;

if (materialBindingIndex != -1) {
unsigned int referencedMaterialID = (unsigned int)materialBindings[materialBindingIndex].getReferencedMaterial().getObjectId();
Expand All @@ -783,8 +755,11 @@ namespace GLTF
unsigned int effectID = (unsigned int)effectUID.getObjectId();
shared_ptr<JSONObject> effectExtras = this->_extraDataHandler->getExtras(effectUID);

std::string materialName = this->_converterContext._materialUIDToName[referencedMaterialID];
shared_ptr <GLTFEffect> effect = this->_converterContext._uniqueIDToEffect[effectID];
std::string materialName = this->_converterContext._materialUIDToName[referencedMaterialID];

if (this->_converterContext._uniqueIDToEffect.count(effectID) > 0) {
effect = this->_converterContext._uniqueIDToEffect[effectID];
}

// retrieve the semantic to be associated
size_t coordBindingsCount = textureCoordBindings.getCount();
Expand Down Expand Up @@ -836,9 +811,55 @@ namespace GLTF

effect->setTechniqueGenerator(techniqueGenerator);
effect->setName(materialName);
primitive->setMaterialID(effect->getID());
} else {
//https://github.com/KhronosGroup/glTF/issues/194
//We'll deal with two cases cases of default materials
//With or without NORMALS
shared_ptr <JSONObject> attributeSemantics = serializeAttributeSemanticsForPrimitiveAtIndex(mesh.get(),j);

unsigned int effectId = 0;
if (attributeSemantics->contains(GLTFUtils::getStringForSemantic(GLTF::NORMAL))) {
effectId = 1;
} else {
effectId = 0;
}

//we have no material, create default
if (this->_converterContext._uniqueIDToDefaultEffect.count(effectId) > 0) {
effect = this->_converterContext._uniqueIDToDefaultEffect[effectId];
} else {
std::string uniqueId = "__glTF__defaultMaterial" + GLTFUtils::toString(effectId);

effect = shared_ptr<GLTFEffect> (new GLTFEffect(uniqueId));
shared_ptr <JSONObject> values(new JSONObject());

effect->setValues(values);
effect->setLightingModel("Phong");

//retrieve the type, parameterName -> symbol -> type
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", this->_converterContext.profile->getGLenumForString("FLOAT_VEC4"));
values->setValue("diffuse", slotObject);

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

techniqueGenerator->setString("lightingModel", effect->getLightingModel());
techniqueGenerator->setValue("attributeSemantics", attributeSemantics);
techniqueGenerator->setValue("values", effect->getValues());
techniqueGenerator->setValue("techniqueExtras", shared_ptr<JSONObject>(new JSONObject()));
techniqueGenerator->setValue("texcoordBindings", shared_ptr<JSONObject>(new JSONObject()));

effect->setTechniqueGenerator(techniqueGenerator);
effect->setName(uniqueId);

this->_converterContext._uniqueIDToDefaultEffect[0] = effect;
}

primitive->setMaterialID(effect->getID());
}
//printf(":::: %s\n",primitive->getMaterialID().c_str());
}

meshesArray->appendValue(shared_ptr <GLTF::JSONString> (new GLTF::JSONString(mesh->getID())));
Expand Down Expand Up @@ -1017,7 +1038,6 @@ namespace GLTF

//looks like the only way to get the <skeleton> information from OpenCOLLADA
Loader::InstanceControllerDataList list = this->_loader.getInstanceControllerDataListMap()[skinDataUniqueId];

Loader::InstanceControllerData instanceControllerData = *list.begin();

shared_ptr<JSONObject> instanceSkin(new JSONObject());
Expand Down
1 change: 1 addition & 0 deletions converter/COLLADA2GLTF/GLTFConverterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ namespace GLTF
UniqueIDToMesh _uniqueIDToMesh;
UniqueIDToMeshes _uniqueIDToMeshes;
UniqueIDToEffect _uniqueIDToEffect;
UniqueIDToEffect _uniqueIDToDefaultEffect;
MaterialUIDToEffectUID _materialUIDToEffectUID;
MaterialUIDToName _materialUIDToName;
UniqueIDToAnimation _uniqueIDToAnimation;
Expand Down
68 changes: 40 additions & 28 deletions converter/COLLADA2GLTF/shaders/commonProfileShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,12 @@ namespace GLTF
addSemantic("vs", "attribute",
"POSITION", "position" , 1, false);

//normal attribute
addSemantic("vs", "attribute",
"NORMAL", "normal", 1, true);
bool hasNormals = attributeSemantics->contains("NORMAL");
if (hasNormals) {
//normal attribute
addSemantic("vs", "attribute",
"NORMAL", "normal", 1, true);
}

if (hasSkinning) {
addSemantic("vs", "attribute",
Expand All @@ -705,10 +708,11 @@ namespace GLTF

}

//normal matrix
addSemantic("vs", "uniform",
if (hasNormals) {
//normal matrix
addSemantic("vs", "uniform",
MODELVIEWINVERSETRANSPOSE, "normalMatrix" , 1, false);

}
//modeliew matrix
addSemantic("vs", "uniform",
MODELVIEW, "modelViewMatrix" , 1, false);
Expand All @@ -731,16 +735,20 @@ namespace GLTF
vertexShader->GLSLTypeForGLType(vec4Type).c_str(),
"u_modelViewMatrix",
"a_position");
vertexShader->appendCode("%s = normalize(%s * mat3(skinMat)* %s);\n",
"v_normal", "u_normalMatrix", "a_normal");
if (hasNormals) {
vertexShader->appendCode("%s = normalize(%s * mat3(skinMat)* %s);\n",
"v_normal", "u_normalMatrix", "a_normal");
}

} else {
vertexShader->appendCode("%s pos = %s * vec4(%s,1.0);\n",
vertexShader->GLSLTypeForGLType(vec4Type).c_str(),
"u_modelViewMatrix",
"a_position");
vertexShader->appendCode("%s = normalize(%s * %s);\n",
"v_normal", "u_normalMatrix", "a_normal");
if (hasNormals) {
vertexShader->appendCode("%s = normalize(%s * %s);\n",
"v_normal", "u_normalMatrix", "a_normal");
}

}

Expand All @@ -766,12 +774,14 @@ namespace GLTF
}


bool lightingIsEnabled = modelContainsLights || CONFIG_BOOL("useDefaultLight");
bool lightingIsEnabled = hasNormals && (modelContainsLights || CONFIG_BOOL("useDefaultLight"));

if (lightingIsEnabled) {
fragmentShader->appendCode("vec3 normal = normalize(%s);\n", "v_normal");
if (techniqueExtras->getBool("double_sided")) {
fragmentShader->appendCode("if (gl_FrontFacing == false) normal = -normal;\n");
if (techniqueExtras) {
if (techniqueExtras->getBool("double_sided")) {
fragmentShader->appendCode("if (gl_FrontFacing == false) normal = -normal;\n");
}
}
} else {
//https://github.com/KhronosGroup/glTF/issues/121
Expand Down Expand Up @@ -810,7 +820,7 @@ namespace GLTF
shared_ptr <JSONObject> shininessObject;

size_t lightIndex = 0;
if (modelContainsLights && converterContext.root->contains("lightsIds")) {
if (lightingIsEnabled && converterContext.root->contains("lightsIds")) {
shared_ptr<JSONArray> lightsIds = converterContext.root->createArrayIfNeeded("lightsIds");
std::vector <shared_ptr <JSONValue> > ids = lightsIds->values();

Expand Down Expand Up @@ -878,20 +888,22 @@ namespace GLTF
program->addVarying(varyingLightDirection, vec3Type);
lightTransformParameter->setValue("source", nodesIds[j]);

if (!useSimpleLambert) {
if (inputParameters->contains("shininess") && (!shininessObject)) {
shininessObject = inputParameters->getObject("shininess");
addValue("fs", "uniform", shininessObject->getUnsignedInt32("type") , 1, "shininess", converterContext);
if (hasNormals) {
if (!useSimpleLambert) {
if (inputParameters->contains("shininess") && (!shininessObject)) {
shininessObject = inputParameters->getObject("shininess");
addValue("fs", "uniform", shininessObject->getUnsignedInt32("type") , 1, "shininess", converterContext);
}

fragmentShader->appendCode("vec3 l = normalize(%s);\n", varyingLightDirection);
fragmentShader->appendCode("vec3 h = normalize(l+vec3(0.,0.,1.));\n");
fragmentShader->appendCode("diffuseIntensity = max(dot(normal,l), 0.);\n");
fragmentShader->appendCode("specularIntensity = pow(max(0.0,dot(normal,h)),u_shininess);\n");
fragmentShader->appendCode("specularLight += u_%s * specularIntensity;\n", lightColor);

} else {
fragmentShader->appendCode("diffuseIntensity = max(dot(normal,normalize(%s)), 0.);\n",varyingLightDirection);
}

fragmentShader->appendCode("vec3 l = normalize(%s);\n", varyingLightDirection);
fragmentShader->appendCode("vec3 h = normalize(l+vec3(0.,0.,1.));\n");
fragmentShader->appendCode("diffuseIntensity = max(dot(normal,l), 0.);\n");
fragmentShader->appendCode("specularIntensity = pow(max(0.0,dot(normal,h)),u_shininess);\n");
fragmentShader->appendCode("specularLight += u_%s * specularIntensity;\n", lightColor);

} else {
fragmentShader->appendCode("diffuseIntensity = max(dot(normal,normalize(%s)), 0.);\n",varyingLightDirection);
}

fragmentShader->appendCode("diffuseLight += u_%s * diffuseIntensity;\n", lightColor);
Expand Down Expand Up @@ -1020,7 +1032,7 @@ namespace GLTF
}
if (modelContainsLights) {
fragmentShader->appendCode("diffuse.xyz *= diffuseLight;\n");
} else if (CONFIG_BOOL("useDefaultLight")) {
} else if (CONFIG_BOOL("useDefaultLight") && hasNormals) {
fragmentShader->appendCode("diffuse.xyz *= max(dot(normal,vec3(0.,0.,1.)), 0.);\n");
}
fragmentShader->appendCode("color.xyz += diffuse.xyz;\n");
Expand Down

0 comments on commit f2a761a

Please sign in to comment.