Browse Source

depth buffer testing

testing
totallyfake 1 year ago
parent
commit
c0aa6817e7
6 changed files with 51 additions and 4 deletions
  1. 2
    0
      include/game.h
  2. 3
    0
      include/voxelutils.h
  3. 2
    1
      src/chunk.cpp
  4. 37
    1
      src/game.cpp
  5. 2
    2
      src/heightmap.cpp
  6. 5
    0
      src/voxelutils.cpp

+ 2
- 0
include/game.h View File

@@ -47,6 +47,8 @@ protected:
Scene* activeScene;
void cleanup();
bool running;

GLuint mColor, mDepth, mFBO;
};

}

+ 3
- 0
include/voxelutils.h View File

@@ -20,6 +20,9 @@ typedef std::tuple<unsigned, unsigned, unsigned> uPos; //used for voxel position
glm::ivec3 worldPosToChunkPos(const glm::ivec3& pos);
glm::ivec2 worldPosToChunkPos(const glm::ivec2& pos);

//takes chunk and local positions, returns a world position
glm::ivec3 chunkPosToWorldPos(const glm::ivec3& cPos, const glm::ivec3& lPos);

/*
//TODO: move this to cpp file
constexpr glm::ivec3 FaceDirectionToVec(const FaceDirection& direction) {

+ 2
- 1
src/chunk.cpp View File

@@ -92,7 +92,8 @@ unsigned Chunk::getLightPacked(const glm::ivec3 &pos) {
if (isVoxelSolid(pos.x, pos.y, pos.z)) {
return 0x00000000;
}
if (getHeightMap()->getHeight(glm::ivec2(pos.x, pos.z)) > pos.y + (mPos.y * 16)) { //voxel is below ground
auto wPos = chunkPosToWorldPos(mPos, pos);
if (mLinkedWorld.getHeight(glm::ivec2(wPos.x, wPos.z)) > wPos.y) { //voxel is below ground
//std::cout << mLinkedWorld.getHeight(glm::ivec2(pos.x, pos.z)) << ", ";
return 0x000000AA;
}

+ 37
- 1
src/game.cpp View File

@@ -68,6 +68,27 @@ void Game::init() {
glClearDepth(0.0f);
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);

//depth buffer stuff
glGenTextures(1, &mColor);
glBindTexture(GL_TEXTURE_2D, mColor);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600));
glBindTexture(GL_TEXTURE_2D, 0);

glGenTextures(1, &mDepth);
glBindTexture(GL_TEXTURE_2D, mDepth);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600));
glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &mFBO);
glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColor, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepth, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
fprintf(stderr, "glCheckFramebufferStatus: %x\n", status);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

running = false;
gls::setTracking(true); // track OpenGL state changes
}
@@ -93,7 +114,22 @@ void Game::loop() {
activeScene->update(dTime);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
activeScene->draw();

glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
glDepthFunc(GL_GREATER);
glClearDepth(0.0f);
activeScene->draw(); //draw the scene
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, mFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // default FBO
glBlitFramebuffer(
0, 0, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600),
0, 0, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600),
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

SDL_GL_SwapWindow(window.getWindow());
}

+ 2
- 2
src/heightmap.cpp View File

@@ -19,7 +19,7 @@ glm::ivec2 HeightMap::getPos() {

int HeightMap::getHeight(const glm::ivec2 &pos) {
int index = pos.x + (16 * pos.y);
return mHeights[index].load();
return mHeights[index].load(std::memory_order_consume);
}

bool HeightMap::pushUpdate(const glm::ivec3 &pos, const bool &destroyed) {
@@ -65,7 +65,7 @@ void HeightMap::flushUpdates() {
}

void HeightMap::setHeight(const glm::ivec3& pos) {
mHeights[pos.x + (16 * pos.z)].store(pos.y);
mHeights[pos.x + (16 * pos.z)].store(pos.y, std::memory_order_release);
}

}

+ 5
- 0
src/voxelutils.cpp View File

@@ -13,4 +13,9 @@ glm::ivec2 worldPosToChunkPos(const glm::ivec2& pos) {
return glm::ivec2(mp::floor_div3(pos.x, 16),
mp::floor_div3(pos.y, 16));
}


glm::ivec3 chunkPosToWorldPos(const glm::ivec3& cPos, const glm::ivec3& lPos) {
return glm::ivec3((cPos * 16) + lPos);
}
}

Loading…
Cancel
Save