Skip to content

Commit e6c64e4

Browse files
committed
Screen Space Decals
1 parent 7b80333 commit e6c64e4

9 files changed

Lines changed: 262 additions & 20 deletions

File tree

3dEngine2.0.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@
182182
</ItemGroup>
183183
<ItemGroup>
184184
<None Include="Assets\Saves\mainScene.json" />
185+
<None Include="Assets\Shaders\Decal\decal.frag" />
186+
<None Include="Assets\Shaders\Decal\decal.vert" />
185187
<None Include="Assets\Shaders\Deffered\geomerty.frag" />
186188
<None Include="Assets\Shaders\Deffered\geomerty.vert" />
187189
<None Include="Assets\Shaders\Editor\Editor.frag" />

3dEngine2.0.vcxproj.filters

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@
237237
<None Include="Assets\Shaders\Shadow\depth.vert" />
238238
<None Include="Assets\Shaders\Shadow\depth.frag" />
239239
<None Include="Assets\Shaders\Shadow\depth.gs" />
240+
<None Include="Assets\Shaders\Decal\decal.vert" />
241+
<None Include="Assets\Shaders\Decal\decal.frag" />
240242
</ItemGroup>
241243
<ItemGroup>
242244
<Text Include="todo.txt" />

Assets/Shaders/Decal/decal.frag

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#version 330 core
2+
layout (location = 2) out vec4 gAlbedoSpec; // Stores both albedo and specular in one vector
3+
4+
5+
in vec4 posFS;
6+
in vec4 posW;
7+
in vec2 uvFS;
8+
9+
10+
11+
uniform sampler2D gPostion; // View-space position
12+
uniform sampler2D gAlbeido;
13+
uniform sampler2D gNormal;
14+
15+
uniform sampler2D gDepth;
16+
uniform sampler2D decalTexture;
17+
18+
19+
uniform mat4 V; // View matrix
20+
uniform mat4 M; // Model matrix
21+
uniform mat4 P; // Projection matrix
22+
uniform mat4 inverseV;
23+
uniform mat4 inverseP;
24+
uniform mat4 inverseM;
25+
26+
27+
uniform vec2 resolution;
28+
uniform vec3 size;
29+
in vec2 TexCoords;
30+
in vec3 DecalCenterPosition;
31+
32+
33+
void main () {
34+
35+
36+
vec2 depthCoords = gl_FragCoord.xy / vec2(resolution.x, resolution.y);
37+
float z = texture(gDepth, vec2(depthCoords.s, depthCoords.t)).x * 2.0f - 1.0f;
38+
vec3 normal = normalize(texture(gNormal, depthCoords).xyz); // Assuming you have a normal G-buffer
39+
40+
vec4 clipSpacePosition = vec4(vec2(depthCoords.s, depthCoords.t) * 2.0 - 1.0, z, 1.0);
41+
42+
//clipSpacePosition = vec4(vec2(depthCoords.s * 2.0 - 1.0, ((depthCoords.t * 2 + -1) * 2.0 - 1.0) ), z, 1.0);
43+
vec4 viewSpacePosition = inverseP * clipSpacePosition;
44+
45+
viewSpacePosition /= viewSpacePosition.w;
46+
47+
vec4 worldSpacePosition = inverseV * viewSpacePosition;
48+
vec3 FragPos = worldSpacePosition.xyz;
49+
50+
vec4 localpos = inverseM * vec4(FragPos,1);
51+
52+
float d = length(FragPos - DecalCenterPosition);
53+
if (d > 0.5)
54+
discard;
55+
else{
56+
57+
58+
59+
// Calculate texture coordinates, adjusting for aspect ratio
60+
vec2 textureCoordinate = (localpos.xz) + 0.5;
61+
62+
// Clamp to [0, 1] to avoid repetition
63+
textureCoordinate = clamp(textureCoordinate, 0.0, 1.0);
64+
65+
66+
vec4 gBaseColor = texture( decalTexture, vec2(textureCoordinate.x, textureCoordinate.y));
67+
//gBaseColor = vec4(textureCoordinate.x,textureCoordinate.y,0,1);
68+
gAlbedoSpec = gBaseColor;
69+
70+
}
71+
}

Assets/Shaders/Decal/decal.vert

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#version 330 core
2+
3+
layout(location = 0) in vec3 vertexPosition_modelspace;
4+
layout(location = 1) in vec2 vertexUV;
5+
layout(location = 2) in vec3 vertexNormal_modelspace;
6+
layout(location = 3) in vec3 vertexTangent_modelspace;
7+
layout(location = 4) in vec3 vertexBitangent_modelspace;
8+
9+
out vec4 posFS;
10+
out vec4 posW;
11+
out vec2 uvFS;
12+
13+
out vec3 DecalCenterPosition;
14+
out vec2 TexCoords;
15+
16+
uniform mat4 V; // View matrix
17+
uniform mat4 M; // Model matrix
18+
uniform mat4 P; // Projection matrix
19+
uniform mat3 normalMatrix3;
20+
21+
22+
23+
void main()
24+
{
25+
posW = M * vec4(vertexPosition_modelspace, 1);
26+
//Move position to clip space
27+
posFS = P * V * posW;
28+
uvFS = vertexUV;
29+
gl_Position = posFS;
30+
TexCoords = vertexUV;
31+
32+
DecalCenterPosition = vec3(M[3][0],M[3][1],M[3][2]);
33+
34+
35+
}

Assets/Shaders/Lighting/lighting.frag

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ void main()
177177

178178
// HDR tonemapping
179179
color = color / (color + vec3(1.0));
180+
// color = albedo;
180181
// gamma
181182
FragColor = vec4(color, alpha);
182183
}

Engine/Core/Decal.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Decal::Decal(glm::vec3 position, glm::vec3 normal, glm::vec3 scale, Texture* tex
77

88
glm::vec3 up(0, 1, 0);
99
glm::vec3 rotationAxis = glm::cross(normal, up);
10-
float angle = acos(glm::dot(normal, up));
10+
float angle = acos(glm::dot(normal, up));
1111

1212
transform.position = position + (normal * 0.01f);
1313
transform.rotation = (rotationAxis * -angle);
@@ -61,18 +61,10 @@ void Decal::RenderDecal(GLuint& programID) {
6161
glUseProgram(programID);
6262

6363
if (texture != nullptr) {
64-
glActiveTexture(GL_TEXTURE0);
64+
glActiveTexture(GL_TEXTURE4);
6565
glBindTexture(GL_TEXTURE_2D, texture->GetTexture());
66-
glActiveTexture(GL_TEXTURE1);
67-
glBindTexture(GL_TEXTURE_2D, texture->GetTextureNormal());
68-
glActiveTexture(GL_TEXTURE2);
69-
glBindTexture(GL_TEXTURE_2D, texture->GetTextureRoughness());
70-
glActiveTexture(GL_TEXTURE3);
71-
glBindTexture(GL_TEXTURE_2D, texture->GetTextureMetalic());
72-
73-
glUniform1f(glGetUniformLocation(programID, "Roughness"), texture->GetRoughness());
74-
glUniform1f(glGetUniformLocation(programID, "Metalic"), texture->GetMetalic());
7566
}
67+
7668

7769
// 1st attribute buffer : vertices
7870
glEnableVertexAttribArray(0);
@@ -151,3 +143,8 @@ void Decal::RenderDecal(GLuint& programID) {
151143
glDisableVertexAttribArray(3);
152144
glDisableVertexAttribArray(4);
153145
}
146+
147+
glm::vec3 Decal::GetScale() {
148+
return transform.scale;
149+
}
150+

Engine/Core/Decal.h

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Decal
1414
Decal(glm::vec3 position, glm::vec3 normal, glm::vec3 scale, Texture* texture, GameObject* Parent);
1515
glm::mat4 GetModel();
1616
glm::vec3 GetNormal();
17+
glm::vec3 GetScale();
1718
bool CheckParentIsNull();
1819
void RenderDecal(GLuint& programID);
1920

@@ -26,12 +27,107 @@ class Decal
2627
GameObject* parent = nullptr;
2728
Transform transform;
2829

29-
std::vector<unsigned short> indices = { 0, 1, 2, 0, 3, 1 };
30-
std::vector<glm::vec3> indexed_vertices = { glm::vec3(1.0, 0.0,1.0),glm::vec3(-1.0,0.0,-1.0),glm::vec3(-1.0, 0.0,1.0),glm::vec3(1.0,0.0,-1.0) };
31-
std::vector<glm::vec2> indexed_uvs = { glm::vec2(1.0,-0.0),glm::vec2(0.0,-1.0),glm::vec2(0.0,-0.0),glm::vec2(1.0,-1.0) };
32-
std::vector<glm::vec3> indexed_normals = { glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0) };
33-
std::vector<glm::vec3> indexed_tangents = { glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0) };
34-
std::vector<glm::vec3> indexed_bitangents = { glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0),glm::vec3(-0.0,1.0,-0.0) };
30+
std::vector<glm::vec3> indexed_vertices = {
31+
// Front face
32+
glm::vec3(-1.0f, -1.0f, 1.0f), // Bottom-left
33+
glm::vec3(1.0f, -1.0f, 1.0f), // Bottom-right
34+
glm::vec3(1.0f, 1.0f, 1.0f), // Top-right
35+
glm::vec3(-1.0f, 1.0f, 1.0f), // Top-left
36+
37+
// Back face
38+
glm::vec3(-1.0f, -1.0f, -1.0f), // Bottom-left
39+
glm::vec3(1.0f, -1.0f, -1.0f), // Bottom-right
40+
glm::vec3(1.0f, 1.0f, -1.0f), // Top-right
41+
glm::vec3(-1.0f, 1.0f, -1.0f) // Top-left
42+
};
43+
44+
// Correct winding order for each face
45+
std::vector<unsigned short> indices = {
46+
// front
47+
0, 1, 2,
48+
2, 3, 0,
49+
// right
50+
1, 5, 6,
51+
6, 2, 1,
52+
// back
53+
7, 6, 5,
54+
5, 4, 7,
55+
// left
56+
4, 0, 3,
57+
3, 7, 4,
58+
// bottom
59+
4, 5, 1,
60+
1, 0, 4,
61+
// top
62+
3, 2, 6,
63+
6, 7, 3
64+
};
65+
66+
// UV coordinates (matching vertex order)
67+
std::vector<glm::vec2> indexed_uvs = {
68+
// front face
69+
glm::vec2(0.0f, 0.0f), // 0
70+
glm::vec2(1.0f, 0.0f), // 1
71+
glm::vec2(1.0f, 1.0f), // 2
72+
glm::vec2(0.0f, 1.0f), // 3
73+
74+
// right face
75+
glm::vec2(0.0f, 0.0f), // 4
76+
glm::vec2(1.0f, 0.0f), // 5
77+
glm::vec2(1.0f, 1.0f), // 6
78+
glm::vec2(0.0f, 1.0f), // 7
79+
80+
// back face
81+
glm::vec2(0.0f, 0.0f), // 8
82+
glm::vec2(1.0f, 0.0f), // 9
83+
glm::vec2(1.0f, 1.0f), // 10
84+
glm::vec2(0.0f, 1.0f), // 11
85+
86+
// left face
87+
glm::vec2(0.0f, 0.0f), // 12
88+
glm::vec2(1.0f, 0.0f), // 13
89+
glm::vec2(1.0f, 1.0f), // 14
90+
glm::vec2(0.0f, 1.0f), // 15
91+
92+
// bottom face
93+
glm::vec2(0.0f, 0.0f), // 16
94+
glm::vec2(1.0f, 0.0f), // 17
95+
glm::vec2(1.0f, 1.0f), // 18
96+
glm::vec2(0.0f, 1.0f), // 19
97+
98+
// top face
99+
glm::vec2(0.0f, 0.0f), // 20
100+
glm::vec2(1.0f, 0.0f), // 21
101+
glm::vec2(1.0f, 1.0f), // 22
102+
glm::vec2(0.0f, 1.0f) // 23
103+
};
104+
105+
// Normals (one per face)
106+
std::vector<glm::vec3> indexed_normals = {
107+
glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, -1.0f),
108+
glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, -1.0f),
109+
glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f),
110+
glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f),
111+
glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f),
112+
glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f),
113+
glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f),
114+
glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)
115+
};
116+
117+
// Set tangents and bitangents to point upwards
118+
std::vector<glm::vec3> indexed_tangents = {
119+
glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f),
120+
glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f),
121+
glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f),
122+
glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f)
123+
};
124+
125+
std::vector<glm::vec3> indexed_bitangents = {
126+
glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f),
127+
glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f),
128+
glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f),
129+
glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f)
130+
};
35131

36132

37133
//TODO:: Fix this so all decals are renderer in one draw call

Engine/Core/Renderer.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ namespace Renderer
146146
LoadShader("Assets/Shaders/Texture_Render/Texture_Render.vert", "Assets/Shaders/Texture_Render/Texture_Render.frag", "screen");
147147
LoadShader("Assets/Shaders/Transparent/transparent.vert", "Assets/Shaders/Transparent/transparent.frag", "transparent");
148148
LoadShader("Assets/Shaders/Shadow/depth.vert", "Assets/Shaders/Shadow/depth.frag", "Assets/Shaders/Shadow/depth.gs", "shadow");
149+
LoadShader("Assets/Shaders/Decal/decal.vert", "Assets/Shaders/Decal/decal.frag", "decal");
150+
149151

150152
UseProgram(GetProgramID("lighting"));
151153
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "gPostion"), 0);
@@ -180,6 +182,15 @@ namespace Renderer
180182
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "gNormal"), 1);
181183
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "texNoise"), 2);
182184

185+
UseProgram(GetProgramID("decal"));
186+
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "gPostion"), 0);
187+
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "gNormal"), 1);
188+
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "gAlbeido"), 2);
189+
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "gDepth"), 3);
190+
glUniform1i(glGetUniformLocation(GetCurrentProgramID(), "decalTexture"), 4);
191+
glUniform2f(glGetUniformLocation(GetCurrentProgramID(), "resolution"), SCREENWIDTH, SCREENHEIGHT);
192+
193+
183194
std::cout << "Done loading shaders \n";
184195
}
185196

@@ -188,6 +199,7 @@ namespace Renderer
188199
glEnable(GL_DEPTH_TEST);
189200
glDepthFunc(GL_LESS);
190201
glEnable(GL_CULL_FACE);
202+
glCullFace(GL_BACK); // Cull back-facing triangles
191203
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
192204
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
193205

@@ -226,7 +238,7 @@ namespace Renderer
226238

227239
glGenTextures(1, &gPosition);
228240
glBindTexture(GL_TEXTURE_2D, gPosition);
229-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, SCREENWIDTH, SCREENHEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
241+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, SCREENWIDTH, SCREENHEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
230242
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
231243
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232244

@@ -250,7 +262,7 @@ namespace Renderer
250262

251263
glGenTextures(1, &depthTexture);
252264
glBindTexture(GL_TEXTURE_2D, depthTexture);
253-
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, SCREENWIDTH, SCREENHEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
265+
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, SCREENWIDTH, SCREENHEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
254266
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
255267
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256268
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -461,15 +473,41 @@ namespace Renderer
461473
needRendering[i]->RenderObject(programid);
462474
}
463475

476+
programid = Renderer::GetProgramID("decal");
477+
Renderer::UseProgram(programid);
478+
glActiveTexture(GL_TEXTURE0);
479+
glBindTexture(GL_TEXTURE_2D, gPosition);
480+
glActiveTexture(GL_TEXTURE1);
481+
glBindTexture(GL_TEXTURE_2D, gNormal);
482+
glActiveTexture(GL_TEXTURE2);
483+
glBindTexture(GL_TEXTURE_2D, gAlbeido);
484+
glActiveTexture(GL_TEXTURE3);
485+
glBindTexture(GL_TEXTURE_2D, depthTexture);
486+
glUniformMatrix4fv(glGetUniformLocation(programid, "P"), 1, GL_FALSE, &Camera::getProjectionMatrix()[0][0]);
487+
glUniformMatrix4fv(glGetUniformLocation(programid, "V"), 1, GL_FALSE, &Camera::getViewMatrix()[0][0]);
488+
glUniformMatrix4fv(glGetUniformLocation(programid, "inverseV"), 1, GL_FALSE, &glm::inverse(Camera::getViewMatrix())[0][0]);
489+
glUniformMatrix4fv(glGetUniformLocation(programid, "inverseP"), 1, GL_FALSE, &glm::inverse(Camera::getProjectionMatrix())[0][0]);
490+
491+
492+
493+
glDisable(GL_DEPTH_TEST);
494+
464495
std::vector<Decal>* decals = AssetManager::GetAllDecals();
465496
for (int i = 0; i < decals->size(); i++) {
466497
Decal& decal = (*decals)[i];
467498
if (decal.CheckParentIsNull())
468499
continue;
469500
glm::mat4 ModelMatrix = decal.GetModel();
501+
glm::vec3 size = decal.GetScale();
470502
Renderer::setMat4(glGetUniformLocation(Renderer::GetCurrentProgramID(), "M"), ModelMatrix);
503+
glUniformMatrix4fv(glGetUniformLocation(programid, "inverseM"), 1, GL_FALSE, &glm::inverse(ModelMatrix)[0][0]);
504+
505+
Renderer::setVec3(glGetUniformLocation(Renderer::GetCurrentProgramID(), "size"), size);
471506
decal.RenderDecal(programid);
472507
}
508+
509+
glEnable(GL_DEPTH_TEST);
510+
473511
glDisable(GL_BLEND);
474512

475513

Engine/Game/Player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace Player
112112
glm::vec3 normal = glm::vec3(hit.m_hitNormalWorld.getX(), hit.m_hitNormalWorld.getY(), hit.m_hitNormalWorld.getZ());
113113
glm::mat4 rotation_matrix = glm::mat4_cast(glm::quat(gameobject->getRotation()));
114114
normal = glm::vec3(glm::inverse(rotation_matrix) * glm::vec4(normal, 0));
115-
AssetManager::AddDecal(vec3local, normal, glm::vec3(0.025, 0.025, 0.025), AssetManager::GetTexture("bullet_hole"), gameobject);
115+
AssetManager::AddDecal(vec3local, normal, glm::vec3(0.04, 0.01, 0.04), AssetManager::GetTexture("bullet_hole"), gameobject);
116116
}
117117
}
118118
}

0 commit comments

Comments
 (0)