So I'm working on a 3d painting application. I managed to make a basic renderer and model loader.
I created a camera system where I can use it to navigate around the scene(mouse/keyboard) but that's not what I want, so I made that camera static and now I'm trying to rotate/pan/zoom the model itself. I managed to implement panning and zooming. for panning i change the x/y position according to the mouse and for zooming I add or substract from the z-axis according to the mouse scroll.
But now I want to be able to rotate the 3d model with the mouse. Example: When i hold right mouse button and move the mouse up the model should rotate on it's x-axis(pitch) and if i move the mouse to the left/right it should rotate on y-axis(yaw). And I just couldn't do it.
The code bellow I get xpos/ypos of the cursor on the screen, calculate the offset and "trying to rotate the cube". The only problem is that i can't rotate the cuber normally if i move the mouse up the model rotate on the x-axis and y-axis with a little tilt and vice-versa.
This is the code in my rendering loop:
shader.use();
glm::mat4 projection = glm::perspective(glm::radians(45.0f),
(float)SCR_WIDTH/(float)SCR_HEIGHT, 0.01f, 100.0f);
shader.setMat4f("projection", projection);
glm::mat4 view = camera.getViewMatrix();
shader.setMat4f("view", view);
glm::mat4 modelm = glm::mat4(1.0f);
modelm = glm::translate(modelm, object_translation);
// Should rotate cube according to mouse movement
//modelm = glm::rotate(modelm, glm::radians(angle), glm::vec3(0.0f));
shader.setMat4f("model", modelm);
renderer.draw(model, shader);
This is the call where i handle the mouse movement callback:
void mouseCallback(GLFWwindow* window, double xpos, double ypos)
{
if (is_rotating)
{
if (is_first_mouse)
{
lastX = xpos;
lastY = ypos;
is_first_mouse = false;
}
// xpos and ypos are the cursor coords i get those with the
mouse_callback
double xoffset = xpos - lastX;
double yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
object_rotation.y += xoffset; // If i use yoffset the rotation flips
object_rotation.x += yoffset;
rotation_angle += (xoffset + yoffset) * 0.25f;
}
}
Mouse panning works fine too can't say the same for the rotation.