在摄像机沿Y轴和X旋转并随后移动之后,是摄像机沿Z轴的奇怪旋转。
例如,这是正常状态
< br />,但是我在舞台上随意走动,全都扭曲
我不知道该怎么办以及如何解决该问题,希望对您有所帮助。
我看到了这个问题,但对我没有帮助,因为我什至不使用glm,也不要将其标记为重复项。
代码:
#include <iostream>
#include <chrono>
#include <GL/glut.h>
#include "Camera.h"
using namespace std;
constexpr auto FPS_RATE = 120;
int windowHeight = 600, windowWidth = 600, windowDepth = 600;
float angle = 0, speedRatio = 0.25;
struct MyPoint3f
{
float x;
float y;
float z;
};
MyPoint3f lastMousePos = { };
bool mouseButtonWasPressed = false;
float mouseSensitivity = 0.1;
float camMoveSpeed = 3;
float camPitchAngle = 0, camYawAngle = 0;
Camera cam;
void init();
void displayFunction();
void idleFunction();
void reshapeFunction(int, int);
void keyboardFunction(unsigned char, int, int);
void specialKeysFunction(int, int, int);
void mouseFunc(int, int, int, int);
void motionFunction(int, int);
double getTime();
double getTime()
{
using Duration = std::chrono::duration<double>;
return std::chrono::duration_cast<Duration>(
std::chrono::high_resolution_clock::now().time_since_epoch()
).count();
}
const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;
void init()
{
glutDisplayFunc(displayFunction);
glutIdleFunc(idleFunction);
glutReshapeFunc(reshapeFunction);
glutKeyboardFunc(keyboardFunction);
glutMouseFunc(mouseFunc);
glutMotionFunc(motionFunction);
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, -windowDepth / 2, windowDepth / 2);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
cam.setShape(45, (double)windowWidth / windowHeight, 0.1, 1000);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
cam.set(Point3(0, 0, 350), Point3(0, 0, 349), Vector3(0, 1, 0));
}
void displayFunction()
{
angle += speedRatio;
if (angle >= 360 || angle <= -360) angle = 0;
if (camPitchAngle <= -360) camPitchAngle = 0;
if (camPitchAngle >= 360) camPitchAngle = 0;
if (camYawAngle <= -360) camYawAngle = 0;
if (camYawAngle >= 360) camYawAngle = 0;
cout << camPitchAngle << " " << camYawAngle << endl;
cam.pitch(-(camPitchAngle *= mouseSensitivity));
cam.yaw(-(camYawAngle *= mouseSensitivity));
camPitchAngle = 0; camYawAngle = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glColor3f(0, 1, 0);
glutWireCube(150.0);
glBegin(GL_LINES);
glColor3f(1, 0, 0);
for (int i = 0; i <= 75; i += 5)
{
glVertex3i(i, 0, 0);
glVertex3i(-i, 0, 0);
glVertex3i(0, i, 0);
glVertex3i(0, -i, 0);
glVertex3i(0, 0, i);
glVertex3i(0, 0, -i);
}
glEnd();
glPopMatrix();
//RSHIFT and CTRL
if (GetAsyncKeyState(VK_LSHIFT))
{
cam.slide(0, 1.0 * camMoveSpeed, 0);
}
if (GetAsyncKeyState(VK_LCONTROL))
{
cam.slide(0, -1.0 * camMoveSpeed, 0);
}
glutSwapBuffers();
}
void idleFunction()
{
const double current_time = getTime();
if ((current_time - last_render) > frame_delay)
{
last_render = current_time;
glutPostRedisplay();
}
}
void reshapeFunction(int w, int h)
{
}
void keyboardFunction(unsigned char key, int w, int h)
{
switch (key)
{
case '+': case '=':
speedRatio += 0.125;
break;
case '-': case '_':
speedRatio -= 0.125;
break;
case 'A': case 'a':
cam.slide(-1.0 * camMoveSpeed, 0, 0);
break;
case 'D': case 'd':
cam.slide(1.0 * camMoveSpeed, 0, 0);
break;
case 'W': case 'w':
cam.slide(0, 0, -1.0 * camMoveSpeed);
break;
case 'S': case 's':
cam.slide(0, 0, 1.0 * camMoveSpeed);
break;
case 'Z': case 'z':
cam.yaw(-1);
break;
case 'X': case 'x':
cam.yaw(1);
break;
case 27:
angle = 0;
speedRatio = 0;
cam.set(Point3(0, 0, 350), Point3(0, 0, 349), Vector3(0, 1, 0));
break;
default:
cout << key << endl;
break;
}
}
void specialKeysFunction(int key, int x, int y)
{
cout << key << endl;
}
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseButtonWasPressed = true;
lastMousePos.x = x;
lastMousePos.y = y;
}
}
void motionFunction(int mousePosX, int mousePosY)
{
if (mousePosX >= 0 && mousePosX < windowWidth && mousePosY >= 0 && mousePosY < windowHeight)
{
if (mouseButtonWasPressed)
{
camPitchAngle += -mousePosY + lastMousePos.y;
camYawAngle += mousePosX - lastMousePos.x;
lastMousePos.x = mousePosX;
lastMousePos.y = mousePosY;
}
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
glutCreateWindow("Window");
init();
glutMainLoop();
return 0;
}
答案 0 :(得分:1)
这样做的时候
glRotatef(angle, 1, 0, 0); glRotatef(angle, 0, 1, 0);
然后,将模型绕y轴旋转,然后将旋转的模型绕x轴旋转,因为glRotatef
设置旋转矩阵并将其乘以当前矩阵。
因为模型在绕x轴旋转之前先绕y轴旋转,所以(视图空间)y轴保持为视图空间的yz平面。
如果要将x轴保持在视图空间的xz平面中,则必须先绕x轴旋转:
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 1, 0, 0);
将pich()
和yaw()
应用于相机对象时,会发生相同的问题。如果您进行切换(首先切换yaw()
,然后切换pitch()
),则将无法解决问题,因为每次鼠标移动(pich()
,{{1} },yaw()
,pich()
,yaw()
,pich()
...)。因此,在yaw()
之后总会有yaw()
,并且模型会倾斜。
要解决此问题,您必须总结pitch()
和camPitchAngle
。考虑鼠标强度:
camYawAngle
在void motionFunction(int mousePosX, int mousePosY)
{
if (mousePosX >= 0 && mousePosX < windowWidth && mousePosY >= 0 && mousePosY < windowHeight)
{
if (mouseButtonWasPressed)
{
camPitchAngle += (-mousePosY + lastMousePos.y) * mouseSensitivity;
camYawAngle += (mousePosX - lastMousePos.x) * mouseSensitivity;
lastMousePos.x = mousePosX;
lastMousePos.y = mousePosY;
}
}
}
中复制相机对象(cam
/ curr_cam
),然后将displayFunction
和camPitchAngle
应用于副本。使用副本设置视图和投影矩阵:
camYawAngle
当然,当按下 z , x 或 ESC 时,您必须分别设置void displayFunction()
{
// [...]
// cam.pitch(-(camPitchAngle *= mouseSensitivity)); <--- delete
// cam.yaw(-(camYawAngle *= mouseSensitivity)); <--- delete
// [...]
Camera curr_cam = cam;
curr_cam.yaw( -camYawAngle );
curr_cam.pitch( -camPitchAngle );
// [...]
if (GetAsyncKeyState(VK_LSHIFT))
{
curr_cam.slide(0, 1.0 * camMoveSpeed, 0);
}
if (GetAsyncKeyState(VK_LCONTROL))
{
curr_cam.slide(0, -1.0 * camMoveSpeed, 0);
}
// [...]
}
camYawAngle = 0