如何使用Open TK C#

时间:2016-04-24 14:53:30

标签: c# opengl

我目前停留在一些工作上,我无法弄清楚如何计算出他们需要去的面和顶点或面。如果有人能够向我解释代码背后的数学,以及我如何能够将六边形组合成一个非常棒的3D。

谢谢

    protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    initProgram();

    vertdata = new Vector3[] { 

        //new Vector3(0.0f,0.0f,0.0f), // center
        //new Vector3(2.0f, 0f,0f), // right hand side
        //new Vector3(0f,2f,0f), // up

        new Vector3(0.0f,0.0f,-0.8f), // center point
        new Vector3(2.0f,0.0f,-0.8f), // right hand side
        new Vector3(1.0f,1.7f,-0.8f), // right hand top 
        new Vector3(-1.0f,1.7f,-0.8f), // right hand top 
        new Vector3(-2.0f,0.0f,-0.8f), // left hand top
        new Vector3(-1.0f,-1.7f,-0.8f),
        new Vector3(1.0f,-1.7f,-0.8f), // right hand top 
    };

    indicedata = new int[]{
        //front
        0, 1, 2,
        0, 2, 3,
        //back
        0, 3, 4,
        0, 4, 5,
        //left
        0, 5, 6,
        0, 6, 1,
    };

    coldata = new Vector3[] { new Vector3(1f, 0f, 0f),
        new Vector3( 0f, 0f, 1f),
        new Vector3( 0f,  1f, 0f),new Vector3(1f, 0f, 0f),
        new Vector3( 0f, 0f, 1f),
        new Vector3( 0f,  1f, 0f),new Vector3(1f, 0f, 0f),
        new Vector3( 0f, 0f, 1f)};

    mviewdata = new Matrix4[]{
        Matrix4.Identity
    };

    Title = "Hello OpenTK!";
    GL.ClearColor(Color.DarkTurquoise);
    GL.PointSize(5f);
}

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的方法来更好地控制六边形:

points[0] = vec3(0.0f,0.0f,0.0f)
for(int i = 1; i < 7; ++i) {
    points[i] = vec3(sin(i/6.0*HEX_SIZE*M_PI),
                    cos(i/6.0*HEX_SIZE*M_PI));
}

基本上你一次移动60度并在正确的位置创建一个点。如果你要创建单个三角形,你的索引看起来是正确的但是你可以创建一个三角扇,(有点过时),使用:

private short[] indicedata = { 0, 1, 2, 3, 4, 5, 6, 1 };
...
glDrawElements(GL_TRIANGLE_FAN, indicedata.length, GL_UNSIGNED_SHORT, indexBuffer);