source: trunk/tutorial/04_texture.lolfx @ 2001

Last change on this file since 2001 was 2001, checked in by sam, 10 years ago

tutorial: add a new texture generation example.

File size: 1.3 KB
Line 
1[vert.glsl]
2
3#version 120
4
5attribute vec2 in_Position;
6
7varying vec4 pass_Position;
8
9void main(void)
10{
11    pass_Position = vec4(0.5 * in_Position + 0.5, 0.0, 1.0);
12    gl_Position = vec4(in_Position, 0.0, 1.0);
13}
14
15[frag.glsl]
16
17#version 120
18
19uniform sampler2D u_Texture;
20
21varying vec4 pass_Position;
22
23float rand(in vec2 p, in float v)
24{
25    return fract(v * sin(dot(p, vec2(1298.9837, 7823.33145))));
26}
27
28void main(void)
29{
30    vec2 t = pass_Position.xy;
31    vec4 c0 = texture2D(u_Texture, t);
32    float f = rand(pass_Position.xy, 12345.67);
33
34    if (t.y > c0.x)
35    {
36        /* Sky */
37        float val = min(t.y * 2.0, 1.0);
38        if (f > 0.999)
39            gl_FragColor = vec4(1.0);
40        else
41            gl_FragColor = vec4(0.4, t.y, val, 1.0);
42    }
43    else if (t.y > c0.x - 0.02)
44    {
45        /* Grass */
46        if (f > 0.99)
47            gl_FragColor = vec4(0.4, 0.7, 0.3, 1.0);
48        else if (f > 0.9)
49            gl_FragColor = vec4(0.3, 0.6, 0.2, 1.0);
50        else
51            gl_FragColor = vec4(0.2, 0.5, 0.1, 1.0);
52    }
53    else
54    {
55        /* Earth */
56        if (f > 0.99)
57            gl_FragColor = vec4(0.7, 0.4, 0.3, 1.0);
58        else if (f > 0.9)
59            gl_FragColor = vec4(0.6, 0.3, 0.2, 1.0);
60        else
61            gl_FragColor = vec4(0.5, 0.2, 0.1, 1.0);
62    }
63}
64
Note: See TracBrowser for help on using the repository browser.