【发布时间】:2017-10-01 03:11:07
【问题描述】:
我一直想知道如何让我的 FPS 相机根据相机的方向基本向前和向后移动,但我一直失败得很惨,我想知道如何以最佳方式做到这一点,就像现在我的代码是在我的三角形后面切换方向(w 变成 s 并且 s 变成 w)并且通常不起作用(移动有时,对角线而不是向前),旋转完美,但平移搞砸了我的矩阵......
void glfwCursorCallback(GLFWwindow* window, double x, double y) {
camera.rx += (x - camera.lcx) * 0.01f;
camera.ry += (y - camera.lcy) * 0.01f;
kmMat4RotationYawPitchRoll(&camera.mat, camera.ry , camera.rx, 0.0f);
camera.lcx = x;
camera.lcy = y;
}
...
kmMat4PerspectiveProjection(&projection, 90.0f, aspect, 0.1f, 1000.f);
float x = 0.0f, y = 0.0f, z = -1.0f;
while(!glfwWindowShouldClose(window)) {
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
/* pitch - ry */
x += 0.1*sin(camera.ry)*cos(camera.rx);
y += 0.1*sin(camera.ry)*sin(camera.rx);
z += 0.1*cos(camera.ry);
}
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
x -= 0.1*sin(camera.ry)*cos(camera.rx);
y -= 0.1*sin(camera.ry)*sin(camera.rx);
z -= 0.1*cos(camera.ry);
}
glClear(GL_COLOR_BUFFER_BIT);
kmMat4Translation(&transform, x, y, z);
kmMat4Multiply(&object, &camera.mat, &transform);
kmMat4Multiply(&final, &projection, &object);
glUniformMatrix4fv(shader.mpm, 1, GL_FALSE, final.mat);
...
我不知道怎么做,因为我以前从来没有做过,所以我希望这里有经验的人能给我一些建议!
编辑:目的是让相机根据方向向前移动。此外,如果我省略 x、y 并将 z 设置为 +- 0.1...,它也能完美运行...所以这不是矩阵乘法的问题
【问题讨论】:
标签: c opengl 3d camera coordinate-transformation