annotate shader.frag @ 47:9909014498f0

Add helper functions dmError() and dmMsg() and use them.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Dec 2019 23:52:45 +0200
parents d2839cbfaad8
children 71f6c5cc8eec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
1 varying vec3 snormal;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
2 varying vec3 svertex;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
3 uniform int nlights;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
4
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
5
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
6 float maxdot(vec3 sveca, vec3 svecb)
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
7 {
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
8 return max(dot(sveca, svecb), 0.0);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
9 }
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
10
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
11
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
12 vec4 mclamp(vec4 svec)
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
13 {
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
14 return clamp(svec, 0.0, 1.0);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
15 }
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
16
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
17
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
18 void main(void)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 {
28
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
20 vec3 snormal = normalize(snormal);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
21 vec3 srcVec = normalize(-svertex);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
22 //vec4 poo = vec4(0.0, 0.0, 0.0, 0.0);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
23 vec4 poo = gl_FragCoord;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
24 //poo += vec4(snormal.x, snormal.y, snormal.z, 0.0);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
25 vec4 finalColor = vec4(poo.x * 0.1, poo.y * 0.001, poo.z, 1.0);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
26
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
27 for (int i = 0; i < nlights; i++)
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
28 {
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
29 vec3 lightVec = normalize(gl_LightSource[i].position.xyz - svertex);
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
30 vec3 refVec = normalize(-reflect(lightVec, snormal));
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
28
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
32 vec4 ambient = gl_FrontLightProduct[i].ambient;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
33 vec4 diffuse = mclamp(gl_FrontLightProduct[i].diffuse * maxdot(snormal, lightVec));
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
34 vec4 specular = mclamp(
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
35 gl_FrontLightProduct[i].specular *
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
36 pow(maxdot(refVec, srcVec), gl_FrontMaterial.shininess));
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
37
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
38 finalColor += ambient + diffuse + specular;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
39 }
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
40
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
41 gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColor;
d2839cbfaad8 Some work on the default shaders.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
42 }