Skip to content

Commit

Permalink
VOXELRENDER: fixed invalid face culling for negative scaling values
Browse files Browse the repository at this point in the history
fixes parts of issue #440
  • Loading branch information
mgerhardy committed Apr 2, 2024
1 parent c2fd757 commit caa73fe
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/modules/voxelrender/MeshState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ void MeshState::hide(int idx, bool hide) {
_volumeData[idx]._hidden = hide;
}

video::Face MeshState::cullFace(int idx) const {
if (idx < 0 || idx >= MAX_VOLUMES) {
return video::Face::Back;
}
return _volumeData[idx]._cullFace;
}

void MeshState::setCullFace(int idx, video::Face face) {
if (idx < 0 || idx >= MAX_VOLUMES) {
return;
}
_volumeData[idx]._cullFace = face;
}

bool MeshState::grayed(int idx) const {
if (idx < 0 || idx >= MAX_VOLUMES) {
return true;
Expand Down
8 changes: 8 additions & 0 deletions src/modules/voxelrender/MeshState.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "core/collection/PriorityQueue.h"
#include "core/concurrent/ThreadPool.h"
#include "palette/Palette.h"
#include "video/Types.h"
#include "voxel/ChunkMesh.h"
#include "voxel/Mesh.h"

Expand Down Expand Up @@ -44,6 +45,9 @@ class MeshState {
core::Optional<palette::Palette> _palette;
bool _hidden = false;
bool _gray = false;
// if all axes scale positive: cull the back face
// if one or three axes are negative, then cull the front face
video::Face _cullFace = video::Face::Back;
int _reference = -1;
glm::mat4 _model{1.0f};
glm::vec3 _pivot{0.0f};
Expand Down Expand Up @@ -164,6 +168,10 @@ class MeshState {
void gray(int idx, bool gray);
bool grayed(int idx) const;

// for scaling on 1 or 3 axes negative we need to flip the face culling
video::Face cullFace(int idx) const;
void setCullFace(int idx, video::Face face);

/**
* @brief In case of a reference - this gives us the index for the
* referenced object
Expand Down
4 changes: 4 additions & 0 deletions src/modules/voxelrender/RawVolumeRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "video/Camera.h"
#include "video/FrameBufferConfig.h"
#include "video/Renderer.h"
#include "video/ScopedFaceCull.h"
#include "video/ScopedFrameBuffer.h"
#include "video/ScopedPolygonMode.h"
#include "video/ScopedState.h"
Expand Down Expand Up @@ -494,6 +495,7 @@ void RawVolumeRenderer::render(RenderContext &renderContext, const video::Camera
var.pivot = _meshState->pivot(idx);
_shadowMapUniformBlock.update(var);
_shadowMapShader.setBlock(_shadowMapUniformBlock.getBlockUniformBuffer());
video::ScopedFaceCull scopedFaceCull(_meshState->cullFace(idx));
static_assert(sizeof(voxel::IndexType) == sizeof(uint32_t), "Index type doesn't match");
video::drawElements<voxel::IndexType>(video::Primitive::Triangles, indices);
}
Expand Down Expand Up @@ -562,6 +564,7 @@ void RawVolumeRenderer::render(RenderContext &renderContext, const video::Camera
core_assert_always(_voxelData.update(_voxelShaderVertData));

video::ScopedPolygonMode polygonMode(mode);
video::ScopedFaceCull scopedFaceCull(_meshState->cullFace(idx));
video::ScopedBuffer scopedBuf(_state[bufferIndex]._vertexBuffer[MeshType_Opaque]);
if (normals) {
core_assert_always(_voxelNormShader.setFrag(_voxelData.getFragUniformBuffer()));
Expand Down Expand Up @@ -617,6 +620,7 @@ void RawVolumeRenderer::render(RenderContext &renderContext, const video::Camera
core_assert_always(_voxelData.update(_voxelShaderVertData));

video::ScopedPolygonMode polygonMode(mode);
video::ScopedFaceCull scopedFaceCull(_meshState->cullFace(idx));
video::ScopedBuffer scopedBuf(_state[bufferIndex]._vertexBuffer[MeshType_Transparency]);
if (normals) {
core_assert_always(_voxelNormShader.setFrag(_voxelData.getFragUniformBuffer()));
Expand Down
8 changes: 8 additions & 0 deletions src/modules/voxelrender/SceneGraphRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,20 @@ void SceneGraphRenderer::prepare(const RenderContext &renderContext) {
}
if (sceneMode) {
const scenegraph::FrameTransform &transform = sceneGraph.transformForFrame(node, frame);
const int negative = (int)std::signbit(transform.scale.x) + (int)std::signbit(transform.scale.y) +
(int)std::signbit(transform.scale.z);
if (negative == 1 || negative == 3) {
meshState->setCullFace(id, video::Face::Front);
} else {
meshState->setCullFace(id, video::Face::Back);
}
const glm::mat4 worldMatrix = transform.worldMatrix();
const glm::vec3 maxs = worldMatrix * glm::vec4(region.getUpperCorner(), 1.0f);
const glm::vec3 mins = worldMatrix * glm::vec4(region.getLowerCorner(), 1.0f);
const glm::vec3 pivot = transform.scale * node.pivot() * glm::vec3(region.getDimensionsInVoxels());
meshState->setModelMatrix(id, worldMatrix, pivot, mins, maxs);
} else {
meshState->setCullFace(id, video::Face::Back);
meshState->setModelMatrix(id, glm::mat4(1.0f), glm::vec3(0.0f), region.getLowerCorner(),
region.getUpperCorner());
}
Expand Down

0 comments on commit caa73fe

Please sign in to comment.