简体   繁体   中英

OpenGL: How to draw a cube in OpenTK?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM