Skip to content

Commit 3645244

Browse files
authored
examples/shaders: Add an example for deferred shading (#3496)
* add example for deferred rendering/shading * adapt convention --------- Co-authored-by: 27justin <me@justin.cx>
1 parent de7beef commit 3645244

File tree

9 files changed

+442
-6
lines changed

9 files changed

+442
-6
lines changed

examples/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,8 @@ SHADERS = \
562562
shaders/shaders_mesh_instancing \
563563
shaders/shaders_multi_sample2d \
564564
shaders/shaders_write_depth \
565-
shaders/shaders_hybrid_render
565+
shaders/shaders_hybrid_render \
566+
shaders/shaders_deferred_render
566567

567568
AUDIO = \
568569
audio/audio_module_playing \

examples/Makefile.Web

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ SHADERS = \
468468
shaders/shaders_mesh_instancing \
469469
shaders/shaders_multi_sample2d \
470470
shaders/shaders_write_depth \
471-
shaders/shaders_hybrid_render
471+
shaders/shaders_hybrid_render \
472+
shaders/shaders_deferred_render
472473

473474
AUDIO = \
474475
audio/audio_module_playing \

examples/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,18 @@ Examples using raylib shaders functionality, including shaders loading, paramete
176176
| 114 | [shaders_mesh_instancing](shaders/shaders_mesh_instancing.c) | <img src="shaders/shaders_mesh_instancing.png" alt="shaders_mesh_instancing" width="80"> | ⭐️⭐️⭐️⭐️ | 3.7 | **4.2** | [seanpringle](https://github.com/seanpringle) |
177177
| 115 | [shaders_multi_sample2d](shaders/shaders_multi_sample2d.c) | <img src="shaders/shaders_multi_sample2d.png" alt="shaders_multi_sample2d" width="80"> | ⭐️⭐️☆☆ | 3.5 | 3.5 | [Ray](https://github.com/raysan5) |
178178
| 116 | [shaders_spotlight](shaders/shaders_spotlight.c) | <img src="shaders/shaders_spotlight.png" alt="shaders_spotlight" width="80"> | ⭐️⭐️☆☆ | 2.5 | 3.7 | [Chris Camacho](https://github.com/codifies) |
179+
| 117 | [shaders_deferred_render](shaders/shaders_deferred_render.c) | <img src="shaders/shaders_deferred_render.png" alt="shaders_deferred_render" width="80"> | ⭐️⭐️⭐️⭐️ | 4.5 | 4.5 | [Justin Andreas Lacoste](https://github.com/27justin) |
179180

180181
### category: audio
181182

182183
Examples using raylib audio functionality, including sound/music loading and playing. This functionality is provided by raylib [raudio](../src/raudio.c) module. Note this module can be used standalone independently of raylib, check [raudio_standalone](others/raudio_standalone.c) example.
183184

184185
| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
185186
|----|----------|--------|:-------------------:|:------------------:|:------------------:|:----------|
186-
| 117 | [audio_module_playing](audio/audio_module_playing.c) | <img src="audio/audio_module_playing.png" alt="audio_module_playing" width="80"> | ⭐️☆☆☆ | 1.5 | 3.5 | [Ray](https://github.com/raysan5) |
187-
| 118 | [audio_music_stream](audio/audio_music_stream.c) | <img src="audio/audio_music_stream.png" alt="audio_music_stream" width="80"> | ⭐️☆☆☆ | 1.3 | **4.2** | [Ray](https://github.com/raysan5) |
188-
| 119 | [audio_raw_stream](audio/audio_raw_stream.c) | <img src="audio/audio_raw_stream.png" alt="audio_raw_stream" width="80"> | ⭐️⭐️⭐️☆ | 1.6 | **4.2** | [Ray](https://github.com/raysan5) |
189-
| 120 | [audio_sound_loading](audio/audio_sound_loading.c) | <img src="audio/audio_sound_loading.png" alt="audio_sound_loading" width="80"> | ⭐️☆☆☆ | 1.1 | 3.5 | [Ray](https://github.com/raysan5) |
187+
| 118 | [audio_module_playing](audio/audio_module_playing.c) | <img src="audio/audio_module_playing.png" alt="audio_module_playing" width="80"> | ⭐️☆☆☆ | 1.5 | 3.5 | [Ray](https://github.com/raysan5) |
188+
| 119 | [audio_music_stream](audio/audio_music_stream.c) | <img src="audio/audio_music_stream.png" alt="audio_music_stream" width="80"> | ⭐️☆☆☆ | 1.3 | **4.2** | [Ray](https://github.com/raysan5) |
189+
| 120 | [audio_raw_stream](audio/audio_raw_stream.c) | <img src="audio/audio_raw_stream.png" alt="audio_raw_stream" width="80"> | ⭐️⭐️⭐️☆ | 1.6 | **4.2** | [Ray](https://github.com/raysan5) |
190+
| 121 | [audio_sound_loading](audio/audio_sound_loading.c) | <img src="audio/audio_sound_loading.png" alt="audio_sound_loading" width="80"> | ⭐️☆☆☆ | 1.1 | 3.5 | [Ray](https://github.com/raysan5) |
190191

191192
### category: others
192193

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#version 330 core
2+
out vec4 finalColor;
3+
4+
in vec2 texCoord;
5+
in vec2 texCoord2;
6+
7+
uniform sampler2D gPosition;
8+
uniform sampler2D gNormal;
9+
uniform sampler2D gAlbedoSpec;
10+
11+
struct Light {
12+
int enabled;
13+
int type; // Unused in this demo.
14+
vec3 position;
15+
vec3 target; // Unused in this demo.
16+
vec4 color;
17+
};
18+
19+
const int NR_LIGHTS = 4;
20+
uniform Light lights[NR_LIGHTS];
21+
uniform vec3 viewPosition;
22+
23+
const float QUADRATIC = 0.032;
24+
const float LINEAR = 0.09;
25+
26+
void main() {
27+
vec3 fragPosition = texture(gPosition, texCoord).rgb;
28+
vec3 normal = texture(gNormal, texCoord).rgb;
29+
vec3 albedo = texture(gAlbedoSpec, texCoord).rgb;
30+
float specular = texture(gAlbedoSpec, texCoord).a;
31+
32+
vec3 ambient = albedo * vec3(0.1f);
33+
vec3 viewDirection = normalize(viewPosition - fragPosition);
34+
35+
for(int i = 0; i < NR_LIGHTS; ++i)
36+
{
37+
if(lights[i].enabled == 0) continue;
38+
vec3 lightDirection = lights[i].position - fragPosition;
39+
vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz;
40+
41+
vec3 halfwayDirection = normalize(lightDirection + viewDirection);
42+
float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0);
43+
vec3 specular = specular * spec * lights[i].color.xyz;
44+
45+
// Attenuation
46+
float distance = length(lights[i].position - fragPosition);
47+
float attenuation = 1.0 / (1.0 + LINEAR * distance + QUADRATIC * distance * distance);
48+
diffuse *= attenuation;
49+
specular *= attenuation;
50+
ambient += diffuse + specular;
51+
}
52+
53+
finalColor = vec4(ambient, 1.0);
54+
}
55+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330 core
2+
3+
layout (location = 0) in vec3 vertexPosition;
4+
layout (location = 1) in vec2 vertexTexCoord;
5+
6+
out vec2 texCoord;
7+
8+
void main() {
9+
gl_Position = vec4(vertexPosition, 1.0);
10+
texCoord = vertexTexCoord;
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 330 core
2+
layout (location = 0) out vec3 gPosition;
3+
layout (location = 1) out vec3 gNormal;
4+
layout (location = 2) out vec4 gAlbedoSpec;
5+
6+
in vec3 fragPosition;
7+
in vec2 fragTexCoord;
8+
in vec3 fragNormal;
9+
10+
uniform sampler2D diffuseTexture;
11+
uniform sampler2D specularTexture;
12+
13+
void main() {
14+
// store the fragment position vector in the first gbuffer texture
15+
gPosition = fragPosition;
16+
// also store the per-fragment normals into the gbuffer
17+
gNormal = normalize(fragNormal);
18+
// and the diffuse per-fragment color
19+
gAlbedoSpec.rgb = texture(diffuseTexture, fragTexCoord).rgb;
20+
// store specular intensity in gAlbedoSpec's alpha component
21+
gAlbedoSpec.a = texture(specularTexture, fragTexCoord).r;
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 vertexPosition;
3+
layout (location = 1) in vec2 vertexTexCoord;
4+
layout (location = 2) in vec3 vertexNormal;
5+
6+
out vec3 fragPosition;
7+
out vec2 fragTexCoord;
8+
out vec3 fragNormal;
9+
10+
uniform mat4 matModel;
11+
uniform mat4 matView;
12+
uniform mat4 matProjection;
13+
14+
void main()
15+
{
16+
vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
17+
fragPosition = worldPos.xyz;
18+
fragTexCoord = vertexTexCoord;
19+
20+
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
21+
fragNormal = normalMatrix * vertexNormal;
22+
23+
gl_Position = matProjection * matView * worldPos;
24+
}

0 commit comments

Comments
 (0)