Added triangle function, not working perfectly but u see something like a triangle
This commit is contained in:
parent
2eb55b9e98
commit
f98e578f94
3 changed files with 54 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -7,6 +7,7 @@ build/
|
|||
/data/*
|
||||
!/data/README.txt
|
||||
/result*
|
||||
/cmake-build-debug
|
||||
|
||||
# Latex stuff
|
||||
*.aux
|
||||
|
|
4
ex1.cpp
4
ex1.cpp
|
@ -40,8 +40,8 @@ int main() {
|
|||
// Set up the camera
|
||||
PerspectiveCamera camera;
|
||||
camera.setFovAngle(70);
|
||||
camera.setPosition(Vector3d(-2.5f, 2.5f, -10.0f));
|
||||
camera.setForwardDirection(Vector3d(0.25f, -0.33f, 1.0f));
|
||||
camera.setPosition(Vector3d(-2.5f, 2.5f, -12.0f));
|
||||
camera.setForwardDirection(Vector3d(0.0f, -0.33f, 1.0f));
|
||||
camera.setUpDirection(Vector3d(0.2f, 1.0f, 0.0f));
|
||||
|
||||
// Render the scene
|
||||
|
|
|
@ -21,19 +21,63 @@ Triangle::Triangle(Vector3d const &a, Vector3d const &b, Vector3d const &c, Vect
|
|||
|
||||
|
||||
bool Triangle::intersect(Ray &ray) const {
|
||||
// IMPLEMENT ME!
|
||||
Vector3d edge1 = this->vertex[0] - this->vertex[2];
|
||||
Vector3d edge2 = this->vertex[1] - this->vertex[2];
|
||||
Vector3d normalVector = crossProduct(edge1, edge2);
|
||||
float d = dotProduct(this->vertex[0], normalVector) / length(normalVector);
|
||||
|
||||
// Determine whether the ray intersects the triangle
|
||||
// catch divided by 0
|
||||
float dotDirectionNormalVector = dotProduct(ray.direction, normalVector);
|
||||
if (dotDirectionNormalVector == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Test whether this is the foremost primitive in front of the camera
|
||||
float t = (d - dotProduct(ray.origin, ray.direction)) / dotDirectionNormalVector;
|
||||
Vector3d p = ray.origin + t * ray.direction;
|
||||
|
||||
// (Optional for now) Calculate the normal
|
||||
// Barycentric Coordinates a,b to determine if intersect with Triangle
|
||||
float triangleArea = length(normalVector) / 2;
|
||||
|
||||
// (Optional for now) Calculate the surface position
|
||||
Vector3d rightSide = crossProduct(this->vertex[2] - this->vertex[1], p - this->vertex[1]);
|
||||
Vector3d leftSide = crossProduct(this->vertex[0] - this->vertex[2], p - this->vertex[2]);
|
||||
|
||||
// Set the new length and the current primitive
|
||||
float triangleAreaRightSide = length(rightSide) / 2;
|
||||
float triangleAreaLeftSide = length(leftSide) / 2;
|
||||
|
||||
return false;
|
||||
float a = triangleAreaRightSide / triangleArea;
|
||||
|
||||
float b = triangleAreaLeftSide / triangleArea;
|
||||
|
||||
// Determine whether the ray intersects the triangle
|
||||
|
||||
|
||||
if(1.0f - a - b >= 1.0f || 0.0f >= a || 0.0f >= b){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(dotProduct(normalVector, leftSide) < 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(dotProduct(normalVector, rightSide) < 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Test whether this is the foremost primitive in front of the camera
|
||||
if (t >= ray.length) {
|
||||
//it is bigger so further away
|
||||
return false;
|
||||
}
|
||||
|
||||
// (Optional for now) Calculate the normal
|
||||
|
||||
// (Optional for now) Calculate the surface position
|
||||
|
||||
// Set the new length and the current primitive
|
||||
ray.length = t;
|
||||
ray.primitive = this;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Bounding box ////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in a new issue