-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
examples/shaders: Add an example for deferred shading #3496
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
examples/shaders/resources/shaders/glsl330/deferred_shading.fs
This file contains hidden or 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,54 @@ | ||
| #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; | ||
raysan5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.