source: trunk/src/easymesh/shiny.lolfx @ 1765

Last change on this file since 1765 was 1765, checked in by sam, 8 years ago

easymesh: fragment-based lighting.

File size: 3.4 KB
Line 
1
2-- GLSL.Vert --
3#version 120
4
5attribute vec3 in_Vertex;
6attribute vec3 in_Normal;
7attribute vec4 in_Color;
8
9uniform mat4 in_ModelView;
10uniform mat4 in_Proj;
11uniform mat3 in_NormalMat;
12
13varying vec4 pass_Eye;
14varying vec3 pass_TNormal;
15varying vec4 pass_Color;
16
17void main(void)
18{
19    vec4 eye = in_ModelView * vec4(in_Vertex, 1.0);
20    vec3 tnorm = normalize(in_NormalMat * in_Normal);
21
22    pass_Eye = eye;
23    pass_TNormal = tnorm;
24    pass_Color = in_Color;
25
26    gl_Position = in_Proj * eye;
27}
28
29-- GLSL.Frag --
30#version 120
31
32#if defined GL_ES
33precision highp float;
34#endif
35
36uniform float in_Damage;
37
38varying vec4 pass_Eye;
39varying vec3 pass_TNormal;
40varying vec4 pass_Color;
41
42// FIXME: the light direction should be passed in the code
43vec3 in_LightDir = vec3(0.3, 0.3, 0.7);
44
45void main(void) {
46    /* Material properties */
47    vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
48    float specular_power = 60.0;
49
50    /* World properties */
51    float ambient_mul = 0.5;
52    vec3 ambient_color = vec3(0.25, 0.2, 0.35);
53    vec3 diffuse_color = vec3(1.0, 1.0, 0.6);
54    vec3 specular_color = vec3(1.0, 1.0, 0.6);
55
56    vec3 s = normalize(in_LightDir); /* normalize(pass_Eye - lightpos); */
57    vec3 v = normalize(-pass_Eye.xyz);
58    vec3 r = reflect(-s, pass_TNormal);
59
60    vec3 ambient = ambient_color;
61    float sdotn = max(dot(s, pass_TNormal), 0.0);
62    vec3 diffuse = diffuse_color * sdotn;
63    vec3 specular = vec3(0.0, 0.0, 0.0);
64    if (sdotn > 0.0)
65        specular = specular_color * specular_reflect
66                 * pow(max(dot(r, v), 0.0), specular_power);
67    vec3 light = ambient + diffuse + specular;
68
69    vec4 real_color = in_Damage * vec4(1.2, 1.2, 1.2, 1.0)
70                    + (1.0 - in_Damage) * pass_Color;
71    gl_FragColor = real_color * vec4(light, 1.0);
72}
73
74-- HLSL.Vert --
75
76void main(float3 in_Vertex : POSITION,
77          float3 in_Normal : NORMAL,
78          float4 in_Color : COLOR,
79          uniform float4x4 in_ModelView,
80          uniform float4x4 in_Proj,
81          uniform float3x3 in_NormalMat,
82          uniform float in_Damage,
83          out float4 out_Position : POSITION,
84          out float4 pass_Color : COLOR)
85{
86    float3 in_LightDir = normalize(float3(0.3, 0.3, 0.7));
87
88    float3 specular_reflect = float3(0.8, 0.75, 0.4);
89    float specular_power = 60.0;
90    float ambient_mul = 0.5;
91    float3 ambient_color = float3(0.25, 0.2, 0.35);
92    float3 diffuse_color = float3(1.0, 1.0, 0.6);
93    float3 specular_color = float3(1.0, 1.0, 0.6);
94    float3 tnorm = normalize(mul(in_NormalMat, in_Normal));
95    float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0));
96    float3 s = in_LightDir;
97    float3 v = normalize(-eye.xyz);
98    float3 r = reflect(-s, tnorm);
99    float3 ambient = ambient_color;
100    float sdotn = max(dot(s, tnorm), 0.0);
101    float3 diffuse = diffuse_color * sdotn;
102    float3 specular = float3(0.0, 0.0, 0.0);
103    if (sdotn > 0.0)
104        specular = specular_color * specular_reflect
105                 * pow(max(dot(r, v), 0.0), specular_power);
106    float3 light = ambient + diffuse + specular;
107#ifdef _XBOX
108    float4 real_color = in_Color.abgr;
109#else
110    float4 real_color = in_Color;
111#endif
112    real_color = in_Damage * float4(1.2, 1.2, 1.2, 1.0)
113               + (1.0 - in_Damage) * real_color;
114    pass_Color = real_color * float4(light, 1.0);
115    out_Position = mul(in_Proj, eye);
116}
117
118-- HLSL.Frag --
119
120void main(float4 pass_Color : COLOR,
121          out float4 out_FragColor : COLOR)
122{
123    out_FragColor = pass_Color;
124}
125
Note: See TracBrowser for help on using the repository browser.