Advertisement
spacechase0

gfx::Camera

Nov 6th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. #ifndef GFX_CAMERA_H
  2. #define GFX_CAMERA_H
  3.  
  4. #include <SFML/System.hpp>
  5.  
  6. namespace gfx
  7. {
  8.     class Camera
  9.     {
  10.         public:
  11.             Camera();
  12.             Camera( sf::Vector3f thePos );
  13.             Camera( float thePosX, float thePosY, float thePosZ );
  14.             Camera( sf::Vector3f thePos, sf::Vector3f theRot );
  15.             Camera( float thePosX, float thePosY, float thePosZ, float theRotX, float theRotY, float theRotZ );
  16.            
  17.             void SetPosition( sf::Vector3f thePos );
  18.             void SetPosition( float thePosX, float thePosY, float thePosZ );
  19.             void Move( sf::Vector3f thePos );
  20.             void Move( float thePosX, float thePosY, float thePosZ );
  21.             sf::Vector3f GetPosition();
  22.            
  23.             void SetRotation( sf::Vector3f theRot );
  24.             void SetRotation( float theRotX, float theRotY, float theRotZ );
  25.             void Rotate( sf::Vector3f theRot );
  26.             void Rotate( float theRotX, float theRotY, float theRotZ );
  27.             sf::Vector3f GetRotation();
  28.            
  29.             void Render();
  30.            
  31.             void MoveForward( float amount );
  32.             void MoveSideways( float amount );
  33.            
  34.             void RotateUp( float amount );
  35.             void RotateLeft( float amount );
  36.        
  37.         private:
  38.             sf::Vector3f pos;
  39.             sf::Vector3f rot;
  40.     };
  41. }
  42.  
  43. #endif // GFX_CAMERA_H
  44.  
  45. #include "gfx/Camera.h"
  46.  
  47. #include <SFML/OpenGL.hpp>
  48. #include <cmath>
  49.  
  50. #include "util/Math.h"
  51.  
  52. namespace gfx
  53. {
  54.     Camera::Camera()
  55.        : pos( 0, 0, 0 ),
  56.          rot( 0, 0, 0 )
  57.     {
  58.     }
  59.  
  60.     Camera::Camera( sf::Vector3f thePos )
  61.        : pos( thePos ),
  62.          rot( 0, 0, 0 )
  63.     {
  64.     }
  65.  
  66.     Camera::Camera( float thePosX, float thePosY, float thePosZ )
  67.        : pos( thePosX, thePosY, thePosZ ),
  68.          rot( 0, 0, 0 )
  69.     {
  70.     }
  71.  
  72.     Camera::Camera( sf::Vector3f thePos, sf::Vector3f theRot )
  73.        : pos( thePos ),
  74.          rot( theRot )
  75.     {
  76.     }
  77.  
  78.     Camera::Camera( float thePosX, float thePosY, float thePosZ, float theRotX, float theRotY, float theRotZ )
  79.        : pos( thePosX, thePosY, thePosZ ),
  80.          rot( theRotX, theRotY, theRotZ )
  81.     {
  82.     }
  83.  
  84.     void Camera::SetPosition( sf::Vector3f thePos )
  85.     {
  86.         SetPosition( thePos.x, thePos.y, thePos.z );
  87.     }
  88.  
  89.     void Camera::SetPosition( float thePosX, float thePosY, float thePosZ )
  90.     {
  91.         pos.x = thePosX;
  92.         pos.y = thePosY;
  93.         pos.z = thePosZ;
  94.     }
  95.  
  96.     void Camera::Move( sf::Vector3f thePos )
  97.     {
  98.         pos += thePos;
  99.     }
  100.  
  101.     void Camera::Move( float thePosX, float thePosY, float thePosZ )
  102.     {
  103.         pos.x += thePosX;
  104.         pos.y += thePosY;
  105.         pos.z += thePosZ;
  106.     }
  107.  
  108.     sf::Vector3f Camera::GetPosition()
  109.     {
  110.         return pos;
  111.     }
  112.  
  113.     void Camera::SetRotation( sf::Vector3f theRot )
  114.     {
  115.         SetRotation( theRot.x, theRot.y, theRot.z );
  116.     }
  117.  
  118.     void Camera::SetRotation( float theRotX, float theRotY, float theRotZ )
  119.     {
  120.         rot.x = theRotX;
  121.         rot.y = theRotY;
  122.         rot.z = theRotZ;
  123.     }
  124.  
  125.     void Camera::Rotate( sf::Vector3f theRot )
  126.     {
  127.         rot += theRot;
  128.     }
  129.  
  130.     void Camera::Rotate( float theRotX, float theRotY, float theRotZ )
  131.     {
  132.         rot.x += theRotX;
  133.         rot.y += theRotY;
  134.         rot.z += theRotZ;
  135.     }
  136.  
  137.     sf::Vector3f Camera::GetRotation()
  138.     {
  139.         return rot;
  140.     }
  141.  
  142.     void Camera::Render()
  143.     {
  144.         glRotatef( rot.x, 1.f, 0.f, 0.f );
  145.         glRotatef( rot.y, 0.f, 1.f, 0.f );
  146.         glRotatef( rot.z, 0.f, 0.f, 1.f );
  147.         glTranslatef( -pos.x, -pos.y, -pos.z );
  148.     }
  149.  
  150.     void Camera::MoveForward( float amount )
  151.     {
  152.         pos.x += std::sin( util::DegreesToRadians( rot.z ) ) * amount;
  153.         pos.y += std::cos( util::DegreesToRadians( rot.z ) ) * amount;
  154.     }
  155.  
  156.     void Camera::MoveSideways( float amount )
  157.     {
  158.         pos.x += -std::cos( util::DegreesToRadians( rot.z ) ) * amount;
  159.         pos.y += std::sin( util::DegreesToRadians( rot.z ) ) * amount;
  160.     }
  161.  
  162.     void Camera::RotateUp( float amount )
  163.     {
  164.         rot.z += amount;
  165.        
  166.         // Wrap all of the rotation
  167.         for ( ; rot.x >= 360.f; rot.x -= 360.f );
  168.         for ( ; rot.x <= -360.f; rot.x += 360.f );
  169.        
  170.         for ( ; rot.y >= 360.f; rot.y -= 360.f );
  171.         for ( ; rot.y <= -360.f; rot.y += 360.f );
  172.        
  173.         for ( ; rot.z >= 360.f; rot.z -= 360.f );
  174.         for ( ; rot.z <= -360.f; rot.z += 360.f );
  175.     }
  176.  
  177.     void Camera::RotateLeft( float amount )
  178.     {
  179.         rot.x += amount;
  180.        
  181.         // Wrap all of the rotation
  182.         for ( ; rot.x >= 360.f; rot.x -= 360.f );
  183.         for ( ; rot.x <= -360.f; rot.x += 360.f );
  184.        
  185.         for ( ; rot.y >= 360.f; rot.y -= 360.f );
  186.         for ( ; rot.y <= -360.f; rot.y += 360.f );
  187.        
  188.         for ( ; rot.z >= 360.f; rot.z -= 360.f );
  189.         for ( ; rot.z <= -360.f; rot.z += 360.f );
  190.     }
  191. }
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement