OpenTK BufferObject没有任何内容

时间:2015-05-26 00:33:51

标签: c# graphics 2d opentk

我一直在通过OpenTK库学习OpenGL,希望学习如何制作游戏。到目前为止,我已将这个代码拼凑在一起,该代码应该使用VBO在屏幕上绘制一个三角形。然而,在让它编译并运行没有错误后,它没有任何可见,当我正在阅读的指南说它现在应该绘制一个三角形。它还会发出警告,指出" GL.DrawArray(Beginmode mode,int,int)已过时,请使用PrimitiveType重载",但更改行以使用PrimitiveType.Triangles会产生相同的结果。我做错了什么?

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

namespace ConsoleApplication1
{
class Game : GameWindow
{
    int pgmID;
    int vsID;
    int fsID;
    int attribute_vcol;
    int attribute_vpos;
    int uniform_mview;
    int vbo_position;
    int vbo_color;
    int vbo_mview;
    Vector3[] vertdata;
    Vector3[] coldata;
    Matrix4[] mviewdata;
    static void Main(string[] args)
    {
        using(Game game = new Game())
        game.Run(30, 30);
    }


    void initProgram()
    {
        pgmID = GL.CreateProgram();
        loadShader("vs.glsl", ShaderType.VertexShader, pgmID, out vsID);
        loadShader("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.GetAttribLocation(pgmID, "modelview");
        GL.GenBuffers(1, out vbo_color);
        GL.GenBuffers(1, out vbo_position);
        GL.GenBuffers(1, out vbo_mview);
    }
    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 OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        initProgram();

        vertdata = new Vector3[] { new Vector3(-0.8f, -0.8f, 0f),
            new Vector3( 0.8f, -0.8f, 0f),
            new Vector3( 0f,  0.8f, 0f)};


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


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



        Title = "Hello OpenTK!";
        GL.ClearColor(Color.CornflowerBlue);
        GL.PointSize(5f);
    }
    protected override void OnUpdateFrame(FrameEventArgs 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);

        GL.UniformMatrix4(uniform_mview, false, ref mviewdata[0]);
        GL.UseProgram(pgmID);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
    }
    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.DrawArrays(PrimitiveType.Triangles,0,3);


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


        GL.Flush();
        SwapBuffers();
    }
}

}

0 个答案:

没有答案