Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[OpenGL GLFW GLAD] Obracająca się kamera

Ostatnio zmodyfikowano 2025-01-25 23:02
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[OpenGL GLFW GLAD] Obracająca się kamera
» 2025-01-25 22:58:13
Witam. Znalazłem kod na kamerę w OpenGL GLFW. Ale jest BUG związany z obracaniem kamery tzn. gdy będziemy poruszać kursorem zgodnie (lub nie) z kierunkiem wskazówek zegara to kamera zacznie się obracać. Wie ktoś jak wyłączyć tę rotację ?

C/C++
#ifndef Camera_hpp
#define Camera_hpp

enum class Camera_Movement { FORWARD, BACKWARD, LEFT, RIGHT };

class Camera {
public:
   
// Camera Attributes
   
glm::vec3 _position;
   
glm::vec3 _front;
   
glm::vec3 _up;
   
glm::vec3 _right;
   
   
// Euler Angles
   
float _yaw;
   
float _pitch;
   
   
// camera options
   
float _movementSpeed;
   
float _mouseSensitivity;
   
float _zoom;
   
   
Camera( glm::vec3 position = glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3 up = glm::vec3( 0.0f, 1.0f, 0.0f ), float yaw = - 90.0f, float pitch = 0.0f ) {
       
_position = position;
       
_up = up;
       
_yaw = yaw;
       
_pitch = pitch;
       
       
_front = glm::vec3( 0.0f, 0.0f, - 1.0f );
       
       
_movementSpeed = 2.5f;
       
_mouseSensitivity = 0.1f;
       
_zoom = 45.0f;
       
updateCameraVectors();
   
}
   
   
~Camera() { }
   
   
glm::mat4 GetViewMatrix()
   
{
       
return glm::lookAt( _position, _position + _front, _up );
   
}
   
   
void processKeyboard( Camera_Movement direction, float deltaTime )
   
{
       
float velocity = _movementSpeed * deltaTime;
       
       
if( direction == Camera_Movement::FORWARD )
           
 _position += _front * velocity;
       
       
if( direction == Camera_Movement::BACKWARD )
           
 _position -= _front * velocity;
       
       
if( direction == Camera_Movement::LEFT )
           
 _position -= _right * velocity;
       
       
if( direction == Camera_Movement::RIGHT )
           
 _position += _right * velocity;
       
   
}
   
   
void processMouseMovement( float xoffset, float yoffset, GLboolean constrainPitch = true )
   
{
       
xoffset *= _mouseSensitivity;
       
yoffset *= _mouseSensitivity;
       
       
_yaw += xoffset;
       
_pitch += yoffset;
       
       
// make sure that when pitch is out of bounds, screen doesn't get flipped
       
if( constrainPitch )
       
{
           
if( _pitch > 89.0f )
               
 _pitch = 89.0f;
           
           
if( _pitch < - 89.0f )
               
 _pitch = - 89.0f;
           
       
}
       
       
// update Front, Right and Up Vectors using the updated Euler angles
       
updateCameraVectors();
   
}
   
private:
   
void updateCameraVectors()
   
{
       
// calculate the new Front vector
       
glm::vec3 front;
       
front.x = cos( glm::radians( _yaw ) ) * cos( glm::radians( _pitch ) );
       
front.y = sin( glm::radians( _pitch ) );
       
front.z = sin( glm::radians( _yaw ) ) * cos( glm::radians( _pitch ) );
       
_front = glm::normalize( front );
       
       
// also re-calculate the Right and Up vector
       
_right = glm::normalize( glm::cross( _front, _up ) ); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
       
_up = glm::normalize( glm::cross( _right, _front ) );
   
}
}
;



#endif
P-182052
tBane
Temat założony przez niniejszego użytkownika
» 2025-01-25 23:02:09
Należy wyłączyć obliczanie _up w funkcji updateCameraVectors()

C/C++
void updateCameraVectors()
{
   
// calculate the new Front vector
   
glm::vec3 front;
   
front.x = cos( glm::radians( _yaw ) ) * cos( glm::radians( _pitch ) );
   
front.y = sin( glm::radians( _pitch ) );
   
front.z = sin( glm::radians( _yaw ) ) * cos( glm::radians( _pitch ) );
   
_front = glm::normalize( front );
   
   
// also re-calculate the Right and Up vector
   
_right = glm::normalize( glm::cross( _front, _up ) ); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
    //_up = glm::normalize(glm::cross(_right, _front));
}
P-182053
« 1 »
  Strona 1 z 1