My goal is to navigate in the view port using the mouse.
Every frame that the mouse move, I recalculate the cameraFront and cameraUp vectors and finally the view matrix. The problem is that the view matrix sometimes create rotation in the z axis, witch I don't expected to be.
I am not sure what I am doing wrong.
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = ypos - lastY;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.05;
xoffset *= sensitivity;
yoffset *= sensitivity;
glm::quat rotY = glm::angleAxis(glm::radians(xoffset), cameraUp);
cameraFront = glm::normalize(rotY * cameraFront);
glm::vec3 rightAxis = glm::cross(cameraUp, cameraFront);
glm::quat rotX = glm::angleAxis(glm::radians(yoffset), rightAxis);
cameraFront = glm::normalize(rotX * cameraFront);
cameraUp = glm::normalize(glm::cross(cameraFront, rightAxis));
}
in the while loop am recalculate the view matrix:
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
I am learning OpenGl from the Tutorial which show example how to navigate in the scene, but I am trying to it differently.
can anyone see my mistake?
Related
I want to select an object in opengl glfw renderer and move the object in the scene. Currently I am able to create ray and its direction. Now I want to check if this ray intersects with my object in the renderer. How can I check if the ray hits my object and then I can say that my object is selected?
glm::vec3 CreateRay()
{
float mouseX = xPos/ (width * 0.5f) - 1.0f;
float mouseY = yPos / (height * 0.5f) - 1.0f;
glm::mat4 invVP = glm::inverse(proj * view);
glm::vec4 screenPos = glm::vec4(mouseX, -mouseY, 1.0f, 1.0f);
glm::vec4 worldPos = invVP * screenPos;
glm::vec3 dir = glm::normalize(glm::vec3(worldPos));
return dir;
}
glm::vec3 rayDirection = CreateRay();
glm::vec3 rayStartPositon = pCamera->GetCameraPosition();
glm::vec3 rayEndPosition = rayStartPositon + rayDirection * 2.0f;
I'm using a "Dymanic Batch Renderer System", and i have an "Object.cpp" that has a function, and when it's call it returns the data it needs for the Batch to render a "Quad" on screen (also i'm gonna mention that this is on a 3D space so the Z movement, Z scaling and the XY rotation exist).
And for the math calculations i'm using the GLM library.
The rendering works fine and the batch too, the problem is the movement. The rotation actually works the way i want it to work, but the movement is what i'm not satisfied because it moves in the "Local Space" of the object. Meaning that, for example, if i rotate an object inside a batch 90° on the Y Axis, the X movement becomes Z movement, and Z movement becomes X movement.
I've been trying to look for an answer and i couldn't find anything. I think the problem probably is from the "rotationMatrix" that allows the object to rotate correctly, but i don't know if there's an extra "function" i have to add to move the object in the "World Space" instead of the "Local Space", and if there is, i don't know what "function" can be.
Now i'm gonna put here the entire code of "Object.cpp" so you guys can see how it works.
Object::Object(glm::vec3 pos, glm::vec3 rot, glm::vec3 sca, int ObjId)
: translationMatrix(glm::mat4(0)), rotationMatrix(glm::mat4(0))
{
id = ObjId;
position = pos;
lastPosition = pos + glm::vec3(1.0f);
scale = sca;
rotation = rot;
lastRotation = rot + glm::vec3(1.0f);
}
glm::mat4 One(1.0f);
Vertex* Object::UpdateObject(Vertex* target)
{
if (lastPosition != position)
{
translationMatrix = glm::translate(glm::identity<glm::mat4>(), -position);
lastPosition = position;
}
if (lastRotation != rotation)
{
glm::mat4 rotMatrixTemp(1.0f);
rotMatrixTemp = glm::rotate(rotMatrixTemp, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
rotMatrixTemp = glm::rotate(rotMatrixTemp, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
rotMatrixTemp = glm::rotate(rotMatrixTemp, glm::radians(rotation.z + 180.0f), glm::vec3(0.0f, 0.0f, 1.0f));
rotationMatrix = -translationMatrix * rotMatrixTemp * translationMatrix;
lastRotation = rotation;
}
float x = 1.0f, y = 1.0f;
if (flipX)
x *= -1;
if (flipY)
y *= -1;
target->position = rotationMatrix * glm::vec4(position.x - 0.5f * scale.x, position.y + 0.5f * scale.y, position.z, 1.0f);
target->color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
target->texcoord = glm::vec2(0.0f, y);
target++;
target->position = rotationMatrix * glm::vec4(position.x - 0.5f * scale.x, position.y - 0.5f * scale.y, position.z, 1.0f);
target->color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
target->texcoord = glm::vec2(0.0f, 0.0f);
target++;
target->position = rotationMatrix * glm::vec4(position.x + 0.5f * scale.x, position.y - 0.5f * scale.y, position.z, 1.0f);
target->color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
target->texcoord = glm::vec2(x, 0.0f);
target++;
target->position = rotationMatrix * glm::vec4(position.x + 0.5f * scale.x, position.y + 0.5f * scale.y, position.z, 1.0f);
target->color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
target->texcoord = glm::vec2(x, y);
target++;
return target;
}
So, to recap, what i'm trying to accomplish is moving these objects in the "World Space" instead of the "Local Space" (while also, keeping the rotation system in the "Local Space", if possible. Because otherwise the object's center is always gonna be (0, 0, 0) instead of being its own position).
To position an object in the world with a given orientation, you usually apply the rotation first then the translation, unless you are indeed trying to rotate the objects about the origin after translating it out. So either check that, or make sure you have a good reason to do have the translationMatrix in
rotationMatrix = -translationMatrix * rotMatrixTemp * translationMatrix
Because it seems like you have translation logic both in and out of the if blocks
I´m trying to implements a bullet so I have this free movement first person camera. I got this camera from learnopengl.com this is the coding:
// Default camera values
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
// An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL
class Camera
{
public:
// Camera Attributes
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;
// Euler Angles
float Yaw;
float Pitch;
// Camera options
float MovementSpeed;
float MouseSensitivity;
float Zoom;
// Constructor with vectors
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 = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
// Constructor with scalar values
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
// Returns the view matrix calculated using Euler Angles and the LookAt Matrix
glm::mat4 GetViewMatrix()
{
return glm::lookAt(Position, Position + Front, Up);
}
// Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
}
// Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
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();
}
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
void ProcessMouseScroll(float yoffset)
{
if (Zoom >= 1.0f && Zoom <= 45.0f)
Zoom -= yoffset;
if (Zoom <= 1.0f)
Zoom = 1.0f;
if (Zoom >= 45.0f)
Zoom = 45.0f;
}
private:
// Calculates the front vector from the Camera's (updated) Euler Angles
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, WorldUp)); // 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));
}
};
So now I want to create a bullet that starts from
model = glm::translate(model, camara.Position+7.0f*camara.Front);
The issue is that as I try to move the camera the object rotates with it which I know why but I don't know how to fix it, I have tried something like this:
model = glm::rotate(model, glm::radians(camara.Pitch), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, -glm::radians(camara.Yaw), glm::vec3(0.0f, 1.0f, 0.0f));
trying to sync the rotations but it's not working.
I want to store the position because then I want the bullets to go straight no matter where I move. Thank you.
This is how I always want it to look:
This is how it rotates as I move:
I am having trouble about understand the translate the camera. I already can successfully rotate the camera, but I am still confused about translating the camera. I include the code about how to rotate the camera, Since translating and rotating need to use the lookat function. The homework says translating the camera means that both the eye and the center should be moved with the same amount. I understand I can change the parameters in the lookat function to implement this.
The definition of lookat function is below:
Lookat(cameraPos, center, up)
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 10.0f);
glm::vec3 center(0.0f, 0.0f, 0.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
modelViewProjectionMatrix.Perspective(glm::radians(fov), float(width) / float(height), 0.1f, 100.0f);
modelViewProjectionMatrix.LookAt(cameraPos, center, cameraUp);
void CursorPositionCallback(GLFWwindow* lWindow, double xpos, double ypos)
{
int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
if (state == GLFW_PRESS)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY- ypos;
lastX = xpos;
lastY = ypos;
yaw += xoffset;
pitch += yoffset;
glm::vec3 front;
front.x = center[0] + 5.0f*cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = center[1] + 5.0f*sin(glm::radians(pitch));
front.z = center[1] + 5.0f*sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraPos = front;
}
}
If you want to translate the camera by an offset, then you've to add the same vector (glm::vec3 offset) to the camera position (cameraPos) and the camera target (center):
center = center + offset;
cameraPos = cameraPos + offset;
When you calculate a new target of the camera (center), by a pitch and yaw angle, then you've to update the up vector (cameraUp) of the camera, too:
glm::vec3 front(
cos(glm::radians(pitch)) * cos(glm::radians(yaw)),
sin(glm::radians(pitch)),
cos(glm::radians(pitch)) * sin(glm::radians(yaw))
);
glm::vec3 up(
-sin(glm::radians(pitch)) * cos(glm::radians(yaw)),
cos(glm::radians(pitch)),
-sin(glm::radians(pitch)) * sin(glm::radians(yaw))
);
cameraPos = center + front * 5.0f;
cameraUp = up;
To translate the camera along the x axis (from left to right) in viewspace you've to calculate the vector to the right by the Cross product of the vector to the target (front) and the up vector (cameraUp or up):
glm::vec3 right = glm::cross(front, up);
The y axis (from bottom to top) in viewspace, is the up vector.
To translate about the scalars (float trans_x) and (trans_y), the scaled right and up vector have to be add to the camera position (cameraPos) and the camera target (center):
center = center + right * trans_x + up * trans_y;
cameraPos = cameraPos + right * trans_x + up * trans_y;
Use the manipulated vectors to set the view matrix:
modelViewProjectionMatrix.LookAt(cameraPos, center, cameraUp);
I'm trying to implement a camera all by myself in OpenGL (I use glfw and gml).
As for now, I don't have any class for it. I will create it later. So here is my try on coding the camera movements; it works fine with simple mouse movements, but otherwise, the camera tilts sideways. I'm still new to OpenGL so I don't have a lot to show but here is illustrated my problem: http://imgur.com/a/p9xXQ
I have a few (global as for now) variables :
float lastX = 0.0f, lastY = 0.0f, yaw = 0.0f, pitch = 0.0f;
glm::vec3 cameraPos(0.0f, 0.0f, 3.0f);
glm::vec3 cameraUp(0.0f, 1.0f, 0.0f); // As a reminder, x points to the right, y points upwards and z points towards you
glm::vec3 cameraFront(0.0f, 0.0f, -1.0f);
With these, I can create a view matrix this way :
glm::mat4 view;
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
I want to be able to move my camera perpendicularly (yaw) and laterally (pitch), i.e. up, down, right, left on my screen. For this, it is enough to rotate the cameraFront vector and the cameraUp vector appropriately and then update the view matrix with the updated vectors.
My Cursor Position Callback looks like this :
glm::vec3 rotateAroundAxis(glm::vec3 toRotate, float angle, glm::vec3 axisDirection, glm::vec3 axisPoint) { // angle in radians
toRotate -= axisPoint;
glm::mat4 rotationMatrix(1.0f);
rotationMatrix = glm::rotate(rotationMatrix, angle, axisDirection);
glm::vec4 result = rotationMatrix*glm::vec4(toRotate, 1.0f);
toRotate = glm::vec3(result.x, result.y, result.z);
toRotate += axisPoint;
return toRotate;
}
void mouseCallback(GLFWwindow* window, double xpos, double ypos) {
const float maxPitch = float(M_PI) - float(M_PI) / 180.0f;
glm::vec3 cameraRight = -glm::cross(cameraUp, cameraFront);
float xOffset = xpos - lastX;
float yOffset = ypos - lastY;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.0005f;
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset; // useless here
pitch += yOffset;
if (pitch > maxPitch) {
yOffset = 0.0f;
}
if (pitch < -maxPitch) {
yOffset = 0.0f;
}
cameraFront = rotateAroundAxis(cameraFront, -xOffset, cameraUp, cameraPos);
cameraFront = rotateAroundAxis(cameraFront, -yOffset, cameraRight, cameraPos);
cameraUp = rotateAroundAxis(cameraUp, -yOffset, cameraRight, cameraPos);
}
As I said, it works fine for simple up-down, left-right camera movements, but when I start to move my mouse in circles or like a madman, the camera starts to rotate longitudinally (roll).
I've tried to force cameraRight.y = cameraPos.y so that the cameraRight vector doesn't tilt upwards/downwards due to numerical errors but it doesn't solve the problem. I've also tried to add a (global) cameraRight vector to keep track of it instead of computing it every time so the end of the function looks like this :
cameraFront = rotateAroundAxis(cameraFront, -xOffset, cameraUp, cameraPos);
cameraRight = rotateAroundAxis(cameraRight, -xOffset, cameraUp, cameraPos);
cameraFront = rotateAroundAxis(cameraFront, -yOffset, cameraRight, cameraPos);
cameraUp = rotateAroundAxis(cameraUp, -yOffset, cameraRight, cameraPos);
but it doesn't solve the problem. Any pieces of advice ?
It seems you have global X-axis to the right, Y-axis going deep in the screen and Z-axis going up. And you local camera axis system is similar.
The desired behaviour is rotate the camera over its current position, left-right mouse movement is rotation around global Z, and up-dowm mouse movement is rotation around local X. Think a bit around these rotations until you understand them well, and why one is around global but the other around local directions. Imagine a security camera and its movements to visualize the axis systems and rotations.
The goal is getting the parameters used to define the View transformation by lookAtfunction.
First rotate around local X. We convert this local vector into global axis system by inverting the current View-matrix, you call view
glm::vec3 currGlobalX = glm::normalize((glm::inverse(view) * glm::vec4(1.0, 0.0, 0.0, 0.0)).xyz);
We need to rotate not only the cameraUp vector, but also the current target defined in global coordinates, what you call cameraPos + cameraFront:
cameraUp = rotateAroundAxis(cameraUp, -yOffset, currGlobalX, glm::vec3(0.0f, 0.0f, 0.0f)); //vector, not needed to translate
cameraUp = glm::normalize(cameraUp);
currenTarget = rotateAroundAxis(currenTarget, -yOffset, currGlobalX, cameraPos); //point, need translation
Now rotate around global Z-axis
cameraUp = rotateAroundAxis(cameraUp, -xOffset, glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); //vector, not needed to translate
cameraUp = glm::normalize(cameraUp);
currenTarget = rotateAroundAxis(currenTarget, -xOffset, glm::vec3(0.0f, 0.0f, 1.0f), cameraPos); //point, need translation
Finally, update view:
view = glm::lookAt(cameraPos, currenTarget, cameraUp);