Advertisement
poorsider

Sphere-Klasse Raytracing

Nov 24th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. //SPHERE.H
  2. #ifndef SPHERE_H
  3. #define SPHERE_H
  4.  
  5. class RenderObject;
  6.  
  7. class Sphere : public RenderObject
  8. {
  9. private:
  10. public:
  11.     Sphere(Renderer& newRenderer);
  12.     Sphere(Renderer& newRenderer, Material& newMaterial);
  13.     int testRay(const sf::Vector3f& origin, const sf::Vector3f& direction, sf::Vector3f& hitPosition, sf::Vector2f& UVCoordinates);
  14.     const sf::Vector3f& getNormal(const sf::Vector3f& point);
  15.     const sf::Vector3f& getNormal();
  16. };
  17.  
  18. #endif //SPHERE_H
  19.  
  20. //SPHERE.CPP
  21. #include <SFML\Graphics.hpp>
  22. #include <math.h>
  23. #include <C:\Users\Laurin\Documents\Visual Studio 2012\Projects\Raytracing\Raytracing\renderer.h>
  24. #include <C:\Users\Laurin\Documents\Visual Studio 2012\Projects\Raytracing\Raytracing\renderObject.h>
  25. #include <C:\Users\Laurin\Documents\Visual Studio 2012\Projects\Raytracing\Raytracing\util.h>
  26. #include <C:\Users\Laurin\Documents\Visual Studio 2012\Projects\Raytracing\Raytracing\sphere.h>
  27.  
  28. Sphere::Sphere(Renderer& newRenderer)
  29.     :RenderObject(newRenderer)
  30. {
  31.     renderer.addRenderObject(this);
  32. }
  33.  
  34. Sphere::Sphere(Renderer& newRenderer, Material& newMaterial)
  35.     :RenderObject(newRenderer, newMaterial)
  36. {
  37.     renderer.addRenderObject(this);
  38. }
  39.  
  40. int Sphere::testRay(const sf::Vector3f& origin, const sf::Vector3f& direction, sf::Vector3f& hitPosition, sf::Vector2f& UVCoordinates)
  41. {
  42.     float factor = -dot((origin-transform.position), direction);
  43.     sf::Vector3f x = origin + factor * direction;
  44.     float xLength = length(x-transform.position);
  45.     float distance = sqrt((transform.scale.x/2 * transform.scale.x/2) - (xLength * xLength));
  46.     if(factor > distance)
  47.     {
  48.         hitPosition = origin + (factor - distance) * direction;
  49.         sf::Vector2f UVs(0.0f, 0.0f);
  50.         if(material.texture)
  51.         {
  52.             sf::Vector3f yAxis(0.0f, 1.0f, 0.0f);
  53.             sf::Vector3f zAxis(0.0f, 0.0f, -1.0f);
  54.             sf::Vector3f normal = getNormal(hitPosition);
  55.             float phi = acos( -dot( yAxis, normal ));
  56.             UVs.y = phi / 3.14f;
  57.  
  58.             float theta = (acos( dot( normal, zAxis) / sin( phi ))) / ( 2 * 3.14f);
  59.             if ( dot(cross( yAxis, zAxis), normal ) > 0 )
  60.             {
  61.                 UVs.x = theta;
  62.             }
  63.             else
  64.             {
  65.                 UVs.x = 1 - theta;
  66.             }
  67.         }
  68.         UVCoordinates = UVs;
  69.         return 1;
  70.     }
  71.     else if(factor + distance > 0)
  72.     {
  73.         hitPosition = origin + (factor + distance) * direction;
  74.         return -1;
  75.     }
  76.     else
  77.     {
  78.         return 0;
  79.     }
  80. }
  81.  
  82.  
  83. const sf::Vector3f& Sphere::getNormal(const sf::Vector3f& point)
  84. {
  85.     sf::Vector3f normal = point-transform.position;
  86.     return normalize(normal);
  87. }
  88.  
  89. const sf::Vector3f& Sphere::getNormal()
  90. {
  91.     return sf::Vector3f(0.0f, 0.0f, 0.0f);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement