C# OpenTK GLControl 绘图立方体不正确

C# OpenTK GLControl drawing cube incorrect

我在 C# WinForm (.NET Framework) 应用程序中使用 OpenTK 的 GLControl, 但我不知道为什么我的 20mm3 立方体会单独缩放。

我用 DCGLControl 继承 GLControl 并在控件调整大小时添加 GL.Viewport(ClientRectangle) :

private void DCGLControl_Resize(object sender, EventArgs e)
{
    GL.Viewport(ClientRectangle);
    Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 1000f);
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadMatrix(ref projection);
}

这是我的 DCGLControl_Load 事件(初始化一些东西并启动一个新线程来渲染它):

private void DCGLControl_Load(object sender, EventArgs e)
{
    GL.Enable(EnableCap.DepthTest);
    GL.ClearColor(ClearColor);
    Resize += DCGLControl_Resize;

    Matrix4 trans;
    trans = Matrix4.CreateScale(0.01f, 0.01f, 0.01f);

    Context.MakeCurrent(null);
    renderThread = new Thread(() =>
    {
        Context.MakeCurrent(WindowInfo);
        while (true)
        {
            int n;
            for (n = 0; n < Models.Count; n++)
                if (Models[n].VAO == 0)
                {
                    Models[n].Shader.Compile();
                    GenVAO(Models[n]);
                }
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Viewport(ClientRectangle);


            for (n = 0; n < Models.Count; n++)
            {
                GL.BindVertexArray(Models[n].VAO);

                Models[n].Shader.Use();
                Models[n].Shader.SetMatrix4("transform", trans);

                if (Models[n].UseEBO)
                    GL.DrawElements(Models[n].PrimitiveType, Models[n].DrawOrder.Length, DrawElementsType.UnsignedInt, 0);
                else
                    GL.DrawArrays(Models[n].PrimitiveType, 0, Models[n].Vertices.Length);
            }
            SwapBuffers();
            Thread.Sleep(1000/60);
        }
    });

    renderThread.Start();
}

这是我的顶点着色器:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;

uniform mat4 transform;

void main()
{
    gl_Position =  transform * vec4(position, 1.0);
}

我得到这样的结果: error screenshot

我是 OpenGL 和 OpenTK 的初学者,感谢您的帮助:)

不能在 3.30 核心着色器中混合使用固定函数矩阵和矩阵变换。这意味着指令 GL.MatrixMode(MatrixMode.Projection); 无效(核心配置文件上下文中的 OpenGL 错误除外)。您必须向着色器添加投影矩阵:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;

uniform mat4 projection;
uniform mat4 transform;

void main()
{
    gl_Position = projection * transform * vec4(position, 1.0);
}

为投影矩阵添加一个属性:

private Matrix4 projection;

DCGLControl_Resize回调中设置矩阵:

private void DCGLControl_Resize(object sender, EventArgs e)
{
    GL.Viewport(ClientRectangle);
    self.projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 1000f);
}

在绘制几何之前设置统一矩阵:

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Viewport(ClientRectangle);

for (n = 0; n < Models.Count; n++)
{
    GL.BindVertexArray(Models[n].VAO);

    Models[n].Shader.Use();
    Models[n].Shader.SetMatrix4("projection", self.projection);
    Models[n].Shader.SetMatrix4("transform", trans);

    if (Models[n].UseEBO)
        GL.DrawElements(Models[n].PrimitiveType, Models[n].DrawOrder.Length, DrawElementsType.UnsignedInt, 0);
    else
        GL.DrawArrays(Models[n].PrimitiveType, 0, Models[n].Vertices.Length);
}
SwapBuffers();