forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGBufferGlobal.frag
More file actions
69 lines (60 loc) · 1.82 KB
/
GBufferGlobal.frag
File metadata and controls
69 lines (60 loc) · 1.82 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
// ----------------------------------------------------------------
// 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 directional light
struct DirectionalLight
{
// Direction of light
vec3 mDirection;
// Diffuse color
vec3 mDiffuseColor;
// Specular color
vec3 mSpecColor;
};
// Uniforms for lighting
// Camera position (in world space)
uniform vec3 uCameraPos;
// Ambient light level
uniform vec3 uAmbientLight;
// Directional Light
uniform DirectionalLight uDirLight;
void main()
{
vec3 gbufferDiffuse = texture(uGDiffuse, fragTexCoord).xyz;
vec3 gbufferNorm = texture(uGNormal, fragTexCoord).xyz;
vec3 gbufferWorldPos = texture(uGWorldPos, fragTexCoord).xyz;
// Surface normal
vec3 N = normalize(gbufferNorm);
// Vector from surface to light
vec3 L = normalize(-uDirLight.mDirection);
// Vector from surface to camera
vec3 V = normalize(uCameraPos - gbufferWorldPos);
// Reflection of -L about N
vec3 R = normalize(reflect(-L, N));
// Compute phong reflection
vec3 Phong = uAmbientLight;
float NdotL = dot(N, L);
if (NdotL > 0)
{
vec3 Diffuse = uDirLight.mDiffuseColor * dot(N, L);
}
// Clamp light between 0-1 RGB values
Phong = clamp(Phong, 0.0, 1.0);
// Final color is texture color times phong light (alpha = 1)
outColor = vec4(gbufferDiffuse * Phong, 1.0);
}