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

Assignment-3 Submission #13

Open
wants to merge 2 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Dealine**: 28.10.2021

Please put your name here:
**Name:** .......
**Name:** Enis Mustafaj
## 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 Down Expand Up @@ -52,7 +52,7 @@ 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>
**T2:** 7:1'007 ms<br>
**Speedup:** .......

> 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
29 changes: 27 additions & 2 deletions src/BSPNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,35 @@ class CBSPNode
{
if (isLeaf()) {
// --- PUT YOUR CODE HERE ---
return false;
for (size_t i = 0; i < this->m_vpPrims; ++i) {
this->m_vpPrims[i].intersect(ray);

}
return ray.hit && ray.t <= t1;
} else {
// --- PUT YOUR CODE HERE ---
return false;

double d = (this->m_splitVal - ray.org[this->m_splitDim]) / ray.dir[this->m_splitDim];

auto leftBox = (ray.dir[this->m_splitDim] < 0) ? Right() : Left();
auto rightBox = (ray.dir[this->m_splitDim] < 0) ? Left() : Right();

if (d <= t0) {

return rightBox->intersect(ray, t0, t1);
}
else if (d >= t1) {

return leftBox->intersect(ray, t0, t1);
}
else {

if (leftBox->intersect(ray, t0, d))
return true;

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

}
}

Expand Down
61 changes: 61 additions & 0 deletions src/BSPTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,44 @@ class CBSPTree
m_maxDepth = maxDepth;
m_minPrimitives = minPrimitives;
std::cout << "Scene bounds are : " << m_treeBoundingBox << std::endl;


m_root = build(m_treeBoundingBox, vpPrims, 0);
}

std::shared_ptr<CBSPNode> build(const CBoundingBox& box, const std::vector<ptr_prim_t>& vpPrims, size_t depth) {
if (depth > this->m_maxDepth || vpPrims.size() <= this->m_minPrimitives) {
return std::shared_ptr<CBSPNode>(vpPrims);
}

int splitDim = MaxDim(box.getMaxPoint() - box.getMinPoint())
float splitValue = box.getMaxPoint[splitDim] - box.getMinPoint()[splitDim];

auto boxes = box.splitBoxes(splitDim, splitValue);
CBoundingBox& f = boxes.first;
CBoundingBox& s = boxes.second;

std::vector<ptr_prim_t> leftBox;
std::vector<ptr_prim_t> rightBox;

for (size_t i = 0; i < vpPrims.size(); ++i) {

if (vpPrims[i].getBoundingBox().overlaps(f)) {
leftBox.push_back(vpPrims[i]);
}

if (vpPrims[i].getBoundingBox().overlaps(s)) {
rightBox.push_back(vpPrims[i]);
}
}

auto leftChild = build(f, leftBox, depth + 1);
auto rightChild = build(s, rightChild, depth + 1);

return std::shared_ptr<CBSPNode>(splitDim, splitValue, leftChild, rightChild);



}
/**
* @brief Checks whether the ray \b ray intersects a primitive.
Expand All @@ -55,9 +92,33 @@ class CBSPTree
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---

if (ray.hit) {
float begin = 0;
float end = ray.t;

this->m_treeBoundingBox.clip(ray, begin, end);

if (begin > end) {
return false;
}

return this->m_root->intersect(ray, begin, end);
}
return false;
}

CBoundingBox calcBoundingBox(const std::vector<ptr_prim_t>& vpPrims) {

CBoundingBox res;

for (size_t i = 0; i < vpPrims.size(); ++i) {
res.extend(vpPrims[i].getBoundingBox());
}

return res;
}


private:
/**
Expand Down
45 changes: 42 additions & 3 deletions src/BoundingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,67 @@ namespace {
void CBoundingBox::extend(const Vec3f& p)
{
// --- PUT YOUR CODE HERE ---
this->m_minPoint = Min3f(p, this->m_minPoint);
this->m_maxPoint = Max3f(p, this->m_maxPoint);
}

void CBoundingBox::extend(const CBoundingBox& box)
{
// --- PUT YOUR CODE HERE ---
this->extend(box.m_minPoint);
this->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);
return res;
if (dim < 3) {
CBoundingBox first(this->m_minPoint, val);
CBoundingBox second(val, this->m_maxPoint);
auto res = std::make_pair(first, second);

return res;
}

return std::make_pair(*this, *this);
}

bool CBoundingBox::overlaps(const CBoundingBox& box) const
{
// --- PUT YOUR CODE HERE ---
return false;
for (int i = 0; i < 3; ++i) {
if (this->m_minPoint.val[i] > box.m_minPoint.val[i]) {
return false;
}

if (this->m_maxPoint.val[i] < box.m_maxPoint.val[i]) {
return false;
}
}
return true;
}

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

for (int i = 0; i < 3; ++i) {

float a = (this->m_minPoint.val[i] - ray.org.val[i]) / ray.dir.val[i];
float b = (this->m_maxPoint.val[i] - ray.org.val[i]) / ray.dir.val[i];

if (ray.dir.val[i] < 0.0f) {
float temp = a;
a = b;
b = temp;
}

t0 = a > t0 ? a : t0;
t1 = b < t1 ? b : t1;

if (t0 > t1) {
return;
}
}
}

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
2 changes: 2 additions & 0 deletions src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class CPrimPlane : public IPrim
{
CBoundingBox bounds;
// --- PUT YOUR CODE HERE ---
bounds.m_minPoint = Vec3f::all(-Infty);
bounds.m_maxPoint - Vec3f::all(Infty);
return bounds;
}

Expand Down
3 changes: 2 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class CPrimSphere : public IPrim

virtual CBoundingBox getBoundingBox(void) const override
{
CBoundingBox res;

// --- PUT YOUR CODE HERE ---
CBoundingBox res(this->m_origin - Vec3f::all(this->m_radius), this->m_origin + Vec3f::all(this->m_radius););
return res;
}

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

res.extend(this->m_a);
res.extend(this->m_b);
res.extend(this->m_c);

return res;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class CScene
void add(const CSolid& solid)
{
// --- PUT YOUR CODE HERE ---

for (size_t i = 0; i < solid.getPrims().size(); ++i) {
this->m_vpPrims.push_back(solid.getPrims()[i]);
}
}
/**
* @brief (Re-) Build the BSP tree for the current geometry present in scene
Expand Down
19 changes: 15 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@ Mat RenderFrame(void)
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);
parallel_for_(Range(0, img.rows), [&](const Range& range) {
for (int y = range.start; y < range.end; ++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++)
// for (int x = 0; x < img.cols; x++) {
// 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