Advertisement
poorsider

util.h Raytracing

Nov 24th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #ifndef UTIL_H
  2. #define UTIL_H
  3.  
  4. #include <SFML\Graphics.hpp>
  5. #include <math.h>
  6.  
  7. inline float dot(const sf::Vector3f& a, const sf::Vector3f& b)
  8. {
  9.     return a.x*b.x + a.y*b.y + a.z*b.z;
  10. }
  11.  
  12. inline sf::Vector3f cross(const sf::Vector3f& a, const sf::Vector3f& b)
  13. {
  14.     return sf::Vector3f(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
  15. }
  16.  
  17. inline float squaredLength(const sf::Vector3f& vec)
  18. {
  19.     return abs(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
  20. }
  21.  
  22. inline float length(const sf::Vector3f& vec)
  23. {
  24.     return sqrt(abs(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z));
  25. }
  26.  
  27. inline sf::Vector3f normalize(const sf::Vector3f& vec)
  28. {
  29.     if(length(vec) > 0.0f)
  30.     {
  31.         return sf::Vector3f(vec.x/length(vec), vec.y/length(vec), vec.z/length(vec));
  32.     }
  33.     else
  34.     {
  35.         return sf::Vector3f(0.0f, 0.0f, 0.0f);
  36.     }
  37. }
  38.  
  39. inline float randFloat(float a, float b)
  40. {
  41.     float random = ((float) rand()) / (float) RAND_MAX;
  42.     float diff = b - a;
  43.     float r = random * diff;
  44.     return a + r;
  45. }
  46.  
  47. inline int randInt(int a, int b)
  48. {
  49.     if(a==b)
  50.     {
  51.         return a;
  52.     }
  53.     if(a<b)
  54.     {
  55.         return rand()%(b-a) + a;
  56.     }
  57.     else
  58.     {
  59.         return rand()%(a-b) + b;
  60.     }
  61. }
  62.  
  63. #endif //UTIL_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement