-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathPhong.frag
More file actions
71 lines (61 loc) · 1.77 KB
/
Phong.frag
File metadata and controls
71 lines (61 loc) · 1.77 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
// ----------------------------------------------------------------
// 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;
// Normal (in world space)
in vec3 fragNormal;
// Position (in world space)
in vec3 fragWorldPos;
// This corresponds to the output color to the color buffer
out vec4 outColor;
// This is used for the texture sampling
uniform sampler2D uTexture;
// 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;
// Specular power for this surface
uniform float uSpecPower;
// Ambient light level
uniform vec3 uAmbientLight;
// Directional Light
uniform DirectionalLight uDirLight;
void main()
{
// Surface normal
vec3 N = normalize(fragNormal);
// Vector from surface to light
vec3 L = normalize(-uDirLight.mDirection);
// Vector from surface to camera
vec3 V = normalize(uCameraPos - fragWorldPos);
// 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 * NdotL;
vec3 Specular = uDirLight.mSpecColor * pow(max(0.0, dot(R, V)), uSpecPower);
Phong += Diffuse + Specular;
}
// Final color is texture color times phong light (alpha = 1)
outColor = texture(uTexture, fragTexCoord) * vec4(Phong, 1.0f);
}