OpenGL:不确定为什么此代码在窗口的左下角呈现绿色方块

发布于 2024-10-30 04:04:07 字数 1097 浏览 0 评论 0原文

protected override void OnRenderFrame(FrameEventArgs e)
{
        base.OnRenderFrame(e);

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

        Matrix4 modelview = Matrix4.LookAt(0, 0, 1, 
                                           0, 0, 0, 
                                           0, 1, 0);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref modelview);

        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        GL.Ortho(0, 320, 0, 480, 1, 2);

        GL.Begin(BeginMode.Quads);

        GL.Color3(0f, .8f, 0f);

        GL.Vertex2(0, 10);
        GL.Vertex2(10, 10);
        GL.Vertex2(10, 0);
        GL.Vertex2(0, 0);

        GL.End();

        SwapBuffers();
    }

我对 LootAt 函数调用的理解是,之后我的相机将在原点上方浮动一个单位,直视原点。因此,当我第一次渲染绿色方块时,我希望它位于窗口的中心附近,因为那是原点所在的位置(其中一个顶点是 (0,0))。但事实并非如此,它位于左下角窗口中。于是我想,也许由于调用了 LoadIdentity 和 Orthoro,所有顶点都将按照窗口左下角的偏移量进行渲染,并且我的眼睛和相机的概念将不再适用。然而,如果我将眼睛的起始坐标放在 (2,2) 处,绿色方块就会平移。所以这让我认为眼睛/相机的概念仍然适用。

所以现在我很困惑......

你能给我解释一下发生了什么事吗?

谢谢

protected override void OnRenderFrame(FrameEventArgs e)
{
        base.OnRenderFrame(e);

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

        Matrix4 modelview = Matrix4.LookAt(0, 0, 1, 
                                           0, 0, 0, 
                                           0, 1, 0);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref modelview);

        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        GL.Ortho(0, 320, 0, 480, 1, 2);

        GL.Begin(BeginMode.Quads);

        GL.Color3(0f, .8f, 0f);

        GL.Vertex2(0, 10);
        GL.Vertex2(10, 10);
        GL.Vertex2(10, 0);
        GL.Vertex2(0, 0);

        GL.End();

        SwapBuffers();
    }

My understanding of the LootAt function's call is that afterwards my camera would be floating one unit above the origin looking straight down at the origin. So when I first rendered my green square I expected it to be near the center of my window since that is where the origin is (one of its vertexes is (0,0)). But it wasn't, it was in the lower left corner window. So then I thought maybe that because of the calls to LoadIdentity and Orthoro all vertices would be rendered in terms of an offset to the lower left hand corner of my window and that the concepts of my eye and the camera would no longer apply. However, if I place my eye's starting coordinates at say (2,2) the green square appears to translate. So that makes me think the eye/camera concept still applies.

So now I'm just confused...

Can you explain to me what is going on?

Thanks

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

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

发布评论

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

评论(2

你穿错了嫁妆 2024-11-06 04:04:07

如果将投影矩阵设置为正交矩阵(GL.Ortho),它将把所有内容渲染到参数指定的屏幕空间:

(left、right、bottom、top、nearVal、farVal);

您的屏幕可能比这个区域大,所以这将是屏幕的左下角,它将相机可以看到的内容渲染到这个正方形。我猜测相机只能看到那个绿色方块,当您平移该方块时,它会超出相机视图,因此不会完全绘制在该区域内。

如果您想绘制到屏幕中间,可以通过更改参数来移动视口。

If you set the Projection matrix to an orthographic one(GL.Ortho) it will render everything to the screen space dictated by the parameters:

(left, right, bottom, top, nearVal, farVal);

Your screen is probably larger than this area so this will be the bottom left of the screen, it renders what the camera can see to this square. I'm geussing the camera can only see that green square and that when you translate the square it goes outside the camera view so isn't drawn fully inside this area.

If you wanted to draw to the middle of the screen you could move the viewport by changing the parameters.

岁月如刀 2024-11-06 04:04:07

您已经创建了一个偏心正交投影:

   GL.Ortho(left, right, bottom, top, near, far);

   GL.Ortho(0, 320, 0, 480, 1, 2);

简而言之,您的窗口原点 (0,0) 位于屏幕的左下角。如果您想将其移至中心,请使用如下内容:

   GL.Ortho(-160, 160, -240, 240, 1, 2);

You have created an off-center orthographic projection:

   GL.Ortho(left, right, bottom, top, near, far);

   GL.Ortho(0, 320, 0, 480, 1, 2);

In short, your window origin (0,0) lies at the bottom left part of the screen. If you wish to move it to the center, use something like this:

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