Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- glm::vec3 Cube::calculateIntersections(glm::vec3 direction, glm::vec3 startingPoint){
- glm::vec3 intersectionPoint(-2.0f, -2.0f, 2.0f);
- for(std::vector<Rectangle *>::iterator it = rectangles.begin(); it != rectangles.end(); ++it){
- if(glm::length(intersectionPoint - startingPoint) > glm::length((*it)->calculateIntersections(direction, startingPoint) - startingPoint)){
- intersectionPoint = (*it)->calculateIntersections(direction, startingPoint);
- intersectionNormal = (*it)->getNormal();
- }
- }
- return intersectionPoint;
- }
- void Cube::computeChildrenRays(Ray *r){
- glm::vec3 intersectionAt = calculateIntersections(r->getDirection(), r->getStartingPoint());
- glm::vec3 inDirection = glm::normalize(r->getDirection());
- glm::vec3 parentPos = r->getStartingPoint();
- glm::vec3 refractedDirection = glm::vec3(0.0f, 0.0f, 0.0f);
- glm::vec3 reflectedDirection = -1.0f * (2.0f * (glm::dot(intersectionNormal,inDirection) * intersectionNormal) - inDirection);
- r->reflectionRay = new Ray(reflectedDirection, intersectionAt);
- if(transparent == true){
- //Refraction
- float n1 = 1.0f;
- float n2 = refractiveIndex;
- if(r->getInsideObject() == true){
- n1 = refractiveIndex;
- n2 = 1.0f;
- }
- float n = n1/n2;
- float cosI = glm::dot(intersectionNormal, inDirection);
- float sinT2 = 1.0f - n * n * (1.0f - cosI * cosI);
- if(sinT2 >= 0.0f){
- refractedDirection = n * inDirection - (n * cosI + (float)sqrt(sinT2)) * intersectionNormal;
- r->refractionRay = new Ray(refractedDirection, intersectionAt);
- if(r->getInsideObject() == false){
- r->refractionRay->setInsideObject(true);
- }else{
- r->refractionRay->setInsideObject(false);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement