gKit2 light
Toggle main menu visibility
Loading...
Searching...
No Matches
gkit2_tutos
M2
raytrace.glsl
Go to the documentation of this file.
1
3
4
#version 330
5
6
#ifdef VERTEX_SHADER
7
out
vec2
position;
8
9
void
main(
void
)
10
{
11
vec3
positions[3]=
vec3
[3](
vec3
(-1, 1, -1),
vec3
( -1, -3, -1),
vec3
( 3, 1, -1) );
12
13
gl_Position=
vec4
(positions[gl_VertexID], 1.0);
14
position= positions[gl_VertexID].xy;
15
}
16
#endif
17
18
19
#ifdef FRAGMENT_SHADER
20
21
struct
Triangle
22
{
23
vec3 a;
// sommet
24
vec3 ab;
// arete 1
25
vec3 ac;
// arete 2
26
};
27
28
// uniform buffer 0
29
uniform triangleData
30
{
31
Triangle
triangles[1024];
32
};
33
34
35
bool
intersect(
const
Triangle
triangle,
const
vec3
o,
const
vec3
d,
const
float
tmax, out
float
rt, out
float
ru, out
float
rv )
36
{
37
vec3
pvec=
cross
(d, triangle.ac);
38
float
det=
dot
(triangle.ab, pvec);
39
float
inv_det= 1.0f / det;
40
41
vec3
tvec= o - triangle.a;
42
float
u=
dot
(tvec, pvec) * inv_det;
43
vec3
qvec=
cross
(tvec, triangle.ab);
44
float
v=
dot
(d, qvec) * inv_det;
45
46
/* calculate t, ray intersects triangle */
47
rt=
dot
(triangle.ac, qvec) * inv_det;
48
ru= u;
49
rv= v;
50
51
// ne renvoie vrai que si l'intersection est valide :
52
// interieur du triangle, 0 < u < 1, 0 < v < 1, 0 < u+v < 1
53
if
(any(greaterThan(
vec3
(u, v, u+v),
vec3
(1, 1, 1))) || any(lessThan(
vec2
(u, v),
vec2
(0, 0))))
54
return
false
;
55
// comprise entre 0 et tmax du rayon
56
return
(rt < tmax && rt > 0);
57
}
58
59
uniform mat4 mvpInvMatrix;
60
uniform
int
triangle_count;
61
62
in
vec2
position;
63
out
vec4
fragment_color;
64
65
void
main( )
66
{
67
// construction du rayon pour le pixel, passage depuis le repere projectif
68
vec4
oh= mvpInvMatrix *
vec4
(position, 0, 1);
// origine sur near
69
vec4
eh= mvpInvMatrix *
vec4
(position, 1, 1);
// extremite sur far
70
71
// origine et direction
72
vec3
o= oh.xyz / oh.w;
// origine
73
vec3
d= eh.xyz / eh.w - oh.xyz / oh.w;
// direction
74
75
float
hit= 1;
// tmax = far, une intersection valide est plus proche que l'extremite du rayon / far...
76
float
hitu= 0;
77
float
hitv= 0;
78
int
hitid= 0;
79
for
(
int
i= 0; i < triangle_count; i++)
80
{
81
float
t, u, v;
82
if
(intersect(triangles[i], o, d, hit, t, u, v))
83
{
84
hit= t;
85
hitu= u;
86
hitv= v;
87
hitid= i;
88
}
89
}
90
91
fragment_color=
vec4
(hitu, hitv, 0, 1);
92
}
93
#endif
dot
float dot(const Vector &u, const Vector &v)
renvoie le produit scalaire de 2 vecteurs.
Definition
vec.cpp:181
cross
Vector cross(const Vector &u, const Vector &v)
renvoie le produit vectoriel de 2 vecteurs.
Definition
vec.cpp:173
Triangle
triangle pour le bvh, cf fonction bounds() et intersect().
Definition
tuto_bvh.cpp:84
vec2
vecteur generique, utilitaire.
Definition
vec.h:152
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