summaryrefslogtreecommitdiff
path: root/example/fragmentshader.glsl
blob: fc7b4c8cd022e983b5192d346c1965c3a6ce272d (plain)
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
#version 130
out vec4 outpix;
uniform vec3 campos;
uniform vec3 lightpos;
uniform vec4 lightattrib;
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;
	} else {
	}
	vec3 camnormal = normalize(campos - refpos);
	lightnormal = normalize((refpos - lightpos)); // this time it is an incoming vector
	vec3 refnormal = lightnormal - (2 * dot(lightnormal, fragnormal.xyz) * fragnormal.xyz);
	float reflect = dot(camnormal, refnormal);
	if (reflect < 0.0) {
		reflect = 0.0;
	} else {
	}
	diffuse = pow(diffuse, 2.0);
	reflect = pow(reflect, 2.0);
	float dist = abs(length(difpos - lightpos)) / lightattrib.a;
	if (dist > 1.0) {
		dist = 1.0;
	}
	dist = 1 - pow(dist, 4.0);
	float lightr = (elevpix.b) * 1.0 * lightattrib.r * reflect + elevpix.a * 1.0 * dist  *  diffuse;
	float lightg = (elevpix.b) * 1.0 * lightattrib.g * reflect + elevpix.a * 1.0 * dist  *  diffuse;
	float lightb = (elevpix.b) * 1.0 * lightattrib.b * reflect + elevpix.a * 1.0 * dist  *  diffuse;
	vec4 texpix = texture(tex, fragtexcoord);
	//this should be the vertex in camera space without the w value
	//outpix = gl_FragCoord;
	outpix = vec4(lightr * texpix.r, lightg * texpix.g, lightb * texpix.b, texpix.a);
	//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);
}