Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. Eigen::Vector3f Flyscene::traceRay(Eigen::Vector3f &origin,
  2.                                    Eigen::Vector3f &dest) {
  3.   // just some fake random color per pixel until you implement your ray tracing
  4.   // remember to return your RGB values as floats in the range [0, 1]!!!
  5.     Eigen::Vector3f color;
  6.      traceRay(0, origin, dest,color);
  7.      return color;
  8. }
  9. void Flyscene::traceRay(int level,Eigen::Vector3f& origin,
  10.     Eigen::Vector3f& dest, Eigen::Vector3f  &color) {
  11.     Eigen::Vector3f point;
  12.     Eigen::Vector3f normal;
  13.         if (intersect(origin,dest-origin, normal,point)) {
  14.             shade(level, point, color);
  15.         }
  16.    
  17. }
  18. void Flyscene::computeDirectLight(Eigen::Vector3f& hit, Eigen::Vector3f& directColor) {
  19.     Eigen::Vector3f notneeded_;
  20.     Eigen::Vector3f notneeded2_;
  21.     // so it intersects with everything? wtf;
  22.     if (intersect(hit, scene_light.getCenter() - hit, notneeded2_, notneeded_)) {
  23.         directColor = { 0,0,0 };
  24.     }
  25.     else {
  26.         directColor = { 1,0,0 };
  27.     }
  28. }
  29. void Flyscene::shade(int level, Eigen::Vector3f& hit, Eigen::Vector3f & color) {
  30.     Eigen::Vector3f directColor;
  31.     computeDirectLight(hit, directColor);
  32.     color = directColor;
  33. }
  34. bool Flyscene::intersect(Eigen::Vector3f& origin,
  35.     Eigen::Vector3f direction, Eigen::Vector3f& normal, Eigen::Vector3f& point) {
  36.     bool returnValue = false;
  37.     float distance = FLT_MAX;
  38.     float tempDistance;
  39.     Eigen::Vector3f tempPoint;
  40.     for (int i = 0; i < mesh.getNumberOfFaces(); i++) {
  41.         if (inTriangle(mesh.getFace(i).vertex_ids, origin,direction,tempPoint,tempDistance)) {
  42.             if (tempDistance<distance)
  43.             {
  44.                 returnValue = true;
  45.                 distance = tempDistance;
  46.                 point = tempPoint;
  47.                 normal = mesh.getFace(i).normal;
  48.             }
  49.         }
  50.     }
  51.     return returnValue;
  52. }
  53. bool Flyscene::inTriangle(std::vector<GLuint> vertexIds, Eigen::Vector3f origin,
  54.     Eigen::Vector3f direction, Eigen::Vector3f&point ,float & distance) {
  55.     // (origin + t * direction) dot n - D = 0;
  56.  
  57.     std::vector<Eigen::Vector4f> vertices;
  58.     vertices.push_back(mesh.getVertex(vertexIds[0]));
  59.     vertices.push_back(mesh.getVertex(vertexIds[1]));
  60.     vertices.push_back(mesh.getVertex(vertexIds[2]));
  61.     Eigen::Vector3f n = (vertices[0].head<3>() - vertices[2].head<3>()).cross((vertices[1].head<3>() - vertices[2].head<3>()));
  62.     n.normalize();
  63.     float d = n.dot(vertices[0].head<3>());
  64.     float t = (d - origin.dot(n)) / (direction.dot(n));
  65.     //point on plane
  66.     point = origin + t * direction;
  67.     //matrix for solving the equation for the baryecentric coords.
  68.     Eigen::Matrix3f matrix;
  69.     matrix << vertices[0].head<3>(), vertices[1].head<3>(), vertices[2].head<3>();
  70.     //baryecentric coords.
  71.     Eigen::Vector3f abc = matrix.lu().solve(point);
  72.  
  73.     if (abc.x() < 0 || abc.y() < 0 || abc.x() + abc.y() > 1) {
  74.     //std::cout <<  << std::endl;
  75.         return false;
  76.     }
  77.     else {
  78.         distance = (point - origin).norm();
  79.         return true;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement