gKit2 light
Toggle main menu visibility
Loading...
Searching...
No Matches
gkit2_tutos
tuto9_materials.glsl
Go to the documentation of this file.
1
2
3
#version 330
4
5
#ifdef VERTEX_SHADER
6
layout(location= 0) in
vec3
position;
7
layout(location= 2) in
vec3
normal;
8
layout(location= 4) in uint material;
9
10
uniform mat4 mvpMatrix;
11
uniform mat4 mvMatrix;
12
13
out
vec3
vertex_position;
14
out
vec3
vertex_normal;
15
flat out uint vertex_material;
16
/* decoration flat : le varying est un entier, donc pas vraiment interpolable... il faut le declarer explicitement */
17
18
void
main( )
19
{
20
gl_Position= mvpMatrix *
vec4
(position, 1);
21
22
// normale dans le repere camera
23
vertex_normal= mat3(mvMatrix) * normal;
24
// et transmet aussi l'indice de la matiere au fragment shader...
25
vertex_material= material;
26
}
27
28
#endif
29
30
31
#ifdef FRAGMENT_SHADER
32
out
vec4
fragment_color;
33
34
in
vec3
vertex_normal;
35
flat in uint vertex_material;
// !! decoration flat, le varying est marque explicitement comme non interpolable !!
36
37
#define MAX_MATERIALS 16
38
uniform
vec4
materials[MAX_MATERIALS];
39
40
void
main( )
41
{
42
vec3
l=
normalize
(
vec3
(0.5, 0.25, 0) );
// todo : passer en parametre...
43
vec3
n=
normalize
(vertex_normal);
44
float
cos_theta=
max
(0,
dot
(n, l));
45
46
// recupere la couleur de la matiere du triangle, en fonction de son indice.
47
vec4
color= materials[vertex_material];
48
49
// terme ambiant
50
float
ambient=
max
(0,
dot
( n,
vec3
(0, 1, 0) ));
51
vec4
ambient_color= color * (1 + ambient) / 2;
52
53
fragment_color= 0.2 * ambient_color + color * cos_theta;
54
}
55
56
#endif
max
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
dot
float dot(const Vector &u, const Vector &v)
renvoie le produit scalaire de 2 vecteurs.
Definition
vec.cpp:181
normalize
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
Definition
vec.cpp:167
vec3
vecteur generique, utilitaire.
Definition
vec.h:169
vec4
vecteur generique 4d, ou 3d homogene, utilitaire.
Definition
vec.h:192
Generated on
for gKit2 light by
1.17.0