Opentk チュートリアル 4---クワッドの描画
Opentk 튜토리얼 4---쿼드 그리기
Opentk 教學4--畫四邊形
Releate object:
How to use?
//Window_Load
Quad.init_shader(); quad = new Quad(); quad.set_color(0, 1, 0, 1f); quad.set_width_height(0.5f, 0.5f); quad.set_xy(0.1f, 0.5f);
//Render
GL.Viewport(0,0,this.Size.X,this.Size.Y); GL.Clear(ClearBufferMask.ColorBufferBit); Quad.use_shader(); quad.draw(); this.SwapBuffers();
//Release
Quad.release_shader(); quad.release();
using OpenTK.Graphics.OpenGL4; using OpenTK.Mathematics; public class Quad { private static Shader shader; public static void use_shader() { shader.Use(); } public static void init_shader() { String vert = "#version 330\n" + "layout (location = 0) in vec2 aPosition;\n"+ "layout (location = 1) in lowp vec4 aColor;\n" + "out lowp vec4 color;\n" + "void main()" + "{" + " color=aColor;" + " gl_Position = vec4(aPosition,0.0, 1.0);" + "}"; String frag = "#version 330\n" + "precision highp float;" + "in lowp vec4 color;\n" + "out lowp vec4 FragColor;\n" + "void main(){" + "FragColor =color;"+ "}"; shader = new Shader(vert, frag); } public static void release_shader() { shader.Delete(); shader = null; } private int vao; private Quad_VBO quad_vbo; public Quad() { if (shader == null) { throw new Exception("Please init shader."); } quad_vbo = new Quad_VBO(); quad_vbo.Create_vbo(); vao = GL.GenVertexArray(); GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, quad_vbo.vbo); GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 6 * sizeof(float), 8); GL.EnableVertexAttribArray(1); GL.BindVertexArray(0); } //gl_position range=(-1~1) public void set_xy(float x,float y) { float w = get_width(); float h= get_height(); quad_vbo.set_xywh(x*2-1,y*2-1,w,h); quad_vbo.Update_vbo(); } public void set_width_height(float w,float h) { float x = get_x(); float y = get_y(); quad_vbo.set_xywh(x, y, w*2, h*2); quad_vbo.Update_vbo(); } public float get_x() { return quad_vbo.vertices[0]; } public float get_y() { return quad_vbo.vertices[1]; } public float get_width() { return quad_vbo.vertices[6] - quad_vbo.vertices[0]; } public float get_height() { return quad_vbo.vertices[13] - quad_vbo.vertices[1]; } public Vector4 get_color() { return new Vector4(quad_vbo.vertices[2], quad_vbo.vertices[3], quad_vbo.vertices[4], quad_vbo.vertices[5]); } public void set_color(float r, float g, float b,float a) { quad_vbo.set_color_all(r, g, b, a); quad_vbo.Update_vbo(); } public void draw() { GL.BindVertexArray(vao); GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4); } public void release() { GL.DeleteVertexArray(vao); quad_vbo.Delete_vbo(); } }
沒有留言:
張貼留言