summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/asymptote/shaders/fragment.glsl
blob: b6a85597451b10e7d3de795b62152db966a16a5b (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
struct Material
{
  vec4 diffuse,ambient,emissive,specular;
  float shininess; 
};

struct Light
{
  vec4 direction;
  vec4 diffuse,ambient,specular;  
};

uniform int nlights;

uniform Light lights[Nlights];

uniform MaterialBuffer {
  Material Materials[Nmaterials];
};

in vec3 Normal;

#ifdef EXPLICIT_COLOR
in vec4 Color; 
#endif
flat in int materialIndex;

out vec4 outColor;

void main()
{
  vec4 Diffuse;
  vec4 Ambient;
  vec4 Emissive;
  vec4 Specular;
  float Shininess;

#ifdef EXPLICIT_COLOR
  if(materialIndex < 0) {
    int index=-materialIndex-1;
    Material m=Materials[index];
    Diffuse=Color;
    Ambient=Color;
    Emissive=vec4(0.0,0.0,0.0,1.0);
    Specular=m.specular;
    Shininess=m.shininess;
  } else {
    Material m=Materials[materialIndex];
    Diffuse=m.diffuse;
    Ambient=m.ambient;
    Emissive=m.emissive;
    Specular=m.specular;
    Shininess=m.shininess;
  }
#else
  Material m=Materials[materialIndex];
  Diffuse=m.diffuse;
  Ambient=m.ambient;
  Emissive=m.emissive;
  Specular=m.specular;
  Shininess=m.shininess;
#endif
  // Phong-Blinn model
  if(nlights > 0) {
    vec3 diffuse=vec3(0,0,0);
    vec3 specular=vec3(0,0,0);
    vec3 ambient=vec3(0,0,0);
    vec3 Z=vec3(0,0,1);
        
    for(int i=0; i < nlights; ++i) {
      vec3 L=normalize(lights[i].direction.xyz);
      diffuse += lights[i].diffuse.rgb*abs(dot(Normal,L));
      ambient += lights[i].ambient.rgb;
      specular += pow(abs(dot(Normal,normalize(L+Z))),Shininess)*
        lights[i].specular.rgb;
    }

    vec3 color=diffuse*Diffuse.rgb+
      ambient*Ambient.rgb+
      specular*Specular.rgb+
      Emissive.rgb;
    outColor=vec4(color,Diffuse[3]);
  } else
    outColor=Diffuse;
}