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
|
#version 130
out vec4 outpix;
uniform vec3 campos;
uniform vec3 lightpos;
uniform sampler2D tex;
uniform sampler2D elev;
//varing blends them it is transformed to world space
varying vec4 fragpos;
varying vec4 fragnormal;
varying vec2 fragtexcoord;
void main()
{
vec4 elevpix = texture(elev, fragtexcoord);
//the format is diffuse reflect and percent
vec3 difpos = vec3(fragpos.xyz) + ((elevpix.r - .5) * 2 * fragnormal.xyz);
vec3 refpos = vec3(fragpos.xyz) + ((elevpix.g - .5) * 2 * fragnormal.xyz);
vec3 lightnormal = normalize((lightpos - difpos));
float diffuse = dot(lightnormal, fragnormal.xyz);
if (diffuse < 0.0) {
diffuse = 0.0;
}
vec3 camnormal = normalize(campos - refpos);
lightnormal = -normalize((lightpos - refpos)); // this time it is an incoming vector
vec3 refnormal = lightnormal - (2 * dot(lightnormal, fragnormal.xyz) * fragnormal.xyz);
float reflect = dot(camnormal, refnormal);
diffuse = pow(diffuse, 2.0);
reflect = pow(reflect, 2.0);
float light = (elevpix.b) * reflect + elevpix.a * diffuse;
vec4 texpix = texture(tex, fragtexcoord);
//this should be the vertex in camera space without the w value
//outpix = gl_FragCoord;
outpix = light * texpix;
//outpix = vec4(light * 1.0,light * 1.0,light * 1.0, 1.0);
//outpix = vec4(fragpos.x ,fragpos.y ,fragpos.z, 0.5);
//outpix = vec4(fragnormal.x, fragnormal.y, fragnormal.z, 0.5);
}
|