2016-04-24 38 views
1

我目前被困在一些工作上,我無法弄清楚如何制定出面和頂點或他們需要去的面。如果有人能夠向我解釋代碼背後的數學知識,以及如何將六邊形變成3D,那會很棒。如何使用Open TK C繪製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); 
} 

回答

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