gKit2 light
Loading...
Searching...
No Matches
cubemap.glsl
Go to the documentation of this file.
1
3
4#version 430
5
6#ifdef VERTEX_SHADER
7uniform mat4 mvpMatrix;
8uniform mat4 modelMatrix;
9
10uniform samplerCube texture0;
11
12layout(location= 0) in vec3 position;
13layout(location= 2) in vec3 normal;
14out vec3 vertex_position;
15out vec3 vertex_normal;
16out vec3 vertex_diffuse;
17
18float sqr( in const float x ) { return x*x; }
19
20void main( )
21{
22 gl_Position= mvpMatrix * vec4(position, 1);
23 vertex_position= vec3(modelMatrix * vec4(position, 1));
24 vertex_normal= mat3(modelMatrix) * normalize(normal);
25
26 float size= textureSize(texture0, 0).x;
27 float dlevel= floor(log2(size));
28
29 //~ // mcguire 2103
30 //~ vertex_diffuse= textureLod(texture0, n, dlevel).rgb;
31
32 // plus precis, cf croubois 2014
33 // https://shared.hadriencroubois.com/publications/2014/MobileReality.pdf
34 // pondere chaque face par son angle solide et en fonction de la normale
35 // todo a deplacer dans le vertex shader et transmettre la couleur interpolee au fragment shader, ce sera bien plus rapide !
36 vec3 diffuse= textureLod(texture0, vec3( 1, 0, 0), dlevel).rgb * sqr(max(0, .75 + vertex_normal.x)) / 1.75;
37 diffuse+= textureLod(texture0, vec3(-1, 0, 0), dlevel).rgb * sqr(max(0, .75 - vertex_normal.x)) / 1.75;
38 diffuse+= textureLod(texture0, vec3( 0, 1, 0), dlevel).rgb * sqr(max(0, .75 + vertex_normal.y)) / 1.75;
39 diffuse+= textureLod(texture0, vec3( 0,-1, 0), dlevel).rgb * sqr(max(0, .75 - vertex_normal.y)) / 1.75;
40 diffuse+= textureLod(texture0, vec3( 0, 0, 1), dlevel).rgb * sqr(max(0, .75 + vertex_normal.z)) / 1.75;
41 diffuse+= textureLod(texture0, vec3( 0, 0, -1), dlevel).rgb * sqr(max(0, .75 - vertex_normal.z)) / 1.75;
42 vertex_diffuse= diffuse / 3;
43}
44#endif
45
46
47#ifdef FRAGMENT_SHADER
48uniform vec3 camera_position;
49uniform samplerCube texture0;
50
51const float alpha= 20;
52const float k= 0.9;
53
54in vec3 vertex_position;
55in vec3 vertex_normal;
56in vec3 vertex_diffuse;
57out vec4 fragment_color;
58
59void main( )
60{
61 vec3 v= vertex_position - camera_position;
62 vec3 n= normalize(vertex_normal);
63 //~ vec3 color= texture(texture0, n).rgb; // couleur dans la direction de la normale
64
65 vec3 m= reflect(v, n);
66 //~ vec3 color= texture(texture0, m).rgb; // couleur dans la direction du reflet miroir
67
68 // approximation pour un modele blinn - phong, cf mcguire 2103
69 float size= textureSize(texture0, 0).x;
70 float glevel= max(0, log2(size * sqrt(3)) - 0.5 * log2(alpha +1));
71 vec3 glossy= textureLod(texture0, m, glevel).rgb;
72
73 vec3 color= k * vertex_diffuse + (1 - k) * glossy;
74
75 fragment_color= vec4(color, 1);
76}
77#endif
Point max(const Point &a, const Point &b)
renvoie la plus grande composante de chaque point { max(a.x, b.x), max(a.y, b.y), max(a....
Definition vec.cpp:35
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
Definition vec.cpp:167
vecteur generique, utilitaire.
Definition vec.h:169
vecteur generique 4d, ou 3d homogene, utilitaire.
Definition vec.h:192