Reputation: 55
Im doing the exercises at the end of this tutorial: Link
However Im stuck on the "Create a camera that rotates around the object"
( position = ObjectCenter + ( radius * cos(time), height, radius * sin(time) ) );
I've used the above code supplied to create the following rotation
GLfloat radius = 10.0f;
position = glm::vec3(0.0f,0.0f,0.0f) + glm::vec3(radius * cos(glfwGetTime()), 0.0f, radius * sin(glfwGetTime()));
Using this the scene rotates around (0,0,0) at a distance of 10, which I thought was the centre of the cube, however it is not, so the cube flashes past the window every so often as the scene rotates.
So how exactly do I find the object centre? I don't understand from the code when I draw the cube where I tell it to centre it to.
For reference the initial position vec is
glm::vec3 position = glm::vec3( 0, 0, 5 );
Upvotes: 0
Views: 1771
Reputation: 26
The easiest you can do is to add the following in the controls.cpp and declare it in controls.hpp
glm::vec3 *getPosition(){
return &position;
}
void setPosition(glm::vec3 var){
position = var;
}
Then you only must declare the following variables in the beginning of the main loop:
float rotCamera = 0.0f;
glm::vec3 origPos = *getPosition();
and add the following lines just after glfwPollEvents(); in the main loop:
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
//To rotate the camera through the object
if (glfwGetKey( window, GLFW_KEY_C ) == GLFW_PRESS){
rotCamera += 0.01;
glm::vec3 rot(cos(rotCamera) * 10.0 - origPos.x, 0, sin(rotCamera) * 10.0 - origPos.z);
setPosition(origPos + rot);
}
Finally if you want the camera looking to the cube all time, you must change lookAt from controls.cpp to look always to the (0,0,0):
ViewMatrix = glm::lookAt(position, // Camera is here
glm::vec3( 0, 0, 0 ), //To look always to the cube
up // Head is up (set to 0,-1,0 to look upside-down)
);
Upvotes: 1