I want to create a game in which I rotate a cube around 2 fixed axes(x and y) using my mouse.
Here is what I want to do. Just use the mouse to see what kind of rotation I want.
I calculated my yaw and pitch values according to mouse movement and they work fine when I try to rotate only around one axis(x or y), but when I try to rotate around both axes it doesn't work(because OpenGL rotates axes when you apply a rotation). How do I make them both work at the same time? Here my code:
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
float cameraDistance=3.0;
glm::vec3 center=glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 modelDistance= glm::vec3(0.0,0.0,3.0);
int oldX,oldY;
GLfloat yaw = 0.0f; // Yaw is initialized to -90.0 degrees since a yaw of 0.0
// results in a direction vector pointing to the right (due
// to how Euler angles work) so we initially rotate a bit
// to the left.
GLfloat pitch = 0.0f;
while (running)
{
oldX=sf::Mouse::getPosition(window).x;
oldY=sf::Mouse::getPosition(window).y;
yaw=oldX*0.1;
pitch=oldY*0.1;
projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1.0f, 100.0f);
view=glm::mat4();
view = glm::lookAt(center+modelDistance, center+modelDistance + cameraFront, cameraUp);
model= glm::mat4();
//Here is the problem,how do i use both at the same time?
model=glm::rotate(model,glm::radians(yaw),glm::vec3(0.0,1.0,0.0));
model=glm::rotate(model,glm::radians(pitch),glm::vec3(1.0,0.0,0.0));
//
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ourShader.Use();
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 27*n*n);