OpenGL:如何在 OpenTK 中绘制立方体?

发布于 2024-10-24 17:36:58 字数 58 浏览 3 评论 0原文

glutWireCube()是C++中绘制立方体的函数,OpenTK中怎么样?使用任何函数来替换它吗?

glutWireCube() is the function for drawing a cube in C++, How about in OpenTK? Any function was used for replace that?

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

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

发布评论

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

评论(2

思念满溢 2024-10-31 17:36:58

该函数使用旧的固定函数管道绘制实体立方体,根据您的需要对其进行修改。
为了简洁起见,我将变量声明写在函数内,但如果您想要更高的性能,则应该将它们移出函数。我还建议重写该函数以使用 GL.DrawElements() 代替。

    private void DrawBox(float size)
    {
        float[,] n = new float[,]{
            {-1.0f, 0.0f, 0.0f},
            {0.0f, 1.0f, 0.0f},
            {1.0f, 0.0f, 0.0f},
            {0.0f, -1.0f, 0.0f},
            {0.0f, 0.0f, 1.0f},
            {0.0f, 0.0f, -1.0f}
        };
        int[,] faces = new int[,]{
            {0, 1, 2, 3},
            {3, 2, 6, 7},
            {7, 6, 5, 4},
            {4, 5, 1, 0},
            {5, 6, 2, 1},
            {7, 4, 0, 3}
        };
        float[,] v = new float[8,3];
        int i;

        v[0,0] = v[1,0] = v[2,0] = v[3,0] = -size / 2;
        v[4,0] = v[5,0] = v[6,0] = v[7,0] = size / 2;
        v[0,1] = v[1,1] = v[4,1] = v[5,1] = -size / 2;
        v[2,1] = v[3,1] = v[6,1] = v[7,1] = size / 2;
        v[0,2] = v[3,2] = v[4,2] = v[7,2] = -size / 2;
        v[1,2] = v[2,2] = v[5,2] = v[6,2] = size / 2;

        GL.Begin(BeginMode.Quads);
        for (i = 5; i >= 0; i--) {
            GL.Normal3(ref n[i, 0]);
            GL.Vertex3(ref v[faces[i, 0], 0]);
            GL.Vertex3(ref v[faces[i, 1], 0]);
            GL.Vertex3(ref v[faces[i, 2], 0]);
            GL.Vertex3(ref v[faces[i, 3], 0]);
        }
        GL.End();
    }

This function draws a solid cube using the old fixed function pipeline, modify it for your needs.
I've written the variable declarations inside the function for brevity but you should move them out of the function if you want more performance. I would also recommend rewriting the function to use GL.DrawElements() instead.

    private void DrawBox(float size)
    {
        float[,] n = new float[,]{
            {-1.0f, 0.0f, 0.0f},
            {0.0f, 1.0f, 0.0f},
            {1.0f, 0.0f, 0.0f},
            {0.0f, -1.0f, 0.0f},
            {0.0f, 0.0f, 1.0f},
            {0.0f, 0.0f, -1.0f}
        };
        int[,] faces = new int[,]{
            {0, 1, 2, 3},
            {3, 2, 6, 7},
            {7, 6, 5, 4},
            {4, 5, 1, 0},
            {5, 6, 2, 1},
            {7, 4, 0, 3}
        };
        float[,] v = new float[8,3];
        int i;

        v[0,0] = v[1,0] = v[2,0] = v[3,0] = -size / 2;
        v[4,0] = v[5,0] = v[6,0] = v[7,0] = size / 2;
        v[0,1] = v[1,1] = v[4,1] = v[5,1] = -size / 2;
        v[2,1] = v[3,1] = v[6,1] = v[7,1] = size / 2;
        v[0,2] = v[3,2] = v[4,2] = v[7,2] = -size / 2;
        v[1,2] = v[2,2] = v[5,2] = v[6,2] = size / 2;

        GL.Begin(BeginMode.Quads);
        for (i = 5; i >= 0; i--) {
            GL.Normal3(ref n[i, 0]);
            GL.Vertex3(ref v[faces[i, 0], 0]);
            GL.Vertex3(ref v[faces[i, 1], 0]);
            GL.Vertex3(ref v[faces[i, 2], 0]);
            GL.Vertex3(ref v[faces[i, 3], 0]);
        }
        GL.End();
    }
聽兲甴掵 2024-10-31 17:36:58

glutWireCube 不是 OpenGL 函数。它是 GLUT 库的一部分,经常被误认为是 OpenGL 的一部分。

glutWireCube is not an OpenGL function. It's part of GLUT a library oftenly mistaken for being part of OpenGL.

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