-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathGBufferPointLight.frag
More file actions
74 lines (63 loc) · 2.15 KB
/
GBufferPointLight.frag
File metadata and controls
74 lines (63 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Request GLSL 3.3
#version 330
// Inputs from vertex shader
// Tex coord
in vec2 fragTexCoord;
// This corresponds to the output color to the color buffer
layout(location = 0) out vec4 outColor;
// Different textures from G-buffer
uniform sampler2D uGDiffuse;
uniform sampler2D uGNormal;
uniform sampler2D uGWorldPos;
// Create a struct for the point light
struct PointLight
{
// Position of light
vec3 mWorldPos;
// Diffuse color
vec3 mDiffuseColor;
// Radius of the light
float mInnerRadius;
float mOuterRadius;
};
uniform PointLight uPointLight;
// Stores width/height of screen
uniform vec2 uScreenDimensions;
void main()
{
// From this fragment, calculate the coordinate to sample into the G-buffer
vec2 gbufferCoord = gl_FragCoord.xy / uScreenDimensions;
// Sample from G-buffer
vec3 gbufferDiffuse = texture(uGDiffuse, gbufferCoord).xyz;
vec3 gbufferNorm = texture(uGNormal, gbufferCoord).xyz;
vec3 gbufferWorldPos = texture(uGWorldPos, gbufferCoord).xyz;
// Surface normal
vec3 N = normalize(gbufferNorm);
// Vector from surface to light
vec3 L = normalize(uPointLight.mWorldPos - gbufferWorldPos);
// Compute Phong diffuse component for the light
vec3 Phong = vec3(0.0, 0.0, 0.0);
float NdotL = dot(N, L);
if (NdotL > 0)
{
// Get the distance between the light and the world pos
float dist = distance(uPointLight.mWorldPos, gbufferWorldPos);
// Use smoothstep to compute value in range [0,1]
// between inner/outer radius
float intensity = smoothstep(uPointLight.mInnerRadius,
uPointLight.mOuterRadius, dist);
// The diffuse color of the light depends on intensity
vec3 DiffuseColor = mix(uPointLight.mDiffuseColor,
vec3(0.0, 0.0, 0.0), intensity);
Phong = DiffuseColor * NdotL;
}
// Final color is texture color times phong light (alpha = 1)
outColor = vec4(gbufferDiffuse * Phong, 1.0);
}