Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw3 checiu #7

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD 17)
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi /wd5040")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /INCREMENTAL:NO /OPT:REF /OPT:ICF")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS} /RELEASE /INCREMENTAL:NO /OPT:REF /OPT:ICF")
endif(MSVC)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#set(IB_API_INCLUDE_DIR "" CACHE PATH "Path to the IP API")
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Dealine**: 29.10.2020

Please put your name here:
**Name:** .......
**Name:** Checiu Eliza
## Problem 1
### Rendering complex geometry (Points 5)
Until now we have only hardcoded our scene geometry in main.cpp. This is of course not practical. In the new framework, a class ```CSolid``` is added. This class may contain complex geometry formed by multiple primitives. Such geometry may be saved / read from an .obj file. For this problem we will read _torus knot.obj_ file and rended this object, which consists of 12 960 triangles. To make the method work proceed as follows:
Expand All @@ -11,7 +11,7 @@ Until now we have only hardcoded our scene geometry in main.cpp. This is of cour
3. Have a look at the file _torus knot.obj_ and at the class ```CSolid```. Study how triangles are stored in the obj-format and in the class. The _v_ ’s indicate a single 3d-vertex position, and the _f_ ’s (faces) are indecies to 3 vertex numbers a triangle consits of (please note that the face indecies are starting with **1 and not 0**).
4. Implement function ```CScene::add(const CSolid& solid)``` which adds a solid to the scene.
5. Make sure that you work with Release and not Debug and disable BSP support in CMake (if it was enabled). Render the scene and write the time needed for 1 frame below:<br>
**T0:** .......
**T0:** (~11.9 ms)

> **Note:** Rendering may take several minutes.

Expand All @@ -29,8 +29,8 @@ In order to use not one but all cores of CPU proceed as follows:
1. Study the OpenCV function for parallel data processing ```parallel_for_```: [How to use the OpenCV parallel_for_ to parallelize your code](https://docs.opencv.org/master/d7/dff/tutorial_how_to_use_OpenCV_parallel_for_.html)
2. In main.cpp file rewrite the main rendering loop (lines 53 - 57), by paralellizing the loop ```for (int y = 0; y < img.rows; y++)``` with help of ```parallel_for_``` function and enclosing the inner body into a lambda-expression. You do not need to parallelize the inner loop ```for (int x = 0; x < img.cols; x++)```.
3. Render the scene and write the time needed for 1 frame T1 and speedup = T0 / T1 below:<br>
**T1:** .......<br>
**Speedup:** .......
**T1:** ~3.44ms<br>
**Speedup:** T0/T1 = 668 / 223 = 2.9

## Problem 3
### Implementation of a kd-tree acceleration structure (Points 30)
Expand All @@ -52,8 +52,8 @@ If everything so far was implemented correctly, the Scene bounds will be: [-6, 0
For more information please read the chapter 7.2 in the [thesis of Dr. Ingo Wald](http://www.sci.utah.edu/~wald/PhD/wald_phd.pdf).
6. Implement the method ```std::shared_ptr<CBSPNode> build(const CBoundingBox& box, const std::vector<ptr_prim_t>& vpPrims, size_t depth)``` of the class ```CBSPTree```. Use the ideas presented at the lecture. As soon as you have reached a maximum depth (_e.g._ 20), or you have less then a minimum number of primitives (_e.g._ 3 or 4), stop subdividing and generate a leaf node. Otherweise, split your bounding box in the middle (in the maximum dimension), sort your current primitives into two vector left and right, and recursively call BuildTree with the respective bounding boxes and vector for left and right. Start subdivision with a list of all primitives, the total scene bounds, and an initial recursion depth of 0.<br>
9. Render the scene and write the time needed for 1 frame T2 and speedup = T0 / T2 below:<br>
**T2:** .......<br>
**Speedup:** .......
**T2:** ~3.56ms<br>
**Speedup:** T0 / T1 = 668 / 235 = 2.84

> A the solution for this problem can be found in OpenRT library: www.openrt.org However it is highly recommended to solve this problem using lecture slides only and resorting to the solution only as a last resort.

Expand Down
Binary file added renders/img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions src/BSPNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,38 @@ class CBSPNode
{
if (isLeaf()) {
// --- PUT YOUR CODE HERE ---
return false;
//return false;

for (auto& pPrim : m_vpPrims)
pPrim->intersect(ray);

return (ray.hit && ray.t < t1 + Epsilon);

} else {
// --- PUT YOUR CODE HERE ---
return false;
//return false;

double element = (m_splitVal - ray.org[m_splitDim]) / ray.dir[m_splitDim];

auto frontNode = (ray.dir[m_splitDim] < 0) ? Right() : Left();
auto backNode = (ray.dir[m_splitDim] < 0) ? Left() : Right();

if (element <= t0)
{
return backNode->intersect(ray, t0, t1);
}
else if (element >= t1)
{
return frontNode->intersect(ray, t0, t1);
}
else
{
if (frontNode->intersect(ray, t0, d))
return true;

return backNode->intersect(ray, d, t1);
}

}
}

Expand Down
18 changes: 17 additions & 1 deletion src/BSPTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace {
{
CBoundingBox res;
// --- PUT YOUR CODE HERE ---

for (auto pPrim : vpPrims)
res.extend(pPrim->getBoundingBox());

return res;

}

// Returns the best dimension index for next split
Expand Down Expand Up @@ -55,7 +61,17 @@ class CBSPTree
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return false;
//return false;

double t0 = 0;
double t1 = ray.t;

m_treeBoundingBox.clip(ray, t0, t1);

if (t1 < t0)
return false;

return m_root->intersect(ray, t0, t1);
}


Expand Down
84 changes: 83 additions & 1 deletion src/BoundingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,110 @@ namespace {
void CBoundingBox::extend(const Vec3f& p)
{
// --- PUT YOUR CODE HERE ---

m_minPoint = Min3f(p, m_minPoint);
m_maxPoint = Max3f(p, m_maxPoint);
}

void CBoundingBox::extend(const CBoundingBox& box)
{
// --- PUT YOUR CODE HERE ---
extend(box.m_minPoint);
extend(box.m_maxPoint);
}

std::pair<CBoundingBox, CBoundingBox> CBoundingBox::split(int dim, float val) const
{
// --- PUT YOUR CODE HERE ---
auto res = std::make_pair(*this, *this);

res.first.m_maxPoint[dim] = val;
res.second.m_minPoint[dim] = val;

return res;
}

bool CBoundingBox::overlaps(const CBoundingBox& box) const
{
// --- PUT YOUR CODE HERE ---
return false;
//return false;

for (int dim = 0; dim < 3; dim++)
{
if (box.m_minPoint[dim] - Epsilon > m_maxPoint[dim]) return false;
if (box.m_maxPoint[dim] + Epsilon < m_minPoint[dim]) return false;
}

return true;

}

void CBoundingBox::clip(const Ray& ray, double& t0, double& t1) const
{
// --- PUT YOUR CODE HERE ---

float d, den;
if (ray.dir.val[0] != 0)
{
den = 1.0f / ray.dir.val[0];

if (ray.dir.val[0] > 0)
{
if ((d = (m_minPoint.val[0] - ray.org.val[0]) * den) > t0)
t0 = d;
if ((d = (m_maxPoint.val[0] - ray.org.val[0]) * den) < t1)
t1 = d;
}
else {
if ((d = (m_maxPoint.val[0] - ray.org.val[0]) * den) > t0)
t0 = d;
if ((d = (m_minPoint.val[0] - ray.org.val[0]) * den) < t1)
t1 = d;
}
if (t0 > t1)
return;
}

if (ray.dir.val[1] != 0)
{
den = 1.0f / ray.dir.val[1];

if (ray.dir.val[1] > 0)
{
if ((d = (m_minPoint.val[1] - ray.org.val[1]) * den) > t0)
t0 = d;
if ((d = (m_maxPoint.val[1] - ray.org.val[1]) * den) < t1)
t1 = d;
}
else
{
if ((d = (m_maxPoint.val[1] - ray.org.val[1]) * den) > t0)
t0 = d;
if ((d = (m_minPoint.val[1] - ray.org.val[1]) * den) < t1)
t1 = d;
}
if (t0 > t1)
return;
}

if (ray.dir.val[2] != 0)
{
den = 1.0f / ray.dir.val[2];

if (ray.dir.val[2] > 0)
{
if ((d = (m_minPoint.val[2] - ray.org.val[2]) * den) > t0)
t0 = d;
if ((d = (m_maxPoint.val[2] - ray.org.val[2]) * den) < t1)
t1 = d;
}
else
{
if ((d = (m_maxPoint.val[2] - ray.org.val[2]) * den) > t0)
t0 = d;
if ((d = (m_minPoint.val[2] - ray.org.val[2]) * den) < t1)
t1 = d;
}
}
}

2 changes: 1 addition & 1 deletion src/LightOmni.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ILight.h"
#include "Ray.h"
#include "ray.h"

/**
* @brief Point light source class
Expand Down
15 changes: 14 additions & 1 deletion src/PrimPlane.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ class CPrimPlane : public IPrim
{
CBoundingBox bounds;
// --- PUT YOUR CODE HERE ---
return bounds;

Vec3f minPoint = Vec3f::all(-Infty);
Vec3f maxPoint = Vec3f::all(Infty);

for (int i = 0; i < 3; i++)
if (m_normal.val[i] == 1)
{
minPoint.val[i] = m_origin.val[i];
maxPoint.val[i] = m_origin.val[i];
break;
}
return CBoundingBox(minPoint, maxPoint);

//return bounds;
}

private:
Expand Down
4 changes: 3 additions & 1 deletion src/PrimSphere.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class CPrimSphere : public IPrim
{
CBoundingBox res;
// --- PUT YOUR CODE HERE ---
return res;
//return res;

return CBoundingBox(m_origin - Vec3f::all(m_radius), m_origin + Vec3f::all(m_radius));
}


Expand Down
7 changes: 7 additions & 0 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class CPrimTriangle : public IPrim
{
CBoundingBox res;
// --- PUT YOUR CODE HERE ---

CBoundingBox res;

res.extend(m_a);
res.extend(m_b);
res.extend(m_c);

return res;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class CScene
void add(const CSolid& solid)
{
// --- PUT YOUR CODE HERE ---
for (const auto& pPrim : solid.getPrims())
add(pPrim);
}
/**
* @brief (Re-) Build the BSP tree for the current geometry present in scene
Expand Down
21 changes: 13 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Mat RenderFrame(void)
#ifdef WIN32
const std::string dataPath = "../data/";
#else
const std::string dataPath = "../../data/";
const std::string dataPath = "../data/";
#endif
CSolid solid(pShader, dataPath + "Torus Knot.obj");
scene.add(solid);
Expand All @@ -48,13 +48,18 @@ Mat RenderFrame(void)
scene.add(std::make_shared<CLightOmni>(pointLightIntensity, lightPosition3));

Mat img(resolution, CV_32FC3); // image array
Ray ray; // primary ray

for (int y = 0; y < img.rows; y++)
for (int x = 0; x < img.cols; x++) {
scene.getActiveCamera()->InitRay(ray, x, y); // initialize ray
img.at<Vec3f>(y, x) = scene.RayTrace(ray);
}

// for (int y = 0; y < img.rows; y++)
cv::parallel_for_(cv::Range(0, img.rows), [&](const Range& range)
{
for (int y = range.start; y < range.end; y++)
for (int x = 0; x < img.cols; x++)
{
Ray ray; // primary ray
scene.getActiveCamera()->InitRay(ray, x, y); // initialize ray
img.at<Vec3f>(y, x) = scene.RayTrace(ray);
}
};

img.convertTo(img, CV_8UC3, 255);
return img;
Expand Down
8 changes: 4 additions & 4 deletions src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ namespace DirectGraphicalModels
}

printf("Done! (");
if (hrs) printf("%lld:", hrs);
if (min) printf("%lld:", min);
if (sec) printf("%lld'", sec);
printf("%03lld ms)\n", ms);
if (hrs) printf("%ld:", hrs);
if (min) printf("%ld:", min);
if (sec) printf("%ld'", sec);
printf("%03ld ms)\n", ms);
}
}
}