Browse Source

I forgot what I did

testing
= 3 years ago
parent
commit
682178667b
7 changed files with 116 additions and 65 deletions
  1. 10
    6
      include/chunk.h
  2. 23
    0
      include/graphics/chunkgeometry.h
  3. 28
    13
      src/chunk.cpp
  4. 37
    36
      src/chunkrenderer.cpp
  5. 9
    0
      src/graphics/chunkgeometry.cpp
  6. 4
    3
      src/terraingen.cpp
  7. 5
    7
      src/world.cpp

+ 10
- 6
include/chunk.h View File

@@ -21,7 +21,6 @@
#include "voxelutils.h"
#include "chunkrenderer.h"

#include <unordered_map>
#include <glm/glm.hpp>

namespace vtk {
@@ -30,17 +29,22 @@ class World;

class Chunk {
public:
Chunk();
Chunk(World& world);
bool isVoxelSolid(const int& x, const int& y, const int& z); //Is the voxel not a transparent type?
void setVoxelType(const int& x, const int& y, const int& z, const unsigned& type);
unsigned getVoxelType(const unsigned& x, const unsigned& y, const unsigned& z);
glm::ivec3 getWorldCoords(const int& x, const int& y, const int& z);

std::array<unsigned, 4096> voxels;
World* linkedWorld;
unsigned chunkSize;
glm::ivec3 chunkPos;
void setPos(const glm::ivec3& pos);
glm::ivec3 getPos();
World& getWorld();


ChunkRenderer renderer;
protected:
std::array<unsigned, 4096> voxels;
World& mLinkedWorld;
glm::ivec3 mPos;
};

}

+ 23
- 0
include/graphics/chunkgeometry.h View File

@@ -0,0 +1,23 @@
/*
* Handles geometry for individual chunks
*/

#pragma once

#include "graphics/drawable.h"

namespace vtk {

class Chunk;

class ChunkGeometry : public Drawable {
public:
void draw(RenderTask& task);
void buildGeometry(Chunk& chunk);
void buildVertices(Chunk& chunk);
void buildLighting(Chunk& chunk);
protected:
};

}

+ 28
- 13
src/chunk.cpp View File

@@ -22,11 +22,12 @@

namespace vtk {

Chunk::Chunk() {
linkedWorld = nullptr;
chunkSize = 16;
chunkPos = glm::ivec3(0,0,0);
Chunk::Chunk(World& world) :
mLinkedWorld(world)
{
mPos = glm::ivec3(0,0,0);
renderer.linkedChunk = this;

//fill voxels with 0
for (unsigned i = 0; i < 4096; i++) {
voxels[i] = 0;
@@ -34,12 +35,13 @@ Chunk::Chunk() {
}

bool Chunk::isVoxelSolid(const int& x, const int& y, const int& z) {
if (x < 0 || x > (int)chunkSize - 1 ||
y < 0 || y > (int)chunkSize - 1 ||
z < 0 || z > (int)chunkSize - 1 ) { //position is outside of the chunk
return linkedWorld->isVoxelSolid(chunkPos.x * 16 + x,
chunkPos.y * 16 + y,
chunkPos.z * 16 + z);
if (x < 0 || x > 15 ||
y < 0 || y > 15 ||
z < 0 || z > 15 )
{ //position is outside of the chunk
return mLinkedWorld.isVoxelSolid(mPos.x * 16 + x,
mPos.y * 16 + y,
mPos.z * 16 + z);
}

return (getVoxelType((unsigned)x,(unsigned)y,(unsigned)z) != 0);
@@ -65,9 +67,22 @@ unsigned Chunk::getVoxelType(const unsigned& x, const unsigned& y, const unsigne
}

glm::ivec3 Chunk::getWorldCoords(const int& x, const int& y, const int& z) {
return glm::ivec3(chunkPos.x * 16 + x,
chunkPos.y * 16 + y,
chunkPos.z * 16 + z);
return glm::ivec3(mPos.x * 16 + x,
mPos.y * 16 + y,
mPos.z * 16 + z);
}

void Chunk::setPos(const glm::ivec3& pos) {
mPos = pos;
}


glm::ivec3 Chunk::getPos() {
return mPos;
}

World& Chunk::getWorld() {
return mLinkedWorld;
}

}

+ 37
- 36
src/chunkrenderer.cpp View File

@@ -207,6 +207,7 @@ void ChunkRenderer::updateGeometry() {
}

void ChunkRenderer::updateTexCoords() {
auto linkedWorld = linkedChunk->getWorld();
for (auto& i : textureCoords)
i.clear();

@@ -216,58 +217,58 @@ void ChunkRenderer::updateTexCoords() {
if (linkedChunk->isVoxelSolid(i,j,k)) {
if (!linkedChunk->isVoxelSolid(i+1,j,k)) {
//right face
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[0], linkedChunk->getWorldCoords(i,j,k), Face3D::RIGHT, Corner2D::TOPRIGHT);
}
if (!linkedChunk->isVoxelSolid(i-1,j,k)) {
//left face
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[1], linkedChunk->getWorldCoords(i,j,k), Face3D::LEFT, Corner2D::TOPRIGHT);
}
if (!linkedChunk->isVoxelSolid(i,j+1,k)) {
//top face
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[2], linkedChunk->getWorldCoords(i,j,k), Face3D::TOP, Corner2D::TOPLEFT);
}
if (!linkedChunk->isVoxelSolid(i,j-1,k)) {
//bottom face
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[3], linkedChunk->getWorldCoords(i,j,k), Face3D::BOTTOM, Corner2D::BOTTOMRIGHT);
}
if (!linkedChunk->isVoxelSolid(i,j,k-1)) {
//back face
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[4], linkedChunk->getWorldCoords(i,j,k), Face3D::BACK, Corner2D::TOPRIGHT);

}
if (!linkedChunk->isVoxelSolid(i,j,k+1)) {
//front face
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::BOTTOMRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::BOTTOMLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::TOPLEFT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::TOPRIGHT);
linkedChunk->linkedWorld->voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::BOTTOMRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::BOTTOMLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::TOPLEFT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::TOPRIGHT);
linkedWorld.voxelInfo.pushTexCoordFromWorldCoords(textureCoords[5], linkedChunk->getWorldCoords(i,j,k), Face3D::FRONT, Corner2D::BOTTOMLEFT);
}
}
}

+ 9
- 0
src/graphics/chunkgeometry.cpp View File

@@ -0,0 +1,9 @@
#include "graphics/chunkgeometry.h"

namespace vtk {

void draw(RenderTask& task) {
}

}

+ 4
- 3
src/terraingen.cpp View File

@@ -38,14 +38,15 @@ TerrainGen::TerrainGen() {
}

void TerrainGen::generateChunk(Chunk* chunk) {
auto chunkPos = chunk->getPos();
//first pass
for (int i = 0; i < 16; i++) { //x
for (int j = 0; j < 16; j++) { //y
for (int k = 0; k < 16; k++) { //z
double nVal = mNoise->get3D(
(double)(chunk->chunkPos.x * 16 + i),
(double)(chunk->chunkPos.y * 16 + j),
(double)(chunk->chunkPos.z * 16 + k));
(double)(chunkPos.x * 16 + i),
(double)(chunkPos.y * 16 + j),
(double)(chunkPos.z * 16 + k));
if (nVal <= 0.0) {
chunk->setVoxelType(i,j,k,1);
} else {

+ 5
- 7
src/world.cpp View File

@@ -92,13 +92,11 @@ bool World::makeChunk(const int& x, const int& y, const int& z) {
return false;
}

auto newChunk = new Chunk;
auto newChunk = new Chunk(*this);
chunks[pos] = newChunk;

newChunk->chunkPos = posvec;
newChunk->chunkSize = 16;
newChunk->setPos(posvec);

newChunk->linkedWorld = this;
//newChunk->renderer.linkedWorld = this;

newChunk->renderer.init();
@@ -130,9 +128,9 @@ void World::queueChunkUpdate(const iPos& pos) {
void World::draw() {
for (auto& i : chunks) {
glm::mat4 modelMat = glm::translate(glm::mat4(), glm::vec3(
(float)i.second->chunkPos.x * 16,
(float)i.second->chunkPos.y * 16,
(float)i.second->chunkPos.z * 16
(float)i.second->getPos().x * 16,
(float)i.second->getPos().y * 16,
(float)i.second->getPos().z * 16
));
glUniformMatrix4fv(modelMatUni, 1, GL_FALSE, glm::value_ptr(modelMat));
i.second->renderer.drawChunk();

Loading…
Cancel
Save