Added the OOP infastructure to work on rendering chunks.

This commit is contained in:
Warwick 2022-08-10 16:13:32 +01:00
parent 38b1d8c92e
commit 28cd790e31
6 changed files with 97 additions and 8 deletions

View file

@ -1,8 +1,10 @@
#include "Chunk.h" #include "Chunk.h"
Chunk::Chunk(FastNoise::SmartNode<> &noiseGenerator, TerrainInfo ti) { Chunk::Chunk(FastNoise::SmartNode<> &noiseGenerator, TerrainInfo ti) {
this->terrainInfo = ti;
this->fnGenerator = &noiseGenerator; this->fnGenerator = &noiseGenerator;
// Create location to store noise values.
// Create location to store noise values temporarily.
std::vector<float> noiseOutput((ti.xRange[1] - ti.xRange[0]) * std::vector<float> noiseOutput((ti.xRange[1] - ti.xRange[0]) *
(ti.yRange[1] - ti.yRange[0]) * (ti.yRange[1] - ti.yRange[0]) *
(ti.zRange[1] - ti.zRange[0])); (ti.zRange[1] - ti.zRange[0]));
@ -13,15 +15,25 @@ Chunk::Chunk(FastNoise::SmartNode<> &noiseGenerator, TerrainInfo ti) {
ti.xRange[1] - ti.xRange[0], ti.yRange[1] - ti.yRange[0], ti.xRange[1] - ti.xRange[0], ti.yRange[1] - ti.yRange[0],
ti.zRange[1] - ti.zRange[0], ti.frequency, ti.seed); ti.zRange[1] - ti.zRange[0], ti.frequency, ti.seed);
// do stuff with values // Place noise into 3D vector array for easy access.
int index = 0; int index = 0;
for (int z = 0; z < 16; z++) { std::vector<std::vector<float>> yArray;
for (int y = 0; y < 16; y++) { std::vector<float> xArray;
for (int x = 0; x < 16; x++) { for (int z = 0; z < ti.zRange[1] - ti.zRange[0]; z++) {
for (int y = 0; y < ti.yRange[1] - ti.yRange[0]; y++) {
for (int x = 0; x < ti.xRange[1] - ti.xRange[0]; x++) {
error.log(std::to_string(x) + " " + std::to_string(y) + " " + error.log(std::to_string(x) + " " + std::to_string(y) + " " +
std::to_string(z) + ": " + std::to_string(z) + ": " +
std::to_string(noiseOutput[index++])); std::to_string(noiseOutput[index]));
xArray.push_back(noiseOutput[index++]);
} }
yArray.push_back(xArray);
xArray.clear();
} }
this->noise.push_back(yArray);
yArray.clear();
} }
} }
float Chunk::getNoise(int x, int y, int z) { return this->noise[x][y][z]; }
TerrainInfo Chunk::getTerrainInfo() { return this->terrainInfo; }

View file

@ -15,9 +15,18 @@ class Chunk {
private: private:
Error error = Error("Chunk"); Error error = Error("Chunk");
// Favourite noise generated values using the NoiseTool for now: // Pointer to the noise generator that generated this chunk
FastNoise::SmartNode<> *fnGenerator; FastNoise::SmartNode<> *fnGenerator;
// Values that initialised the chunk
TerrainInfo terrainInfo;
// 3D vector of noise values
std::vector<std::vector<std::vector<float>>> noise;
public: public:
Chunk(FastNoise::SmartNode<> &noiseGenerator, TerrainInfo ti); Chunk(FastNoise::SmartNode<> &noiseGenerator, TerrainInfo ti);
float getNoise(int x, int y, int z);
TerrainInfo getTerrainInfo();
}; };

View file

@ -0,0 +1,13 @@
#include "MarchingCubeChunkRenderer.h"
MarchingCubeChunkRenderer::MarchingCubeChunkRenderer(
std::vector<Chunk> &chunks, std::vector<Model> models) {
this->chunks = &chunks;
this->models = models;
}
void MarchingCubeChunkRenderer::draw(ShaderLoader &shader) {
for (int i = 0; i < chunks->size(); i++) {
// Render Chunk
}
}

View file

@ -0,0 +1,24 @@
#pragma once
#include "Chunk.h"
#include "Error.h"
#include "Model.h"
#include <vector>
class MarchingCubeChunkRenderer {
private:
Error error = Error("MarchingCubeChunkRenderer");
// Chunks to render
std::vector<Chunk> *chunks;
// Here we will store a model corresponding to ease polygon of the marching
// cubes algorithm plus maybe more if models become orientation specific:
// https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.545.613
std::vector<Model> models;
public:
MarchingCubeChunkRenderer(std::vector<Chunk> &chunks,
std::vector<Model> models);
void draw(ShaderLoader &shader);
};

View file

@ -1,6 +1,7 @@
#include "Terrain.h" #include "Terrain.h"
Terrain::Terrain() { Terrain::Terrain() : renderer(nullptr) {
// Create chunk
TerrainInfo ti; TerrainInfo ti;
ti.xRange[0] = 0; ti.xRange[0] = 0;
ti.xRange[1] = 16; ti.xRange[1] = 16;
@ -12,4 +13,24 @@ Terrain::Terrain() {
ti.seed = 0; ti.seed = 0;
chunks.push_back(Chunk(fnGenerator, ti)); chunks.push_back(Chunk(fnGenerator, ti));
// Create chunks renderer.
std::vector<Model> models = {
Model(ROOT_DIR "data/models/wooden_boxbarrel/wooden_box_and_barrel.obj")};
renderer = new MarchingCubeChunkRenderer(this->chunks, models);
}
Terrain::~Terrain() {
// Since we created our renderer member in our constructer it has to be a
// nullptr before being updated to the created object, which means it's
// lifecycle needs to be managed by this class.
delete renderer;
}
void Terrain::draw(ShaderLoader &shader) {
if (renderer != nullptr)
renderer->draw(shader);
else
error.warn("Terrain attempted to draw with no chunk renderer!");
} }

View file

@ -1,6 +1,10 @@
#pragma once #pragma once
#include "Chunk.h" #include "Chunk.h"
#include "Error.h" #include "Error.h"
#include "MarchingCubeChunkRenderer.h"
#include "Model.h"
#include "ShaderLoader.h"
#include "helpers/RootDir.h"
#include <FastNoise/FastNoise.h> #include <FastNoise/FastNoise.h>
#include <vector> #include <vector>
@ -15,6 +19,12 @@ private:
// Currently used chunks // Currently used chunks
std::vector<Chunk> chunks; std::vector<Chunk> chunks;
MarchingCubeChunkRenderer *renderer;
public: public:
Terrain(); Terrain();
void draw(ShaderLoader &shader);
~Terrain();
}; };