OpenTK 中的第一人称相机问题

First Person Camera issue in OpenTK

我一直在使用 OpenTK 编写代码,到目前为止,我编写了一个脚本,其中草纹理的立方体随光照旋转。我已经让立方体可以使用 W A D S 移动,除了模拟第一人称视角,我让立方体朝相反的方向移动。我想创建一个 lookAt 函数,但它不起作用。我搜索了教程,但没有一个有效。现在我卡住了,有人可以帮帮我吗?

void Start()
    {
        window.Load += loaded;
        window.Resize += resize;
        window.RenderFrame += renderF;
        window.KeyPress += keyPress;
        window.Run(1.0/60.0);
    }

    void resize(object ob,EventArgs e)
    {
        GL.Viewport(0,0,window.Width,window.Height);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        Matrix4 matrix = Matrix4.Perspective(45.0f,window.Width/window.Height,1.0f,1000.0f);

        Matrix4 view = Matrix4.LookAt(new Vector3(0.0f, 0.0f, 
        0.0f),newVector3(0.0f,0.0f,0.0f),newVector3(0.0f, 0.0f, 0.0f));
        Vector3 Position = new Vector3(0.0f, 0.0f, 3.0f);
        GL.LoadMatrix(ref matrix);
        //GL.LoadMatrix(ref view);
        GL.MatrixMode(MatrixMode.Modelview);
    }


    void renderF(object o , EventArgs e)
    {
        GL.LoadIdentity();
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        //CUBE 1:
        GL.PushMatrix();

        GL.Translate(x,y,z);
        GL.Rotate(theta,1.0,0.0,0.0);
        GL.Rotate(theta,1.0,0.0,1.0);
        GL.Scale(0.7,0.7,0.7);

        draw_cube();

        GL.PopMatrix();

        window.SwapBuffers();

        theta += count;
    }   

Legacy OpenGL there are different current matrices. See MatrixMode.
投影矩阵必须设置为当前 MatrixMode.Projection 矩阵,模型矩阵必须设置为当前 MatrixMode.Modelview。当前矩阵模式必须在操作矩阵的操作之前设置。

Matrix4.LookAt 的第一个参数定义观察者的位置,第二个参数定义目标。从观察位置到目标的向量定义了视线。第三个参数是向上向量:

void resize(object ob,EventArgs e)
{
    GL.Viewport(0,0,window.Width,window.Height);

    Matrix4 matrix = Matrix4.Perspective(45.0f,window.Width/window.Height,1.0f,1000.0f);
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadMatrix(ref matrix);
}

void renderF(object o , EventArgs e)
{
    Matrix4 view = Matrix4.LookAt(
        new Vector3(0.0f, -3.0f, 0.0f),
        new Vector3(0.0f,0.0f,0.0f),
        new Vector3(0.0f, 0.0f, 1.0f));

    GL.MatrixMode(MatrixMode.Modelview);
    GL.LoadMatrix(ref view);

    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    // [...]
}

无论如何我建议不要使用已弃用的 Legacy OpenGL. Read a good OpenGL tutorial like LearnOpenGL and use Shader. Some OpenTK examples can be found at GitHub - Rabbid76/c_sharp_opengl