From f58fc836b7001f1905ecfdbc57ccb7b6bdef0e28 Mon Sep 17 00:00:00 2001 From: "YingLiang Ma (ac7020)" Date: Wed, 8 Sep 2021 11:45:05 +0100 Subject: [PATCH] Add Ray Tracing --- Session 3/README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Session 3/README.md b/Session 3/README.md index 50ab22c..9e98345 100644 --- a/Session 3/README.md +++ b/Session 3/README.md @@ -100,9 +100,53 @@ position (1,3,1) and intensity (1,1,1). ## Multithread +You might find your ray tracing programming is quite slow if including a lot of objects. You can use multithread to speed up. An simple implementation of multithread can be found in _"MThread.cpp"_. It creates 10 threads and display "Hello from thread". +To use multithread in your ray tracing progamming. You should split your intersection and computing color tasks into different threads. +For example, you create 32 threads. Each one 1/32 of screen rendering for ray tracing. + +```C++ + std::vector threads; + const int threadCount = 32; + for (int i = 0; i < threadCount; i++) { + threads.push_back( std::thread(MultiThread,i*WIDTH/threadCount,(i+1)* WIDTH / threadCount)); + + } + + for (int i = 0; i < threadCount; i++) { + threads[i].join(); + + } +``` + +Then you define a multithread function which executes in each thread. It assign colors directly to image[x][y], +which will be updated in computer screen. + +```C++ +void MultiThread(int start, int end) { + vec3 finalCol; + + //loop through pixels + for (int x = start; x < end; x++) + { + for (int y = 0; y < HEIGHT; y++) + { + vec3 thisPixelRayDir = ConstructRay(x, y); + + finalCol = vec3(0,0,0); + TraceAndGetColour(thisPixelRayDir, RAYORIGIN, finalCol, 0); + image[x][y] = finalCol; + + } + OutputToScreen(); + } +} +``` + +The TraceAndGetColour is your ray tracing function which does heavy computation. + ## Rendering This section \ No newline at end of file