File: fs_shadowvolume_texture_lighting.sc

package info (click to toggle)
mame 0.228%2Bdfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 956,280 kB
  • sloc: cpp: 4,712,769; xml: 1,946,353; ansic: 829,986; sh: 49,187; lisp: 18,100; python: 17,897; makefile: 10,815; cs: 10,047; javascript: 8,196; yacc: 7,565; java: 7,151; objc: 5,791; asm: 4,639; perl: 2,850; ada: 1,681; lex: 1,174; pascal: 1,139; ruby: 317; awk: 35
file content (89 lines) | stat: -rw-r--r-- 2,610 bytes parent folder | download | duplicates (4)
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
$input v_normal, v_view, v_texcoord0

/*
 * Copyright 2013-2014 Dario Manesku. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "../common/common.sh"
uniform vec4 u_params;
uniform vec4 u_svparams;
uniform vec4 u_ambient;
uniform vec4 u_diffuse;
uniform vec4 u_specular_shininess;
uniform vec4 u_fog;
uniform vec4 u_lightPosRadius;
uniform vec4 u_lightRgbInnerR;
SAMPLER2D(s_texColor,   0);
SAMPLER2D(s_texStencil, 1);

#define u_ambientPass   u_params.x
#define u_lightingPass  u_params.y
#define u_texelHalf     u_params.z
#define u_specular      u_specular_shininess.xyz
#define u_shininess     u_specular_shininess.w

#define u_fogColor      u_fog.xyz
#define u_fogDensity    u_fog.w

#define u_useStencilTex u_svparams.x

vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
{
	float ndotl = dot(_normal, _lightDir);
	vec3 reflected = 2.0*ndotl*_normal - _lightDir; // reflect(_lightDir, _normal);
	float rdotv = dot(reflected, _viewDir);
	return vec2(ndotl, rdotv);
}

vec4 lit(float _ndotl, float _rdotv, float _m)
{
	float diff = max(0.0, _ndotl);
	float spec = step(0.0, _ndotl) * pow(max(0.0, _rdotv), _m);
	return vec4(1.0, diff, spec, 1.0);
}

vec3 calcLight(vec3 _view, vec3 _normal, vec3 _viewDir)
{
	vec3 lightPos = mul(u_view, vec4(u_lightPosRadius.xyz, 1.0)).xyz;
	vec3 toLight = lightPos - _view;
	vec3 lightDir = normalize(toLight);

	vec2 bln = blinn(lightDir, _normal, _viewDir);
	vec4 lc = lit(bln.x, bln.y, u_shininess);

	float dist = max(length(toLight), u_lightPosRadius.w);
	float attn = 50.0 * pow(dist, -2.0);
	vec3 rgb = (lc.y * u_diffuse.xyz + lc.z * u_specular) * u_lightRgbInnerR.rgb * attn;

	return rgb;
}

void main()
{
	vec3 ambientColor = u_ambient.xyz * u_ambientPass;

	vec3 normal = normalize(v_normal);
	vec3 viewDir = -normalize(v_view);
	vec3 lightColor = calcLight(v_view, normal, viewDir) * u_lightingPass;

	vec2 ndc = gl_FragCoord.xy * u_viewTexel.xy + u_viewTexel.xy * u_texelHalf;
	vec4 texcolor = texture2D(s_texStencil, ndc);
	float s = (texcolor.x - texcolor.y) + 2.0 * (texcolor.z - texcolor.w);
	s *= u_useStencilTex;

	const float LOG2 = 1.442695;
	float z = length(v_view);
	float fogFactor = 1.0/exp2(u_fogDensity*u_fogDensity*z*z*LOG2);
	fogFactor = clamp(fogFactor, 0.0, 1.0);

	vec3 color = toLinear(texture2D(s_texColor, v_texcoord0)).xyz;

	vec3 ambient = toGamma(ambientColor * color);
	vec3 diffuse = toGamma(lightColor * color);
	vec3 final   = mix(ambient, ambient + diffuse, float((abs(s) < 0.0001)));

	gl_FragColor.xyz = mix(u_fogColor, final, fogFactor);
	gl_FragColor.w   = 1.0;
}