From 03f13de198fa3ec1a6d54d52f38d10f147bc3af9 Mon Sep 17 00:00:00 2001 From: "YingLiang Ma (ac7020)" Date: Wed, 8 Sep 2021 11:38:00 +0100 Subject: [PATCH] Add Multihread --- Session 3/MThread.cpp | 37 ++++++++++++++ Session 3/README.md | 116 +++--------------------------------------- 2 files changed, 43 insertions(+), 110 deletions(-) create mode 100644 Session 3/MThread.cpp diff --git a/Session 3/MThread.cpp b/Session 3/MThread.cpp new file mode 100644 index 0000000..5f37b38 --- /dev/null +++ b/Session 3/MThread.cpp @@ -0,0 +1,37 @@ +// MThread.cpp : Defines the entry point for the console application. +// + +#include "stdafx.h" +#include +#include + +static const int num_threads = 10; + +void call_from_thread() { + std::cout << "Hello from thread" << std::endl; +} + +int main() +{ + //Launch a thread + std::thread t1(call_from_thread); + //Join the thread with the main thread + t1.join(); + + getchar(); + + std::thread t[num_threads]; + //Launch a group of threads + for (int i = 0; i < num_threads; ++i) { + t[i] = std::thread(call_from_thread); + } + std::cout << "Launched from the main\n"; + //Join the threads with the main thread + for (int i = 0; i < num_threads; ++i) { + t[i].join(); + } + + getchar(); + return 0; +} + diff --git a/Session 3/README.md b/Session 3/README.md index a3c3503..50ab22c 100644 --- a/Session 3/README.md +++ b/Session 3/README.md @@ -3,7 +3,7 @@ #### Table of Contents 1. [Ray Tracing Spheres](https://github.coventry.ac.uk/ac7020/322COM_TeachingMaterial/blob/master/Session%203#Ray-Tracing-Spheres) 2. [Ray Tracing Primitives](https://github.coventry.ac.uk/ac7020/322COM_TeachingMaterial/blob/master/Session%203#Ray-Tracing-Primitives) -3. [Add triangle class](https://github.coventry.ac.uk/ac7020/322COM_TeachingMaterial/blob/master/Session%203#Add-triangle-class) +3. [Multithread](https://github.coventry.ac.uk/ac7020/322COM_TeachingMaterial/blob/master/Session%203#Multithread) 4. [Rendering complex shape](https://github.coventry.ac.uk/ac7020/322COM_TeachingMaterial/blob/master/Session%203#Rendering-complex-shape) 5. [Example of mesh class](https://github.coventry.ac.uk/ac7020/322COM_TeachingMaterial/blob/master/Session%203#Example-of-mesh-class) @@ -98,115 +98,11 @@ Teapot - diffuse colour (0.5,0.5,0.0), specular colour (0.7, 0.7, 0.7) and shininess (100). Light with position (1,3,1) and intensity (1,1,1). -## Add triangle class +## Multithread -Add a Triangle class that also inherits from your Shape class. A triangle can be defined by 3 -vertices. Add the intersect method using the ray-triangle intersection method (see lecture slide for -more details). Add a triangle to a scene and test that it appears correctly. +An simple implementation of multithread can be found in _"MThread.cpp"_. +It creates 10 threads and display "Hello from thread". +## Rendering - -Triangle with vertices: (0, 1, -2), (-1.9, -1, -2), (1.6, -0.5, -2) - -## Rendering complex shape - -This section is advanced level so it is optional. -To render more complex shapes e.g. teapot you will need to be able to load a mesh from a file and -render each of its triangles. A simple mesh loader (OBJloader.h) written in C++ can be downloaded from Week 2 folder. -Note this code is limited to only loading meshes of file type OBJ. - -The mesh loader handle the import of OBJ file is inside the header file : OBJloader.h. -This is a head file only library. It is a simplified version of Bly7 OBJ Loader library ( https://github.com/Bly7/OBJ-Loader ). - -Examples of a cube and a teapot OBJ files can also be downloaded from week 2. - -## Example of mesh class - -This section is advanced level so it is optional. - -An example of abstract shape class - ```C++ -#pragma once -#include "glm/glm/glm.hpp" - -using namespace glm; - -class shape -{ -private: - vec3 center; - vec3 diffuseColor; - vec3 diffuseItensity; - -public: - shape(); - virtual bool intersection(vec3 rayDirection, vec3 rayOrigin, float& t, vec3& IntPt, vec3& normVec); - - //for future ray tracing - virtual void ComputeColor(const float ambientIntensity, const vec3 IntPt, const vec3 lightPt, const vec3 rayDirection, float& ColValue); - ~shape(); - vec3 position; - vec3 mcolor; -}; -``` - -You need to write your own CPP file for shape class - -An example of mesh/triangle class - ```C++ -#pragma once -#include "shape.h" -#include "glm/glm/glm.hpp" - -class triangle : public shape -{ -private: - vec3 vertex0, vertex1, vertex2; - vec3 norm0, norm1, norm2; - -public: - triangle(vec3 pos, vec3 v0, vec3 v1, vec3 v2, vec3 color); - triangle(vec3 pos, vec3 v0, vec3 v1, vec3 v2, - vec3 n0, vec3 n1, vec3 n2, - vec3 color) : - vertex0(v0), vertex1(v1), vertex2(v2), norm0(n0), norm1(n1), norm2(n2) - { - mcolor = color; - }; - - bool intersection(vec3 rayDirection, vec3 rayOrigin, float& t, vec3 &IntPt, vec3& normVec)override; - void ComputeColor(const float ambientIntensity, const vec3 IntPt, const vec3 lightPt, const vec3 rayDirection, const vec3 tNormvec, float& ColValue); - ~triangle(); -}; -``` - -An example of triangle interection implementation (Moller-Trumbore method). -The method is explained in https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection - -```C++ -bool triangle::intersection(vec3 rayDirection, vec3 rayOrigin, float &t, vec3 &IntPt, vec3 &normVec) -{ - //Moller-Trumbore - - vec3 v0v1 = vertex1 - vertex0; - vec3 v0v2 = vertex2 - vertex0; - - float u = (dot((rayOrigin - vertex0), (cross(rayDirection, v0v2)))) / dot(v0v1, cross(rayDirection, v0v2)); - float v = (dot(rayDirection, cross(rayOrigin - vertex0, v0v1))) / dot(v0v1, cross(rayDirection, v0v2)); - - float w = 1 - u - v; - - if (u < 0 || u > 1) - return false; - else if (v < 0 || (u + v) > 1) - return false; - else - { - IntPt = rayOrigin + t * rayDirection; - normVec = glm::normalize(w * norm0 + u * norm1 + v * norm2); - //normVec = norm0; - t = dot(v0v2, cross((rayOrigin - vertex0), v0v1)) / dot(v0v1, cross(rayDirection, v0v2)); - return true; - } -} -``` +This section \ No newline at end of file