opengl 如何在OpenTK中建立正确的透视图?

vptzau2j  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(152)

我做了一个全屏大小的方块显示在窗口上。
但可悲的是,我被困在改变视点(相机或透视?),使广场看起来很小,在窗口的中心。
正如许多人在网上建议的那样,我遵循了设置矩阵和透视视野的指南,但这并不起作用。
我想知道我的代码中缺少了什么。

private void ImageControl_OnRender(TimeSpan delta)
{
    //Create perspective camera matrix
    //ImageControl is the name of window
    GL.Viewport(0, 0, (int)ImageControl.Width, (int)ImageControl.Height);
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();

    Matrix4 perspectiveMatrix;
    Matrix4.CreatePerspectiveFieldOfView(45.0f * (float)Math.PI / 180, (float)(ImageControl.Width / ImageControl.Height), 0.1f, 100.0f, out perspectiveMatrix);

    //Set perspective camera
    //GL.MatrixMode(MatrixMode.Projection);
    //GL.LoadIdentity();

    GL.LoadMatrix(ref perspectiveMatrix);
    GL.LoadIdentity();

    GL.MatrixMode(MatrixMode.Modelview);

    //GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();

    //Now starting to draw objects

    //Set the background colour
    GL.ClearColor(Color4.SkyBlue);

    //Clear the colour and depth buffer for next matrix.
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

    //Set the scale of object first hand
    //GL.Scale(0.5f, 0.5f, 0.5f);

    //GL.Translate() <<< Set the translation of object first hand
    GL.Translate(0.0f, 0.0f, -2.0f);
    //Set the colour of object first hand
    GL.Color3(0.3f, 0.2f, 0.5f);

    //Tells that we are going to draw a sqare consisting of vertices. Can be Triangle too!
    GL.Begin(PrimitiveType.Quads);

    GL.Vertex3(-1.0f, -1.0f, 0.0f);

    GL.Vertex3(1.0f, -1.0f, 0.0f);

    GL.Vertex3(1.0f, 1.0f, 0.0f);

    GL.Vertex3(-1.0f, 1.0f, 0.0f);

    //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
    //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

    //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);

    GL.End();
    GL.Finish();
}
vxbzzdmp

vxbzzdmp1#

在投影矩阵之后加载单位矩阵。这将覆盖投影矩阵。请执行以下操作:

// 1. Select projection matrix mode
GL.MatrixMode(MatrixMode.Projection); // <--- INSERT

// 2. Load projection matrix
GL.LoadMatrix(ref perspectiveMatrix);
// GL.LoadIdentity();                    <--- DELETE

// 3. Select model view matrix mode  
GL.MatrixMode(MatrixMode.Modelview);

// 4. Clear model view matrix (load the identity matrix) 
GL.LoadIdentity();

// 5. Multiply model view matrix with the translation matrix
GL.Translate(0.0f, 0.0f, -2.0f);

请注意,GL.MatrixMode会选取目前的矩阵。所有搁置的矩阵作业都会影响选取的矩阵。GL.LoadIdentity会“清除”矩阵。它会载入Identity matrix

相关问题