Implémentation du raytracing pour Global Memory/Shared Memory/Constant Memory
[GPU.git] / WCudaMSE / Student_Cuda_Image / src / cpp / core / 04_RayTracing / moo / device / Sphere.h
1 #ifndef SPHERE_H
2 #define SPHERE_H
3
4 #include "cudaTools.h"
5 #include "MathTools.h"
6
7 class Sphere
8 {
9 public:
10 /*__host__
11 Sphere(float3 centre, float r, float hue) :
12 centre(centre),
13 hueInitial(hue)
14 {
15 this->setR(r);
16 }*/
17
18 __host__
19 __device__
20 Sphere()
21 {
22 }
23
24 /*Sphere(const Sphere& other) :
25 r(other.r),
26 centre(other.centre),
27 rCarre(other.rCarre),
28 T(other.T)
29 {
30 }*/
31
32 __host__
33 void setCentre(const float3& centre)
34 {
35 this->centre = centre;
36 }
37
38 __host__
39 void setR(float r)
40 {
41 this->r = r;
42 this->rCarre = r * r;
43 }
44
45 __host__
46 void setHueInitial(float hue)
47 {
48 this->T = asinf(2 * hue - 1) - 3 * PI / 2;
49 }
50
51 /*
52 * Calcul de h².
53 */
54 __device__
55 float hSquare(float2 xySol) const
56 {
57 const float a = (this->centre.x - xySol.x);
58 const float b = (this->centre.y - xySol.y);
59 return a * a + b * b;
60 }
61
62 /*
63 * Est-ce que la sphre se trouve en dessous d'un point (x, y) en fonction de h².
64 * Voir la méthode 'hCarre(..)'.
65 */
66 __device__
67 bool isBelow(float hCarre) const
68 {
69 return hCarre < this->rCarre;
70 }
71
72 /*
73 * Hauteur du point de collision du rayon par rapport au centre de la sphère.
74 */
75 __device__
76 float dz(float hCarre) const
77 {
78 return sqrtf(rCarre - hCarre);
79 }
80
81 __device__
82 float distance(float dz) const
83 {
84 return this->centre.z - dz;
85 }
86
87 /**
88 * Renvoie la le B de HSB compris entre 0 et 1.
89 */
90 __device__
91 float brightness(float dz) const
92 {
93 return dz / this->r;
94 }
95
96 /*
97 * La couleur
98 */
99 __device__
100 float hue(float t) const
101 {
102 return 0.5 + 0.5 * sinf(t + this->T + 3 * PI / 2);
103 }
104
105 private:
106 float r; // Rayon.
107 float3 centre; // Position.
108
109 float rCarre; // Précalul de r².
110
111 float T; // Utilisé pour l'animation.
112 };
113 #endif