Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #pragma once
  2. #include "GraphicsEngine/Component.h"
  3. #include "GraphicsEngine/Input.h"
  4. #include "GraphicsEngine/Object.h"
  5. #include "GraphicsEngine/Transform.h"
  6. #include "GraphicsEngine/Time.h"
  7. #include "GraphicsEngine/Vector3.h"
  8.  
  9. #include "GraphicsEngine/GraphicsApi/OpenGL20/GL20.h"
  10.  
  11. #include <iostream>
  12.  
  13. class CameraController : public Component
  14. {
  15.     Vector3 mousePos;
  16.     Vector3 mousePosPrev;
  17.     bool ignoreNext = false;
  18. public:
  19.     CameraController()
  20.     {
  21.         mousePos        = Vector3::Zero();
  22.         mousePosPrev    = Vector3::Zero();
  23.     }
  24.  
  25.     virtual ~CameraController() {}
  26.  
  27.     virtual void Update()
  28.     {
  29.         Transform * pTransform = m_pObject->m_pTransform;
  30.        
  31.         // Camera Translation
  32.         {
  33.             Vector3 forward = pTransform->GetForward();
  34.             Vector3 right = pTransform->GetRight();
  35.  
  36.             double dt = Time::GetDeltaTime();
  37.  
  38.             const double speed = 1.0;
  39.             Vector3 direction;
  40.  
  41.             if (Input::GetKey(KEY_CODE_W))
  42.             {
  43.                 direction += forward;
  44.             }
  45.  
  46.             if (Input::GetKey(KEY_CODE_S))
  47.             {
  48.                 direction -= forward;
  49.             }
  50.  
  51.  
  52.             if (Input::GetKey(KEY_CODE_D))
  53.             {
  54.                 direction += right;
  55.             }
  56.  
  57.             if (Input::GetKey(KEY_CODE_A))
  58.             {
  59.                 direction -= right;
  60.             }
  61.            
  62.             // TODO : Task08
  63.            
  64.             pTransform->Translate( speed * dt * direction );
  65.         }
  66.  
  67.         // Camera Rotation
  68.         {
  69.  
  70.             if (mousePosPrev.x == 0 && mousePosPrev.y == 0) {
  71.                 mousePosPrev = Input::GetMousePosition();
  72.                 return;
  73.             }
  74.  
  75.             const double speed = 0.1;
  76.             Vector3 direction;
  77.  
  78.             mousePos = Input::GetMousePosition();
  79.  
  80.             if (ignoreNext) {
  81.                 if (fabs(mousePos.x - mousePosPrev.x) > 0.0002f && fabs(mousePos.y - mousePosPrev.y) > 0.0002f) {
  82.                     return;
  83.                 }
  84.                 ignoreNext = false;
  85.             }
  86.  
  87.  
  88.  
  89.             direction = mousePos - mousePosPrev;
  90.             direction = direction * speed;
  91.  
  92.            
  93.  
  94.             pTransform->Rotate(direction.y, direction.x, 0);           
  95.  
  96.             ignoreNext = true; 
  97.         }
  98.     }
  99. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement