Browse Source

work on new voxel format

testing
totallyfake 2 years ago
parent
commit
d7430645f7

+ 25
- 24
include/graphics/chunkmesh.h View File

@@ -5,32 +5,33 @@
#include <vector>

namespace vtk {
class World;
class World;

class ChunkMesh {
public:
ChunkMesh(World& world, glm::ivec3 linkedChunkPos);
void rebuildChunkGeometry();
void rebuildChunkLighting();
void draw();
bool updateGeometry(); //puts the geometry into the VBOs because OGL can't into threading
class ChunkMesh {
public:
ChunkMesh(World& world, glm::ivec3 linkedChunkPos);
void rebuildChunkGeometry();
void rebuildChunkLighting();
void draw();
bool updateGeometry(); //puts the geometry into the VBOs because OGL can't into threading

protected:
//position of chunk since chunk does NOT have to exist to rebuild
//if chunk is not loaded, rebuild will fail silently
//this is useful for allowing you to unload distant chunks but still draw them
//it's also faster to look up adjacent chunk positions when you have the position
glm::ivec3 mLinkedChunkPos;
World& mLinkedWorld;
protected:
//position of chunk since chunk does NOT have to exist to rebuild
//if chunk is not loaded, rebuild will fail silently
//this is useful for allowing you to unload distant chunks but still draw them
//it's also faster to look up adjacent chunk positions when you have the position
glm::ivec3 mLinkedChunkPos;
World& mLinkedWorld;

std::vector<float> geometry;
bool mLocked;
bool mUpdated;
unsigned mVAO;
unsigned mGeometryTexVBO;
unsigned mLightVBO;
int mFaceCount;
int mGeometryFaceCount;
};
std::vector<float> geometry;
std::vector<unsigned> mGeometry;
bool mLocked;
bool mUpdated;
unsigned mVAO;
unsigned mGeometryTexVBO;
unsigned mLightVBO;
int mFaceCount;
int mGeometryFaceCount;
};
}

+ 10
- 4
res/shaders/voxel.vert View File

@@ -1,9 +1,9 @@

#version 430

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 texCoord;
layout(location = 2) in vec3 lightData;
layout(location = 0) in uint position;
//layout(location = 1) in vec3 texCoord;
//layout(location = 2) in vec3 lightData;

uniform mat4 proj, view, model; //transform matrix

@@ -12,9 +12,15 @@ out vec3 lightDataInterp;
out vec4 eyeSpacePos;

void main() {
vec3 pos_unpacked;
pos_unpacked.x = float((position >> 8)&((1<<4)-1))
pos_unpacked.y = float((position >> 4)&((1<<4)-1))
pos_unpacked.z = float((position >> 0)&((1<<4)-1))
vec4 eyeSpacePosVert = view * model * vec4(position, 1.0);
gl_Position = proj * eyeSpacePosVert;
texCoordInterp = texCoord;
//texCoordInterp = texCoord;
texCoordInterp = vec3(0.0, 0.0, 0.0);
lightDataInterp = lightData;
eyeSpacePos = eyeSpacePosVert;
}

+ 2
- 2
res/shaders/voxelfrag.frag.glsl View File

@@ -24,8 +24,8 @@ float getFogFactor(float fogCoord) {
void main() {
//fog calculations

vec4 texel = texture2DArray(texture, texCoordInterp);
outColor = texel; //* vec4(lightDataInterp, 1.0f);
//vec4 texel = texture2DArray(texture, texCoordInterp);
outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); //texel; //* vec4(lightDataInterp, 1.0f);

float fogCoord = abs(eyeSpacePos.z/eyeSpacePos.w);
outColor = mix(outColor, fogColor, getFogFactor(fogCoord));

+ 9
- 4
res/shaders/voxelvert.vert.glsl View File

@@ -1,7 +1,8 @@
#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 texCoord;
layout(location = 0) in uint position;
//layout(location = 0) in vec3 position;
//layout(location = 1) in vec3 texCoord;

uniform mat4 proj, view, model; //transform matrix

@@ -10,9 +11,13 @@ out vec3 lightDataInterp;
out vec4 eyeSpacePos;

void main() {
vec4 eyeSpacePosVert = view * model * vec4(position, 1.0);
vec3 pos_unpacked;
pos_unpacked.x = float((position ) & 15u) / 15.0f;
pos_unpacked.y = float((position >> 4u) & 15u) / 15.0f;
pos_unpacked.z = float((position >> 8u) & 15u) / 15.0f;
vec4 eyeSpacePosVert = view * model * vec4(pos_unpacked, 1.0);
gl_Position = proj * eyeSpacePosVert;
texCoordInterp = texCoord;
texCoordInterp = vec3(1.0f, 1.0f, 1.0f);//texCoord;
lightDataInterp = vec3(1.0f, 1.0f, 1.0f);
eyeSpacePos = eyeSpacePosVert;
}

+ 1
- 0
src/.#main.cpp View File

@@ -0,0 +1 @@
user@hiro.1737

+ 52
- 5
src/graphics/chunkmesh.cpp View File

@@ -28,12 +28,12 @@ ChunkMesh::ChunkMesh(World& world, glm::ivec3 chunkPos) :


//vertices
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), NULL);
glVertexAttribPointer(0, 1, GL_UNSIGNED_INT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);

//tex uv coords
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 4*sizeof(unsigned), (void*)(1*sizeof(unsigned)));
//glEnableVertexAttribArray(1);
}

void ChunkMesh::rebuildChunkGeometry() {
@@ -42,6 +42,35 @@ void ChunkMesh::rebuildChunkGeometry() {
//get the chunk from position
auto chunk = mLinkedWorld.getChunk(mLinkedChunkPos);

unsigned uTopModel [] = { 0,0,0,0,0,0 };
//single vertex
uTopModel[0] = (uTopModel[0] << 4) | unsigned(0);
uTopModel[0] = (uTopModel[0] << 4) | unsigned(15);
uTopModel[0] = (uTopModel[0] << 4) | unsigned(0);

uTopModel[1] = (uTopModel[1] << 4) | unsigned(15);
uTopModel[1] = (uTopModel[1] << 4) | unsigned(15);
uTopModel[1] = (uTopModel[1] << 4) | unsigned(0);

uTopModel[2] = (uTopModel[2] << 4) | unsigned(0);
uTopModel[2] = (uTopModel[2] << 4) | unsigned(15);
uTopModel[2] = (uTopModel[2] << 4) | unsigned(15);

uTopModel[3] = (uTopModel[3] << 4) | unsigned(15);
uTopModel[3] = (uTopModel[3] << 4) | unsigned(15);
uTopModel[3] = (uTopModel[3] << 4) | unsigned(15);

uTopModel[4] = (uTopModel[4] << 4) | unsigned(0);
uTopModel[4] = (uTopModel[4] << 4) | unsigned(15);
uTopModel[4] = (uTopModel[4] << 4) | unsigned(15);

uTopModel[5] = (uTopModel[5] << 4) | unsigned(15);
uTopModel[5] = (uTopModel[5] << 4) | unsigned(15);
uTopModel[5] = (uTopModel[5] << 4) | unsigned(0);
//unsigned((uTopModel[0] >> 8)&((1<<4)-1)); extract x, y ,z
//unsigned((uTopModel[0] >> 4)&((1<<4)-1));
//unsigned((uTopModel[0] >> 0)&((1<<4)-1));
//temporary until I finish mesh providers
float texUV [] = {
0.0f, 1.0f,
@@ -111,6 +140,22 @@ void ChunkMesh::rebuildChunkGeometry() {
mFaceCount = 0;
int chunkSize = 16; // TODO: make this not hardcoded

for (int i = 0; i < chunkSize; ++i) {
for (int j = 0; j < chunkSize; ++j) {
for (int k = 0; k < chunkSize; ++k) {
if (chunk->isVoxelSolid(i,j,k)) {
if (!chunk->isVoxelSolid(i,j+1,k)) {
++mFaceCount;
for (int l = 0; l < 6; ++l) {
mGeometry.push_back(uTopModel[l]);
}
}
}
}
}
}
/*
for (int i = 0; i < chunkSize; ++i) {
for (int j = 0; j < chunkSize; ++j) {
for (int k = 0; k < chunkSize; ++k) {
@@ -207,7 +252,8 @@ void ChunkMesh::rebuildChunkGeometry() {
}
}
}

*/
std::cout << mGeometry[1];
mUpdated = true;
mLocked = false;
}
@@ -228,7 +274,8 @@ bool ChunkMesh::updateGeometry() {
if (mUpdated) {
mUpdated = false;
glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * geometry.size(), geometry.data(), GL_STATIC_DRAW);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float) * geometry.size(), geometry.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * mGeometry.size(), mGeometry.data(), GL_STATIC_DRAW);
mGeometryFaceCount = mFaceCount;
return true;
}

+ 7
- 0
src/main.cpp View File

@@ -12,6 +12,13 @@

int main (int argc, char *argv[])
{
//testing
unsigned uTopModel [] = { 0,0,0,0,0,0 };
uTopModel[0] = (uTopModel[0] << 4) | unsigned(8);
uTopModel[0] = (uTopModel[0] << 4) | unsigned(6);
std::cout << "Extracted: " << unsigned((uTopModel[0] >> 4)&((1<<4)-1)) <<
", " << unsigned((uTopModel[0] >> 0)&((1<<4)-1));

vtk::LoggerSetup lSetup;
lSetup.setup();
spdlog::get("general")->info("Starting Voxeltronik Engine...");

+ 1
- 0
src/scenes/.#testscene.cpp View File

@@ -0,0 +1 @@
user@hiro.1737

+ 3
- 3
src/scenes/testscene.cpp View File

@@ -133,9 +133,9 @@ void TestScene::init() {
std::cout << std::endl;

int chunkCount = 1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 1; j++) {
for (int k = 0; k < 1; k++) {
std::cout << "\rGenerating chunks (" << chunkCount << "/" << 8*8*8 << ")" << std::flush;
world.generateChunk(i,j,k);
chunkCount++;

+ 2
- 2
src/terraingen.cpp View File

@@ -31,8 +31,8 @@ namespace vtk {

TerrainGen::TerrainGen() {
std::shared_ptr<noise::NoiseModule> noise = std::make_shared<noise::Noise>(6969696);
std::shared_ptr<noise::NoiseModule> yGrad = std::make_shared<noise::YGradient>(0.0, 128.0);
mNoise = std::make_shared<noise::YTurbulence>(yGrad, noise, 40.0);
std::shared_ptr<noise::NoiseModule> yGrad = std::make_shared<noise::YGradient>(0.0, 10.0);
mNoise = yGrad; //std::make_shared<noise::YTurbulence>(yGrad, noise, 40.0);

mTerrainScale = 32.0;
}

Loading…
Cancel
Save