gKit2 light
Loading...
Searching...
No Matches
raytrace_compute.glsl
1
2#version 430
3
4#ifdef COMPUTE_SHADER
5
6struct Triangle
7{
8 vec3 a; // sommet
9 vec3 ab; // arete 1
10 vec3 ac; // arete 2
11};
12
13// shader storage buffer 0
14layout(std430, binding= 0) readonly buffer triangleData
15{
16 Triangle triangles[];
17};
18
19
20bool intersect( const Triangle triangle, const vec3 o, const vec3 d, const float tmax, out float rt, out float ru, out float rv )
21{
22 vec3 pvec= cross(d, triangle.ac);
23 float det= dot(triangle.ab, pvec);
24 float inv_det= 1.0f / det;
25
26 vec3 tvec= o - triangle.a;
27 float u= dot(tvec, pvec) * inv_det;
28 vec3 qvec= cross(tvec, triangle.ab);
29 float v= dot(d, qvec) * inv_det;
30
31 /* calculate t, ray intersects triangle */
32 rt= dot(triangle.ac, qvec) * inv_det;
33 ru= u;
34 rv= v;
35
36 // ne renvoie vrai que si l'intersection est valide :
37 // interieur du triangle, 0 < u < 1, 0 < v < 1, 0 < u+v < 1
38 if(any(greaterThan(vec3(u, v, u+v), vec3(1, 1, 1))) || any(lessThan(vec2(u, v), vec2(0, 0))))
39 return false;
40 // comprise entre 0 et tmax du rayon
41 return (rt < tmax && rt > 0);
42}
43
44uniform mat4 invMatrix;
45
46// image resultat
47layout(binding= 0, rgba8) writeonly uniform image2D image;
48
49// 8x8 threads
50layout( local_size_x= 8, local_size_y= 8 ) in;
51void main( )
52{
53 // recupere le threadID 2d, et l'utilise directement comme coordonnees de pixel
54 vec2 position= vec2(gl_GlobalInvocationID.xy);
55
56 // construction du rayon pour le pixel, passage depuis le repere image vers le repere monde
57 vec4 oh= invMatrix * vec4(position, 0, 1); // origine sur near
58 vec4 eh= invMatrix * vec4(position, 1, 1); // extremite sur far
59
60 // origine et direction
61 vec3 o= oh.xyz / oh.w; // origine
62 vec3 d= eh.xyz / eh.w - oh.xyz / oh.w; // direction
63
64 float hit= 1; // tmax = far, une intersection valide est plus proche que l'extremite du rayon / far...
65 float hitu= 0;
66 float hitv= 0;
67 for(int i= 0; i < triangles.length(); i++)
68 {
69 float t, u, v;
70 if(intersect(triangles[i], o, d, hit, t, u, v))
71 {
72 hit= t;
73 hitu= u;
74 hitv= v;
75 }
76 }
77
78 // ecrire le resultat dans l'image
79 imageStore(image, ivec2(gl_GlobalInvocationID.xy), vec4(hitu, hitv, 0, 1));
80}
81#endif
float dot(const Vector &u, const Vector &v)
renvoie le produit scalaire de 2 vecteurs.
Definition vec.cpp:181
Vector cross(const Vector &u, const Vector &v)
renvoie le produit vectoriel de 2 vecteurs.
Definition vec.cpp:173
triangle pour le bvh, cf fonction bounds() et intersect().
Definition tuto_bvh.cpp:84
vecteur generique, utilitaire.
Definition vec.h:152
vecteur generique, utilitaire.
Definition vec.h:169
vecteur generique 4d, ou 3d homogene, utilitaire.
Definition vec.h:192