]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
World can now generate its own mesh
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 14 Sep 2019 05:10:55 +0000 (01:10 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 14 Sep 2019 05:10:55 +0000 (01:10 -0400)
src/world.cpp
src/world.hpp

index eb101f28e23af5e13e46df5cdce39f0115815a2b..1dca763944298f3c03434a658b51e270fe791175 100644 (file)
@@ -46,10 +46,16 @@ World::World(sol::object param)
         std::cerr << "World paramaters must be stored in a table" << std::endl;
     }
 
+    // If there is a register function, we should call it here
     if (registerMat != sol::nil)
         registerMat(this);
+
+    // If a generate function is defined, call it
     if (generate != sol::nil)
         generate(this);
+
+    // Generate our mesh
+    generateMesh();
 }
 
 /* REGISTRY */
@@ -105,9 +111,9 @@ World::setSize(unsigned int x, unsigned int y, unsigned int z)
     height = y;
     layers = z;
 
-    data = std::vector<std::vector<std::vector<unsigned int>>>
-        (z, std::vector<std::vector<unsigned int>>
-            (x,std::vector<unsigned int>
+    data = std::vector<std::vector<std::vector<int>>>
+        (z, std::vector<std::vector<int>>
+            (x,std::vector<int>
                 (y, -1)
             )
         );
@@ -124,11 +130,32 @@ World::getSize()
 /* RENDERING */
 void World::generateMesh()
 {
-    const unsigned int coordLength = 3 + // x, y, z
-                                     2 + // texture coords
-                                     1;  // transparency
-
-    (void)coordLength;
+    //const unsigned int voxelLength = 6; // 2 triangles @ 3 points each
+
+    // Preallocate size of vertexes
+    mesh = std::basic_string<WorldMeshData>();
+    for (float Z = 0; Z < data.size(); Z++) {
+        for (float X = 0; X < data.at(Z).size(); X++) {
+            for (float Y = 0; Y < data.at(Z).at(X).size(); Y++) {
+                int d = data.at(Z).at(X).at(Y);
+
+                if (d == -1) // Don't make a mesh for air of course
+                    continue;
+
+                Texture &t = registry.at(d).texture;
+                glm::vec2& to = t.offset;
+                glm::vec2& ts = t.offsetSize;
+
+                mesh += {X  , Y  , Z, to.x     , to.y+ts.y, 1.0};
+                mesh += {X+1, Y  , Z, to.x+ts.x, to.y+ts.y, 1.0};
+                mesh += {X  , Y+1, Z, to.x     , to.y     , 1.0};
+
+                mesh += {X+1, Y  , Z, to.x+ts.x, to.y+ts.y, 1.0};
+                mesh += {X+1, Y+1, Z, to.x+ts.x, to.y     , 1.0};
+                mesh += {X  , Y+1, Z, to.x     , to.y     , 1.0};
+            }
+        }
+    }
 }
 
 /* SEED */
index 7a7461fef1cd92a80ecef71c017d4c1b24bf0bb6..cea599b76954d40180152b0740af8ebdfb1f9a46 100644 (file)
 
 #include "texture.hpp"
 
+struct WorldMeshData
+{
+    float posX, posY, posZ;
+    float texX, texY;
+    float transparency;
+};
+
 struct WorldMaterial
 {
     bool passable = false;
@@ -59,12 +66,12 @@ private:
     unsigned int height;
     unsigned int width;
 
-    std::vector<std::vector<std::vector<unsigned int>>> data;
+    std::vector<std::vector<std::vector<int>>> data;
 
-    std::unordered_map<std::string, unsigned int> string_registry;
+    std::unordered_map<std::string, int> string_registry;
     std::vector<WorldMaterial> registry;
 
-    std::vector<float> mesh;
+    std::basic_string<WorldMeshData> mesh;
 public:
     /* VARS */
     sol::function generate;
@@ -79,10 +86,6 @@ public:
         data.clear();
     }
 
-    /* SEED */
-    unsigned int getSeed();
-    unsigned int setSeed(unsigned int);
-
     /* REGISTRY */
     void registerMaterial(std::string, sol::object);
 
@@ -97,6 +100,10 @@ public:
 
     /* RENDERING */
     void generateMesh();
+
+    /* SEED */
+    unsigned int getSeed();
+    unsigned int setSeed(unsigned int);
 };
 
 /**