فهرست منبع

Merge pull request #303 from kwhatmough/next

Resolved merge of heightmap precision revert from master into next
Sean Paul Taylor 13 سال پیش
والد
کامیت
73f12a8e05

+ 6 - 2
gameplay-encoder/src/EncoderArguments.cpp

@@ -185,7 +185,8 @@ void EncoderArguments::printUsage() const
         "\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");
+        "\t\tMultiple -h arguments can be supplied to generate more than one heightmap.\n" \
+        "\t\tFor 24-bit packed height data use -hp instead of -h.\n");
     fprintf(stderr,"\n");
     fprintf(stderr,"TTF file options:\n");
     fprintf(stderr,"  -s <size>\tSize of the font.\n");
@@ -315,13 +316,16 @@ void EncoderArguments::readOption(const std::vector<std::string>& options, size_
         break;
     case 'h':
         {
-            if (str.compare("-heightmap") == 0 || str.compare("-h") == 0)
+            bool isHighPrecision = str.compare("-hp") == 0;
+            if (str.compare("-heightmap") == 0 || str.compare("-h") == 0 || isHighPrecision)
             {
                 (*index)++;
                 if (*index < (options.size() + 1))
                 {
                     _heightmaps.resize(_heightmaps.size() + 1);
                     HeightmapOption& heightmap = _heightmaps.back();
+                    
+                    heightmap.isHighPrecision = isHighPrecision;
 
                     // Split node id list into tokens
                     unsigned int length = options[*index].size() + 1;

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

@@ -24,6 +24,7 @@ public:
     {
         std::vector<std::string> nodeIds;
         std::string filename;
+        bool isHighPrecision;
     };
 
     /**

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

@@ -330,7 +330,7 @@ void GPBFile::adjust()
     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());
+        Heightmap::generate(heightmaps[i].nodeIds, heightmaps[i].filename.c_str(), heightmaps[i].isHighPrecision);
     }
 }
 

+ 18 - 8
gameplay-encoder/src/Heightmap.cpp

@@ -36,7 +36,7 @@ bool intersect(const Vector3& rayOrigin, const Vector3& rayDirection, const Vect
 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)
+void Heightmap::generate(const std::vector<std::string>& nodeIds, const char* filename, bool highP)
 {
     printf("Generating heightmap: %s...\n", filename);
 
@@ -203,14 +203,24 @@ void Heightmap::generate(const std::vector<std::string>& nodeIds, const char* fi
             // 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);
+            if (highP)
+            {
+                // high precision packed 24-bit (RGB)
+                int bits = (int)(nh * 16777215.0f); // 2^24-1
+                
+                row[pos+2] = (png_byte)(bits & 0xff);
+                bits >>= 8;
+                row[pos+1] = (png_byte)(bits & 0xff);
+                bits >>= 8;
+                row[pos] = (png_byte)(bits & 0xff);
+            }
+            else
+            {
+                // standard precision 8-bit (grayscale)
+                png_byte b = (png_byte)(nh * 255.0f);
+                row[pos] = row[pos+1] = row[pos+2] = b;
+            }
         }
         png_write_row(png_ptr, row);
     }

+ 2 - 1
gameplay-encoder/src/Heightmap.h

@@ -15,8 +15,9 @@ public:
      *
      * @param nodeIds List of node ids to include in the heightmap generation.
      * @param filename Output PNG file to write the heightmap image to.
+     * @param highP Use packed 24-bit (RGB) instead of standard 8-bit grayscale.
      */
-    static void generate(const std::vector<std::string>& nodeIds, const char* filename);
+    static void generate(const std::vector<std::string>& nodeIds, const char* filename, bool highP = false);
 
 
 };