Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | #version 140 |
2 | #extension GL_ARB_explicit_attrib_location : require |
||
3 | |||
4 | layout (location = 0) in vec3 a_pos; |
||
5 | layout (location = 1) in vec3 a_normal; |
||
6 | layout (location = 2) in vec2 a_uv; |
||
7 | layout (location = 3) in float a_color; |
||
8 | |||
9 | out vec3 v_frag_pos; |
||
10 | out vec3 v_normal; |
||
11 | out vec2 v_tex_coord; |
||
12 | out float v_color; |
||
13 | |||
14 | uniform mat4 u_model; |
||
15 | uniform mat4 u_view; |
||
16 | uniform mat4 u_projection; |
||
17 | uniform uint u_material_flags; |
||
18 | uniform mat4 u_normal_matrix; |
||
19 | |||
20 | // material_flags values |
||
21 | const uint BR_MATF_ENVIRONMENT_L = 0x10u; |
||
22 | |||
23 | // https://rr2000.cwaboard.co.uk/R4/BRENDER/TEBK_43.HTM#HEADING331 |
||
24 | // https://www.clicktorelease.com/blog/creating-spherical-environment-mapping-shader/ |
||
25 | void do_spherical_environment_map(void) { |
||
26 | mat4 view_model = u_view * u_model; |
||
27 | vec3 e = normalize( vec3( view_model * vec4( a_pos, 1.0 ) ) ); |
||
28 | vec3 n = normalize( mat3(u_normal_matrix) * a_normal ); |
||
29 | vec3 r = reflect( e, n ); |
||
30 | float m = 2. * sqrt( pow( r.x, 2. ) + pow( r.y, 2. ) + pow( r.z + 1., 2. ) ); |
||
31 | v_tex_coord = r.xy / m + .5; |
||
32 | } |
||
33 | |||
34 | void main(void) { |
||
35 | v_frag_pos = vec3(u_model * vec4(a_pos, 1.0)); |
||
36 | v_normal = a_normal; |
||
37 | v_tex_coord = a_uv; |
||
38 | v_color = a_color; |
||
39 | gl_Position = u_projection * u_view * vec4(v_frag_pos, 1.0); |
||
40 | |||
41 | if ((u_material_flags & BR_MATF_ENVIRONMENT_L) != 0u) { |
||
42 | do_spherical_environment_map(); |
||
43 | } |
||
44 | } |