相对相机旋转?
我目前已设置此更新代码来旋转和移动我的相机。
float move = 0.5f;
float look = 0.01f;
//Rotation
if (Keyboard[Key.Left]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationY (-look));
}
if (Keyboard[Key.Right]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationY (look));
}
if (Keyboard[Key.Up])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationX (-look));
}
if (Keyboard[Key.Down])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationX (look));
}
//Movement
if (Keyboard[Key.W])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (0f, 0f, move));
}
if (Keyboard[Key.S]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (0f, 0f, -move));
}
if (Keyboard[Key.A])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (move, 0f, 0));
}
if (Keyboard[Key.D]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (-move, 0f, 0));
}
当我移动相机时,模拟围绕世界原点(0,0,0)而不是当前位置旋转的效果。
我的模型视图矩阵的加载方式如下:
GL.MatrixMode (MatrixMode.Modelview);
GL.LoadMatrix (ref modelview);
I currently have this update code set up to rotate and move my camera.
float move = 0.5f;
float look = 0.01f;
//Rotation
if (Keyboard[Key.Left]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationY (-look));
}
if (Keyboard[Key.Right]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationY (look));
}
if (Keyboard[Key.Up])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationX (-look));
}
if (Keyboard[Key.Down])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationX (look));
}
//Movement
if (Keyboard[Key.W])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (0f, 0f, move));
}
if (Keyboard[Key.S]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (0f, 0f, -move));
}
if (Keyboard[Key.A])
{
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (move, 0f, 0));
}
if (Keyboard[Key.D]) {
modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (-move, 0f, 0));
}
When I move my camera simulates the effect of rotating around the world origin(0,0,0) rather than its current position.
My model view matrix is loaded like so:
GL.MatrixMode (MatrixMode.Modelview);
GL.LoadMatrix (ref modelview);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码还有很多工作要做。长话短说:
我相信您的代码是渲染循环的一部分?
您需要:
move
替换为 3 个变量,用于 x、y、z 方向的移动,look
替换为 2 个变量,yaw
对于左右外观和上下外观的pitch
,请谷歌搜索“欧拉角”以获取更多相关理论;之后,在每一帧中,您应该:
yaw
,pitch
代码>.总而言之,您需要将输入处理与矩阵处理分开,然后每个帧根据当前位置和方向设置正确的模型视图矩阵(作为一组转换)。
There's a lot to be done with this code. Long story short:
I believe that your code is a part of a rendering loop?
You'd need to:
move
with 3 variables, for movement in x, y, z directions,look
with 2 variables,yaw
for left-right look andpitch
for up-down look, google for "Euler angles" for more theory on this;After that, in each frame, you're supposed to:
yaw
,pitch
.To sum up, you need to separate the input handling from matrix handling, and then each frame set the correct model-view matrix (as a set of transformations) basing on current position and direction.