aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-14 01:10:55 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-14 01:10:55 -0400
commitbccf02982941feaf93ba94a29644eb5559a11655 (patch)
treee8749edfb35e7907238feec513cf9b73ffc1aecb
parentd4d9e0d35a1609c72ea65df95e9c3ce5706e221f (diff)
World can now generate its own mesh
-rw-r--r--src/world.cpp43
-rw-r--r--src/world.hpp21
2 files changed, 49 insertions, 15 deletions
diff --git a/src/world.cpp b/src/world.cpp
index eb101f2..1dca763 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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 */
diff --git a/src/world.hpp b/src/world.hpp
index 7a7461f..cea599b 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -28,6 +28,13 @@
#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);
};
/**