2016-04-22 5 views
0

Ich habe ein 2D Hectogon in meiner Szene erstellt, aber ich bin jetzt verwirrt, wie man die Form dreidimensional machen. Jede Hilfe in der Mathematik oder Methode, die verwendet wird, um dies zu berechnen, würde sehr geschätzt werden. Ich habe gerade erst mit C# angefangen und ich denke, dass dies eine große Herausforderung ist, wenn man bedenkt, dass OpenTk keine neuen relevanten Inhalte hat, da die meisten Aufrufe in den meisten Tutorials veraltet sind.Wie zeichne ich ein 3D Hectogon mit OpenTK C#

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 ist, Hepta- 7 –

+0

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

+1

Was meinen Sie, indem Sie eine 2D-Form dreidimensional machen? Sie arbeiten mit 3D-Vektoren, also ist Ihr Polygon möglicherweise flach, aber es ist bereits dreidimensional. Willst du es in ein Prisma umwandeln? –

Antwort

0

Ich glaube nicht, dass es ein in Verfahren aufgebaut ist Prismen für die Erstellung. Ein Prisma auf der Basis eines Siebenecks (7-seitiges Polygon) besteht aus zwei Heptagonen (eines auf der Unterseite, eines auf der Oberseite) plus 7 vertikalen 4-seitigen Polygonen. So ist der Algorithmus für ein Prisma aus einem horizontalen Polygon Extrudieren wäre (in Pseudocode)

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