Browse Source

more lighting work

testing
totallyfake 2 years ago
parent
commit
b6becaaa58

+ 1
- 18
include/chunk.h View File

@@ -1,21 +1,3 @@
/*
* =====================================================================================
*
* Filename: chunk.h
*
* Description: Stores chunk information
*
* Version: 1.0
* Created: 04/03/2014 07:23:43 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/

#pragma once

#include "voxelutils.h"
@@ -39,6 +21,7 @@ public:
unsigned getVoxelType(const unsigned& x, const unsigned& y, const unsigned& z);
glm::ivec3 getWorldCoords(const int& x, const int& y, const int& z);
unsigned getLightLevel(const glm::ivec3& pos);
unsigned getLightPacked(const glm::ivec3& pos);

void setPos(const glm::ivec3& pos);
glm::ivec3 getPos();

+ 17
- 1
include/graphics/voxelmodel.h View File

@@ -3,6 +3,8 @@
#include <vector>
#include <array>

#include "voxelmath.h"

namespace vtk {

class VoxelModel {
@@ -14,8 +16,22 @@ public:
bool packDataIntoFace(const std::vector<unsigned>& data, const int& face);
bool uploadFaceToMesh(std::vector<unsigned>& mesh, const int& face);
std::vector<unsigned>& getFaceMesh(const int& face);

/* surrounding light is an array containing the light data around the block in the following configuration
where X stands for none and any numbers stand for array indexes
y = 1 y = 0 y = -1
x -1 0 1
z -1 0 X 1 X 5 X 9 X 10
0 X 2 X 6 X 7 X 11 X
1 3 X 4 X 8 X 12 X 13
zero_weight controls how "smoothed" the lighting is. Read the function body for more info on how this works
*/
void getFaceLighting(std::vector<unsigned>& lighting, const FaceDirection& face, const std::array<unsigned, 14>& surrounding_light, const unsigned& zero_weight);
protected:
// top bottom north south east west
// top bottom north south east west, special
std::array<std::vector<unsigned>, 7> mGeometry;
};


+ 1
- 1
include/voxelutils.h View File

@@ -5,7 +5,7 @@

namespace vtk {

enum class FaceDirection {
enum class FaceDirection : unsigned int {
TOP = 0,
BOTTOM = 1,
NORTH = 2,

+ 6
- 1
res/shaders/voxelvert.vert.glsl View File

@@ -32,10 +32,15 @@ void main() {
light_unpacked.z = float((light >> 8u) & 255u) / 255.0f;
light_unpacked.w = float((light) & 255u) / 255.0f;

vec3 sun_color = vec3(1.0f, 1.0f, 1.0f) * light_unpacked.w;
vec3 light_color = vec3(max(light_unpacked.x, sun_color.x),
max(light_unpacked.y, sun_color.y),
max(light_unpacked.z, sun_color.z));

vec3 final_position = pos_unpacked + offset_unpacked;
vec4 eyeSpacePosVert = view * model * vec4(final_position, 1.0);
gl_Position = proj * eyeSpacePosVert;
texCoordInterp = vec3(uv_unpacked, tex_index);//texCoord;
lightDataInterp = vec3(light_unpacked.x, light_unpacked.y, light_unpacked.z);
lightDataInterp = light_color;
eyeSpacePos = eyeSpacePosVert;
}

+ 7
- 0
src/chunk.cpp View File

@@ -88,6 +88,13 @@ unsigned Chunk::getLightLevel(const glm::ivec3 &pos) {
return 15;
}

unsigned Chunk::getLightPacked(const glm::ivec3 &pos) {
if (isVoxelSolid(pos.x, pos.y, pos.z)) {
return 0xFF000000;
}
return 0xFFFFFFFF;
}

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

+ 37
- 12
src/graphics/chunkmesh.cpp View File

@@ -55,6 +55,7 @@ bool ChunkMesh::rebuildChunkGeometry() {
// geometry format: x,y,z,u,v,i
mGeometry.clear();
mLighting.clear();
mFaceAttribs.clear();

mFaceCount = 0;
@@ -65,6 +66,15 @@ bool ChunkMesh::rebuildChunkGeometry() {
for (int k = 0; k < chunkSize; ++k) {
if (chunk->isVoxelSolid(i,j,k)) {

std::array<unsigned, 14> surrounding_light;
surrounding_light[2] = 0x000000FF;
surrounding_light[11] = 0xFF000000;
surrounding_light[8] = 0xFFFFFFFF;
surrounding_light[5] = 0xFFFFFFFF;
surrounding_light[7] = 0xFFFFFFFF;
surrounding_light[6] = 0xFFFFFFFF;
//build face attrib
// 0b00000000000000000xxxxxyyyyyzzzzz
unsigned faceAttrib = 0;
@@ -73,27 +83,42 @@ bool ChunkMesh::rebuildChunkGeometry() {
faceAttrib = (faceAttrib << 5) | k;

//lambda expression for adding vertices
auto addFaceModel = [&](int faceIndex) {
auto mesh = mVoxelModel.getFaceMesh(faceIndex);
unsigned texIndex = mLinkedWorld.voxelInfo.getTextureIndex(chunk->getVoxelType(i,j,k),
static_cast<FaceDirection>(faceIndex));
auto addFaceModel = [&](FaceDirection faceDirection) {
auto mesh = mVoxelModel.getFaceMesh(static_cast<unsigned>(faceDirection));
static std::vector<unsigned> faceLighting;
faceLighting.clear();
mVoxelModel.getFaceLighting(faceLighting, faceDirection, surrounding_light, 0);
unsigned texIndex = mLinkedWorld.voxelInfo.getTextureIndex(chunk->getVoxelType(i,j,k), faceDirection);
unsigned faceAttribT = faceAttrib | (texIndex << 15); // pack texture index into faceAttrib
unsigned lightVal = 0xFFFFFFFF;
unsigned lightVal = 0x000000FF;

++mFaceCount;
for (int l = 0; l < 6; ++l) {
mGeometry.push_back(mesh[l]);
mFaceAttribs.push_back(faceAttribT);
mLighting.push_back(lightVal);
mLighting.push_back(faceLighting[0]);
}
};

if (!chunk->isVoxelSolid(i,j+1,k)) addFaceModel(0); // T
if (!chunk->isVoxelSolid(i,j-1,k)) addFaceModel(1); // B
if (!chunk->isVoxelSolid(i,j,k+1)) addFaceModel(2); // N
if (!chunk->isVoxelSolid(i,j,k-1)) addFaceModel(3); // S
if (!chunk->isVoxelSolid(i+1,j,k)) addFaceModel(4); // E
if (!chunk->isVoxelSolid(i-1,j,k)) addFaceModel(5); // W

if (!chunk->isVoxelSolid(i,j+1,k)) {
addFaceModel(FaceDirection::TOP); // T
}
if (!chunk->isVoxelSolid(i,j-1,k)) {
addFaceModel(FaceDirection::BOTTOM); // B
}
if (!chunk->isVoxelSolid(i,j,k+1)) {
addFaceModel(FaceDirection::NORTH); // N
}
if (!chunk->isVoxelSolid(i,j,k-1)) {
addFaceModel(FaceDirection::SOUTH); // S
}
if (!chunk->isVoxelSolid(i+1,j,k)) {
addFaceModel(FaceDirection::EAST); // E
}
if (!chunk->isVoxelSolid(i-1,j,k)) {
addFaceModel(FaceDirection::WEST); // W
}
}
}
}

+ 34
- 1
src/graphics/voxelmodel.cpp View File

@@ -1,4 +1,6 @@
#include <iostream>
#include <glm/glm.hpp>

#include "graphics/voxelmodel.h"

namespace vtk {
@@ -64,7 +66,6 @@ VoxelModel::VoxelModel() {
15, 15, 0, 15, 15
};
packDataIntoFace(west, 5);

}

VoxelModel::~VoxelModel() {
@@ -72,6 +73,7 @@ VoxelModel::~VoxelModel() {
}

bool VoxelModel::packDataIntoFace(const std::vector<unsigned>& data, const int& face) {

for (int i = 0; i < data.size(); i += 5) {
unsigned vertex = 0;
vertex = (vertex ) | data[i ];
@@ -102,4 +104,35 @@ bool VoxelModel::uploadFaceToMesh(std::vector<unsigned>& mesh, const int& face)
std::vector<unsigned>& VoxelModel::getFaceMesh(const int& face) {
return mGeometry[face];
}

void VoxelModel::getFaceLighting(std::vector<unsigned>& lighting, const FaceDirection& face, const std::array<unsigned, 14>& surrounding_light, const unsigned& zero_weight) {
auto& face_vec = mGeometry.at(static_cast<unsigned>(face));
for (int i = 0; i < face_vec.size(); ++i) {
FaceDirection nface = FaceDirection::TOP;
switch(face) {
case FaceDirection::TOP:
lighting.push_back(surrounding_light[2]);
break;
case FaceDirection::BOTTOM:
lighting.push_back(surrounding_light[11]);
break;
case FaceDirection::NORTH:
lighting.push_back(surrounding_light[8]);
break;
case FaceDirection::SOUTH:
lighting.push_back(surrounding_light[5]);
break;
case FaceDirection::EAST:
lighting.push_back(surrounding_light[7]);
break;
case FaceDirection::WEST:
lighting.push_back(surrounding_light[6]);
break;
default:
lighting.push_back(0xFFFFFFFF);
break;
}
}
}

}

Loading…
Cancel
Save