1
\$\begingroup\$

I am using modern OpenGL with glfw. I made a FPS camera with mouse and it works very well. But I want to move my mouse like Blender's model view mode. When I press the middle mouse button and move the mouse, it should move the camera. I tried it but I couldn't succeed it. I just want it like Blender.

That is my camera class (sorry for indentation):

#pragma once
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

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

const GLfloat YAW        = -90.0f;
const GLfloat PITCH      =  0.0f;
const GLfloat SPEED      =  3.0f;
const GLfloat SENSITIVTY =  0.05f;
const GLfloat ZOOM       =  45.0f;



class Camera
{
public:

glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;

GLfloat Yaw;
GLfloat Pitch;

GLfloat MovementSpeed;
GLfloat MouseSensitivity;
GLfloat Zoom;

Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up =            glm::vec3(0.0f, 1.0f, 0.0f), GLfloat yaw = YAW, GLfloat pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
{
    this->Position = position;
    this->WorldUp = up;
    this->Yaw = yaw;
    this->Pitch = pitch;
    this->updateCameraVectors();
}
Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat  upY, GLfloat upZ, GLfloat yaw, GLfloat pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
{
    this->Position = glm::vec3(posX, posY, posZ);
    this->WorldUp = glm::vec3(upX, upY, upZ);
    this->Yaw = yaw;
    this->Pitch = pitch;
    this->updateCameraVectors();
}

glm::mat4 GetViewMatrix()
{
    return glm::lookAt(this->Position, this->Position + this->Front,  this->Up);
}

void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime)
{
    GLfloat velocity = this->MovementSpeed * deltaTime;
    if (direction == FORWARD)
        this->Position += this->Front * velocity;
    if (direction == BACKWARD)
        this->Position -= this->Front * velocity;
    if (direction == LEFT)
        this->Position -= this->Right * velocity;
    if (direction == RIGHT)
        this->Position += this->Right * velocity;
}

void ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean  constrainPitch = true)
{
    xoffset *= this->MouseSensitivity;
    yoffset *= this->MouseSensitivity;

    this->Yaw   += xoffset;
    this->Pitch += yoffset;

    if (constrainPitch)
    {
        if (this->Pitch > 89.0f)
            this->Pitch = 89.0f;
        if (this->Pitch < -89.0f)
            this->Pitch = -89.0f;
    }

    this->updateCameraVectors();
}

void ProcessMouseScroll(GLfloat yoffset)
{
    if (this->Zoom >= 1.0f && this->Zoom <= 45.0f)
        this->Zoom -= yoffset;
    if (this->Zoom <= 1.0f)
        this->Zoom = 1.0f;
    if (this->Zoom >= 45.0f)
        this->Zoom = 45.0f;
}

private:
void updateCameraVectors()
{
    glm::vec3 front;
    front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this-  >Pitch));
    front.y = sin(glm::radians(this->Pitch));
    front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch));
    this->Front = glm::normalize(front);
    this->Right = glm::normalize(glm::cross(this->Front, this->WorldUp));
    this->Up    = glm::normalize(glm::cross(this->Right, this->Front));
}
};

Thank you.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

First check if the middle mouse button is down, then get the movement of the mouse and convert it to a vector (to a vec3, where x is the movement on the x axis, y is the movement on the y axis and z is 0.

With glm create a rotation matrix using your camera's angles:

glm::mat4 rotPitch = glm::rotate(this->Pitch, glm::vec3(1, 0, 0));
glm::mat4 rotYaw = glm::rotate(this->Yaw, glm::vec3(0, 1, 0));
glm::mat4 rot = rotPitch * rotYaw;

Then multiply your movement vector with this matrix (you need to convert it to a 4d vector):

glm::vec3 transform = vec3(rot * glm::vec4(movement, 0));

Add this vector to the position of the camera and done

\$\endgroup\$
2
  • \$\begingroup\$ glm::rotate method accepts 3 arguments. You provided 2. \$\endgroup\$ Commented Jun 6 at 21:45
  • \$\begingroup\$ @GáborDANI glm.g-truc.net/0.9.4/api/a00206.html The options here either take 2 (like the one I provided) or 4. Make sure to check what you're including next time \$\endgroup\$
    – Bálint
    Commented Jun 12 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .