Przeglądaj źródła

Updated heightmap support in gameplay-encoder:
- Multiple nodes/meshes can now be input into a single heightmap file generation.
- Output filename is now a required parameter.
- Fixed some issues related to meshes with complex geometry, such as overhangs.
- Heightmap generation is now multi-threaded for improved speed on multi-core cpus.

Steve Grenier 13 lat temu
rodzic
commit
9e1b3110c0

+ 15 - 10
gameplay-encoder/gameplay-encoder.vcxproj

@@ -30,6 +30,7 @@
     <ClCompile Include="src\Glyph.cpp" />
     <ClCompile Include="src\Glyph.cpp" />
     <ClCompile Include="src\GPBDecoder.cpp" />
     <ClCompile Include="src\GPBDecoder.cpp" />
     <ClCompile Include="src\Animations.cpp" />
     <ClCompile Include="src\Animations.cpp" />
+    <ClCompile Include="src\Heightmap.cpp" />
     <ClCompile Include="src\Light.cpp" />
     <ClCompile Include="src\Light.cpp" />
     <ClCompile Include="src\main.cpp" />
     <ClCompile Include="src\main.cpp" />
     <ClCompile Include="src\Material.cpp" />
     <ClCompile Include="src\Material.cpp" />
@@ -74,6 +75,7 @@
     <ClInclude Include="src\Glyph.h" />
     <ClInclude Include="src\Glyph.h" />
     <ClInclude Include="src\GPBDecoder.h" />
     <ClInclude Include="src\GPBDecoder.h" />
     <ClInclude Include="src\Animations.h" />
     <ClInclude Include="src\Animations.h" />
+    <ClInclude Include="src\Heightmap.h" />
     <ClInclude Include="src\Light.h" />
     <ClInclude Include="src\Light.h" />
     <ClInclude Include="src\Material.h" />
     <ClInclude Include="src\Material.h" />
     <ClInclude Include="src\MaterialParameter.h" />
     <ClInclude Include="src\MaterialParameter.h" />
@@ -89,6 +91,7 @@
     <ClInclude Include="src\ReferenceTable.h" />
     <ClInclude Include="src\ReferenceTable.h" />
     <ClInclude Include="src\Scene.h" />
     <ClInclude Include="src\Scene.h" />
     <ClInclude Include="src\StringUtil.h" />
     <ClInclude Include="src\StringUtil.h" />
+    <ClInclude Include="src\Thread.h" />
     <ClInclude Include="src\Transform.h" />
     <ClInclude Include="src\Transform.h" />
     <ClInclude Include="src\TTFFontEncoder.h" />
     <ClInclude Include="src\TTFFontEncoder.h" />
     <ClInclude Include="src\Vector2.h" />
     <ClInclude Include="src\Vector2.h" />
@@ -148,20 +151,21 @@
       </PrecompiledHeader>
       </PrecompiledHeader>
       <WarningLevel>Level4</WarningLevel>
       <WarningLevel>Level4</WarningLevel>
       <Optimization>Disabled</Optimization>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;NO_BOOST;NO_ZAE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>../external-deps/freetype2/include;../external-deps/collada-dom/include;../external-deps/collada-dom/include/1.4;../external-deps/libpng/include;../external-deps/zlib/include</AdditionalIncludeDirectories>
+      <PreprocessorDefinitions>USE_FBX;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;NO_BOOST;NO_ZAE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>C:/Program Files/Autodesk/FBX/FbxSdk/2013.1/include;../external-deps/freetype2/include;../external-deps/collada-dom/include;../external-deps/collada-dom/include/1.4;../external-deps/libpng/include;../external-deps/zlib/include</AdditionalIncludeDirectories>
       <DisableLanguageExtensions>
       <DisableLanguageExtensions>
       </DisableLanguageExtensions>
       </DisableLanguageExtensions>
     </ClCompile>
     </ClCompile>
     <Link>
     <Link>
       <SubSystem>Console</SubSystem>
       <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalLibraryDirectories>../external-deps/freetype2/lib/win32;../external-deps/collada-dom/lib/win32;../external-deps/libpng/lib/win32;../external-deps/zlib/lib/win32</AdditionalLibraryDirectories>
-      <AdditionalDependencies>freetype245.lib;libcollada14dom22-d.lib;libpng14.lib;zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalLibraryDirectories>C:/Program Files/Autodesk/FBX/FbxSdk/2013.1/lib/vs2010/x86;../external-deps/freetype2/lib/win32;../external-deps/collada-dom/lib/win32;../external-deps/libpng/lib/win32;../external-deps/zlib/lib/win32</AdditionalLibraryDirectories>
+      <AdditionalDependencies>fbxsdk-2013.1-mdd.lib;wininet.lib;freetype245.lib;libcollada14dom22-d.lib;libpng14.lib;zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <IgnoreSpecificDefaultLibraries>MSVCRT</IgnoreSpecificDefaultLibraries>
       <IgnoreSpecificDefaultLibraries>MSVCRT</IgnoreSpecificDefaultLibraries>
     </Link>
     </Link>
     <PostBuildEvent>
     <PostBuildEvent>
-      <Command>copy /Y "$(ProjectDir)..\bin\win32\*.dll" "$(TargetDir)"</Command>
+      <Command>copy /Y "$(ProjectDir)..\bin\win32\*.dll" "$(TargetDir)"
+copy /Y "C:\Program Files\Autodesk\FBX\FbxSdk\2013.1\lib\vs2010\x86\fbxsdk-2013.1d.dll" "$(TargetDir)"</Command>
     </PostBuildEvent>
     </PostBuildEvent>
   </ItemDefinitionGroup>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -172,22 +176,23 @@
       <Optimization>MaxSpeed</Optimization>
       <Optimization>MaxSpeed</Optimization>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;NO_BOOST;NO_ZAE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>../external-deps/freetype2/include;../external-deps/collada-dom/include;../external-deps/collada-dom/include/1.4;../external-deps/libpng/include;../external-deps/zlib/include</AdditionalIncludeDirectories>
+      <PreprocessorDefinitions>USE_FBX;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;NO_BOOST;NO_ZAE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>C:/Program Files/Autodesk/FBX/FbxSdk/2013.1/include;../external-deps/freetype2/include;../external-deps/collada-dom/include;../external-deps/collada-dom/include/1.4;../external-deps/libpng/include;../external-deps/zlib/include</AdditionalIncludeDirectories>
     </ClCompile>
     </ClCompile>
     <Link>
     <Link>
       <SubSystem>Console</SubSystem>
       <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>freetype245.lib;libcollada14dom22-d.lib;libpng14.lib;zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories>../external-deps/freetype2/lib/win32;../external-deps/collada-dom/lib/win32;../external-deps/libpng/lib/win32;../external-deps/zlib/lib/win32</AdditionalLibraryDirectories>
+      <AdditionalDependencies>fbxsdk-2013.1-mdd.lib;wininet.lib;freetype245.lib;libcollada14dom22-d.lib;libpng14.lib;zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalLibraryDirectories>C:/Program Files/Autodesk/FBX/FbxSdk/2013.1/lib/vs2010/x86;../external-deps/freetype2/lib/win32;../external-deps/collada-dom/lib/win32;../external-deps/libpng/lib/win32;../external-deps/zlib/lib/win32</AdditionalLibraryDirectories>
       <IgnoreSpecificDefaultLibraries>
       <IgnoreSpecificDefaultLibraries>
       </IgnoreSpecificDefaultLibraries>
       </IgnoreSpecificDefaultLibraries>
     </Link>
     </Link>
     <PostBuildEvent>
     <PostBuildEvent>
       <Command>copy /Y "$(ProjectDir)..\bin\win32\*.dll" "$(TargetDir)"
       <Command>copy /Y "$(ProjectDir)..\bin\win32\*.dll" "$(TargetDir)"
-</Command>
+
+copy /Y "C:\Program Files\Autodesk\FBX\FbxSdk\2013.1\lib\vs2010\x86\fbxsdk-2013.1d.dll" "$(TargetDir)"</Command>
     </PostBuildEvent>
     </PostBuildEvent>
   </ItemDefinitionGroup>
   </ItemDefinitionGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 9 - 0
gameplay-encoder/gameplay-encoder.vcxproj.filters

@@ -127,6 +127,9 @@
     <ClCompile Include="src\Curve.cpp">
     <ClCompile Include="src\Curve.cpp">
       <Filter>src</Filter>
       <Filter>src</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="src\Heightmap.cpp">
+      <Filter>src</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\VertexElement.h">
     <ClInclude Include="src\VertexElement.h">
@@ -252,6 +255,12 @@
     <ClInclude Include="src\Curve.h">
     <ClInclude Include="src\Curve.h">
       <Filter>src</Filter>
       <Filter>src</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="src\Heightmap.h">
+      <Filter>src</Filter>
+    </ClInclude>
+    <ClInclude Include="src\Thread.h">
+      <Filter>src</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <None Include="src\Vector2.inl">
     <None Include="src\Vector2.inl">

+ 1 - 2
gameplay-encoder/gameplay-encoder.vcxproj.user

@@ -1,8 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
-    <LocalDebuggerCommandArguments>
-    </LocalDebuggerCommandArguments>
+    <LocalDebuggerCommandArguments>-h "panel1 panel2 panel3 panel4 panel5 panel6 panel7 panel8 panel9 panel10 panel11 panel12 panel13 panel14 panel15 panel16" "C:\git\GamePlay\gameplay-internal\sample07-racer\res\heightmap.png" "C:\git\GamePlay\gameplay-internal\sample07-racer\res\scene.fbx" "C:\git\GamePlay\gameplay-internal\sample07-racer\res\scene.gpb"</LocalDebuggerCommandArguments>
     <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
     <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
     <LocalDebuggerEnvironment>PATH=%PATH%;../bin/win32;</LocalDebuggerEnvironment>
     <LocalDebuggerEnvironment>PATH=%PATH%;../bin/win32;</LocalDebuggerEnvironment>
   </PropertyGroup>
   </PropertyGroup>

+ 9 - 0
gameplay-encoder/src/BoundingVolume.cpp

@@ -92,6 +92,15 @@ void BoundingVolume::transform(const Matrix& m)
 
 
 void BoundingVolume::merge(const BoundingVolume& v)
 void BoundingVolume::merge(const BoundingVolume& v)
 {
 {
+    // Merge the box portion
+    min.x = std::min(min.x, v.min.x);
+    min.y = std::min(min.y, v.min.y);
+    min.z = std::min(min.z, v.min.z);
+    max.x = std::max(max.x, v.max.x);
+    max.y = std::max(max.y, v.max.y);
+    max.z = std::max(max.z, v.max.z);
+
+    // Merge the sphere portion
     // Calculate the distance between the two centers.
     // Calculate the distance between the two centers.
     float vx = center.x - v.center.x;
     float vx = center.x - v.center.x;
     float vy = center.y - v.center.y;
     float vy = center.y - v.center.y;

+ 42 - 15
gameplay-encoder/src/EncoderArguments.cpp

@@ -145,9 +145,9 @@ const std::string EncoderArguments::getAnimationId(const std::string& nodeId) co
     return "";
     return "";
 }
 }
 
 
-const std::vector<std::string>& EncoderArguments::getHeightmapNodeIds() const
+const std::vector<EncoderArguments::HeightmapOption>& EncoderArguments::getHeightmapOptions() const
 {
 {
-    return _heightmapNodeIds;
+    return _heightmaps;
 }
 }
 
 
 bool EncoderArguments::parseErrorOccured() const
 bool EncoderArguments::parseErrorOccured() const
@@ -177,18 +177,20 @@ void EncoderArguments::printUsage() const
     fprintf(stderr,"  .ttf\t(TrueType Font)\n");
     fprintf(stderr,"  .ttf\t(TrueType Font)\n");
     fprintf(stderr,"\n");
     fprintf(stderr,"\n");
     fprintf(stderr,"COLLADA and FBX file options:\n");
     fprintf(stderr,"COLLADA and FBX file options:\n");
-    fprintf(stderr,"  -i <id>\t\tFilter by node ID.\n");
-    fprintf(stderr,"  -t\t\t\tWrite text/xml.\n");
+    fprintf(stderr,"  -i <id>\tFilter by node ID.\n");
+    fprintf(stderr,"  -t\t\tWrite text/xml.\n");
     fprintf(stderr,"  -g <node id> <animation id>\n" \
     fprintf(stderr,"  -g <node id> <animation id>\n" \
-        "\t\t\tGroup all animation channels targeting the nodes into a new animation.\n");
-    fprintf(stderr,"  -h \"<node ids>\"\n" \
-        "\t\t\tList of nodes to generate heightmaps for.\n" \
-        "\t\t\tNode id list should be in quotes with a space between each id.\n" \
-        "\t\t\tHeightmaps will be saved in files named <nodeid>.png.\n");
+        "\t\tGroup all animation channels targeting the nodes into a new animation.\n");
+    fprintf(stderr,"  -h \"<node ids>\" <filename>\n" \
+        "\t\tGenerates a single heightmap image using meshes from the specified\n" \
+        "\t\tnodes. Node id list should be in quotes with a space between each id.\n" \
+        "\t\tFilename is the name of the image (PNG) to be saved.\n" \
+        "\t\tMultiple -h arguments can be supplied to generate more than one heightmap.\n");
     fprintf(stderr,"\n");
     fprintf(stderr,"\n");
     fprintf(stderr,"TTF file options:\n");
     fprintf(stderr,"TTF file options:\n");
-    fprintf(stderr,"  -s <size of font>\tSize of the font.\n");
-    fprintf(stderr,"  -p\t\t\tOutput font preview.\n");
+    fprintf(stderr,"  -s <size>\tSize of the font.\n");
+    fprintf(stderr,"  -p\t\tOutput font preview.\n");
+    fprintf(stderr, "\n");
     exit(8);
     exit(8);
 }
 }
 
 
@@ -313,11 +315,14 @@ void EncoderArguments::readOption(const std::vector<std::string>& options, size_
         break;
         break;
     case 'h':
     case 'h':
         {
         {
-            if (str.compare("-heightmaps") == 0 || str.compare("-h") == 0)
+            if (str.compare("-heightmap") == 0 || str.compare("-h") == 0)
             {
             {
                 (*index)++;
                 (*index)++;
-                if (*index < options.size())
+                if (*index < (options.size() + 1))
                 {
                 {
+                    _heightmaps.resize(_heightmaps.size() + 1);
+                    HeightmapOption& heightmap = _heightmaps.back();
+
                     // Split node id list into tokens
                     // Split node id list into tokens
                     unsigned int length = options[*index].size() + 1;
                     unsigned int length = options[*index].size() + 1;
                     char* nodeIds = new char[length];
                     char* nodeIds = new char[length];
@@ -326,14 +331,36 @@ void EncoderArguments::readOption(const std::vector<std::string>& options, size_
                     char* id = strtok(nodeIds, " ");
                     char* id = strtok(nodeIds, " ");
                     while (id)
                     while (id)
                     {
                     {
-                        _heightmapNodeIds.push_back(id);
+                        heightmap.nodeIds.push_back(id);
                         id = strtok(NULL, " ");
                         id = strtok(NULL, " ");
                     }
                     }
                     delete[] nodeIds;
                     delete[] nodeIds;
+
+                    // Store output filename
+                    (*index)++;
+                    heightmap.filename = options[*index];
+                    if (heightmap.filename.empty())
+                    {
+                        fprintf(stderr, "Error: missing filename argument for -h|-heightmap.\n");
+                        _parseError = true;
+                        return;
+                    }
+                    
+                    // Ensure the output filename has a .png extention
+                    if (heightmap.filename.length() > 5)
+                    {
+                        const char* ext = heightmap.filename.c_str() + (heightmap.filename.length() - 4);
+                        if (ext[0] != '.' || tolower(ext[1]) != 'p' || tolower(ext[2]) != 'n' || tolower(ext[3]) != 'g')
+                            heightmap.filename += ".png";
+                    }
+                    else
+                        heightmap.filename += ".png";
                 }
                 }
                 else
                 else
                 {
                 {
-                    fprintf(stderr, "Error: missing argument for -heightmaps.\n");
+                    fprintf(stderr, "Error: missing argument for -h|-heightmap.\n");
+                    _parseError = true;
+                    return;
                 }
                 }
             }
             }
         }
         }

+ 8 - 2
gameplay-encoder/src/EncoderArguments.h

@@ -20,6 +20,12 @@ public:
         FILEFORMAT_GPB
         FILEFORMAT_GPB
     };
     };
 
 
+    struct HeightmapOption
+    {
+        std::vector<std::string> nodeIds;
+        std::string filename;
+    };
+
     /**
     /**
      * Constructor.
      * Constructor.
      */
      */
@@ -73,7 +79,7 @@ public:
     bool containsGroupNodeId(const std::string& nodeId) const;
     bool containsGroupNodeId(const std::string& nodeId) const;
     const std::string getAnimationId(const std::string& nodeId) const;
     const std::string getAnimationId(const std::string& nodeId) const;
 
 
-    const std::vector<std::string>& getHeightmapNodeIds() const;
+    const std::vector<HeightmapOption>& getHeightmapOptions() const;
 
 
     /**
     /**
      * Returns true if an error occurred while parsing the command line arguments.
      * Returns true if an error occurred while parsing the command line arguments.
@@ -142,7 +148,7 @@ private:
 
 
     std::vector<std::string> _groupAnimationNodeId;
     std::vector<std::string> _groupAnimationNodeId;
     std::vector<std::string> _groupAnimationAnimationId;
     std::vector<std::string> _groupAnimationAnimationId;
-    std::vector<std::string> _heightmapNodeIds;
+    std::vector<HeightmapOption> _heightmaps;
 
 
 };
 };
 
 

+ 9 - 1
gameplay-encoder/src/GPBFile.cpp

@@ -2,6 +2,8 @@
 #include "GPBFile.h"
 #include "GPBFile.h"
 #include "Transform.h"
 #include "Transform.h"
 #include "StringUtil.h"
 #include "StringUtil.h"
+#include "EncoderArguments.h"
+#include "Heightmap.h"
 
 
 #define EPSILON 1.2e-7f;
 #define EPSILON 1.2e-7f;
 
 
@@ -323,6 +325,13 @@ void GPBFile::adjust()
     //   Search for animations that have the same target and key times and see if they can be merged.
     //   Search for animations that have the same target and key times and see if they can be merged.
     //   Blender will output a simple translation animation to 3 separate animations with the same key times but targeting X, Y and Z.
     //   Blender will output a simple translation animation to 3 separate animations with the same key times but targeting X, Y and Z.
     //   This can be merged into one animation. Same for scale animations.
     //   This can be merged into one animation. Same for scale animations.
+
+    // Generate heightmaps
+    const std::vector<EncoderArguments::HeightmapOption>& heightmaps = EncoderArguments::getInstance()->getHeightmapOptions();
+    for (unsigned int i = 0, count = heightmaps.size(); i < count; ++i)
+    {
+        Heightmap::generate(heightmaps[i].nodeIds, heightmaps[i].filename.c_str());
+    }
 }
 }
 
 
 void GPBFile::groupMeshSkinAnimations()
 void GPBFile::groupMeshSkinAnimations()
@@ -406,7 +415,6 @@ void GPBFile::optimizeTransformAnimations()
     }
     }
 }
 }
 
 
-
 void GPBFile::decomposeTransformAnimationChannel(Animation* animation, const AnimationChannel* channel)
 void GPBFile::decomposeTransformAnimationChannel(Animation* animation, const AnimationChannel* channel)
 {
 {
     const std::vector<float>& keyTimes = channel->getKeyTimes();
     const std::vector<float>& keyTimes = channel->getKeyTimes();

+ 1 - 0
gameplay-encoder/src/GPBFile.h

@@ -116,6 +116,7 @@ public:
     void renameAnimations(std::vector<std::string>& animationIds, const char* newId);
     void renameAnimations(std::vector<std::string>& animationIds, const char* newId);
 
 
 private:
 private:
+
     /**
     /**
      * Computes the bounds of all meshes in the node hierarchy.
      * Computes the bounds of all meshes in the node hierarchy.
      */
      */

+ 518 - 0
gameplay-encoder/src/Heightmap.cpp

@@ -0,0 +1,518 @@
+#include "Heightmap.h"
+#include "GPBFile.h"
+#include "Thread.h"
+
+namespace gameplay
+{
+
+// Number of threads to spawn for the heightmap generator
+#define THREAD_COUNT 8
+
+// Thread data structure
+struct HeightmapThreadData
+{
+    float rayHeight;                    // [in]
+    const Vector3* rayDirection;        // [in]
+    const std::vector<Mesh*>* meshes;   // [in]
+    const BoundingVolume* bounds;       // [in]
+    int minX;                           // [in]
+    int maxX;                           // [in]
+    int minZ;                           // [in]
+    int maxZ;                           // [in]
+    float minHeight;                    // [out]
+    float maxHeight;                    // [out]
+    float* heights;                     // [in][out]
+    int heightIndex;                    // [in]
+};
+
+// Globals used by thread
+int __processedHeightmapScanLines = 0;
+int __totalHeightmapScanlines = 0;
+int __failedRayCasts = 0;
+
+// Forward declarations
+int generateHeightmapChunk(void* threadData);
+bool intersect(const Vector3& rayOrigin, const Vector3& rayDirection, const Vector3& boxMin, const Vector3& boxMax, float* distance = NULL);
+int intersect_triangle(const float orig[3], const float dir[3], const float vert0[3], const float vert1[3], const float vert2[3], float *t, float *u, float *v);
+bool intersect(const Vector3& rayOrigin, const Vector3& rayDirection, const std::vector<Vertex>& vertices, const std::vector<MeshPart*>& parts, Vector3* point);
+
+void Heightmap::generate(const std::vector<std::string>& nodeIds, const char* filename)
+{
+    printf("Generating heightmap: %s...\n", filename);
+
+    GPBFile* gpbFile = GPBFile::getInstance();
+
+    // Lookup nodes in GPB file and compute a single bounding volume that encapsulates all meshes
+    // to be included in the heightmap generation.
+    BoundingVolume bounds;
+    bounds.min.set(FLT_MAX, FLT_MAX, FLT_MAX);
+    bounds.max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
+    std::vector<Mesh*> meshes;
+    for (unsigned int j = 0, ncount = nodeIds.size(); j < ncount; ++j)
+    {
+        Node* node = gpbFile->getNode(nodeIds[j].c_str());
+        if (node)
+        {
+            Mesh* mesh = node->getModel() ? node->getModel()->getMesh() : NULL;
+            if (mesh)
+            {
+                // Add this mesh and update our bounding volume
+                if (meshes.size() == 0)
+                    bounds = mesh->bounds;
+                else
+                    bounds.merge(mesh->bounds);
+
+                meshes.push_back(mesh);
+            }
+            else
+            {
+                fprintf(stderr, "WARNING: Node passed to heightmap argument does not have a mesh: %s\n", nodeIds[j].c_str());
+            }
+        }
+        else
+        {
+            fprintf(stderr, "WARNING: Failed to locate node for heightmap argument: %s\n", nodeIds[j].c_str());
+        }
+    }
+
+    if (meshes.size() == 0)
+    {
+        fprintf(stderr, "WARNING: Skipping generation of heightmap '%s'. No nodes found.\n", filename);
+        return;
+    }
+
+    // Shoot rays down from a point just above the max Y position of the mesh.
+    // Compute ray-triangle intersection tests against the ray and this mesh to 
+    // generate heightmap data.
+    Vector3 rayOrigin(0, bounds.max.y + 10, 0);
+    Vector3 rayDirection(0, -1, 0);
+
+    int minX = (int)ceil(bounds.min.x);
+    int maxX = (int)floor(bounds.max.x);
+    int minZ = (int)ceil(bounds.min.z);
+    int maxZ = (int)floor(bounds.max.z);
+    int width = maxX - minX + 1;
+    int height = maxZ - minZ + 1;
+    int size = width * height;
+    float* heights = new float[size];
+    float minHeight = FLT_MAX;
+    float maxHeight = -FLT_MAX;
+
+    __totalHeightmapScanlines = height;
+
+    // Split the work into separate threads to make max use of available cpu cores and speed up computation.
+    HeightmapThreadData threadData[THREAD_COUNT];
+    THREAD_HANDLE threads[THREAD_COUNT];
+    int stepSize = height / THREAD_COUNT;
+    for (int i = 0; i < THREAD_COUNT; ++i)
+    {
+        HeightmapThreadData& data = threadData[i];
+        data.rayHeight = rayOrigin.y;
+        data.rayDirection = &rayDirection;
+        data.bounds = &bounds;
+        data.meshes = &meshes;
+        data.minX = minX;
+        data.maxX = maxX;
+        data.minZ = minZ + (stepSize * i);
+        data.maxZ = data.minZ + stepSize - 1;
+        if (i == THREAD_COUNT - 1)
+            data.maxZ = maxZ;
+        data.heights = heights;
+        data.heightIndex = width * (stepSize * i);
+
+        // Start the processing thread
+        if (!createThread(&threads[i], &generateHeightmapChunk, &data))
+        {
+            fprintf(stderr, "ERROR: Failed to spawn worker thread for generation of heightmap: %s\n", filename);
+            return;
+        }
+    }
+
+    // Wait for all threads to terminate
+    waitForThreads(THREAD_COUNT, threads);
+
+    // Close all thread handles and free memory allocations.
+    for (int i = 0; i < THREAD_COUNT; ++i)
+        closeThread(threads[i]);
+
+    // Update min/max height from all completed threads
+    for (int i = 0; i < THREAD_COUNT; ++i)
+    {
+        if (threadData[i].minHeight < minHeight)
+            minHeight = threadData[i].minHeight;
+        if (threadData[i].maxHeight > maxHeight)
+            maxHeight = threadData[i].maxHeight;
+    }
+
+    printf("\r\tDone.\n");
+
+    if (__failedRayCasts)
+    {
+        fprintf(stderr, "Warning: %d triangle intersections failed for heightmap: %s\n", __failedRayCasts, filename);
+
+        // Go through and clamp any height values that are set to -FLT_MAX to the min recorded height value
+        // (otherwise the range of height values will be far too large).
+        for (int i = 0; i < size; ++i)
+        {
+            if (heights[i] == -FLT_MAX)
+                heights[i] = minHeight;
+        }
+    }
+    
+    // Normalize the max height value
+    maxHeight = maxHeight - minHeight;
+
+    png_structp png_ptr = NULL;
+    png_infop info_ptr = NULL;
+    png_bytep row = NULL;
+
+    FILE* fp = fopen(filename, "wb");
+    if (fp == NULL)
+    {
+        fprintf(stderr, "Error: Failed to open file for writing: %s\n", filename);
+        goto error;
+    }
+
+    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+    if (png_ptr == NULL)
+    {
+        fprintf(stderr, "Error: Write struct creation failed: %s\n", filename);
+        goto error;
+    }
+
+    info_ptr = png_create_info_struct(png_ptr);
+    if (info_ptr == NULL)
+    {
+        fprintf(stderr, "Error: Info struct creation failed: %s\n", filename);
+        goto error;
+    }
+
+    png_init_io(png_ptr, fp);
+
+    png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+    png_write_info(png_ptr, info_ptr);
+
+    // Allocate memory for a single row of image data
+    row = (png_bytep)malloc(3 * width * sizeof(png_byte));
+
+    for (int y = 0; y < height; y++)
+    {
+        for (int x = 0; x < width; x++)
+        {
+            // Write height value normalized between 0-255 (between min and max height)
+            float h = heights[y*width + x];
+            float nh = (h - minHeight) / maxHeight;
+            int bits = (int)(nh * 16777215.0f); // 2^24-1
+
+            int pos = x*3;
+            row[pos+2] = (png_byte)(bits & 0xff);
+            bits >>= 8;
+            row[pos+1] = (png_byte)(bits & 0xff);
+            bits >>= 8;
+            row[pos] = (png_byte)(bits & 0xff);
+        }
+        png_write_row(png_ptr, row);
+    }
+
+    png_write_end(png_ptr, NULL);
+    printf("Saved heightmap: %s\n", filename);
+
+error:
+    if (heights)
+        delete[] heights;
+    if (fp)
+        fclose(fp);
+    if (row)
+        free(row);
+    if (info_ptr)
+        png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
+    if (png_ptr)
+        png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
+}
+
+int generateHeightmapChunk(void* threadData)
+{
+    HeightmapThreadData* data = (HeightmapThreadData*)threadData;
+
+    Vector3 rayOrigin(0, data->rayHeight, 0);
+    const Vector3& rayDirection = *data->rayDirection;
+    int minX = data->minX;
+    int maxX = data->maxX;
+    int minZ = data->minZ;
+    int maxZ = data->maxZ;
+    const std::vector<Mesh*>& meshes = *data->meshes;
+    float* heights = data->heights;
+
+    Vector3 intersectionPoint;
+    float minHeight = FLT_MAX;
+    float maxHeight = -FLT_MAX;
+    int index = data->heightIndex;
+
+    for (int z = minZ; z <= maxZ; ++z)
+    {
+        printf("\r\t%d%%", (int)(((float)__processedHeightmapScanLines / __totalHeightmapScanlines) * 100.0f));
+
+        rayOrigin.z = (float)z;
+        for (int x = minX; x <= maxX; ++x)
+        {
+            float h = -FLT_MAX;
+            rayOrigin.x = (float)x;
+
+            for (unsigned int i = 0, count = meshes.size(); i < count; ++i)
+            {
+                // Pick the highest intersecting Y value of all meshes
+                Mesh* mesh = meshes[i];
+
+                // Perform a quick ray/bounding box test to quick-out
+                if (!intersect(rayOrigin, rayDirection, mesh->bounds.min, mesh->bounds.max))
+                    continue;
+
+                // Computer intersection point of ray with mesh
+                if (intersect(rayOrigin, rayDirection, mesh->vertices, mesh->parts, &intersectionPoint))
+                {
+                    if (intersectionPoint.y > h)
+                    {
+                        h = intersectionPoint.y;
+
+                        // Update min/max height values
+                        if (h < minHeight)
+                            minHeight = h;
+                        if (h > maxHeight)
+                            maxHeight = h;
+                    }
+                }
+            }
+
+            // Update the glboal height array
+            heights[index++] = h;
+
+            if (h == -FLT_MAX)
+                ++__failedRayCasts;
+        }
+
+        ++__processedHeightmapScanLines;
+    }
+
+    // Update min/max height for this thread data
+    data->minHeight = minHeight;
+    data->maxHeight = maxHeight;
+
+    return 0;
+}
+
+/////////////////////////////////////////////////////////////
+// 
+// Fast, Minimum Storage Ray-Triangle Intersection
+// 
+// Authors: Tomas Möller, Ben Trumbore
+// http://jgt.akpeters.com/papers/MollerTrumbore97
+//
+// Implementation of algorithm from Real-Time Rendering (vol 1), pg. 305.
+//
+// Adapted slightly for use here.
+// 
+#ifndef EPSILON
+#define EPSILON 0.000001
+#endif
+#define CROSS(dest,v1,v2) \
+          dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
+          dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
+          dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
+#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
+#define SUB(dest,v1,v2) \
+          dest[0]=v1[0]-v2[0]; \
+          dest[1]=v1[1]-v2[1]; \
+          dest[2]=v1[2]-v2[2]; 
+int intersect_triangle(const float orig[3], const float dir[3], const float vert0[3], const float vert1[3], const float vert2[3], float *t, float *u, float *v)
+{
+   float edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
+   float det,inv_det;
+
+   /* find vectors for two edges sharing vert0 */
+   SUB(edge1, vert1, vert0);
+   SUB(edge2, vert2, vert0);
+
+   /* begin calculating determinant - also used to calculate U parameter */
+   CROSS(pvec, dir, edge2);
+
+   /* if determinant is near zero, ray lies in plane of triangle */
+   det = DOT(edge1, pvec);
+
+   if (det > -EPSILON && det < EPSILON)
+     return 0;
+   inv_det = 1.0f / det;
+
+   /* calculate distance from vert0 to ray origin */
+   SUB(tvec, orig, vert0);
+
+   /* calculate U parameter and test bounds */
+   *u = DOT(tvec, pvec) * inv_det;
+   if (*u < 0.0 || *u > 1.0)
+     return 0;
+
+   /* prepare to test V parameter */
+   CROSS(qvec, tvec, edge1);
+
+   /* calculate V parameter and test bounds */
+   *v = DOT(dir, qvec) * inv_det;
+   if (*v < 0.0 || *u + *v > 1.0)
+     return 0;
+
+   /* calculate t, ray intersects triangle */
+   *t = DOT(edge2, qvec) * inv_det;
+
+   return 1;
+}
+
+// Performs an intersection test between a ray and the given mesh part and stores the result in "point".
+bool intersect(const Vector3& rayOrigin, const Vector3& rayDirection, const std::vector<Vertex>& vertices, const std::vector<MeshPart*>& parts, Vector3* point)
+{
+    const float* orig = &rayOrigin.x;
+    const float* dir = &rayDirection.x;
+
+    float minT = FLT_MAX;
+
+    for (unsigned int i = 0, partCount = parts.size(); i < partCount; ++i)
+    {
+        MeshPart* part = parts[i];
+
+        for (unsigned int j = 0, indexCount = part->getIndicesCount(); j < indexCount; j += 3)
+        {
+            const float* v0 = &vertices[part->getIndex( j )].position.x;
+            const float* v1 = &vertices[part->getIndex(j+1)].position.x;
+            const float* v2 = &vertices[part->getIndex(j+2)].position.x;
+
+            // Perform a quick check (in 2D) to determine if the point is definitely NOT in the triangle
+            float xmin, xmax, zmin, zmax;
+            xmin = v0[0] < v1[0] ? v0[0] : v1[0]; xmin = xmin < v2[0] ? xmin : v2[0];
+            xmax = v0[0] > v1[0] ? v0[0] : v1[0]; xmax = xmax > v2[0] ? xmax : v2[0];
+            zmin = v0[2] < v1[2] ? v0[2] : v1[2]; zmin = zmin < v2[2] ? zmin : v2[2];
+            zmax = v0[2] > v1[2] ? v0[2] : v1[2]; zmax = zmax > v2[2] ? zmax : v2[2];
+            if (orig[0] < xmin || orig[0] > xmax || orig[2] < zmin || orig[2] > zmax)
+                continue;
+
+            // Perform a full ray/traingle intersection test in 3D to get the intersection point
+            float t, u, v;
+            if (intersect_triangle(orig, dir, v0, v1, v2, &t, &u, &v))
+            {
+                // Found an intersection!
+                if (t < minT)
+                {
+                    minT = t;
+
+                    if (point)
+                    {
+                        Vector3 rd(rayDirection);
+                        rd.scale(t);
+                        Vector3::add(rayOrigin, rd, point);
+                    }
+                }
+                //return true;
+            }
+        }
+    }
+
+    return (minT != FLT_MAX);//false;
+}
+
+// Ray/Box intersection test.
+bool intersect(const Vector3& rayOrigin, const Vector3& rayDirection, const Vector3& boxMin, const Vector3& boxMax, float* distance)
+{
+    const Vector3& origin = rayOrigin;
+    const Vector3& direction = rayDirection;
+    const Vector3& min = boxMin;
+    const Vector3& max = boxMax;
+
+    // Intermediate calculation variables.
+    float dnear = 0.0f;
+    float dfar = 0.0f;
+    float tmin = 0.0f;
+    float tmax = 0.0f;
+
+    // X direction.
+    float div = 1.0f / direction.x;
+    if (div >= 0.0f)
+    {
+        tmin = (min.x - origin.x) * div;
+        tmax = (max.x - origin.x) * div;
+    }
+    else
+    {
+        tmin = (max.x - origin.x) * div;
+        tmax = (min.x - origin.x) * div;
+    }
+    dnear = tmin;
+    dfar = tmax;
+
+    // Check if the ray misses the box.
+    if (dnear > dfar || dfar < 0.0f)
+    {
+        return false;
+    }
+
+    // Y direction.
+    div = 1.0f / direction.y;
+    if (div >= 0.0f)
+    {
+        tmin = (min.y - origin.y) * div;
+        tmax = (max.y - origin.y) * div;
+    }
+    else
+    {
+        tmin = (max.y - origin.y) * div;
+        tmax = (min.y - origin.y) * div;
+    }
+
+    // Update the near and far intersection distances.
+    if (tmin > dnear)
+    {
+        dnear = tmin;
+    }
+    if (tmax < dfar)
+    {
+        dfar = tmax;
+    }
+    // Check if the ray misses the box.
+    if (dnear > dfar || dfar < 0.0f)
+    {
+        return false;
+    }
+
+    // Z direction.
+    div = 1.0f / direction.z;
+    if (div >= 0.0f)
+    {
+        tmin = (min.z - origin.z) * div;
+        tmax = (max.z - origin.z) * div;
+    }
+    else
+    {
+        tmin = (max.z - origin.z) * div;
+        tmax = (min.z - origin.z) * div;
+    }
+
+    // Update the near and far intersection distances.
+    if (tmin > dnear)
+    {
+        dnear = tmin;
+    }
+    if (tmax < dfar)
+    {
+        dfar = tmax;
+    }
+
+    // Check if the ray misses the box.
+    if (dnear > dfar || dfar < 0.0f)
+    {
+        return false;
+    }
+
+    // The ray intersects the box
+    if (distance)
+        *distance = dnear;
+
+    return true;
+}
+
+}

+ 26 - 0
gameplay-encoder/src/Heightmap.h

@@ -0,0 +1,26 @@
+#ifndef HEIGHTMAP_H_
+#define HEIGHTMAP_H_
+
+#include "Node.h"
+
+namespace gameplay
+{
+
+class Heightmap
+{
+public:
+
+    /**
+     * Generates heightmap data and saves the result to the specified filename (PNG file).
+     *
+     * @param nodeIds List of node ids to include in the heightmap generation.
+     * @param filename Output PNG file to write the heightmap image to.
+     */
+    static void generate(const std::vector<std::string>& nodeIds, const char* filename);
+
+
+};
+
+}
+
+#endif

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

@@ -38,222 +38,6 @@ void Mesh::writeBinary(FILE* file)
     writeBinaryObjects(parts, file);
     writeBinaryObjects(parts, file);
 }
 }
 
 
-/////////////////////////////////////////////////////////////
-// 
-// Fast, Minimum Storage Ray-Triangle Intersection
-// 
-// Authors: Tomas Möller, Ben Trumbore
-// http://jgt.akpeters.com/papers/MollerTrumbore97
-//
-// Implementation of algorithm from Real-Time Rendering (vol 1), pg. 305.
-//
-// Adapted slightly for use here.
-// 
-#define EPSILON 0.000001
-#define CROSS(dest,v1,v2) \
-          dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
-          dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
-          dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
-#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
-#define SUB(dest,v1,v2) \
-          dest[0]=v1[0]-v2[0]; \
-          dest[1]=v1[1]-v2[1]; \
-          dest[2]=v1[2]-v2[2]; 
-
-int
-intersect_triangle(const float orig[3], const float dir[3],
-                   const float vert0[3], const float vert1[3], const float vert2[3],
-                   float *t, float *u, float *v)
-{
-   float edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
-   float det,inv_det;
-
-   /* find vectors for two edges sharing vert0 */
-   SUB(edge1, vert1, vert0);
-   SUB(edge2, vert2, vert0);
-
-   /* begin calculating determinant - also used to calculate U parameter */
-   CROSS(pvec, dir, edge2);
-
-   /* if determinant is near zero, ray lies in plane of triangle */
-   det = DOT(edge1, pvec);
-
-   if (det > -EPSILON && det < EPSILON)
-     return 0;
-   inv_det = 1.0f / det;
-
-   /* calculate distance from vert0 to ray origin */
-   SUB(tvec, orig, vert0);
-
-   /* calculate U parameter and test bounds */
-   *u = DOT(tvec, pvec) * inv_det;
-   if (*u < 0.0 || *u > 1.0)
-     return 0;
-
-   /* prepare to test V parameter */
-   CROSS(qvec, tvec, edge1);
-
-   /* calculate V parameter and test bounds */
-   *v = DOT(dir, qvec) * inv_det;
-   if (*v < 0.0 || *u + *v > 1.0)
-     return 0;
-
-   /* calculate t, ray intersects triangle */
-   *t = DOT(edge2, qvec) * inv_det;
-
-   return 1;
-}
-
-// Performs an intersection test between a ray and the given mesh part
-// and stores the result in "point".
-bool intersect(const Vector3& rayOrigin, const Vector3& rayDirection, const std::vector<Vertex>& vertices, const std::vector<MeshPart*>& parts, Vector3* point)
-{
-    const float* orig = &rayOrigin.x;
-    const float* dir = &rayDirection.x;
-
-    for (unsigned int i = 0, partCount = parts.size(); i < partCount; ++i)
-    {
-        MeshPart* part = parts[i];
-
-        for (unsigned int j = 0, indexCount = part->getIndicesCount(); j < indexCount; j += 3)
-        {
-            const float* v0 = &vertices[part->getIndex( j )].position.x;
-            const float* v1 = &vertices[part->getIndex(j+1)].position.x;
-            const float* v2 = &vertices[part->getIndex(j+2)].position.x;
-
-            float t, u, v;
-            if (intersect_triangle(orig, dir, v0, v1, v2, &t, &u, &v))
-            {
-                // Found an intersection!
-                if (point)
-                {
-                    Vector3 rd(rayDirection);
-                    rd.scale(t);
-                    Vector3::add(rayOrigin, rd, point);
-                }
-                return true;
-            }
-        }
-    }
-
-    return false;
-}
-
-void Mesh::generateHeightmap(const char* filename)
-{
-    // Shoot rays down from a point just above the max Y position of the mesh.
-    // Compute ray-triangle intersection tests against the ray and this mesh to 
-    // generate heightmap data.
-    Vector3 rayOrigin(0, bounds.max.y + 10, 0);
-    Vector3 rayDirection(0, -1, 0);
-    Vector3 intersectionPoint;
-    int minX = (int)ceil(bounds.min.x);
-    int maxX = (int)floor(bounds.max.x);
-    int minZ = (int)ceil(bounds.min.z);
-    int maxZ = (int)floor(bounds.max.z);
-    int width = maxX - minX + 1;
-    int height = maxZ - minZ + 1;
-    float* heights = new float[width * height];
-    int index = 0;
-    float minHeight = FLT_MAX;
-    float maxHeight = -FLT_MAX;
-    for (int z = minZ; z <= maxZ; z++)
-    {
-        rayOrigin.z = (float)z;
-        for (int x = minX; x <= maxX; x++)
-        {
-            float h;
-            rayOrigin.x = (float)x;
-            if (intersect(rayOrigin, rayDirection, vertices, parts, &intersectionPoint))
-            {
-                h = intersectionPoint.y;
-            }
-            else
-            {
-                h = 0;
-                fprintf(stderr, "Warning: Heightmap triangle intersection failed for (%d, %d).\n", x, z);
-            }
-            if (h < minHeight)
-                minHeight = h;
-            if (h > maxHeight)
-                maxHeight = h;
-            heights[index++] = h;
-        }
-    }
-    
-    // Normalize the max height value
-    maxHeight = maxHeight - minHeight;
-
-    png_structp png_ptr = NULL;
-    png_infop info_ptr = NULL;
-    png_bytep row = NULL;
-
-    FILE* fp = fopen(filename, "wb");
-    if (fp == NULL)
-    {
-        fprintf(stderr, "Error: Failed to open file for writing: %s\n", filename);
-        goto error;
-    }
-
-    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-    if (png_ptr == NULL)
-    {
-        fprintf(stderr, "Error: Write struct creation failed: %s\n", filename);
-        goto error;
-    }
-
-    info_ptr = png_create_info_struct(png_ptr);
-    if (info_ptr == NULL)
-    {
-        fprintf(stderr, "Error: Info struct creation failed: %s\n", filename);
-        goto error;
-    }
-
-    png_init_io(png_ptr, fp);
-
-    png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-
-    png_write_info(png_ptr, info_ptr);
-
-    // Allocate memory for a single row of image data
-    row = (png_bytep)malloc(3 * width * sizeof(png_byte));
-
-    for (int y = 0; y < height; y++)
-    {
-        for (int x = 0; x < width; x++)
-        {
-            // Write height value normalized between 0-255 (between min and max height)
-            float h = heights[y*width + x];
-            float nh = (h - minHeight) / maxHeight;
-            int bits = (int)(nh * 16777215.0f); // 2^24-1
-
-            int pos = x*3;
-            row[pos+2] = (png_byte)(bits & 0xff);
-            bits >>= 8;
-            row[pos+1] = (png_byte)(bits & 0xff);
-            bits >>= 8;
-            row[pos] = (png_byte)(bits & 0xff);
-        }
-        png_write_row(png_ptr, row);
-    }
-
-    png_write_end(png_ptr, NULL);
-    DEBUGPRINT_VARG("> Saved heightmap: %s\n", filename);
-
-error:
-    if (heights)
-        delete[] heights;
-    if (fp)
-        fclose(fp);
-    if (row)
-        free(row);
-    if (info_ptr)
-        png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
-    if (png_ptr)
-        png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
-
-}
-
 void Mesh::writeBinaryVertices(FILE* file)
 void Mesh::writeBinaryVertices(FILE* file)
 {
 {
     if (vertices.size() > 0)
     if (vertices.size() > 0)

+ 0 - 5
gameplay-encoder/src/Mesh.h

@@ -60,11 +60,6 @@ public:
 
 
     unsigned int getVertexIndex(const Vertex& vertex);
     unsigned int getVertexIndex(const Vertex& vertex);
 
 
-    /**
-     * Generates a heightmap with the given filename for this mesh.
-     */
-    void generateHeightmap(const char* filename);
-
     void computeBounds();
     void computeBounds();
 
 
     Model* model;
     Model* model;

+ 0 - 25
gameplay-encoder/src/Node.cpp

@@ -78,8 +78,6 @@ void Node::writeBinary(FILE* file)
     {
     {
         writeZero(file);
         writeZero(file);
     }
     }
-
-    generateHeightmap();
 }
 }
 
 
 void Node::writeText(FILE* file)
 void Node::writeText(FILE* file)
@@ -117,29 +115,6 @@ void Node::writeText(FILE* file)
         _model->writeText(file);
         _model->writeText(file);
     }
     }
     fprintElementEnd(file);
     fprintElementEnd(file);
-
-    generateHeightmap();
-}
-
-void Node::generateHeightmap()
-{
-    // Is this node flagged to have a heightmap generated for it?
-    const std::vector<std::string>& heightmapNodes = EncoderArguments::getInstance()->getHeightmapNodeIds();
-    if (std::find(heightmapNodes.begin(), heightmapNodes.end(), getId()) != heightmapNodes.end())
-    {
-        Mesh* mesh = _model ? _model->getMesh() : NULL;
-        if (mesh)
-        {
-            DEBUGPRINT_VARG("> Generating heightmap for node: %s\n", getId().c_str());
-
-            std::string heightmapFilename(EncoderArguments::getInstance()->getOutputDirPath());
-            heightmapFilename += "/heightmap_";
-            heightmapFilename += getId();
-            heightmapFilename += ".png";
-
-            mesh->generateHeightmap(heightmapFilename.c_str());
-        }
-    }
 }
 }
 
 
 void Node::addChild(Node* child)
 void Node::addChild(Node* child)

+ 0 - 6
gameplay-encoder/src/Node.h

@@ -171,12 +171,6 @@ public:
     
     
 private:
 private:
 
 
-    /**
-     * Internal method to generate heightmap for a node's mesh data
-     * if the node was flagged as a heightmap via encoder arguments.
-     */
-    void generateHeightmap();
-
     Matrix _transform;
     Matrix _transform;
     mutable Matrix _worldTransform;
     mutable Matrix _worldTransform;
 
 

+ 99 - 0
gameplay-encoder/src/Thread.h

@@ -0,0 +1,99 @@
+#ifndef THREAD_H_
+#define THREAD_H_
+
+namespace gameplay
+{
+
+#ifdef WIN32
+
+    #include <Windows.h>
+
+    typedef HANDLE THREAD_HANDLE;
+
+    struct WindowsThreadData
+    {
+        int(*threadFunction)(void*);
+        void* arg;
+    };
+
+    DWORD WINAPI WindowsThreadProc(LPVOID lpParam)
+    {
+        WindowsThreadData* data = (WindowsThreadData*)lpParam;
+        int(*threadFunction)(void*) = data->threadFunction;
+        void* arg = data->arg;
+        delete data;
+        data = NULL;
+        return threadFunction(arg);
+    }
+
+    static bool createThread(THREAD_HANDLE* handle, int(*threadFunction)(void*), void* arg)
+    {
+        WindowsThreadData* data = new WindowsThreadData();
+        data->threadFunction = threadFunction;
+        data->arg = arg;
+        *handle = CreateThread(NULL, 0, &WindowsThreadProc, data, 0, NULL);
+        return (*handle != NULL);
+    }
+
+    static void waitForThreads(int count, THREAD_HANDLE* threads)
+    {
+        WaitForMultipleObjects(count, threads, TRUE, INFINITE);
+    }
+
+    static void closeThread(THREAD_HANDLE thread)
+    {
+        CloseHandle(thread);
+    }
+
+#else
+
+    #include <pthread.h>
+
+    typedef pthread_t THREAD_HANDLE;
+
+    struct PThreadData
+    {
+        int(*threadFunction)(void*);
+        void* arg;
+    };
+
+    void* PThreadProc(void* threadData)
+    {
+        PThreadData* data = (PThreadData*)threadData;
+        int(*threadFunction)(void*) = data->threadFunction;
+        void* arg = data->arg;
+        delete data;
+        data = NULL;
+        int retVal = threadFunction(arg);
+        pthread_exit(retVal);
+    }
+
+    static bool createThread(THREAD_HANDLE* handle, int(*threadFunction)(void*), void* arg)
+    {
+        PThreadData* data = new PThreadData();
+        data->threadFunction = threadFunction;
+        data->arg = arg;
+        return pthread_create(handle, NULL, &PThreadProc, data) == 0;
+    }
+
+    static void waitForThreads(int count, THREAD_HANDLE* threads)
+    {
+        // Call join on all threads to wait for them to terminate.
+        // This also frees any resources allocated by the threads,
+        // essentially cleaning them up.
+        for (int i = 0; i < count; ++i)
+        {
+            pthread_join(threads[i], NULL);
+        }
+    }
+
+    static void closeThread(THREAD_HANDLE thread)
+    {
+        // nothing to do... waitForThreads (which calls join) cleans up
+    }
+
+#endif
+
+}
+
+#endif