Skip to content

Commit ee36b8b

Browse files
sp0lshMuniuDev
authored andcommitted
Rendering: Shadows pt 2 (#155)
* test case * calculating light bounds in AABox around frustum in game example for debug * added tight projection around camera frustum, light projection near and far planes based on scene intersection and moving light in texel-sized increments * shadow bug dependent on camera direction * imgui reacting to input and drawing on screen from render device * proper init, render deinit and windows created by game * comments * comments * commented #pragma failing travis ci * removed #pragma * removed pragma from resource * commented unused components * commented out wrong variable doh... * prototype of shadow casters culling * moved code to rendering device, tested on example scene and on sponza, seems ok * added AABox around every drawn mesh and still fails on sponza * removed shadow casters AABox * stable shadows working on sponza and test scene * stable shadowmap * interpolated Percentage Closer Filtering * shadows prototype cleanup * shadow ramblings * merged with dev * cmake attempt * moved imgui to third parties, cmake updated for Engine, RenderingDevice and Game Project. Spams a lot * removed sanity spam * resource cleanup * imguisystem cleanup * imgui world component cleanup * added non windows API attributes to imconfig.h * moved imgui to pch * silencing third party gcc -Wimplicit-fallthrough= * enabled -fPIC (Position Independent Code) for imgui static lib to be included in shared libs * fixing gcc includes * fixing cgg includes * made includes same as in GLTextureDeviceProxy * removed commented out code * ImguiUpdate is now in System, Imgui context creation moved to ImguiSystem constructor, removed Resource and WorldComponent * moved Imgui to Engine subdirectory * Added clipboard support, tweaked mouse handling and input that can be consumed by any system * added UTF8 char input from keyboard keys * fixing input consumption (works on all widowns: engine and game) * added output queue and events handled by SDL * CI fix * CI fix and cleanup * CI fix * CI fix * fix * Moved getting axis vector from MovementSystem to EntityTransform * shadowmap ortho projection has correct direction based on dir light forward vector * shadowmap projection in [0, 1] range * stable shadows on frustum transform and properties change * shadows prototype with tight bound around frustum and ortho projection in [0, 1] on Z * more changes * tweaks to AABB intersection tests * added tweakable shadow bias and glPolygonOffset * working evsm2 and evsm4 * shadow projection working with shadow bounds extended by vertical shadow casters * some EVSM4 shadows that work * created pass with shadowmap * switched renderer to use Shadommap pass * parametrizing shadow types * EVSM2 seems to work as well * removed debug light parameters, shadowmap pass has pcf and evsm versions * removed debug variables from light and camera components * CI fix * CI fix * CI fix * CI fix * CI Fix * debug rendering cleanup * cleanup and comments * cleanup * compilation fix * cleanup * cleanup * ignore shader dump extension * fix * Added comment for .dump file extention * cleanup * AABB unit tests * CI fix, tabbed documents * CI fix, tabbed documents * review fix, tabbed code * assert and nullptr fix * None RenderingSettingsComponent fix * review fix * review fix, updated includes * review fix, removed commented code * magic variable moved to constant * CI fix * CI fix
1 parent 36c0a14 commit ee36b8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1412
-565
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# Engine specific files
88
console.log
9+
*.dump # shader content dumps for debuging
910

1011
# Engine local configurations
1112
AssetsPathConfig.json
@@ -69,4 +70,3 @@ xcuserdata/
6970

7071
# Exculdes from ignore
7172
!**/ThirdParty/**
72-

PolyEngine/Core/Src/Math/AABox.cpp

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
using namespace Poly;
66

77
//------------------------------------------------------------------------------
8-
AABox::AABox(const Vector& position, const Vector& size)
9-
: Pos(position), Size(size)
8+
AABox::AABox(const Vector& min, const Vector& size)
9+
: Min(min), Size(size)
1010
{
1111
ASSERTE(Size.X >= 0 && Size.Y >= 0 && Size.Z >= 0, "Invalid aabox size!");
1212
}
1313

1414
//------------------------------------------------------------------------------
1515
AABox AABox::GetIntersectionVolume(const AABox& rhs) const
1616
{
17-
const float r1MinX = std::min(Pos.X, Pos.X + Size.X);
18-
const float r1MaxX = std::max(Pos.X, Pos.X + Size.X);
19-
const float r1MinY = std::min(Pos.Y, Pos.Y + Size.Y);
20-
const float r1MaxY = std::max(Pos.Y, Pos.Y + Size.Y);
21-
const float r1MinZ = std::min(Pos.Z, Pos.Z + Size.Z);
22-
const float r1MaxZ = std::max(Pos.Z, Pos.Z + Size.Z);
17+
const float r1MinX = std::min(Min.X, Min.X + Size.X);
18+
const float r1MaxX = std::max(Min.X, Min.X + Size.X);
19+
const float r1MinY = std::min(Min.Y, Min.Y + Size.Y);
20+
const float r1MaxY = std::max(Min.Y, Min.Y + Size.Y);
21+
const float r1MinZ = std::min(Min.Z, Min.Z + Size.Z);
22+
const float r1MaxZ = std::max(Min.Z, Min.Z + Size.Z);
2323

24-
const float r2MinX = std::min(rhs.Pos.X, rhs.Pos.X + rhs.Size.X);
25-
const float r2MaxX = std::max(rhs.Pos.X, rhs.Pos.X + rhs.Size.X);
26-
const float r2MinY = std::min(rhs.Pos.Y, rhs.Pos.Y + rhs.Size.Y);
27-
const float r2MaxY = std::max(rhs.Pos.Y, rhs.Pos.Y + rhs.Size.Y);
28-
const float r2MinZ = std::min(rhs.Pos.Z, rhs.Pos.Z + rhs.Size.Z);
29-
const float r2MaxZ = std::max(rhs.Pos.Z, rhs.Pos.Z + rhs.Size.Z);
24+
const float r2MinX = std::min(rhs.Min.X, rhs.Min.X + rhs.Size.X);
25+
const float r2MaxX = std::max(rhs.Min.X, rhs.Min.X + rhs.Size.X);
26+
const float r2MinY = std::min(rhs.Min.Y, rhs.Min.Y + rhs.Size.Y);
27+
const float r2MaxY = std::max(rhs.Min.Y, rhs.Min.Y + rhs.Size.Y);
28+
const float r2MinZ = std::min(rhs.Min.Z, rhs.Min.Z + rhs.Size.Z);
29+
const float r2MaxZ = std::max(rhs.Min.Z, rhs.Min.Z + rhs.Size.Z);
3030

3131
const float interLeft = std::max(r1MinX, r2MinX);
3232
const float interTop = std::max(r1MinY, r2MinY);
@@ -36,9 +36,7 @@ AABox AABox::GetIntersectionVolume(const AABox& rhs) const
3636
const float interDown = std::min(r1MaxZ, r2MaxZ);
3737

3838
if ((interLeft < interRight) && (interTop < interBottom) && (interUp < interDown))
39-
{
4039
return AABox(Vector(interLeft, interTop, interUp), Vector(interRight - interLeft, interBottom - interTop, interDown - interUp));
41-
}
4240
else
4341
return AABox(Vector::ZERO, Vector::ZERO);
4442
}
@@ -47,34 +45,34 @@ std::array<Vector, 8> Poly::AABox::GetVertices() const
4745
{
4846
SILENCE_CLANG_WARNING(-Wmissing-braces, "Everything is ok here.");
4947
return std::array<Vector, 8>{
50-
Pos,
51-
Pos + Vector(Size.X, 0, 0),
52-
Pos + Vector(Size.X, Size.Y, 0),
53-
Pos + Vector(Size.X, Size.Y, Size.Z),
54-
Pos + Vector(0, Size.Y, Size.Z),
55-
Pos + Vector(0, 0, Size.Z),
56-
Pos + Vector(0, Size.Y, 0),
57-
Pos + Vector(Size.X, 0, Size.Z)
48+
Min,
49+
Min + Vector(Size.X, 0, 0),
50+
Min + Vector(Size.X, Size.Y, 0),
51+
Min + Vector(Size.X, Size.Y, Size.Z),
52+
Min + Vector(0, Size.Y, Size.Z),
53+
Min + Vector(0, 0, Size.Z),
54+
Min + Vector(0, Size.Y, 0),
55+
Min + Vector(Size.X, 0, Size.Z)
5856
};
5957
UNSILENCE_CLANG_WARNING();
6058
}
6159

6260
AABox Poly::AABox::GetTransformed(const Matrix& transform) const
6361
{
64-
Vector min = transform * Pos;
62+
Vector min = transform * Min;
6563
Vector max = min;
6664

6765
// Gather other 7 points
6866
SILENCE_CLANG_WARNING(-Wmissing-braces, "Everything is ok here.");
6967
std::array<Vector, 7> points =
7068
{
71-
Pos + Vector(Size.X, 0, 0),
72-
Pos + Vector(Size.X, Size.Y, 0),
73-
Pos + Vector(Size.X, Size.Y, Size.Z),
74-
Pos + Vector(0, Size.Y, Size.Z),
75-
Pos + Vector(0, 0, Size.Z),
76-
Pos + Vector(0, Size.Y, 0),
77-
Pos + Vector(Size.X, 0, Size.Z)
69+
Min + Vector(Size.X, 0, 0),
70+
Min + Vector(Size.X, Size.Y, 0),
71+
Min + Vector(Size.X, Size.Y, Size.Z),
72+
Min + Vector(0, Size.Y, Size.Z),
73+
Min + Vector(0, 0, Size.Z),
74+
Min + Vector(0, Size.Y, 0),
75+
Min + Vector(Size.X, 0, Size.Z)
7876
};
7977
UNSILENCE_CLANG_WARNING();
8078

@@ -93,7 +91,16 @@ Poly::AABox& Poly::AABox::Expand(const AABox& rhs)
9391
{
9492
Vector min = Vector::Min(GetMin(), rhs.GetMin());
9593
Vector max = Vector::Max(GetMax(), rhs.GetMax());
96-
Pos = min;
94+
Min = min;
95+
Size = max - min;
96+
return *this;
97+
}
98+
99+
Poly::AABox& Poly::AABox::Expand(const Vector& rhs)
100+
{
101+
Vector min = Vector::Min(GetMin(), rhs);
102+
Vector max = Vector::Max(GetMax(), rhs);
103+
Min = min;
97104
Size = max - min;
98105
return *this;
99106
}
@@ -102,6 +109,6 @@ Poly::AABox& Poly::AABox::Expand(const AABox& rhs)
102109
namespace Poly {
103110
std::ostream & operator<<(std::ostream& stream, const AABox& rect)
104111
{
105-
return stream << "AABox[Pos: " << rect.Pos << " Size: " << rect.Size << " ]";
112+
return stream << "AABox[Pos: " << rect.Min << " Size: " << rect.Size << " ]";
106113
}
107114
}

PolyEngine/Core/Src/Math/AABox.hpp

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,92 @@ namespace Poly {
1212
{
1313
public:
1414

15-
/// <summary>Constructor from position and size.</summary>
16-
/// <param name="position">Position of the min point of the box.</param>
15+
/// <summary>Constructor from min and size.</summary>
16+
/// <param name="min">Position of the min point of the box.</param>
1717
/// <param name="size">Size (extent) of the box in each of the directions.</param>
18-
AABox(const Vector& position, const Vector& size);
18+
AABox(const Vector& min, const Vector& size);
1919
AABox() = default;
2020

2121
/// <summary>Calculates center of the box.</summary>
2222
/// <returns>Center of the box.</returns>
23-
Vector GetCenter() const { return Pos + (Size * 0.5f); }
23+
Vector GetCenter() const { return Min + (Size * 0.5f); }
2424

2525
/// <summary>Returns the min point of the box. (posiiton)</summary>
2626
/// <returns>Min point of the box.</returns>
27-
const Vector& GetMin() const { return Pos; }
27+
const Vector& GetMin() const { return Min; }
2828

2929
/// <summary>Returns the max point of the box.</summary>
3030
/// <returns>Max point of the box.</returns>
31-
Vector GetMax() const { return Pos + Size; }
31+
Vector GetMax() const { return Min + Size; }
3232

3333
/// <summary>Returns Size of the box in all three dimensions.</summary>
3434
/// <returns>Size of the box. (extent)</returns>
3535
const Vector& GetSize() const { return Size; }
3636

3737
/// <summary>Sets position of the box.</summary>
3838
/// <param name="pos">New position</param>
39-
void SetPosition(const Vector& pos) { Pos = pos; }
39+
void SetMin(const Vector& pos) { Min = pos; }
4040

4141
/// <summary>Sets size of the box.</summary>
4242
/// <param name="size">New size</param>
4343
void SetSize(const Vector& size) { Size = size; }
4444

45+
/// <summary>Checks whether a given AABox is interecting with this AABox.</summary>
46+
/// <param name="rhs">Other box.</param>
47+
/// <see cref="AABox.Contains()"/>
48+
inline bool Intersects(const AABox& rhs) const
49+
{
50+
return (abs(Min.X - rhs.Min.X) * 2.0f < (Size.X + rhs.Size.X))
51+
&& (abs(Min.Y - rhs.Min.Y) * 2.0f < (Size.Y + rhs.Size.Y))
52+
&& (abs(Min.Z - rhs.Min.Z) * 2.0f < (Size.Z + rhs.Size.Z));
53+
}
54+
4555
/// <summary>Checks whether this AABox contains a given point.</summary>
4656
/// <param name="point">Point to be checked.</param>
4757
/// <see cref="AABox.IsCollidingWith()"/>
4858
inline bool Contains(const Vector& point) const
4959
{
50-
return point.X >= Pos.X && point.X <= (Pos.X + Size.X)
51-
&& point.Y >= Pos.Y && point.Y <= (Pos.Y + Size.Y)
52-
&& point.Z >= Pos.Z && point.Z <= (Pos.Z + Size.Z);
60+
return point.X >= Min.X && point.X <= (Min.X + Size.X)
61+
&& point.Y >= Min.Y && point.Y <= (Min.Y + Size.Y)
62+
&& point.Z >= Min.Z && point.Z <= (Min.Z + Size.Z);
5363
}
5464

55-
/// <summary>Checks whether a given AABox is interecting with this AABox.</summary>
65+
/// <summary>Checks whether a given AABox contains this AABox on all axes.</summary>
5666
/// <param name="rhs">Other box.</param>
57-
/// <see cref="AABox.Contains()"/>
58-
inline bool Intersects(const AABox& rhs) const
59-
{
60-
return (abs(Pos.X - rhs.Pos.X) * 2 < (Size.X + rhs.Size.X))
61-
&& (abs(Pos.Y - rhs.Pos.Y) * 2 < (Size.Y + rhs.Size.Y))
62-
&& (abs(Pos.Z - rhs.Pos.Z) * 2 < (Size.Z + rhs.Size.Z));
67+
inline bool Contains(const AABox& rhs) const
68+
{
69+
return ContainsX(rhs) && ContainsY(rhs) && ContainsZ(rhs);
70+
}
71+
72+
/// <summary>Checks whether a given AABox contains this AABox on XY plane.</summary>
73+
/// <param name="rhs">Other box.</param>
74+
inline bool ContainsXY(const AABox& rhs) const
75+
{
76+
return ContainsX(rhs) && ContainsY(rhs);
77+
}
78+
79+
/// <summary>Checks whether a given AABox contains this AABox on X axis.</summary>
80+
/// <param name="rhs">Other box.</param>
81+
inline bool ContainsX(const AABox& rhs) const
82+
{
83+
return (Min.X <= rhs.Min.X + rhs.Size.X)
84+
&& (Min.X + Size.X >= rhs.Min.X);
85+
}
86+
87+
/// <summary>Checks whether a given AABox contains this AABox on Y axis.</summary>
88+
/// <param name="rhs">Other box.</param>
89+
inline bool ContainsY(const AABox& rhs) const
90+
{
91+
return (Min.Y <= rhs.Min.Y + rhs.Size.Y)
92+
&& (Min.Y + Size.Y >= rhs.Min.Y);
93+
}
94+
95+
/// <summary>Checks whether a given AABox contains this AABox on Z axis.</summary>
96+
/// <param name="rhs">Other box.</param>
97+
inline bool ContainsZ(const AABox& rhs) const
98+
{
99+
return (Min.Z <= rhs.Min.Z + rhs.Size.Z)
100+
&& (Min.Z + Size.Z >= rhs.Min.Z);
63101
}
64102

65103
std::array<Vector, 8> GetVertices() const;
@@ -74,9 +112,11 @@ namespace Poly {
74112

75113
AABox& Expand(const AABox& rhs);
76114

115+
AABox& Expand(const Vector& rhs);
116+
77117
CORE_DLLEXPORT friend std::ostream& operator<< (std::ostream& stream, const AABox& color);
78118
private:
79-
Vector Pos;
119+
Vector Min;
80120
Vector Size;
81121
};
82122
}

PolyEngine/Core/Src/Math/Matrix.cpp

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,27 @@ Matrix& Matrix::SetScale(const Vector& scale) {
224224
//------------------------------------------------------------------------------
225225
Matrix& Matrix::SetLookAt(const Vector& pos, const Vector& lookAt, const Vector& up)
226226
{
227+
const Vector front((lookAt - pos).GetNormalized());
228+
const Vector side(front.Cross(up).GetNormalized());
229+
const Vector newUp(side.Cross(front));
230+
227231
SetIdentity();
232+
// same as GLM_FUNC_QUALIFIER mat<4, 4, T, Q> lookAtRH(vec<3, T, Q> const& eye, vec<3, T, Q> const& center, vec<3, T, Q> const& up)
233+
Data[0] = side.X; // glm[0][0]
234+
Data[1] = side.Y; // glm[1][0]
235+
Data[2] = side.Z; // glm[2][0]
228236

229-
Vector zAxis = pos - lookAt;
230-
zAxis.Normalize();
231-
Vector xAxis = up.Cross(zAxis);
232-
xAxis.Normalize();
233-
Vector yAxis = zAxis.Cross(xAxis);
237+
Data[4] = newUp.X; // glm[0][1]
238+
Data[5] = newUp.Y; // glm[1][1]
239+
Data[6] = newUp.Z; // glm[2][1]
234240

235-
Data[0] = xAxis.X;
236-
Data[1] = yAxis.X;
237-
Data[2] = zAxis.X;
241+
Data[8] = -front.X; // glm[0][2]
242+
Data[9] = -front.Y; // glm[1][2]
243+
Data[10] = -front.Z; // glm[2][2]
238244

239-
Data[4] = xAxis.Y;
240-
Data[5] = yAxis.Y;
241-
Data[6] = zAxis.Y;
242-
243-
Data[8] = xAxis.Z;
244-
Data[9] = yAxis.Z;
245-
Data[10] = zAxis.Z;
245+
Data[12] = -(side.Dot(pos)); // glm[3][0]
246+
Data[13] = -(newUp.Dot(pos)); // glm[3][1]
247+
Data[14] = front.Dot(pos); // glm[3][2]
246248

247249
return *this;
248250
}
@@ -276,27 +278,47 @@ Matrix& Poly::Matrix::SetPerspective(Angle fov, float aspect, float near, float
276278
}
277279

278280
//------------------------------------------------------------------------------
279-
Matrix& Poly::Matrix::SetOrthographic(float bottom, float top, float left, float right, float near, float far)
281+
Matrix& Poly::Matrix::SetOrthographicZO(float bottom, float top, float left, float right, float near, float far)
280282
{
281-
Data[0] = 2.0f / (right - left);
282-
Data[1] = 0;
283-
Data[2] = 0;
284-
Data[3] = -(right + left) / (right - left);
285-
286-
Data[4] = 0;
287-
Data[5] = 2.0f / (top - bottom);
288-
Data[6] = 0;
289-
Data[7] = -(top + bottom) / (top - bottom);
283+
SetIdentity();
284+
// same as orthoRH_ZO: GLM_RIGHT_HANDED && GLM_DEPTH_ZERO_TO_ONE
285+
Data[0] = 2.0f / (right - left); // glm[0][0]
286+
Data[1] = 0.0f;
287+
Data[2] = 0.0f;
288+
Data[3] = -(right + left) / (right - left); // glm[3][0]
289+
290+
Data[4] = 0.0f;
291+
Data[5] = 2.0f / (top - bottom); // glm[1][1]
292+
Data[6] = 0.0f;
293+
Data[7] = -(top + bottom) / (top - bottom); // glm[3][1]
294+
295+
Data[8] = 0.0f;
296+
Data[9] = 0.0f;
297+
Data[10] = 1.0f / (far - near); // glm[2][2]
298+
Data[11] = near / (far - near); // glm[3][2]
290299

291-
Data[8] = 0;
292-
Data[9] = 0;
293-
Data[10] = 2.0f / (far - near);
294-
Data[11] = -(far + near) / (far - near);
300+
return *this;
301+
}
295302

296-
Data[12] = 0;
297-
Data[13] = 0;
298-
Data[14] = 0;
299-
Data[15] = 1;
303+
//------------------------------------------------------------------------------
304+
Matrix& Poly::Matrix::SetOrthographic(float bottom, float top, float left, float right, float near, float far)
305+
{
306+
SetIdentity();
307+
// same as orthoRH_NO: GLM_RIGHT_HANDED && GLM_DEPTH_NEGATIVE_ONE_TO_ONE
308+
Data[0] = 2.0f / (right - left); // glm[0][0]
309+
Data[1] = 0.0f;
310+
Data[2] = 0.0f;
311+
Data[3] = -(right + left) / (right - left); // glm[3][0]
312+
313+
Data[4] = 0.0f;
314+
Data[5] = 2.0f / (top - bottom); // glm[1][1]
315+
Data[6] = 0.0f;
316+
Data[7] = -(top + bottom) / (top - bottom); // glm[3][1]
317+
318+
Data[8] = 0.0f;
319+
Data[9] = 0.0f;
320+
Data[10] = 2.0f / (far - near); // glm[2][2]
321+
Data[11] = -(far + near) / (far - near); // glm[3][2]
300322

301323
return *this;
302324
}

PolyEngine/Core/Src/Math/Matrix.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ namespace Poly
9292
/// <returns>Reference to itself.</returns>
9393
Matrix& SetPerspective(Angle fov, float aspect, float near, float far);
9494

95+
/// <summary>Initializes matrix with orthographics projection in range of [0, 1]</summary>
96+
/// <param name="top">Top dimension.</param>
97+
/// <param name="bottom">Bottom dimension.</param>
98+
/// <param name="left">Left dimension.</param>
99+
/// <param name="right">Right dimension</param>
100+
/// <param name="near">Near Z plane.</param>
101+
/// <param name="far">Far Z plane.</param>
102+
/// <returns>Reference to itself.</returns>
103+
Matrix& SetOrthographicZO(float top, float bottom, float left, float right, float near, float far);
95104

96105
/// <summary>Initializes matrix with orthographics projection</summary>
97106
/// <param name="top">Top dimension.</param>

0 commit comments

Comments
 (0)