Browse Source

prepare geometry for lighting

testing
totallyfake 2 years ago
parent
commit
83a84d2bb9

+ 1
- 0
include/graphics/chunkmesh.h View File

@@ -28,6 +28,7 @@ protected:
std::vector<float> geometry;
std::vector<unsigned> mGeometry;
std::vector<unsigned> mFaceAttribs;
std::vector<unsigned> mLighting;
bool mLocked;
bool mUpdated;
unsigned mVAO;

+ 2
- 0
include/world.h View File

@@ -35,6 +35,8 @@ public:

Chunk* getChunk(const glm::ivec3& pos);

HeightMap* getHeightMap(const glm::ivec2& pos);

void queueChunkUpdate(const int& x, const int& y, const int& z, const bool& highpriority = false);
void queueChunkUpdate(const glm::ivec3& pos, const bool& highpriority = false);


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

@@ -25,7 +25,7 @@ void main() {
//fog calculations

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

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

+ 36
- 32
res/shaders/voxelvert.vert.glsl View File

@@ -1,37 +1,41 @@
#version 330

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

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

out vec3 texCoordInterp;
out vec3 lightDataInterp;
out vec4 eyeSpacePos;

void main() {
vec3 pos_unpacked;
pos_unpacked.x = float((position >> 8u) & 15u) / 15.0f;
pos_unpacked.y = float((position >> 4u) & 15u) / 15.0f;
pos_unpacked.z = float((position) & 15u) / 15.0f;

vec2 uv_unpacked;
uv_unpacked.x = float((position >> 16u) & 15u) / 15.0f;
uv_unpacked.y = float((position >> 12u) & 15u) / 15.0f;

vec3 offset_unpacked;
offset_unpacked.x = float((face_attrib >> 10u) & 31u);
offset_unpacked.y = float((face_attrib >> 5u) & 31u);
offset_unpacked.z = float((face_attrib) & 31u);

float tex_index = float(face_attrib >> 15u);

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(1.0f, 1.0f, 1.0f);
eyeSpacePos = eyeSpacePosVert;
layout(location = 2) in uint light;

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

out vec3 texCoordInterp;
out vec3 lightDataInterp;
out vec4 eyeSpacePos;

void main() {
vec3 pos_unpacked;
pos_unpacked.x = float((position >> 8u) & 15u) / 15.0f;
pos_unpacked.y = float((position >> 4u) & 15u) / 15.0f;
pos_unpacked.z = float((position) & 15u) / 15.0f;

vec2 uv_unpacked;
uv_unpacked.x = float((position >> 16u) & 15u) / 15.0f;
uv_unpacked.y = float((position >> 12u) & 15u) / 15.0f;

vec3 offset_unpacked;
offset_unpacked.x = float((face_attrib >> 10u) & 31u);
offset_unpacked.y = float((face_attrib >> 5u) & 31u);
offset_unpacked.z = float((face_attrib) & 31u);

float tex_index = float(face_attrib >> 15u);
vec4 light_unpacked;
light_unpacked.x = float((light >> 24u) & 255u) / 255.0f;
light_unpacked.y = float((light >> 16u) & 255u) / 255.0f;
light_unpacked.z = float((light >> 8u) & 255u) / 255.0f;
light_unpacked.w = float((light) & 255u) / 255.0f;

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);
eyeSpacePos = eyeSpacePosVert;
}

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

@@ -25,18 +25,20 @@ ChunkMesh::ChunkMesh(World& world, glm::ivec3 chunkPos) :

glBindVertexArray(mVAO);

glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);

//vertices
glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
glVertexAttribIPointer(0, 1, GL_UNSIGNED_INT, GL_FALSE, 0);
glEnableVertexAttribArray(0);


//face attribs
glBindBuffer(GL_ARRAY_BUFFER, mFaceAttribsVBO);

glVertexAttribIPointer(1, 1, GL_UNSIGNED_INT, GL_FALSE, 0);
glEnableVertexAttribArray(1);

//light
glBindBuffer(GL_ARRAY_BUFFER, mLightVBO);
glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, GL_FALSE, 0);
glEnableVertexAttribArray(2);
}

bool ChunkMesh::rebuildChunkGeometry() {
@@ -76,11 +78,13 @@ bool ChunkMesh::rebuildChunkGeometry() {
unsigned texIndex = mLinkedWorld.voxelInfo.getTextureIndex(chunk->getVoxelType(i,j,k),
static_cast<FaceDirection>(faceIndex));
unsigned faceAttribT = faceAttrib | (texIndex << 15); // pack texture index into faceAttrib
unsigned lightVal = 0xFFFFFFFF;

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

@@ -114,6 +118,8 @@ void ChunkMesh::draw() {
bool ChunkMesh::updateGeometry() {
if (mUpdated) {
mUpdated = false;

//upload geometry, face attribs, and lighting
glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
glBufferData(GL_ARRAY_BUFFER,
sizeof(unsigned) * mGeometry.size(),
@@ -126,6 +132,12 @@ bool ChunkMesh::updateGeometry() {
mFaceAttribs.data(),
GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, mLightVBO);
glBufferData(GL_ARRAY_BUFFER,
sizeof(unsigned) * mLighting.size(),
mLighting.data(),
GL_STATIC_DRAW);

mGeometryFaceCount = mFaceCount;
return true;
}

+ 20
- 1
src/world.cpp View File

@@ -42,7 +42,10 @@ bool World::isVoxelSolid(const int& x, const int& y, const int& z) {
}

bool World::setVoxelType(const int& x, const int& y, const int& z, const unsigned& type, const bool& updateChunk) {
auto chunkPos = glm::ivec3(floor((float)x / (float)chunkSize), floor((float)y / (float)chunkSize), floor((float)z / (float)chunkSize));
auto chunkPos = glm::ivec3(floor((float)x / (float)chunkSize),
floor((float)y / (float)chunkSize),
floor((float)z / (float)chunkSize));

auto chunk = getChunk(chunkPos);
if (!chunk) return false;

@@ -117,6 +120,20 @@ Chunk* World::getChunk(const glm::ivec3& pos) {
else return nullptr;
}

HeightMap* World::getHeightMap(const glm::ivec2& pos) {
HeightMap* heightMap;

//return heightmap if it already exists
if (mHeightMaps.find(pos, heightMap)) {
return heightMap;
}

//else make a new one, add to map, and return
heightMap = new HeightMap(pos);
mHeightMaps.insert(pos, heightMap);
return heightMap;
}

void World::queueChunkUpdate(const int& x, const int& y, const int& z, const bool& highpriority) {
queueChunkUpdate(glm::ivec3(x,y,z));
}
@@ -145,6 +162,8 @@ void World::queueChunkLoad(const glm::ivec3 &pos) {

void World::draw() {
auto lt = mChunkMeshes.lock_table();

//naively iterate through all chunks and draw them
for (const auto& i : lt) {
glm::mat4 modelMat = glm::translate(glm::mat4(), glm::vec3((float)i.first.x * 16,
(float)i.first.y * 16,

Loading…
Cancel
Save