1
2/*
3Copyright (c) 2019 The Khronos Group Inc.
4Use of this source code is governed by an MIT-style license that can be
5found in the LICENSE.txt file.
6*/
7
8
9attribute vec4 gtf_Color;
10attribute vec4 gtf_Vertex;
11attribute vec3 gtf_Normal;
12attribute vec4 gtf_MultiTexCoord0;
13
14uniform mat4 gtf_ModelViewProjectionMatrix;
15uniform mat3 gtf_NormalMatrix;
16
17varying vec4 gtf_TexCoord[1];
18varying vec4 color;
19
20vec4 Ambient;
21vec4 Diffuse;
22vec4 Specular;
23
24const vec3 lightPosition = vec3(0.0, 0.0, 1.0);
25const vec3 spotDirection = vec3(0.0, 0.0, -1.0);
26const float spotCutoff = 180.0;
27const float spotExponent = 0.0;
28
29const float lightAttenuationConstant = 1.0;
30const float lightAttenuationLinear = 0.0;
31const float lightAttenuationQuadratic = 0.0;
32
33const vec4 lightAmbient = vec4(0.0, 0.0, 0.0, 0.0);
34vec4 lightDiffuse = vec4(1.0, 1.0, 1.0, 1.0);
35vec4 lightSpecular = vec4(1.0, 1.0, 1.0, 1.0);
36
37const float materialShininess = 0.0;
38
39const vec4 sceneColor = vec4(0.0, 0.0, 0.0, 0.0);
40
41void spotLight(in int i,
42               in vec3 normal,
43               in vec3 eye,
44               in vec3 ecPosition3
45               )
46{
47    float nDotVP;           // normal . light direction
48    float nDotHV;           // normal . light half vector
49    float pf;               // power factor
50    float spotDot;          // cosine of angle between spotlight
51    float spotAttenuation;  // spotlight attenuation factor
52    float attenuation;      // computed attenuation factor
53    float d;                // distance from surface to light source
54    vec3 VP;                // direction from surface to light position
55    vec3 halfVector;        // direction of maximum highlights
56
57    // Compute vector from surface to light position
58    VP = lightPosition - ecPosition3;
59
60    // Compute distance between surface and light position
61    d = length(VP);
62
63    // Normalize the vector from surface to light position
64    VP = normalize(VP);
65
66    // Compute attenuation
67    attenuation = 1.0 / (lightAttenuationConstant +
68                         lightAttenuationLinear * d +
69                         lightAttenuationQuadratic * d * d);
70
71    // See if point on surface is inside cone of illumination
72    spotDot = dot(-VP, normalize(spotDirection));
73
74    if (spotDot < cos(radians(spotCutoff)))
75        spotAttenuation = 0.0; // light adds no contribution
76    else
77        spotAttenuation = pow(spotDot, spotExponent);
78
79    // Combine the spotlight and distance attenuation.
80    attenuation *= spotAttenuation;
81
82    halfVector = normalize(VP + eye);
83
84    nDotVP = max(0.0, dot(normal, VP));
85    nDotHV = max(0.0, dot(normal, halfVector));
86
87    if (nDotVP == 0.0)
88        pf = 0.0;
89    else
90        pf = pow(nDotHV, materialShininess);
91
92    Ambient  += lightAmbient * attenuation;
93    Diffuse  += lightDiffuse * nDotVP * attenuation;
94    Specular += lightSpecular * pf * attenuation;
95}
96
97vec3 fnormal(void)
98{
99    //Compute the normal
100    vec3 normal = gtf_NormalMatrix * gtf_Normal;
101    normal = normalize(normal);
102
103	return normal;
104}
105
106void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)
107{
108    vec3 ecPosition3;
109    vec3 eye;
110
111    ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;
112    eye = vec3 (0.0, 0.0, 1.0);
113
114    // Clear the light intensity accumulators
115    Ambient  = vec4 (0.0);
116    Diffuse  = vec4 (0.0);
117    Specular = vec4 (0.0);
118
119   //lightSpecular = gtf_Color;
120
121    spotLight(0, normal, eye, ecPosition3);
122
123    color = sceneColor +
124      Ambient  * gtf_Color +
125      Diffuse  * gtf_Color;
126    color += Specular * gtf_Color;
127    color = clamp( color, 0.0, 1.0 );
128
129    color.a *= alphaFade;
130}
131
132void main (void)
133{
134	vec3  transformedNormal;
135    float alphaFade = 1.0;
136
137	vec4 ecPosition = gtf_Vertex;
138
139	color = gtf_Color;
140	gtf_TexCoord[0] = gtf_MultiTexCoord0;
141	gl_Position = gtf_ModelViewProjectionMatrix * gtf_Vertex;
142	transformedNormal = fnormal();
143    flight(transformedNormal, ecPosition, alphaFade);
144}
145