Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Eigen::Vector3f Flyscene::traceRay(Eigen::Vector3f &origin,
- Eigen::Vector3f &dest) {
- // just some fake random color per pixel until you implement your ray tracing
- // remember to return your RGB values as floats in the range [0, 1]!!!
- Eigen::Vector3f color;
- traceRay(0, origin, dest,color);
- return color;
- }
- void Flyscene::traceRay(int level,Eigen::Vector3f& origin,
- Eigen::Vector3f& dest, Eigen::Vector3f &color) {
- Eigen::Vector3f point;
- Eigen::Vector3f normal;
- if (intersect(origin,dest-origin, normal,point)) {
- shade(level, point, color);
- }
- }
- void Flyscene::computeDirectLight(Eigen::Vector3f& hit, Eigen::Vector3f& directColor) {
- Eigen::Vector3f notneeded_;
- Eigen::Vector3f notneeded2_;
- // so it intersects with everything? wtf;
- if (intersect(hit, scene_light.getCenter() - hit, notneeded2_, notneeded_)) {
- directColor = { 0,0,0 };
- }
- else {
- directColor = { 1,0,0 };
- }
- }
- void Flyscene::shade(int level, Eigen::Vector3f& hit, Eigen::Vector3f & color) {
- Eigen::Vector3f directColor;
- computeDirectLight(hit, directColor);
- color = directColor;
- }
- bool Flyscene::intersect(Eigen::Vector3f& origin,
- Eigen::Vector3f direction, Eigen::Vector3f& normal, Eigen::Vector3f& point) {
- bool returnValue = false;
- float distance = FLT_MAX;
- float tempDistance;
- Eigen::Vector3f tempPoint;
- for (int i = 0; i < mesh.getNumberOfFaces(); i++) {
- if (inTriangle(mesh.getFace(i).vertex_ids, origin,direction,tempPoint,tempDistance)) {
- if (tempDistance<distance)
- {
- returnValue = true;
- distance = tempDistance;
- point = tempPoint;
- normal = mesh.getFace(i).normal;
- }
- }
- }
- return returnValue;
- }
- bool Flyscene::inTriangle(std::vector<GLuint> vertexIds, Eigen::Vector3f origin,
- Eigen::Vector3f direction, Eigen::Vector3f&point ,float & distance) {
- // (origin + t * direction) dot n - D = 0;
- std::vector<Eigen::Vector4f> vertices;
- vertices.push_back(mesh.getVertex(vertexIds[0]));
- vertices.push_back(mesh.getVertex(vertexIds[1]));
- vertices.push_back(mesh.getVertex(vertexIds[2]));
- Eigen::Vector3f n = (vertices[0].head<3>() - vertices[2].head<3>()).cross((vertices[1].head<3>() - vertices[2].head<3>()));
- n.normalize();
- float d = n.dot(vertices[0].head<3>());
- float t = (d - origin.dot(n)) / (direction.dot(n));
- //point on plane
- point = origin + t * direction;
- //matrix for solving the equation for the baryecentric coords.
- Eigen::Matrix3f matrix;
- matrix << vertices[0].head<3>(), vertices[1].head<3>(), vertices[2].head<3>();
- //baryecentric coords.
- Eigen::Vector3f abc = matrix.lu().solve(point);
- if (abc.x() < 0 || abc.y() < 0 || abc.x() + abc.y() > 1) {
- //std::cout << << std::endl;
- return false;
- }
- else {
- distance = (point - origin).norm();
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement