Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. glm::vec3 Cube::calculateIntersections(glm::vec3 direction, glm::vec3 startingPoint){
  2.  
  3. glm::vec3 intersectionPoint(-2.0f, -2.0f, 2.0f);
  4. for(std::vector<Rectangle *>::iterator it = rectangles.begin(); it != rectangles.end(); ++it){
  5.  
  6. if(glm::length(intersectionPoint - startingPoint) > glm::length((*it)->calculateIntersections(direction, startingPoint) - startingPoint)){
  7. intersectionPoint = (*it)->calculateIntersections(direction, startingPoint);
  8. intersectionNormal = (*it)->getNormal();
  9. }
  10. }
  11.  
  12. return intersectionPoint;
  13. }
  14.  
  15. void Cube::computeChildrenRays(Ray *r){
  16.  
  17. glm::vec3 intersectionAt = calculateIntersections(r->getDirection(), r->getStartingPoint());
  18.  
  19. glm::vec3 inDirection = glm::normalize(r->getDirection());
  20. glm::vec3 parentPos = r->getStartingPoint();
  21. glm::vec3 refractedDirection = glm::vec3(0.0f, 0.0f, 0.0f);
  22.  
  23. glm::vec3 reflectedDirection = -1.0f * (2.0f * (glm::dot(intersectionNormal,inDirection) * intersectionNormal) - inDirection);
  24.  
  25. r->reflectionRay = new Ray(reflectedDirection, intersectionAt);
  26.  
  27. if(transparent == true){
  28. //Refraction
  29. float n1 = 1.0f;
  30. float n2 = refractiveIndex;
  31.  
  32. if(r->getInsideObject() == true){
  33. n1 = refractiveIndex;
  34. n2 = 1.0f;
  35. }
  36.  
  37. float n = n1/n2;
  38. float cosI = glm::dot(intersectionNormal, inDirection);
  39. float sinT2 = 1.0f - n * n * (1.0f - cosI * cosI);
  40.  
  41. if(sinT2 >= 0.0f){
  42. refractedDirection = n * inDirection - (n * cosI + (float)sqrt(sinT2)) * intersectionNormal;
  43. r->refractionRay = new Ray(refractedDirection, intersectionAt);
  44. if(r->getInsideObject() == false){
  45. r->refractionRay->setInsideObject(true);
  46. }else{
  47. r->refractionRay->setInsideObject(false);
  48. }
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement