Skip to content

Commit 1ca3722

Browse files
Lighting in place 🎉
1 parent afc6d57 commit 1ca3722

Some content is hidden

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

44 files changed

+1434
-868
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#version 460
2+
#extension GL_GOOGLE_include_directive : require
3+
#include "fragment_common.glsl"
4+
5+
layout (location = 0) in vec2 TexCoord;
6+
layout (location = 1) in vec4 ViewPos;
7+
8+
layout (std140, set = 0, binding = 6) readonly buffer DirectionalLightSB
9+
{
10+
uint Count;
11+
DirectionalLight Data[];
12+
} DirectionalLightBuffer;
13+
14+
layout (std140, set = 0, binding = 7) readonly buffer PointLightSB
15+
{
16+
uint Count;
17+
PointLight Data[];
18+
} PointLightBuffer;
19+
20+
layout (std140, set = 0, binding = 8) readonly buffer SpotLightSB
21+
{
22+
uint Count;
23+
SpotLight Data[];
24+
} SpotLightBuffer;
25+
26+
layout (set = 0, binding = 10) uniform sampler2D AlbedoSampler;
27+
layout (set = 0, binding = 11) uniform sampler2D PositionSampler;
28+
layout (set = 0, binding = 12) uniform sampler2D NormalSampler;
29+
layout (set = 0, binding = 13) uniform sampler2D SpecularSampler;
30+
31+
32+
layout (location = 0) out vec4 OutColor;
33+
34+
35+
vec3 ComputeDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir, vec3 albedoMap, vec3 specularMap)
36+
{
37+
vec3 direction = light.Direction.xyz;
38+
39+
vec3 lightDir = normalize(direction);
40+
vec3 ambient = 0.1 * light.Ambient.xyz;
41+
42+
float diff = max(dot(normal, lightDir), 0.0);
43+
vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
44+
45+
vec3 reflectDir = reflect(-lightDir, normal);
46+
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16);
47+
vec3 specular = 0.5 * spec * light.Specular.xyz * specularMap;
48+
49+
return vec3(ambient + diffuse + specular);
50+
}
51+
52+
53+
void main()
54+
{
55+
vec3 norm = texture( NormalSampler, TexCoord).rgb;
56+
vec4 fragPos = texture( PositionSampler, TexCoord);
57+
vec3 albedo = texture( AlbedoSampler, TexCoord).rgb;
58+
vec3 specular = texture( SpecularSampler, TexCoord).rgb;
59+
60+
vec3 viewDir = normalize( ViewPos.xyz - fragPos.xyz);
61+
62+
//Computing Directional Lights
63+
vec3 lighting = vec3(0.0);
64+
for (uint i = 0; i < DirectionalLightBuffer.Count; ++i)
65+
{
66+
DirectionalLight directionalLight = DirectionalLightBuffer.Data[i];
67+
lighting += ComputeDirectionalLight(directionalLight, norm, viewDir, albedo, specular);
68+
}
69+
70+
OutColor = vec4(lighting, 1.0);
71+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#version 460
2+
#extension GL_GOOGLE_include_directive : require
3+
#include "vertex_common.glsl"
4+
5+
layout (location = 0) out vec2 TexCoord;
6+
layout (location = 1) out vec4 ViewPos;
7+
8+
void main()
9+
{
10+
DrawDataView dataView = GetDrawDataView();
11+
12+
vec4 worldPos = dataView.Transform * dataView.Vertex;
13+
ViewPos = Camera.Position;
14+
gl_Position = Camera.Projection * Camera.View * worldPos;
15+
// Convert gl_Position from NDC [-1, 1] to texture coordinates [0, 1]
16+
TexCoord = (gl_Position.xy / gl_Position.w) * 0.5 + 0.5;
17+
}

Resources/Shaders/fragment_common.glsl

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,65 @@
33

44
struct MaterialData
55
{
6-
vec4 AmbientColor;
7-
vec4 EmissiveColor;
8-
vec4 AlbedoColor;
9-
vec4 DiffuseColor;
10-
vec4 RoughnessColor;
11-
12-
float TransparencyFactor;
13-
float MetallicFactor;
14-
float AlphaTest;
15-
16-
uint64_t EmissiveTextureMap;
17-
uint64_t AlbedoTextureMap;
18-
uint64_t NormalTextureMap;
19-
uint64_t OpacityTextureMap;
6+
vec4 Ambient;
7+
vec4 Emissive;
8+
vec4 Albedo;
9+
vec4 Specular;
10+
vec4 Roughness;
11+
vec4 Factors; // {x : transparency, y : Metallic, z : AlphaTest, w : _padding}
12+
13+
uint64_t EmissiveMap;
14+
uint64_t AlbedoMap;
15+
uint64_t SpecularMap;
16+
uint64_t NormalMap;
17+
uint64_t OpacityMap;
18+
uint64_t _padding;
2019
};
2120

21+
struct DirectionalLight
22+
{
23+
vec4 Direction;
24+
vec4 Ambient;
25+
vec4 Diffuse;
26+
vec4 Specular;
27+
};
28+
29+
struct PointLight
30+
{
31+
vec4 Position;
32+
vec4 Ambient;
33+
vec4 Diffuse;
34+
vec4 Specular;
35+
36+
float Constant;
37+
float Linear;
38+
float Quadratic;
39+
float _padding;
40+
};
41+
42+
struct SpotLight
43+
{
44+
vec4 Position;
45+
vec4 Direction;
46+
vec4 Ambient;
47+
vec4 Diffuse;
48+
vec4 Specular;
49+
50+
float CutOff;
51+
vec3 _padding;
52+
};
53+
54+
#define INVALID_MAP_HANDLE 0xFFFFFFFFu
55+
56+
layout(std140, set = 0, binding = 5) readonly buffer MatSB { MaterialData Data[]; } MaterialDataBuffer;
57+
layout(set = 0, binding = 9) uniform sampler2D TextureArray[];
58+
59+
MaterialData FetchMaterial(uint dataIndex)
60+
{
61+
return MaterialDataBuffer.Data[dataIndex];
62+
}
63+
64+
2265
// http://www.thetenthplanet.de/archives/1180
2366
// modified to fix handedness of the resulting cotangent frame
2467
mat3 cotangentFrame( vec3 N, vec3 p, vec2 uv )
@@ -73,13 +116,3 @@ void runAlphaTest(float alpha, float alphaThreshold)
73116
discard;
74117
}
75118
}
76-
77-
#define INVALID_TEXTURE_INDEX 0xFFFFFFFFu
78-
79-
layout(set = 0, binding = 5) readonly buffer MatSB { MaterialData Data[]; } MaterialDataBuffer;
80-
layout(set = 0, binding = 9) uniform sampler2D TextureArray[];
81-
82-
MaterialData FetchMaterial(uint dataIndex)
83-
{
84-
return MaterialDataBuffer.Data[dataIndex];
85-
}

Resources/Shaders/g_buffer.frag

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,40 @@
22
#extension GL_GOOGLE_include_directive : require
33
#include "fragment_common.glsl"
44

5-
layout (location = 0) in vec3 FragmentPosition;
5+
layout (location = 0) in vec2 TexCoord;
66
layout (location = 1) in vec3 WorldNormal;
7-
layout (location = 2) in vec2 TextureCoord;
7+
layout (location = 2) in vec4 FragPos;
88
layout (location = 3) in flat uint MaterialIdx;
99

10-
layout (location = 0) out vec4 OutAlbedoColor;
10+
layout (location = 0) out vec4 OutAlbedo;
1111
layout (location = 1) out vec4 OutSpecular;
1212
layout (location = 2) out vec3 OutNormal;
13-
layout (location = 3) out vec3 OutPosition;
13+
layout (location = 3) out vec4 OutPosition;
1414

1515
void main()
1616
{
1717
MaterialData material = FetchMaterial(MaterialIdx);
1818

19-
OutNormal = normalize(WorldNormal);
20-
OutPosition = FragmentPosition;
21-
OutSpecular = vec4(1.0);
19+
OutNormal = normalize(WorldNormal);
20+
OutSpecular = material.Specular;
21+
OutAlbedo = material.Albedo;
22+
OutPosition = FragPos;
2223

23-
if (material.AlbedoTextureMap < INVALID_TEXTURE_INDEX)
24+
if (material.AlbedoMap < INVALID_MAP_HANDLE)
2425
{
25-
uint texId = uint(material.AlbedoTextureMap);
26-
OutAlbedoColor = texture( TextureArray[nonuniformEXT(texId)], TextureCoord);
26+
uint texId = uint(material.AlbedoMap);
27+
OutAlbedo = texture( TextureArray[nonuniformEXT(texId)], TexCoord );
28+
}
29+
30+
if(material.SpecularMap < INVALID_MAP_HANDLE)
31+
{
32+
uint texId = uint(material.SpecularMap);
33+
OutSpecular = texture( TextureArray[nonuniformEXT(texId)], TexCoord );
34+
}
35+
36+
if(material.NormalMap < INVALID_MAP_HANDLE)
37+
{
38+
uint texId = uint(material.NormalMap);
39+
OutNormal = texture( TextureArray[nonuniformEXT(texId)], TexCoord ).rgb;
2740
}
2841
}

Resources/Shaders/g_buffer.vert

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
#extension GL_GOOGLE_include_directive : require
33
#include "vertex_common.glsl"
44

5-
6-
layout (location = 0) out vec3 FragmentPosition;
5+
layout (location = 0) out vec2 TexCoord;
76
layout (location = 1) out vec3 WorldNormal;
8-
layout (location = 2) out vec2 TextureCoord;
7+
layout (location = 2) out vec4 FragPos;
98
layout (location = 3) out flat uint MaterialIdx;
10-
layout (location = 4) out vec4 ViewPosition;
119

1210
void main()
1311
{
1412
DrawDataView dataView = GetDrawDataView();
1513

16-
vec4 worldPosition = dataView.Transform * dataView.Vertex;
17-
FragmentPosition = worldPosition.xyz;
18-
14+
vec4 worldPos = dataView.Transform * dataView.Vertex;
1915
WorldNormal = transpose(inverse(mat3(dataView.Transform))) * dataView.Normal;
20-
TextureCoord = dataView.TexCoord;
16+
TexCoord = dataView.TexCoord;
2117
MaterialIdx = dataView.MaterialId;
22-
ViewPosition = Camera.Position;
23-
24-
gl_Position = Camera.Projection * Camera.View * worldPosition;
18+
FragPos = worldPos;
19+
gl_Position = Camera.Projection * Camera.View * worldPos;
2520
}

Resources/Shaders/infinite_grid.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ layout (location = 1) out float scaleFactor;
88

99
void main()
1010
{
11-
scaleFactor = 300.0;
11+
scaleFactor = 80.0;
1212

1313
DrawDataView dataView = GetDrawDataView();
1414
vec3 posScale = vec3((dataView.Vertex * scaleFactor).xyz);

Tetragrama/src/Components/HierarchyViewUIComponent.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Tetragrama::Components
1313
{
1414
HierarchyViewUIComponent::HierarchyViewUIComponent(std::string_view name, bool visibility) : UIComponent(name, visibility, false)
1515
{
16-
m_node_flag = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth/* | ImGuiTreeNodeFlags_DefaultOpen*/;
16+
m_node_flag = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth /* | ImGuiTreeNodeFlags_DefaultOpen*/;
1717
}
1818

1919
HierarchyViewUIComponent::~HierarchyViewUIComponent() {}
@@ -78,6 +78,7 @@ namespace Tetragrama::Components
7878
void HierarchyViewUIComponent::RenderTreeNodes()
7979
{
8080
auto root_nodes = GraphicScene::GetRootSceneNodes();
81+
8182
for (int node : root_nodes)
8283
{
8384
RenderSceneNodeTree(node);
@@ -135,7 +136,7 @@ namespace Tetragrama::Components
135136
if (ImGuizmo::IsUsing())
136137
{
137138
// ZEngine::Maths::Vector3 translation, rotation, scale;
138-
// ZEngine::Maths::DecomposeTransformComponent(transform, translation, rotation, scale);
139+
//ZEngine::Maths::DecomposeTransformComponent(transform, translation, rotation, scale);
139140

140141
// entity_transform_component.SetPosition(translation);
141142
// entity_transform_component.SetScaleSize(scale);
@@ -144,7 +145,7 @@ namespace Tetragrama::Components
144145
}
145146
}
146147

147-
void HierarchyViewUIComponent::RenderSceneNodeTree(int32_t node_identifier)
148+
void HierarchyViewUIComponent::RenderSceneNodeTree(int node_identifier)
148149
{
149150
if (node_identifier < 0)
150151
{
@@ -162,6 +163,10 @@ namespace Tetragrama::Components
162163
if (ImGui::IsItemClicked())
163164
{
164165
m_selected_node_identifier = node_identifier;
166+
167+
auto entity = GraphicScene::GetSceneNodeEntityWrapper(m_selected_node_identifier);
168+
Messengers::IMessenger::SendAsync<ZEngine::Components::UI::UIComponent, Messengers::GenericMessage<SceneEntity>>(
169+
EDITOR_COMPONENT_HIERARCHYVIEW_NODE_SELECTED, Messengers::GenericMessage<SceneEntity>{std::move(entity)});
165170
}
166171

167172
if (is_node_opened)
@@ -174,6 +179,7 @@ namespace Tetragrama::Components
174179
{
175180
if (ImGui::MenuItem("Create Empty child"))
176181
{
182+
GraphicScene::CreateEntityAsync("Empty Entity", m_selected_node_identifier, node_hierarchy.DepthLevel + 1);
177183
}
178184

179185
if (ImGui::MenuItem("Rename"))

Tetragrama/src/Components/HierarchyViewUIComponent.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include <Message.h>
66
#include <EditorCameraController.h>
77

8-
namespace Tetragrama::Components {
8+
namespace Tetragrama::Components
9+
{
910
class HierarchyViewUIComponent : public ZEngine::Components::UI::UIComponent
1011
{
1112
public:
@@ -17,13 +18,10 @@ namespace Tetragrama::Components {
1718

1819
void RenderTreeNodes();
1920
void RenderGuizmo();
21+
void RenderSceneNodeTree(int node_identifier);
2022

21-
public:
2223
std::future<void> EditorCameraAvailableMessageHandlerAsync(Messengers::GenericMessage<ZEngine::Ref<EditorCameraController>>&);
2324

24-
protected:
25-
void RenderSceneNodeTree(int32_t node_identifier);
26-
2725
private:
2826
ImGuiTreeNodeFlags m_node_flag;
2927
bool m_is_node_opened{false};

0 commit comments

Comments
 (0)