summaryrefslogtreecommitdiffhomepage
path: root/examples/shaders/resources
diff options
context:
space:
mode:
authorJustin <[email protected]>2023-10-31 20:13:12 +0100
committerGitHub <[email protected]>2023-10-31 20:13:12 +0100
commit3645244f9fc1874e72252138ca4ed0ab849a2fd9 (patch)
treeae24f47d70fdc746ce657c1ecd136e3241f6a175 /examples/shaders/resources
parentde7beef05d56a23c4ab06202013965e3da014ef3 (diff)
downloadraylib-3645244f9fc1874e72252138ca4ed0ab849a2fd9.tar.gz
raylib-3645244f9fc1874e72252138ca4ed0ab849a2fd9.zip
examples/shaders: Add an example for deferred shading (#3496)
* add example for deferred rendering/shading * adapt convention --------- Co-authored-by: 27justin <[email protected]>
Diffstat (limited to 'examples/shaders/resources')
-rw-r--r--examples/shaders/resources/shaders/glsl330/deferred_shading.fs55
-rw-r--r--examples/shaders/resources/shaders/glsl330/deferred_shading.vs11
-rw-r--r--examples/shaders/resources/shaders/glsl330/gbuffer.fs22
-rw-r--r--examples/shaders/resources/shaders/glsl330/gbuffer.vs24
4 files changed, 112 insertions, 0 deletions
diff --git a/examples/shaders/resources/shaders/glsl330/deferred_shading.fs b/examples/shaders/resources/shaders/glsl330/deferred_shading.fs
new file mode 100644
index 00000000..c9c6a313
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/deferred_shading.fs
@@ -0,0 +1,55 @@
+#version 330 core
+out vec4 finalColor;
+
+in vec2 texCoord;
+in vec2 texCoord2;
+
+uniform sampler2D gPosition;
+uniform sampler2D gNormal;
+uniform sampler2D gAlbedoSpec;
+
+struct Light {
+ int enabled;
+ int type; // Unused in this demo.
+ vec3 position;
+ vec3 target; // Unused in this demo.
+ vec4 color;
+};
+
+const int NR_LIGHTS = 4;
+uniform Light lights[NR_LIGHTS];
+uniform vec3 viewPosition;
+
+const float QUADRATIC = 0.032;
+const float LINEAR = 0.09;
+
+void main() {
+ vec3 fragPosition = texture(gPosition, texCoord).rgb;
+ vec3 normal = texture(gNormal, texCoord).rgb;
+ vec3 albedo = texture(gAlbedoSpec, texCoord).rgb;
+ float specular = texture(gAlbedoSpec, texCoord).a;
+
+ vec3 ambient = albedo * vec3(0.1f);
+ vec3 viewDirection = normalize(viewPosition - fragPosition);
+
+ for(int i = 0; i < NR_LIGHTS; ++i)
+ {
+ if(lights[i].enabled == 0) continue;
+ vec3 lightDirection = lights[i].position - fragPosition;
+ vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz;
+
+ vec3 halfwayDirection = normalize(lightDirection + viewDirection);
+ float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0);
+ vec3 specular = specular * spec * lights[i].color.xyz;
+
+ // Attenuation
+ float distance = length(lights[i].position - fragPosition);
+ float attenuation = 1.0 / (1.0 + LINEAR * distance + QUADRATIC * distance * distance);
+ diffuse *= attenuation;
+ specular *= attenuation;
+ ambient += diffuse + specular;
+ }
+
+ finalColor = vec4(ambient, 1.0);
+}
+
diff --git a/examples/shaders/resources/shaders/glsl330/deferred_shading.vs b/examples/shaders/resources/shaders/glsl330/deferred_shading.vs
new file mode 100644
index 00000000..f2b1bd7c
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/deferred_shading.vs
@@ -0,0 +1,11 @@
+#version 330 core
+
+layout (location = 0) in vec3 vertexPosition;
+layout (location = 1) in vec2 vertexTexCoord;
+
+out vec2 texCoord;
+
+void main() {
+ gl_Position = vec4(vertexPosition, 1.0);
+ texCoord = vertexTexCoord;
+}
diff --git a/examples/shaders/resources/shaders/glsl330/gbuffer.fs b/examples/shaders/resources/shaders/glsl330/gbuffer.fs
new file mode 100644
index 00000000..c86e20a9
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/gbuffer.fs
@@ -0,0 +1,22 @@
+#version 330 core
+layout (location = 0) out vec3 gPosition;
+layout (location = 1) out vec3 gNormal;
+layout (location = 2) out vec4 gAlbedoSpec;
+
+in vec3 fragPosition;
+in vec2 fragTexCoord;
+in vec3 fragNormal;
+
+uniform sampler2D diffuseTexture;
+uniform sampler2D specularTexture;
+
+void main() {
+ // store the fragment position vector in the first gbuffer texture
+ gPosition = fragPosition;
+ // also store the per-fragment normals into the gbuffer
+ gNormal = normalize(fragNormal);
+ // and the diffuse per-fragment color
+ gAlbedoSpec.rgb = texture(diffuseTexture, fragTexCoord).rgb;
+ // store specular intensity in gAlbedoSpec's alpha component
+ gAlbedoSpec.a = texture(specularTexture, fragTexCoord).r;
+}
diff --git a/examples/shaders/resources/shaders/glsl330/gbuffer.vs b/examples/shaders/resources/shaders/glsl330/gbuffer.vs
new file mode 100644
index 00000000..7d264ba6
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/gbuffer.vs
@@ -0,0 +1,24 @@
+#version 330 core
+layout (location = 0) in vec3 vertexPosition;
+layout (location = 1) in vec2 vertexTexCoord;
+layout (location = 2) in vec3 vertexNormal;
+
+out vec3 fragPosition;
+out vec2 fragTexCoord;
+out vec3 fragNormal;
+
+uniform mat4 matModel;
+uniform mat4 matView;
+uniform mat4 matProjection;
+
+void main()
+{
+ vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
+ fragPosition = worldPos.xyz;
+ fragTexCoord = vertexTexCoord;
+
+ mat3 normalMatrix = transpose(inverse(mat3(matModel)));
+ fragNormal = normalMatrix * vertexNormal;
+
+ gl_Position = matProjection * matView * worldPos;
+}