-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/shaders: Add an example for deferred shading (#3496)
* add example for deferred rendering/shading * adapt convention --------- Co-authored-by: 27justin <me@justin.cx>
- Loading branch information
Showing
9 changed files
with
442 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
examples/shaders/resources/shaders/glsl330/deferred_shading.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
examples/shaders/resources/shaders/glsl330/deferred_shading.vs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.