Browse Source

More lighting work

testing
totallyfake 1 year ago
parent
commit
fdecea33e0
3 changed files with 82 additions and 11 deletions
  1. 20
    0
      include/voxelutils.h
  2. 16
    8
      src/graphics/chunkmesh.cpp
  3. 46
    3
      src/graphics/voxelmodel.cpp

+ 20
- 0
include/voxelutils.h View File

@@ -2,6 +2,7 @@

#include <tuple>
#include <glm/glm.hpp>
#include <array>

namespace vtk {

@@ -16,6 +17,25 @@ enum class FaceDirection : unsigned int {

typedef std::tuple<unsigned, unsigned, unsigned> uPos; //used for voxel positions

/*
//TODO: move this to cpp file
constexpr glm::ivec3 FaceDirectionToVec(const FaceDirection& direction) {
switch(direction) {
case FaceDirection::TOP:
return glm::ivec3(0,1,0);
case FaceDirection::BOTTOM:
return glm::ivec3(0,-1,0);
case FaceDirection::NORTH:
return glm::ivec3(0,0,1);
case FaceDirection::SOUTH:
return glm::ivec3(0,0,-1);
case FaceDirection::EAST:
return glm::ivec3(1,0,0);
case FaceDirection::WEST:
return glm::ivec3(-1,0,0);
}
*/

struct uPosHash : public std::unary_function<uPos, std::size_t> {
std::size_t operator()(const uPos& k) const {
return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);

+ 16
- 8
src/graphics/chunkmesh.cpp View File

@@ -65,14 +65,22 @@ bool ChunkMesh::rebuildChunkGeometry() {
for (int j = 0; j < chunkSize; ++j) {
for (int k = 0; k < chunkSize; ++k) {
if (chunk->isVoxelSolid(i,j,k)) {
glm::ivec3 lpos(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;
surrounding_light[0] = chunk->getLightPacked(glm::ivec3(i-1,j+1,k-1));
surrounding_light[1] = chunk->getLightPacked(glm::ivec3(i+1,j+1,k-1));
surrounding_light[2] = chunk->getLightPacked(glm::ivec3(i,j+1,k));
surrounding_light[3] = chunk->getLightPacked(glm::ivec3(i-1,j+1,k+1));
surrounding_light[4] = chunk->getLightPacked(glm::ivec3(i+1,j+1,k+1));
surrounding_light[5] = chunk->getLightPacked(glm::ivec3(i,j,k-1));
surrounding_light[6] = chunk->getLightPacked(glm::ivec3(i-1,j,k));
surrounding_light[7] = chunk->getLightPacked(glm::ivec3(i+1,j,k));
surrounding_light[8] = chunk->getLightPacked(glm::ivec3(i,j,k+1));
surrounding_light[9] = chunk->getLightPacked(glm::ivec3(i-1,j-1,-1));
surrounding_light[10] = chunk->getLightPacked(glm::ivec3(i+1,j-1,k-1));
surrounding_light[11] = chunk->getLightPacked(glm::ivec3(i,j-1,k));
surrounding_light[12] = chunk->getLightPacked(glm::ivec3(i-1,j-1,k+1));
surrounding_light[13] = chunk->getLightPacked(glm::ivec3(i+1,j-1,k+1));
//build face attrib
@@ -96,7 +104,7 @@ bool ChunkMesh::rebuildChunkGeometry() {
for (int l = 0; l < 6; ++l) {
mGeometry.push_back(mesh[l]);
mFaceAttribs.push_back(faceAttribT);
mLighting.push_back(faceLighting[0]);
mLighting.push_back(faceLighting[l]);
}
};


+ 46
- 3
src/graphics/voxelmodel.cpp View File

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

#include "graphics/voxelmodel.h"

@@ -106,13 +107,55 @@ std::vector<unsigned>& VoxelModel::getFaceMesh(const int& face) {
}

void VoxelModel::getFaceLighting(std::vector<unsigned>& lighting, const FaceDirection& face, const std::array<unsigned, 14>& surrounding_light, const unsigned& zero_weight) {

auto blend = [](unsigned a, unsigned b, unsigned weight) {
unsigned aw = 7 - weight;
unsigned bw = weight;
unsigned newLight = 0;
newLight = std::min(((((a >> 24) & 0xFF) * aw) / 8u) +
((((b >> 24) & 0xFF) * bw) / 8u), 255u); //feels like lisp
newLight = (newLight << 8) |
std::min(((((a >> 16) & 0xFF) * aw) / 7u) +
((((b >> 16) & 0xFF) * bw) / 7u), 255u);
newLight = (newLight << 8) |
std::min(((((a >> 8) & 0xFF) * aw) / 7u) +
((((b >> 8) & 0xFF) * bw) / 7u), 255u);
newLight = (newLight << 8) |
std::min(((((a) & 0xFF) * aw) / 7u) +
((((b) & 0xFF) * bw) / 7u), 255u);
return newLight;
};
auto& face_vec = mGeometry.at(static_cast<unsigned>(face));
for (int i = 0; i < face_vec.size(); ++i) {
FaceDirection nface = FaceDirection::TOP;
for (auto& i : face_vec) {
unsigned vx = (i >> 8) & 0xF;
unsigned vy = (i >> 4) & 0xF;
unsigned vz = i & 0xF;
switch(face) {
case FaceDirection::TOP:
lighting.push_back(surrounding_light[2]);

//std::cout << vx << ", " << vz << "\n";
if (vx > 7) {
if (vz > 7) {
std::cout << "hit\n";
lighting.push_back(surrounding_light[4]); //4
} else {
lighting.push_back(surrounding_light[1]); //1
}
} else {
if (vz > 7) {
lighting.push_back(surrounding_light[3]); //3
} else {
lighting.push_back(surrounding_light[0]); //0
}
}
/*
lighting.push_back(blend(surrounding_light[2],
surrounding_light[4],
(vx + vy - 14u) / 2u));
*/
break;

case FaceDirection::BOTTOM:
lighting.push_back(surrounding_light[11]);
break;

Loading…
Cancel
Save