Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for shader includes pre-processor directives #269

Merged
merged 12 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Build/
imgui.ini
*.aps
*.vcxproj*
*.sln
*.sln
*.vcxitems*
Binary file added Resources/Editor/Icons/puzzle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
2 changes: 1 addition & 1 deletion Resources/Engine/Materials/Default.ovmat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<root>
<shader>:Shaders\Standard.glsl</shader>
<shader>:Shaders\Standard.ovfx</shader>
<settings>
<blendable>false</blendable>
<backface_culling>true</backface_culling>
Expand Down
9 changes: 9 additions & 0 deletions Resources/Engine/Shaders/Common/Buffers/EngineUBO.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
layout (std140) uniform EngineUBO
{
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
mat4 ubo_UserMatrix;
};
4 changes: 4 additions & 0 deletions Resources/Engine/Shaders/Common/Buffers/LightsSSBO.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
layout(std430, binding = 0) buffer LightSSBO
{
mat4 ssbo_Lights[];
};
1 change: 1 addition & 0 deletions Resources/Engine/Shaders/Common/Constants.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const float PI = 3.14159265359;
12 changes: 12 additions & 0 deletions Resources/Engine/Shaders/Common/Physics.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bool IsPointInAABB(vec3 point, vec3 aabbCenter, vec3 aabbHalfSize)
{
return
point.x > aabbCenter.x - aabbHalfSize.x && point.x < aabbCenter.x + aabbHalfSize.x &&
point.y > aabbCenter.y - aabbHalfSize.y && point.y < aabbCenter.y + aabbHalfSize.y &&
point.z > aabbCenter.z - aabbHalfSize.z && point.z < aabbCenter.z + aabbHalfSize.z;
}

bool IsPointInSphere(vec3 point, vec3 sphereCenter, float sphereRadius)
{
return distance(point, sphereCenter) <= sphereRadius;
}
54 changes: 54 additions & 0 deletions Resources/Engine/Shaders/Common/Utils.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
vec3 UnPack(float target)
{
return vec3 (
float((uint(target) >> 24) & 0xff) * 0.003921568627451,
float((uint(target) >> 16) & 0xff) * 0.003921568627451,
float((uint(target) >> 8) & 0xff) * 0.003921568627451
);
}

vec2 TileAndOffsetTexCoords(vec2 texCoords, vec2 tiling, vec2 offset)
{
return vec2(mod(texCoords.x * tiling.x, 1), mod(texCoords.y * tiling.y, 1)) + offset;
}

vec2 ApplyParallaxMapping(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale)
{
if (heightScale > 0)
{
const vec3 viewDir = normalize(tangentViewPos - tangentFragPos);
const vec2 parallax = viewDir.xy * heightScale * texture(heightMap, texCoords).r;
return texCoords - vec2(parallax.x, 1.0 - parallax.y);
}

return texCoords;
}

bool IsMasked(sampler2D maskMap, vec2 texCoords)
{
return texture(maskMap, texCoords).r == 0.0;
}

mat3 ConstructTBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent)
{
return mat3(
normalize(vec3(model * vec4(tangent, 0.0))),
normalize(vec3(model * vec4(bitangent, 0.0))),
normalize(vec3(model * vec4(normal, 0.0)))
);
}

vec3 ComputeNormal(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN)
{
if (enableNormalMapping)
{
normal = texture(normalMap, texCoords).rgb;
normal = normalize(normal * 2.0 - 1.0);
normal = normalize(TBN * normal);
return normal;
}
else
{
return normalize(normal);
}
}
64 changes: 0 additions & 64 deletions Resources/Engine/Shaders/Lambert.glsl

This file was deleted.

58 changes: 58 additions & 0 deletions Resources/Engine/Shaders/Lambert.ovfx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#shader vertex
#version 430 core

#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"

layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;
layout (location = 2) in vec3 geo_Normal;

out VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
vec3 Normal;
} vs_out;

void main()
{
vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0));
vs_out.TexCoords = geo_TexCoords;
vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal);

gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0);
}

#shader fragment
#version 430 core

#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"
#include ":Shaders/Lighting/Lambert.ovfxh"

in VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
vec3 Normal;
} fs_in;

uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0);
uniform sampler2D u_DiffuseMap;
uniform vec2 u_TextureTiling = vec2(1.0, 1.0);
uniform vec2 u_TextureOffset = vec2(0.0, 0.0);

out vec4 FRAGMENT_COLOR;

void main()
{
vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);

const vec3 kLightPosition = vec3(-9000.0, 10000.0, 11000.0);
const vec3 kLightDiffuse = vec3(1.0);
const vec3 kLightAmbient = vec3(0.3);
const vec3 lambert = ComputeLambertLighting(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient);

FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse * vec4(lambert, 1.0);
}
98 changes: 98 additions & 0 deletions Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include ":Shaders/Common/Physics.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"
#include ":Shaders/Lighting/Shared.ovfxh"
#include ":Shaders/Common/Buffers/LightsSSBO.ovfxh"

vec3 BlinnPhong(vec3 lightDir, vec3 lightColor, float luminosity, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
const vec3 halfwayDir = normalize(lightDir + viewDir);
const float diffuseCoefficient = max(dot(normal, lightDir), 0.0);
const float specularCoefficient = pow(max(dot(normal, halfwayDir), 0.0), shininess * 2.0);

return lightColor * diffuseTexel.rgb * diffuseCoefficient * luminosity + ((luminosity > 0.0) ? (lightColor * specularTexel.rgb * specularCoefficient * luminosity) : vec3(0.0));
}

vec3 ComputePointLight(mat4 light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
/* Extract light information from light mat4 */
const vec3 lightPosition = light[0].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];

const vec3 lightDirection = normalize(lightPosition - fragPos);
const float luminosity = LuminosityFromAttenuation(light, fragPos);

return BlinnPhong(lightDirection, lightColor, intensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess);
}

vec3 ComputeDirectionalLight(mat4 light, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3], diffuseTexel, specularTexel, normal, viewDir, shininess);
}

vec3 ComputeSpotLight(mat4 light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
/* Extract light information from light mat4 */
const vec3 lightPosition = light[0].rgb;
const vec3 lightForward = light[1].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];
const float cutOff = cos(radians(light[3][1]));
const float outerCutOff = cos(radians(light[3][1] + light[3][2]));

const vec3 lightDirection = normalize(lightPosition - fragPos);
const float luminosity = LuminosityFromAttenuation(light, fragPos);

/* Calculate the spot intensity */
const float theta = dot(lightDirection, normalize(-lightForward));
const float epsilon = cutOff - outerCutOff;
const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0);

return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess);
}

vec3 ComputeAmbientBoxLight(mat4 light, vec3 fragPos, vec4 diffuseTexel)
{
const vec3 lightPosition = light[0].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];
const vec3 size = vec3(light[0][3], light[1][3], light[2][3]);

return IsPointInAABB(fragPos, lightPosition, size) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0);
}

vec3 ComputeAmbientSphereLight(mat4 light, vec3 fragPos, vec4 diffuseTexel)
{
const vec3 lightPosition = light[0].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];
const float radius = light[0][3];

return IsPointInSphere(fragPos, lightPosition, radius) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0);
}

vec4 ComputeBlinnPhongLighting(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 diffuse, vec3 specular, sampler2D diffuseMap, sampler2D specularMap, float shininess)
{
vec3 viewDir = normalize(viewPos - fragPos);
vec4 diffuseTexel = texture(diffuseMap, texCoords) * diffuse;
vec4 specularTexel = texture(specularMap, texCoords) * vec4(specular, 1.0);

vec3 lightAccumulation = vec3(0.0);

for (int i = 0; i < ssbo_Lights.length(); ++i)
{
const mat4 light = ssbo_Lights[i];
const int lightType = int(light[3][0]);

switch(lightType)
{
case 0: lightAccumulation += ComputePointLight(light, fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 1: lightAccumulation += ComputeDirectionalLight(light, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 2: lightAccumulation += ComputeSpotLight(light, fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 3: lightAccumulation += ComputeAmbientBoxLight(light, fragPos, diffuseTexel); break;
case 4: lightAccumulation += ComputeAmbientSphereLight(light, fragPos, diffuseTexel); break;
}
}

return vec4(lightAccumulation, diffuseTexel.a);
}
5 changes: 5 additions & 0 deletions Resources/Engine/Shaders/Lighting/Lambert.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vec3 ComputeLambertLighting(vec3 fragPos, vec3 normal, vec3 lightPos, vec3 lightDiffuse, vec3 lightAmbient)
{
const float diffuse = max(dot(normal, normalize(lightPos - fragPos)), 0.0);
return clamp(lightDiffuse * diffuse + lightAmbient, 0.0, 1.0);
}
Loading