Skip to content

Commit 1ba341f

Browse files
committed
Fixed rendering issue in OpenGL Renderer when objects got further away from the origin by switching from world space to view space in shaders.
1 parent fa32d53 commit 1ba341f

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

BrotBoxEngine/BBE/OpenGL/OpenGLManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ namespace bbe
134134
PreviousDrawCall2D previousDrawCall2d = PreviousDrawCall2D::NONE;
135135

136136
bbe::List<bbe::PointLight> pointLights;
137+
bbe::Matrix4 m_view;
137138

138139
public:
139140
OpenGLManager();

BrotBoxEngine/OpenGL/OpenGLManager.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include "BBE/OpenGL/OpenGLSphere.h"
1414
#include <iostream>
1515

16-
// TODO: "ExampleRotatingCubeIntersections" has visual artifacts
17-
// The effect gets worse with objects further away from (0/0/0). Floating Point Issue?
1816
// TODO: "ExampleSandGame" performs much worse than on Vulkan - Why?
1917
// TODO: Is every OpenGL Resource properly freed? How can we find that out?
2018

@@ -351,8 +349,8 @@ bbe::INTERNAL::openGl::Program bbe::INTERNAL::openGl::OpenGLManager::init3dShade
351349
"{"
352350
" vec4 worldPos = model * vec4(inPos, 1.0);"
353351
" gl_Position = projection * view * worldPos * vec4(1.0, -1.0, 1.0, 1.0);"
354-
" passPos = worldPos;"
355-
" passNormal = model * vec4(inNormal, 0.0);"
352+
" passPos = view * worldPos;"
353+
" passNormal = view * model * vec4(inNormal, 0.0);"
356354
" passAlbedo = vec4(inColor.xyz, 1.0);"
357355
"}";
358356

@@ -384,7 +382,6 @@ bbe::INTERNAL::openGl::Program bbe::INTERNAL::openGl::OpenGLManager::init3dShade
384382
static GLint gPositionPos3dLight = 0;
385383
static GLint gNormalPos3dLight = 0;
386384
static GLint gAlbedoSpecPos3dLight = 0;
387-
static GLint cameraPosPos3dLight = 0;
388385
static GLint lightPosPos3dLight = 0;
389386
static GLint lightStrengthPos3dLight = 0;
390387
static GLint falloffModePos3dLight = 0;
@@ -435,7 +432,7 @@ bbe::INTERNAL::openGl::Program bbe::INTERNAL::openGl::OpenGLManager::init3dShade
435432
" vec3 pos = texture(gPosition, uvCoord).xyz;"
436433
" vec3 albedo = texture(gAlbedoSpec, uvCoord).xyz;"
437434
" vec3 toLight = lightPos - pos;"
438-
" vec3 toCamera = cameraPos - pos;"
435+
" vec3 toCamera = -pos;"
439436
" float distToLight = length(toLight);"
440437
" float lightPower = lightStrength;"
441438
" if(distToLight > 0.f)"
@@ -477,15 +474,13 @@ bbe::INTERNAL::openGl::Program bbe::INTERNAL::openGl::OpenGLManager::init3dShade
477474
{UT::UT_vec4 , "lightColor" , &lightColorPos3dLight },
478475
{UT::UT_vec4 , "specularColor", &specularColorPos3dLight },
479476
{UT::UT_vec3 , "lightPos" , &lightPosPos3dLight },
480-
{UT::UT_vec3 , "cameraPos" , &cameraPosPos3dLight },
481477
{UT::UT_float , "ambientFactor", &ambientFactorPos3dLight },
482478
});
483479

484480
program.uniform1i(gPositionPos3dLight, 0);
485481
program.uniform1i(gNormalPos3dLight, 1);
486482
program.uniform1i(gAlbedoSpecPos3dLight, 2);
487483

488-
program.uniform3f(cameraPosPos3dLight, 0.f, 0.f, 0.f);
489484
program.uniform3f(lightPosPos3dLight, 0.f, 0.f, 0.f);
490485
program.uniform1f(lightStrengthPos3dLight, 0.f);
491486
program.uniform1i(falloffModePos3dLight, 0);
@@ -601,7 +596,9 @@ void bbe::INTERNAL::openGl::OpenGLManager::preDraw2D()
601596
for (size_t i = 0; i < pointLights.getLength(); i++)
602597
{
603598
const bbe::PointLight& l = pointLights[i];
604-
m_program3dLight.uniform3f(lightPosPos3dLight, l.pos);
599+
bbe::Vector4 p(l.pos, 1.0f);
600+
p = m_view * p;
601+
m_program3dLight.uniform3f(lightPosPos3dLight, p.xyz());
605602
m_program3dLight.uniform1f(lightStrengthPos3dLight, l.lightStrength);
606603
m_program3dLight.uniform1i(falloffModePos3dLight, (int)l.falloffMode);
607604
m_program3dLight.uniform4f(lightColorPos3dLight, l.lightColor);
@@ -840,7 +837,7 @@ void bbe::INTERNAL::openGl::OpenGLManager::setCamera3D(const Vector3& cameraPos,
840837
{
841838
m_program3dMrt.uniformMatrix4fv(viewPos3dMrt, GL_FALSE, view);
842839
m_program3dMrt.uniformMatrix4fv(projectionPos3dMrt, GL_FALSE, projection);
843-
m_program3dLight.uniform3f(cameraPosPos3dLight, cameraPos);
840+
m_view = view;
844841
}
845842

846843
void bbe::INTERNAL::openGl::OpenGLManager::fillCube3D(const Cube& cube)

Examples/ExampleFarCube/ExampleFarCube.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
// NOTE: As the position offset is growing exponentially, it quickly gets impossible for the vertex shader to keep up. That is an
99
// acceptable limitation of the BBE. However, color flickering must not happen.
1010
// Current eye-measurements:
11-
// Renderer Breaks after...
12-
// Vulkan Vertex: 19.000 Meter
11+
// Renderer Breaks* after...
12+
// Vulkan Vertex: 3.000 Meter
1313
// Vulkan Fragment: Never?
14-
// OpenGL Vertex: 100.000 Meter? Hard to tell due to all the flickering. Probably earlier.
15-
// OpenGL Fragment: 22 Meter (<- BAAAAD!)
14+
// OpenGL Vertex: 3.000 Meter
15+
// OpenGL Fragment: Never?
16+
// *Breaks as in "any pixel changed".
1617
class MyGame : public bbe::Game
1718
{
1819
float timePassed = 0;

0 commit comments

Comments
 (0)