RayTracing.
[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 __device__
12 Sphere()
13 {
14 }
15
16 __host__
17 void setCentre(const float3& centre)
18 {
19 this->centre = centre;
20 }
21
22 __host__
23 void setR(float r)
24 {
25 this->r = r;
26 this->rCarre = r * r;
27 }
28
29 __host__
30 void setHueInitial(float hue)
31 {
32 this->T = asinf(2 * hue - 1) - 3 * PI / 2;
33 }
34
35 /*
36 * Calcul de h².
37 */
38 __device__
39 float hSquare(float2 xySol) const
40 {
41 const float a = (this->centre.x - xySol.x);
42 const float b = (this->centre.y - xySol.y);
43 return a * a + b * b;
44 }
45
46 /*
47 * Est-ce que la sphre se trouve en dessous d'un point (x, y) en fonction de h².
48 * Voir la méthode 'hCarre(..)'.
49 */
50 __device__
51 bool isBelow(float hCarre) const
52 {
53 return hCarre < this->rCarre;
54 }
55
56 /*
57 * Hauteur du point de collision du rayon par rapport au centre de la sphère.
58 */
59 __device__
60 float dz(float hCarre) const
61 {
62 return sqrtf(rCarre - hCarre);
63 }
64
65 __device__
66 float distance(float dz) const
67 {
68 return this->centre.z - dz;
69 }
70
71 /**
72 * Renvoie la le B de HSB compris entre 0 et 1 dépendant du paramètre 'dz'
73 * lui même compris entre 0 et le rayon 'r'.
74 */
75 __device__
76 float brightness(float dz) const
77 {
78 return dz / this->r;
79 }
80
81 /*
82 * La couleur
83 */
84 __device__
85 float hue(float t) const
86 {
87 return 0.5 + 0.5 * sinf(t + this->T + 3 * PI / 2);
88 }
89
90 private:
91 float r; // Rayon.
92 float3 centre; // Position.
93 float rCarre; // Précalul de r².
94 float T; // Utilisé pour l'animation.
95 };
96 #endif