CGI/exercise2/glsl/terrain.frag

58 lines
1.5 KiB
GLSL
Raw Normal View History

2018-11-16 11:47:55 +00:00
#version 330
2018-11-26 08:59:16 +00:00
// This source code is property of the Computer Graphics and Visualization
// chair of the TU Dresden. Do not distribute!
2018-11-13 08:35:18 +00:00
// Copyright (C) CGV TU Dresden - All Rights Reserved
2018-11-26 08:59:16 +00:00
in vec3 n;
2018-11-26 12:11:05 +00:00
in vec2 uv;
2018-11-26 16:58:30 +00:00
in vec2 road_uv;
2018-11-26 08:59:16 +00:00
2018-11-13 08:35:18 +00:00
out vec4 color;
uniform vec3 cameraPos;
2018-11-26 12:11:05 +00:00
uniform sampler2D grass;
2018-11-26 16:58:30 +00:00
uniform sampler2D rock;
uniform sampler2D alpha;
uniform sampler2D road;
uniform sampler2D specular_road;
uniform sampler2D normal_road;
2018-11-13 08:35:18 +00:00
uniform sampler2D background;
uniform vec2 screenSize;
2018-11-26 08:59:16 +00:00
const vec3 dirToLight = normalize(vec3(1, 3, 1));
2018-11-13 08:35:18 +00:00
//Calculates the visible surface color based on the Blinn-Phong illumination model
vec4 calculateLighting(vec4 materialColor, float specularIntensity, vec3 normalizedNormal, vec3 directionToViewer)
{
vec4 color = materialColor;
vec3 h = normalize(dirToLight + directionToViewer);
color.xyz *= 0.9 * max(dot(normalizedNormal, dirToLight), 0) + 0.1;
color.xyz += specularIntensity * pow(max(dot(h, normalizedNormal), 0), 50);
return color;
}
vec4 getBackgroundColor()
{
return texture(background, gl_FragCoord.xy / screenSize);
}
void main()
{
//surface geometry
vec3 dirToViewer = vec3(0, 1, 0);
2018-11-26 08:59:16 +00:00
//material properties
float angle = acos(dot(n, dirToViewer));
color = mix(texture(grass, uv), texture(rock, uv), angle * angle);
2018-11-26 16:58:30 +00:00
color = mix(color, texture(road, road_uv), texture(alpha, uv).x);
2018-11-26 08:59:16 +00:00
2018-11-26 16:58:30 +00:00
float specular = texture(specular_road, road_uv).x;
2018-11-13 08:35:18 +00:00
//Calculate light
color = calculateLighting(color, specular, n, dirToViewer);
2018-11-26 08:59:16 +00:00
2018-11-16 08:27:11 +00:00
}