Hello,
I've decided implement camera rotation based on mouse input, but somewhere during the process it went wrong.
Basically I tried following this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-and-mouse/
Instead of working properly the programm actually rotates camera but in a very peculiar manner: no matter in what direction I move my mouse the image always moves in the same direction and then flips and so on.
After doing the tutorial I ended up with something like this;
void Camera::Rotate(Sint32 dx, Sint32 dy){
m_horizontalAngle+= mouseSpeed*float(width/2-dx);
m_verticalAngle+=mouseSpeed*float(height/2-dy);
m_forward.x = cosf(m_verticalAngle)*sinf(m_horizontalAngle);
m_forward.y = sinf(m_verticalAngle);
m_forward.z = cosf(m_verticalAngle)*cosf(m_horizontalAngle);
m_right = glm::vec3(sinf(m_horizontalAngle-3.14f/2.0f),0,cosf(m_horizontalAngle-3.14f/2.0f));
m_up=glm::cross(m_right,m_forward);
}
The function itself is called from the main loop
void mainLoop(Display* disp, World * wrld){
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type==SDL_MOUSEMOTION){
std::cout<<"("<<e.motion.xrel<<","<<e.motion.yrel<<")"<<std::endl;
wrld->CameraPtr()->Rotate(e.motion.xrel,e.motion.yrel);
}
}
wrld->Draw();
}
wrld->Draw() is made this way
void World::Draw(){
glClearColor(0.2f,0.3f,0.1f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 VP =m_camera->GetViewProjection();
for(unsigned int i =0; i<m_list.size();i++){
m_list[i]->TexturePtr()->Bind(0);
m_shader.Update(*(m_list[i]->PositionPtr()),*(m_list[i]->RotationPtr()),*(m_list[i]->ScalePtr()),VP);
m_list[i]->MeshPtr()->Draw();
}
}
m_camera->GetViewProjection() is the member of Camera class
inline glm::mat4 GetViewProjection() const{
return m_perspective* glm::lookAt(m_position,m_position+m_forward,m_up); }
m_shader.Update(..) is
void Shader::Update(glm::vec3& position,glm::vec3& rotation, glm::vec3& scale, glm::mat4 viewProjection)
{
glm::mat4 posMatrix = glm::translate(position);
glm::mat4 rotXMatrix = glm::rotate(rotation.x,glm::vec3(1,0,0));
glm::mat4 rotYMatrix = glm::rotate(rotation.y,glm::vec3(0,1,0));
glm::mat4 rotZMatrix = glm::rotate(rotation.z,glm::vec3(0,0,1));
glm::mat4 scaleMatrix = glm::scale(scale);
glm::mat4 wmat = viewProjection* posMatrix * rotZMatrix * rotYMatrix * rotXMatrix * scaleMatrix;
glUniformMatrix4fv(m_uniforms[TRANSFORM_U], 1, GL_FALSE,&wmat[0][0]);
}
Without the rotation part all the object display correctly and I can move in space using WASD keys.
I would greatly appreciate any help:)