Bläddra i källkod

GamePlay Encoder: Fixes loading a COLLADA model that has multiple texture coordinates.

Darryl Gough 14 år sedan
förälder
incheckning
3ad559bf99

+ 14 - 0
gameplay-encoder/src/DAESceneEncoder.cpp

@@ -1563,6 +1563,7 @@ Mesh* DAESceneEncoder::loadMesh(const domMesh* meshElement, const std::string& g
         {
             inputCount = (unsigned int)inputArray.getCount();
 
+            int texCoordCount = 0;
             for (unsigned int j = 0; j < inputCount; ++j)
             {
                 const domInputLocalOffsetRef& input = inputArray.get(j);
@@ -1600,6 +1601,14 @@ Mesh* DAESceneEncoder::loadMesh(const domMesh* meshElement, const std::string& g
                     if (type == -1)
                     {
                         warning(std::string("Semantic (") + semantic + ") is invalid/unsupported for geometry mesh: " + geometryId);
+                        break;
+                    }
+                    if (type == TEXCOORD0)
+                    {
+                        // Some meshes have multiple texture coordinates
+                        assert(texCoordCount <= 7);
+                        type += texCoordCount;
+                        ++texCoordCount;
                     }
 
                     DAEPolygonInput* polygonInput = new DAEPolygonInput();
@@ -1749,6 +1758,11 @@ Mesh* DAESceneEncoder::loadMesh(const domMesh* meshElement, const std::string& g
                     vertex.texCoord.y = (float)source.get(polyIndex * 2 + 1);
                 }
                 break;
+
+            case TEXCOORD1:
+                // TODO
+                break;
+
             default:
                 break;
             }

+ 14 - 15
gameplay-encoder/src/FBXSceneEncoder.cpp

@@ -190,15 +190,14 @@ void FBXSceneEncoder::write(const std::string& filepath, const EncoderArguments&
     std::string dstFilename = filepath.substr(0, filepath.find_last_of('/'));
     dstFilename.append(1, '/');
     dstFilename.append(getFilenameNoExt(filenameOnly));
-
-    // TODO remove comments before submitting
-    //if (arguments.textOutputEnabled())
+    
+    if (arguments.textOutputEnabled())
     {
         std::string outFile = dstFilename + ".xml";
         fprintf(stderr, "Saving debug file: %s\n", outFile.c_str());
         _gamePlayFile.saveText(outFile);
     }
-    //else
+    else
     {
         std::string outFile = dstFilename + ".gpb";
         fprintf(stderr, "Saving binary file: %s\n", outFile.c_str());
@@ -219,8 +218,10 @@ void FBXSceneEncoder::loadScene(KFbxScene* fbxScene)
     KFbxNode* rootNode = fbxScene->GetRootNode();
     if (rootNode)
     {
+        print("Triangulate.");
         triangulateRecursive(rootNode);
 
+        print("Load nodes.");
         // Don't include the FBX root node in the GPB.
         const int childCount = rootNode->GetChildCount();
         for (int i = 0; i < childCount; ++i)
@@ -635,22 +636,20 @@ void FBXSceneEncoder::loadLight(KFbxNode* fbxNode, Node* node)
     fbxDouble3 color = fbxLight->Color.Get();
     light->setColor((float)color[0], (float)color[1], (float)color[2]);
     
-    if (fbxLight->LightType.Get() == KFbxLight::ePOINT)
+    switch (fbxLight->LightType.Get())
     {
+    case KFbxLight::ePOINT:
         light->setPointLight();
         // TODO: range
-    }
-    else if (fbxLight->LightType.Get() == KFbxLight::eDIRECTIONAL)
-    {
+        break;
+    case KFbxLight::eDIRECTIONAL:
         light->setDirectionalLight();
-    }
-    else if (fbxLight->LightType.Get() == KFbxLight::eSPOT)
-    {
+        break;
+    case KFbxLight::eSPOT:
         light->setSpotLight();
         // TODO: range and angles
-    }
-    else
-    {
+        break;
+    default:
         warning("Unknown light type in node.");
         return;
     }
@@ -739,7 +738,6 @@ Mesh* FBXSceneEncoder::loadMesh(KFbxMesh* fbxMesh)
     {
         return mesh;
     }
-    print("Loading mesh.");
     mesh = new Mesh();
     // GamePlay requires that a mesh have a unique ID but KFbxMesh doesn't have a string ID.
     const char* name = fbxMesh->GetNode()->GetName();
@@ -1158,6 +1156,7 @@ void loadBlendData(const std::vector<Vector2>& vertexWeights, Vertex* vertex)
         vertex->blendIndices.w = vertexWeights[3].x;
         vertex->blendWeights.w = vertexWeights[3].y;
     }
+    //vertex->normalizeBlendWeight();
 }
 
 bool loadBlendWeights(KFbxMesh* fbxMesh, std::vector<std::vector<Vector2>>& weights)

+ 1 - 1
gameplay-encoder/src/FileIO.cpp

@@ -77,7 +77,7 @@ void writeZero(FILE* file)
 
 void fprintfElement(FILE* file, const char* elementName, const float values[], int length)
 {
-    fprintf(file, "<%s>", elementName);
+    fprintf(file, "<%s count=\"%d\">", elementName, length);
     for (int i = 0; i < length; ++i)
     {
         fprintf(file, "%f ", values[i]);

+ 2 - 2
gameplay-encoder/src/FileIO.h

@@ -26,7 +26,7 @@ void fprintfElement(FILE* file, const char* elementName, const float values[], i
 template <class T>
 void fprintfElement(FILE* file, const char* format, const char* elementName, std::vector<T> list)
 {
-    fprintf(file, "<%s>", elementName);
+    fprintf(file, "<%s count=\"%d\">", elementName, list.size());
     typename std::vector<T>::const_iterator i;
     for (i = list.begin(); i != list.end(); ++i)
     {
@@ -38,7 +38,7 @@ void fprintfElement(FILE* file, const char* format, const char* elementName, std
 template <class T>
 void fprintfElement(FILE* file, const char* format, const char* elementName, std::list<T> list)
 {
-    fprintf(file, "<%s>", elementName);
+    fprintf(file, "<%s count=\"%d\">", elementName, list.size());
     typename std::list<T>::const_iterator i;
     for (i = list.begin(); i != list.end(); ++i)
     {

+ 0 - 1
gameplay-encoder/src/Mesh.cpp

@@ -148,7 +148,6 @@ unsigned int Mesh::getVertexIndex(const Vertex& vertex)
 {
     std::map<Vertex,unsigned int>::iterator it;
     it = vertexLookupTable.find(vertex);
-    // TODO: Remove it from the map because we are going to delete it
     return it->second;
 }
 

+ 9 - 2
gameplay-encoder/src/Vertex.cpp

@@ -92,9 +92,16 @@ void Vertex::writeText(FILE* file) const
     }
 }
 
-float Vertex::getTotalWeight() const
+void Vertex::normalizeBlendWeight()
 {
-    return blendWeights.x + blendWeights.y + blendWeights.z + blendWeights.w;
+    float total = blendWeights.x + blendWeights.y + blendWeights.z + blendWeights.w;
+    if (total > 0.0f)
+    {
+        blendWeights.x = blendWeights.x / total;
+        blendWeights.y = blendWeights.y / total;
+        blendWeights.z = blendWeights.z / total;
+        blendWeights.w = blendWeights.w / total;
+    }   
 }
 
 }

+ 4 - 1
gameplay-encoder/src/Vertex.h

@@ -88,7 +88,10 @@ public:
      */
     void writeText(FILE* file) const;
 
-    float getTotalWeight() const;
+    /**
+     * Normalizes the blend weights of this vertex so that they add up to 1.0.
+     */
+    void normalizeBlendWeight();
 };
 }
 #endif

+ 4 - 4
gameplay-encoder/src/main.cpp

@@ -56,7 +56,7 @@ int main(int argc, const char** argv)
     {
     case EncoderArguments::FILEFORMAT_DAE:
         {
-            std::string realpath = arguments.getFilePath();
+            std::string realpath(arguments.getFilePath());
             DAESceneEncoder daeEncoder;
             daeEncoder.write(realpath, arguments);
             break;
@@ -64,7 +64,7 @@ int main(int argc, const char** argv)
     case EncoderArguments::FILEFORMAT_FBX:
         {
 #ifdef USE_FBX
-            std::string realpath = arguments.getFilePath();
+            std::string realpath(arguments.getFilePath());
             FBXSceneEncoder fbxEncoder;
             fbxEncoder.write(realpath, arguments);
             break;
@@ -75,14 +75,14 @@ int main(int argc, const char** argv)
         }
     case EncoderArguments::FILEFORMAT_TTF:
         {
-            std::string realpath = arguments.getFilePath();
+            std::string realpath(arguments.getFilePath());
             std::string id = getFileName(realpath);
             writeFont(realpath.c_str(), arguments.getFontSize(), id.c_str(), arguments.fontPreviewEnabled());
             break;
         }
     case EncoderArguments::FILEFORMAT_GPB:
         {
-            std::string realpath = arguments.getFilePath();
+            std::string realpath(arguments.getFilePath());
             GPBDecoder decoder;
             decoder.readBinary(realpath);
             break;