相对相机旋转?

发布于 2024-10-02 03:36:28 字数 1399 浏览 0 评论 0原文

我目前已设置此更新代码来旋转和移动我的相机。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-10-09 03:36:28

这段代码还有很多工作要做。长话短说:

我相信您的代码是渲染循环的一部分?

您需要:

  • move 替换为 3 个变量,用于 x、y、z 方向的移动,
  • look 替换为 2 个变量,yaw对于左右外观和上下外观的pitch,请谷歌搜索“欧拉角”以获取更多相关理论;
  • 将所有这些移到循环之外 - 它们需要在帧之间保留。

之后,在每一帧中,您应该:

  • 根据输入更新这些变量(增加或减少一个常数值乘以两帧之间的时间差),
  • 用单位矩阵替换模型视图矩阵(LoadIdentity() ),
  • 将模型视图矩阵平移乘以 (x,y,z),
  • 将 X 平面中的旋转乘以 yaw
  • 将 Y 平面中的旋转乘以 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:

  • replace move with 3 variables, for movement in x, y, z directions,
  • replace look with 2 variables, yaw for left-right look and pitch for up-down look, google for "Euler angles" for more theory on this;
  • move all those outside the loop - they need to persist between frames.

After that, in each frame, you're supposed to:

  • update those variables (increase or decrease by a constant value multiplied by delta time between 2 frames) according to the input,
  • replace the model-view matrix with identity matrix (LoadIdentity()),
  • multiply the model-view matrix by translation by (x,y,z),
  • multiply by rotation in X-plane by yaw,
  • multiply by rotation by Y-plane by 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文