• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 const char kDrawTexOESGles2_vshader[] = R"(
18 precision highp float;
19 attribute highp vec3 pos;
20 attribute highp vec2 texcoord;
21 varying highp vec2 texcoord_varying;
22 void main() {
23     gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
24     texcoord_varying = texcoord;
25 }
26 )";
27 
28 const char kDrawTexOESGles2_fshader[] = R"(
29 precision highp float;
30 uniform sampler2D tex_sampler;
31 varying highp vec2 texcoord_varying;
32 void main() {
33     gl_FragColor = texture2D(tex_sampler, texcoord_varying);
34 }
35 )";
36 
37 const char kDrawTexOESCore_vshader[] = R"(#version 330 core
38 layout(location = 0) in vec3 pos;
39 layout(location = 1) in vec2 texcoord;
40 out vec2 texcoord_varying;
41 void main() {
42     gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
43     texcoord_varying = texcoord;
44 }
45 )";
46 
47 const char kDrawTexOESCore_fshader[] = R"(#version 330 core
48 uniform sampler2D tex_sampler;
49 in vec2 texcoord_varying;
50 out vec4 frag_color;
51 void main() {
52     frag_color = texture(tex_sampler, texcoord_varying);
53 }
54 )";
55 
56 // version, flat,
57 const char kGeometryDrawVShaderSrcTemplateCore[] = R"(%s
58 layout(location = 0) in vec4 pos;
59 layout(location = 1) in vec3 normal;
60 layout(location = 2) in vec4 color;
61 layout(location = 3) in float pointsize;
62 layout(location = 4) in vec4 texcoord;
63 
64 uniform mat4 projection;
65 uniform mat4 modelview;
66 uniform mat4 modelview_invtr;
67 uniform mat4 texture_matrix;
68 
69 uniform bool enable_rescale_normal;
70 uniform bool enable_normalize;
71 
72 out vec4 pos_varying;
73 out vec3 normal_varying;
74 %s out vec4 color_varying;
75 out float pointsize_varying;
76 out vec4 texcoord_varying;
77 
78 void main() {
79 
80     pos_varying = modelview * pos;
81     mat3 mvInvTr3 = mat3(modelview_invtr);
82     normal_varying = mvInvTr3 * normal;
83 
84     if (enable_rescale_normal) {
85         float rescale = 1.0;
86         vec3 rescaleVec = vec3(mvInvTr3[2]);
87         float len = length(rescaleVec);
88         if (len > 0.0) {
89             rescale = 1.0 / len;
90         }
91         normal_varying *= rescale;
92     }
93 
94     if (enable_normalize) {
95         normal_varying = normalize(normal_varying);
96     }
97 
98     color_varying = color;
99     pointsize_varying = pointsize;
100     texcoord_varying = texture_matrix * texcoord;
101 
102     gl_Position = projection * modelview * pos;
103 }
104 )";
105 
106 // version, flat,
107 const char kGeometryDrawFShaderSrcTemplateCore[] = R"(%s
108 // Defines
109 #define kMaxLights 8
110 
111 #define kModulate 0x2100
112 #define kCombine 0x8570
113 #define kReplace 0x1E01
114 
115 #define kAlpha 0x1906
116 #define kRGB 0x1907
117 #define kRGBA 0x1908
118 #define kLuminance 0x1909
119 #define kLuminanceAlpha 0x190A
120 
121 #define kLinear 0x2601
122 #define kExp 0x0800
123 #define kExp2 0x0801
124 
125 precision highp float;
126 uniform sampler2D tex_sampler;
127 uniform samplerCube tex_cube_sampler;
128 uniform bool enable_textures;
129 uniform bool enable_lighting;
130 uniform bool enable_color_material;
131 uniform bool enable_fog;
132 uniform bool enable_reflection_map;
133 
134 uniform int texture_env_mode;
135 uniform int texture_format;
136 
137 // material (front+back)
138 uniform vec4 material_ambient;
139 uniform vec4 material_diffuse;
140 uniform vec4 material_specular;
141 uniform vec4 material_emissive;
142 uniform float material_specular_exponent;
143 
144 // lights
145 uniform vec4 light_model_scene_ambient;
146 uniform bool light_model_two_sided;
147 
148 uniform bool light_enables[kMaxLights];
149 uniform vec4 light_ambients[kMaxLights];
150 uniform vec4 light_diffuses[kMaxLights];
151 uniform vec4 light_speculars[kMaxLights];
152 uniform vec4 light_positions[kMaxLights];
153 uniform vec3 light_directions[kMaxLights];
154 uniform float light_spotlight_exponents[kMaxLights];
155 uniform float light_spotlight_cutoff_angles[kMaxLights];
156 uniform float light_attenuation_consts[kMaxLights];
157 uniform float light_attenuation_linears[kMaxLights];
158 uniform float light_attenuation_quadratics[kMaxLights];
159 
160 // fog
161 uniform int fog_mode;
162 uniform float fog_density;
163 uniform float fog_start;
164 uniform float fog_end;
165 uniform vec4 fog_color;
166 
167 in vec4 pos_varying;
168 in vec3 normal_varying;
169 %s in vec4 color_varying;
170 in float pointsize_varying;
171 in vec4 texcoord_varying;
172 
173 out vec4 frag_color;
174 
175 float posDot(vec3 a, vec3 b) {
176     return max(dot(a, b), 0.0);
177 }
178 
179 void main() {
180     vec4 currentColor;
181 
182     if (enable_textures) {
183         vec4 textureColor;
184         if (enable_reflection_map) {
185             textureColor = texture(tex_cube_sampler, reflect(pos_varying.xyz, normalize(normal_varying)));
186             currentColor = textureColor;
187         } else {
188             textureColor = texture(tex_sampler, texcoord_varying.xy);
189             switch (texture_env_mode) {
190             case kReplace:
191                 switch (texture_format) {
192                 case kAlpha:
193                     currentColor.rgb = color_varying.rgb;
194                     currentColor.a = textureColor.a;
195                     break;
196                 case kRGBA:
197                 case kLuminanceAlpha:
198                     currentColor.rgba = textureColor.rgba;
199                     break;
200                 case kRGB:
201                 case kLuminance:
202                 default:
203                     currentColor.rgb = textureColor.rgb;
204                     currentColor.a = color_varying.a;
205                     break;
206                 }
207                 break;
208             case kCombine:
209             case kModulate:
210             default:
211                 switch (texture_format) {
212                 case kAlpha:
213                     currentColor.rgb = color_varying.rgb;
214                     currentColor.a = color_varying.a * textureColor.a;
215                     break;
216                 case kRGBA:
217                 case kLuminanceAlpha:
218                     currentColor.rgba = color_varying.rgba * textureColor.rgba;
219                     break;
220                 case kRGB:
221                 case kLuminance:
222                 default:
223                     currentColor.rgb = color_varying.rgb * textureColor.rgb;
224                     currentColor.a = color_varying.a;
225                     break;
226                 }
227                 break;
228             }
229         }
230     } else {
231         currentColor = color_varying;
232     }
233 
234     if (enable_lighting) {
235 
236     vec4 materialAmbientActual = material_ambient;
237     vec4 materialDiffuseActual = material_diffuse;
238 
239     if (enable_color_material || enable_textures) {
240         materialAmbientActual = currentColor;
241         materialDiffuseActual = currentColor;
242     }
243 
244     vec4 lit = material_emissive +
245                materialAmbientActual * light_model_scene_ambient;
246 
247     for (int i = 0; i < kMaxLights; i++) {
248 
249         if (!light_enables[i]) continue;
250 
251         vec4 lightAmbient = light_ambients[i];
252         vec4 lightDiffuse = light_diffuses[i];
253         vec4 lightSpecular = light_speculars[i];
254         vec4 lightPos = light_positions[i];
255         vec3 lightDir = light_directions[i];
256         float attConst = light_attenuation_consts[i];
257         float attLinear = light_attenuation_linears[i];
258         float attQuadratic = light_attenuation_quadratics[i];
259         float spotAngle = light_spotlight_cutoff_angles[i];
260         float spotExponent = light_spotlight_exponents[i];
261 
262         vec3 toLight;
263         if (lightPos.w == 0.0) {
264             toLight = lightPos.xyz;
265         } else {
266             toLight = (lightPos.xyz / lightPos.w - pos_varying.xyz);
267         }
268 
269         float lightDist = length(toLight);
270         vec3 h = normalize(toLight) + vec3(0.0, 0.0, 1.0);
271         float ndotL = posDot(normal_varying, normalize(toLight));
272         float ndoth = posDot(normal_varying, normalize(h));
273 
274         float specAtt;
275 
276         if (ndotL != 0.0) {
277             specAtt = 1.0;
278         } else {
279             specAtt = 0.0;
280         }
281 
282         float att;
283 
284         if (lightPos.w != 0.0) {
285             float attDenom = (attConst + attLinear * lightDist +
286                               attQuadratic * lightDist * lightDist);
287             att = 1.0 / attDenom;
288         } else {
289             att = 1.0;
290         }
291 
292         float spot;
293 
294         float spotAngleCos = cos(radians(spotAngle));
295         vec3 toSurfaceDir = -normalize(toLight);
296         float spotDot = posDot(toSurfaceDir, normalize(lightDir));
297 
298         if (spotAngle == 180.0 || lightPos.w == 0.0) {
299             spot = 1.0;
300         } else {
301             if (spotDot < spotAngleCos) {
302                 spot = 0.0;
303             } else {
304                 spot = pow(spotDot, spotExponent);
305             }
306         }
307 
308         vec4 contrib = materialAmbientActual * lightAmbient;
309         contrib += ndotL * materialDiffuseActual * lightDiffuse;
310         if (ndoth > 0.0 && material_specular_exponent > 0.0) {
311             contrib += specAtt * pow(ndoth, material_specular_exponent) *
312                                  material_specular * lightSpecular;
313         } else {
314             if (ndoth > 0.0) {
315                 contrib += specAtt * material_specular * lightSpecular;
316             }
317         }
318         contrib *= att * spot;
319         lit += contrib;
320     }
321 
322     currentColor = lit;
323 
324     }
325 
326     if (enable_fog) {
327 
328     float eyeDist = -pos_varying.z / pos_varying.w;
329     float f = 1.0;
330     switch (fog_mode) {
331         case kExp:
332             f = exp(-fog_density * eyeDist);
333             break;
334         case kExp2:
335             f = exp(-(pow(fog_density * eyeDist, 2.0)));
336             break;
337         case kLinear:
338             f = (fog_end - eyeDist) / (fog_end - fog_start);
339             break;
340         default:
341             break;
342     }
343 
344     currentColor = f * currentColor + (1.0 - f) * fog_color;
345 
346     }
347 
348     frag_color = currentColor;
349 }
350 )";
351