2016-04-22 59 views
0

我在場景視圖中生成了2D Hectogon,但是現在我對如何使這種形狀變爲三維感到困惑。用於計算這個數學或方法的任何幫助將不勝感激。我剛剛開始使用C#,我覺得考慮到OpenTk缺乏新的相關內容,因此大多數教程中使用的大多數調用現在已經過時,這是一個很高的要求。如何使用OpenTK C繪製3D Hectogon#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Drawing; 
using System.IO; 

using OpenTK; 
using OpenTK.Input; 
using OpenTK.Graphics.OpenGL; 
using OpenTK.Graphics; 

namespace SimpleGame 
{ 
    class Game : GameWindow 
    { 

    public Game() : base(1280, 720, new GraphicsMode(32, 24, 0, 4)) // screen resilotion 
    { 

    } 
    int pgmID; 
    int vsID; 
    int fsID; 
    int attribute_vcol; 
    int attribute_vpos; 
    int uniform_mview; 
    int vbo_position; 
    int vbo_color; 
    int vbo_mview; 
    int ibo_elements; 
    Vector3[] vertdata; 
    Vector3[] coldata; 
    Matrix4[] mviewdata; 
    int[] indicedata; 
    float time = 0.0f; 

    void initProgram() 
    { 

     pgmID = GL.CreateProgram(); 

     loadShader("F:/Year 1/Semester 2/Simulation In Games/SimpleGame/SimpleGame/vs.glsl", ShaderType.VertexShader, pgmID, out vsID); 
     loadShader("F:/Year 1/Semester 2/Simulation In Games/SimpleGame/SimpleGame/fs.glsl", ShaderType.FragmentShader, pgmID, out fsID); 


     GL.LinkProgram(pgmID); 
     Console.WriteLine(GL.GetProgramInfoLog(pgmID)); 

     attribute_vpos = GL.GetAttribLocation(pgmID, "vPosition"); 
     attribute_vcol = GL.GetAttribLocation(pgmID, "vColor"); 
     uniform_mview = GL.GetUniformLocation(pgmID, "modelview"); 

     GL.GenBuffers(1, out vbo_position); 
     GL.GenBuffers(1, out vbo_color); 
     GL.GenBuffers(1, out vbo_mview); 

     GL.GenBuffers(1, out ibo_elements); 
    } 

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

    void loadShader(String filename, ShaderType type, int program, out int address) 
    { 
     address = GL.CreateShader(type); 
     using (StreamReader sr = new StreamReader(filename)) 
     { 
      GL.ShaderSource(address, sr.ReadToEnd()); 
     } 
     GL.CompileShader(address); 
     GL.AttachShader(program, address); 
     Console.WriteLine(GL.GetShaderInfoLog(address)); 
    } 

    protected override void OnRenderFrame(FrameEventArgs e) 
    { 
     base.OnRenderFrame(e); 
     GL.Viewport(0, 0, Width, Height); 
     GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 
     GL.Enable(EnableCap.DepthTest); 

     GL.EnableVertexAttribArray(attribute_vpos); 
     GL.EnableVertexAttribArray(attribute_vcol); 

     GL.DrawElements(BeginMode.Triangles, indicedata.Length, DrawElementsType.UnsignedInt, 0); 

     GL.DisableVertexAttribArray(attribute_vpos); 
     GL.DisableVertexAttribArray(attribute_vcol); 

     GL.Flush(); 
     SwapBuffers(); 
    } 

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

     GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position); 
     GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(vertdata.Length * Vector3.SizeInBytes), vertdata, BufferUsageHint.StaticDraw); 
     GL.VertexAttribPointer(attribute_vpos, 3, VertexAttribPointerType.Float, false, 0, 0); 

     GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_color); 
     GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(coldata.Length * Vector3.SizeInBytes), coldata, BufferUsageHint.StaticDraw); 
     GL.VertexAttribPointer(attribute_vcol, 3, VertexAttribPointerType.Float, true, 0, 0); 

     time += (float)e.Time; 

     mviewdata[0] = Matrix4.CreateRotationY(0.2f time) Matrix4.CreateRotationX(0.0f time) Matrix4.CreateTranslation(0.0f, -1.0f, -4.0f) * 
     Matrix4.CreatePerspectiveFieldOfView(1.3f, ClientSize.Width/(float)ClientSize.Height, 1.0f, 40.0f); // rotation 

     GL.UniformMatrix4(uniform_mview, false, ref mviewdata[0]); 

     GL.UseProgram(pgmID); 

     GL.BindBuffer(BufferTarget.ArrayBuffer, 0); 


     GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements); 
     GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw); 
    } 
} 

}

+0

Hecto-是100,七溴是7 –

+0

http://stackoverflow.com/questions/8863790/opengl-object-擠壓 – genpfault

+1

你是什麼意思,使二維形狀三維?你正在使用3D矢量,所以你的多邊形可能是平的,但它已經是三維的了。你想把它轉換成棱鏡嗎? –

回答

0

我不認爲有一個內置的方法來創建棱鏡。基於七邊形(7邊多邊形)的棱鏡由兩個七邊形(一個在底部,一個在頂部)加上7個垂直的4邊​​多邊形組成。這樣的算法從水平多邊形擠出棱鏡將是(在僞代碼)

create_prism(bottom : polygon, height : float) : body 
    var top : polygon 
    top = bottom.Clone() 
    for all vertices v of top 
     v.z = v.z + height 
    end 

    var b = new body 
    b.Add(bottom) 
    b.Add(top) 
    for i : integer = 0 to bottom.Count - 1 
     var j : integer 
     j = (i + 1) modulo bottom.Count 
     var side = new polygon[4] 
     side[0] = bottom[i] 
     side[1] = bottom[j] 
     side[2] = top[j] 
     side[3] = top[i] 
     b.Add(side) 
    end 
    return b 
end