summaryrefslogtreecommitdiff
path: root/src/modules/evas/engines/gl_common/shader_3d/normal_map_vert.shd
blob: 3dc42d288fcd37c9048adae59f45a91870b162af (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
uniform  mat4  uMatrixMvp;
uniform  mat3  uMatrixNormal;
uniform  mat4  uMatrixModelview;
uniform  vec4  uLightPosition;
varying  vec3  vLightVector;
varying  vec3  vLightHalfVector;
varying  vec3  vEyeVector;

#ifndef VERTEX_TANGENT
varying  vec3  vNormal;
#endif //VERTEX_TANGENT

#ifdef SHADOWED
uniform mat4 uMatrixLight;
varying vec4 vLightPosition;
#endif //SHADOWED

#ifdef VERTEX_POSITION
attribute   vec4  aPosition0;
#endif //VERTEX_POSITION

#ifdef VERTEX_POSITION_BLEND
attribute   vec4  aPosition1;
uniform     float uPositionWeight;
#endif //VERTEX_POSITION_BLEND

#ifdef VERTEX_NORMAL
attribute   vec4  aNormal0;
#endif //VERTEX_NORMAL

#ifdef VERTEX_NORMAL_BLEND
attribute   vec4  aNormal1;
uniform     float uNormalWeight;
#endif //VERTEX_NORMAL_BLEND

#ifdef VERTEX_TANGENT
attribute   vec4  aTangent0;
#endif //VERTEX_TANGENT

#ifdef VERTEX_TANGENT_BLEND
attribute   vec4  aTangent1;
uniform     float uTangentWeight;
#endif //VERTEX_TANGENT_BLEND

#ifdef VERTEX_TEXCOORD
attribute   vec4  aTexCoord0;
#endif //VERTEX_TEXCOORD

#ifdef VERTEX_TEXCOORD_BLEND
attribute   vec4  aTexCoord1;
uniform     float uTexCoordWeight;
#endif //VERTEX_TEXCOORD_BLEND

#ifdef NEED_TEX_COORD
varying vec2 vTexCoord;
#endif //NEED_TEX_COORD

#ifdef LIGHT_ATTENUATION
varying  float vLightDist;
#endif //LIGHT_ATTENUATION

#ifndef VERTEX_TANGENT
void vertexNormalMap(vec4 position, vec3 normal)
{
   normal = uMatrixNormal * normal;
   position = uMatrixModelview * position;
   vEyeVector = normalize(-position.xyz);

#ifdef NORMALIZE_NORMALS
   normal = normalize(normal);
#endif //NORMALIZE_NORMALS

#ifdef LIGHT_DIRECTIONAL
   vLightVector = uLightPosition.xyz;
#else
   vLightVector = uLightPosition.xyz - position.xyz;

#ifdef LIGHT_ATTENUATION
   vLightDist = length(vLightVector);
#endif //LIGHT_ATTENUATION

   vLightVector = normalize(vLightVector);
#endif //LIGHT_DIRECTIONAL

   vLightHalfVector = normalize(vEyeVector + vLightVector);
   vNormal = normal;
}

#else

void vertexNormalMap(vec4 position, vec3 normal, vec3 tangent)
{
   vec3 n = normalize(uMatrixNormal * normal);
   vec3 t = normalize(uMatrixNormal * tangent);
   vec3 b = cross(n, t);
   vec3 tmp;

   position = uMatrixModelview * position;

#ifdef LIGHT_DIRECTIONAL
   vec3 lightDir = uLightPosition.xyz;
#else
   vec3 lightDir = uLightPosition.xyz - position.xyz;

#ifdef LIGHT_ATTENUATION
   vLightDist = length(lightDir);
#endif //LIGHT_ATTENUATION

   lightDir = normalize(lightDir);
#endif //LIGHT_DIRECTIONAL

   tmp.x = dot(lightDir, t);
   tmp.y = dot(lightDir, b);
   tmp.z = dot(lightDir, n);
   vLightVector = tmp;

   tmp.x = dot(position.xyz, t);
   tmp.y = dot(position.xyz, b);
   tmp.z = dot(position.xyz, n);
   vEyeVector = normalize(tmp);

   vec3 hv = normalize(normalize(-position.xyz) + lightDir);
   tmp.x = dot(hv, t);
   tmp.y = dot(hv, b);
   tmp.z = dot(hv, n);
   vLightHalfVector = tmp;
}
#endif //VERTEX_TANGENT

void main()
{

#ifdef VERTEX_POSITION_BLEND
   vec4 position = aPosition0 * uPositionWeight +
   aPosition1 * (1.0 - uPositionWeight);
   position = vec4(position.xyz, 1.0);
#else

#ifdef VERTEX_POSITION
   vec4 position = vec4(aPosition0.xyz, 1.0);
#endif // VERTEX_POSITION

#endif //VERTEX_POSITION_BLEND

#ifdef VERTEX_NORMAL_BLEND
   vec3 normal = aNormal0.xyz * uNormalWeight +
   aNormal1.xyz * (1.0 - uNormalWeight);
#else

#ifdef VERTEX_NORMAL
   vec3 normal = aNormal0.xyz;
#endif //VERTEX_NORMAL

#endif //VERTEX_NORMAL_BLEND

#ifdef VERTEX_TANGENT_BLEND
   vec3 tangent = aTangent0.xyz * uTangentWeight +
   aTangent1.xyz * (1.0 - uTangentWeight);
#else

#ifdef VERTEX_TANGENT
   vec3 tangent = aTangent0.xyz;
#endif //VERTEX_TANGENT

#endif //VERTEX_TANGENT_BLEND

#ifdef VERTEX_TEXCOORD_BLEND
   vTexCoord = aTexCoord0.st * uTexCoordWeight +
   aTexCoord1.st * (1.0 - uTexCoordWeight);
#else

#ifdef VERTEX_TEXCOORD
   vTexCoord = aTexCoord0.st;
#endif //VERTEX_TEXCOORD

#endif //VERTEX_TEXCOORD_BLEND

   gl_Position = uMatrixMvp * position;

#ifdef  VERTEX_TANGENT
   vertexNormalMap(position, normal, tangent);
#else
   vertexNormalMap(position, normal);
#endif //VERTEX_TANGENT

#ifdef SHADOWED
   vLightPosition = uMatrixLight * position;
#endif
}