如何在Opentk中设置正确的透视视图?

发布于 2025-02-08 00:52:04 字数 2087 浏览 2 评论 0原文

我做了一个全屏幕大小的正方形,以在窗户上显示。

但是可悲的是,我一直困扰着改变视点(相机还是透视图?),以使正方形在窗户的中心看起来很小。

正如许多人在网络上建议的那样,我遵循了建立矩阵和视角视野的GUID,这是行不通的……

我想知道我在代码上缺少什么。

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();
}

I made a full-screen size square to show on window.

But sadly, I am stuck at changing viewpoint (camera or perspective?) to make square look small at the center of window.

As many people suggested on web, I followed guid of setting up Matrix and perspective field of view which does not work...

I am wondering what I am missing on my code.

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

带刺的爱情 2025-02-15 00:52:04

您在投影矩阵之后加载身份矩阵。这覆盖了投影矩阵。执行以下操作:

// 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“清除”矩阵。它加载“身份”>“身份矩阵”。

You load the identity matrix after the projection matrix. This overrides the projection matrix. Do the following:

// 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);

Note that GL.MatrixMode selects the current matrix. All pending matrix operations affect the selected matrix. GL.LoadIdentity "clears" the matrix. It loads the Identity matrix.

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