我正在使用C ++ openGL库处理简单的房间, 我有转换问题,
首先我的系统工作如下:
按w:沿-z轴移动,s:沿+ z轴移动,a:向左移动,d:向右移动。
按左键&右键:围绕y轴旋转, 按下&向下键:围绕x轴旋转。
这是我将旋转和翻译结合在一起的代码。
glm::vec3 _camVelocity = glm::vec3(0.1, 0.1, 0.1);
glm::vec3 _camRotation = glm::vec3(0,0,0);
SDL_Event evnt;
while (SDL_PollEvent(&evnt))
{
switch (evnt.type)
{
case SDL_KEYDOWN:
switch(evnt.key.keysym.sym) {
case SDLK_w:
_camera.translate(-_camVelocity.x * sin(glm::radians(_camRotation.y)), 0.0,- _camVelocity.z * cos(glm::radians(_camRotation.y)));
break;
case SDLK_s:
_camera.translate(_camVelocity.x * sin(glm::radians(_camRotation.y)), 0.0, _camVelocity.z * cos(glm::radians(_camRotation.y)));
break;
case SDLK_a:
_camera.translate(-_camVelocity.x * cos(glm::radians(-_camRotation.y)), 0.0, -_camVelocity.z * sin(glm::radians(-_camRotation.y)));
break;
case SDLK_d:
_camera.translate(_camVelocity.x * cos(glm::radians(-_camRotation.y)), 0.0, _camVelocity.z * sin(glm::radians(-_camRotation.y)));
break;
case SDLK_LEFT:
_camRotation.y += 1;
break;
case SDLK_RIGHT:
_camRotation.y -= 1;
break;
case SDLK_UP:
_camRotation.x += 1;
break;
case SDLK_DOWN:
_camRotation.x -= 1;
break;
}
break;
}
_camera.setRotation(0, 1, 1, 1);
_camera.rotate(_camRotation.x, 1, 0, 0);
_camera.rotate(_camRotation.y, 0, 1, 0);
}}
用于声明使用的函数:
void Camera3D::setRotation(float rotationAngle, float x, float y, float z){
rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(rotationAngle), glm::vec3(x, y, z));
updateViewMatrix();
}
void Camera3D::translate(float x, float y, float z){
translationMatrix = glm::translate(translationMatrix, glm::vec3(x, y, z));
updateViewMatrix();
}
void Camera3D::rotate(float rotationAngle, float x, float y, float z){
rotationMatrix = glm::rotate(rotationMatrix, glm::radians(rotationAngle), glm::vec3(x, y, z));
updateViewMatrix();
}
void Camera3D::updateViewMatrix() {
viewMatrix = rotationMatrix * translationMatrix;
}
对于我将其发送到顶点着色器以与所有顶点相乘的均匀矩阵:
projectMatrix = glm::perspective(glm::radians(45.0f), float (4/3), 0.01f, 1000.0f);
sendMatrix = projectMatrix * viewMatrix;
现在,这段代码与房间完美配合,但只有当我看到房间的任何角落(面对我的脸)时,翻译才会翻转!
当我按下(w& s)时,我会通过x轴!和(a& d)我通过z轴!
我不知道为什么!