503459473dcfad67948bc7107f7aa6f5734e642b
[GPU.git] / WCudaMSE / Student_Cuda_Image / src / cpp / core / 04_RayTracing / moo / host / RayTracing.cu
1 #include <iostream>
2 #include <assert.h>
3 #include <stdio.h>
4 using namespace std;
5
6 #include "AleaTools.h"
7
8 #include "RayTracing.h"
9 #include "Device.h"
10 #include "RayTracingDevice.h"
11 #include "Sphere.h"
12
13 RayTracing::RayTracing(int w, int h)
14     : w(w), h(h),
15       dg(8, 8, 1),
16       db(32, 32, 1),
17       title("Ray Tracing")
18     {
19     Device::assertDim(dg, db);
20
21     const int nbSpheres = 10;
22     Sphere* shperes = this->createSpheres(10);
23
24     // Copie les spheres dans la constant memory.
25     }
26
27 RayTracing::~RayTracing()
28     {
29     }
30
31 void RayTracing::runGPU(uchar4* ptrDevPixels)
32     {
33     rayTracing<<<dg,db>>>(ptrDevPixels, this->w, this->h, this->t);
34
35     HANDLE_ERROR(cudaDeviceSynchronize()); // Pour flusher les 'printf' (pour le DEBUG).
36     }
37
38 void RayTracing::animationStep()
39     {
40     this->t += 0.1; // TODO
41     }
42
43 int RayTracing::getW()
44     {
45     return this->w;
46     }
47
48 int RayTracing::getH()
49     {
50     return this->h;
51     }
52
53 float RayTracing::getT()
54     {
55     return this->t;
56     }
57
58 string RayTracing::getTitle()
59     {
60     return this->title;
61     }
62
63 Sphere* RayTracing::createSpheres(int n)
64     {
65     Sphere* spheres = new Sphere[n];
66     const float bord = 200;
67
68     for (int i = 0; i < n; ++i)
69         {
70         spheres[i].setR(float(this->alea.uniformeAB(20, this->w / 10 - 1)));
71
72         cpu::float3 centre
73             {
74             float(this->alea.uniformeAB(double(bord), double(this->w - bord))),
75             float(this->alea.uniformeAB(double(bord), double(this->h - bord))),
76             float(this->alea.uniformeAB(10.0, 2.0 * this->w))
77             };
78
79         spheres[i].setCentre(centre);
80         spheres[i].setHueInitial(float(this->alea.uniformeAB(0.0, 1.0)));
81         }
82     }