如何使用OpenTK C#

时间:2016-04-22 20:54:26

标签: c# visual-studio opengl opentk

我在场景视图中生成了一个2D Hectogon,但我现在对如何使形状三维感到困惑。任何用于计算此数学或方法的帮助都将非常感激。我刚刚开始使用C#而且我认为这是一个很高的顺序,因为在大多数教程中使用的大多数调用现在已经过时,考虑到OpenTk缺少新的相关内容。

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

}

1 个答案:

答案 0 :(得分: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