2023年9月29日

Opentk Tutorial 3---Quad_VBO

 

Opentk チュートリアル 3---クアッド VBO

Opentk 튜토리얼 3---쿼드 VBO

Opentk 教學3---Quad VBO


using OpenTK.Graphics.OpenGL4;
 
 
public class Quad_VBO
{
     //(x,y, r,g,b,a)
    public float[] vertices = {
            -1,-1,  1, 0,0,1,
             1,-1,  1, 0,0,1,
             1, 1,  1, 0,0,1,
            -1, 1,  1, 0,0,1,
    };
    //gl_position range=(-1~1)
    public void set_xywh(float x,float y ,float w,float h)
    {
        float left = x ;
        float bottom = y ;
        float right=left+w;
        float top=bottom+h;
 
        set_vertex_coord(0, left, bottom);
        set_vertex_coord(1, right, bottom);
        set_vertex_coord(2, right, top);
        set_vertex_coord(3, left, top);
    }
 
 
    public void set_vertex_coord(int index, float x, float y)
    {
        int offset = index * 6;
        vertices[offset] = x;
        vertices[offset + 1] = y;
    }
    public void set_color(int index, float r, float g,float b,float a)
    {
        int offset = index * 6;
        vertices[offset + 2] = r;
        vertices[offset + 3] = g;
        vertices[offset + 4] = b;
        vertices[offset + 5] = a;
    }
    public void set_color_all(float r,float g, float b,float a)
    {
 
        set_color(0, r, g, b, a);
        set_color(1, r, g, b, a);
        set_color(2, r, g, b, a);
        set_color(3, r, g, b, a);
    }
 
    public int vbo;
    public  void Create_vbo()
    {
        vbo =GL.GenBuffer();
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
        GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
    }
 
    public  void Update_vbo()
    {
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);   
        GL.BufferSubData(BufferTarget.ArrayBuffer, 0, vertices.Length * sizeof(float),
                this.vertices);        
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
    }
    public void Delete_vbo()
    {
      GL.DeleteBuffer(vbo);
    }
}

沒有留言:

張貼留言