Skip to content

Commit

Permalink
Implement threading in a checker pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
vitor-k committed Nov 2, 2019
1 parent d24bc57 commit 98b7a06
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions RayTracer/RayTracer/RayTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ class RayTracerEngine : public olc::PixelGameEngine {
float totalTime = 0;

void drawingThread(int start, int end, int32_t width, int32_t height, olc::Pixel* tela) {
for (int i = start; i < end && i<width*height; i++) {
for (int i = start; i < end; i++) {
tela[i] = inicialTrace(i%width - width / 2, i/width - height / 2);
}
}
void drawingThread2(int offset, int32_t width, int32_t height, olc::Pixel* tela) {
for (int i = 0; i * nThreads + offset < width * height; i++) {
tela[i*nThreads + offset] = inicialTrace((i * nThreads + offset) % width - width / 2, (i * nThreads + offset) / width - height / 2);
}
}
bool OnUserUpdate(float fElapsedTime) override {
totalTime += 0.1f;
sceneObjects[0]->center.x = 10 + 8 *cos(1 * totalTime) + 2 * sin(3*totalTime);
Expand All @@ -60,9 +65,13 @@ class RayTracerEngine : public olc::PixelGameEngine {
olc::Pixel *tela = new olc::Pixel[width * height];

std::thread threads[nThreads];
for (int i = 0; i < nThreads; i++) {
/*for (int i = 0; i < nThreads; i++) {
threads[i] = std::thread(&RayTracerEngine::drawingThread, this, i * width * height / nThreads, (i + 1) * width * height / nThreads, width, height, tela);
}*/
for (int i = 0; i < nThreads; i++) {
threads[i] = std::thread(&RayTracerEngine::drawingThread2, this, i, width, height, tela);
}

for (int i = 0; i < nThreads; i++) {
threads[i].join();
}
Expand Down

0 comments on commit 98b7a06

Please sign in to comment.