Beispiel #1
0
        public void Run()
        {
            //  Init
            // -------------------------------------------
            Gl.Initialize();
            Glfw.Init();

            Glfw.WindowHint(Hint.ClientApi, ClientApi.OpenGL);
            Glfw.WindowHint(Hint.ContextVersionMajor, 4);
            Glfw.WindowHint(Hint.ContextVersionMinor, 6);
            Glfw.WindowHint(Hint.OpenglProfile, Profile.Core);
            Glfw.WindowHint(Hint.Doublebuffer, true);
            Glfw.WindowHint(Hint.Decorated, true);
            Glfw.WindowHint(Hint.OpenglForwardCompatible, true);


            var window = Glfw.CreateWindow(800, 600,
                                           "Learn OpenGL Book", Monitor.None, Window.None);

            if (window == Window.None)
            {
                Console.Error.WriteLine("Failed to create GLFW Window");
                Glfw.Terminate();
                return;
            }
            Glfw.MakeContextCurrent(window);
            Glfw.SwapInterval(1);

            Gl.Viewport(0, 0, 800, 600);
            Glfw.SetFramebufferSizeCallback(window, OnViewportResize);
            // -------------------------------------------


            // Build and Compile our shader program
            // ----------------------------------
            //  Vertex shader
            var vertexShader = Gl.CreateShader(ShaderType.VertexShader);

            Gl.ShaderSource(vertexShader, vertexShaderSource);
            Gl.CompileShader(vertexShader);
            //  Fragment Shader
            var fragmentShader = Gl.CreateShader(ShaderType.FragmentShader);

            Gl.ShaderSource(fragmentShader, fragmentShaderSource);
            Gl.CompileShader(fragmentShader);
            //  Link shaders
            var shaderProgram = Gl.CreateProgram();

            Gl.AttachShader(shaderProgram, vertexShader);
            Gl.AttachShader(shaderProgram, fragmentShader);
            Gl.LinkProgram(shaderProgram);
            //  Delete not useful shaders
            Gl.DeleteShader(vertexShader);
            Gl.DeleteShader(fragmentShader);


            // Set up vertex data (and buffer(s)) and configure vertex attributes
            var vao = Gl.GenVertexArray();
            var vbo = Gl.GenBuffer();
            var ebo = Gl.GenBuffer();

            // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
            Gl.BindVertexArray(vao);
            //
            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(4 * vertices.Length), vertices, BufferUsage.StaticDraw);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)(4 * indices.Length), indices, BufferUsage.StaticDraw);
            //
            Gl.VertexAttribPointer(0, 3, VertexAttribType.Float, false, 3 * sizeof(float), IntPtr.Zero);
            Gl.EnableVertexAttribArray(0);
            // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
            // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
            // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
            Gl.BindVertexArray(0);

            while (!Glfw.WindowShouldClose(window))
            {
                // Input
                ProcessInput(window);

                // Rendering
                Gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
                Gl.Clear(ClearBufferMask.ColorBufferBit);

                // Draw rectangle
                Gl.UseProgram(shaderProgram);
                Gl.BindVertexArray(vao);
                Gl.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, IntPtr.Zero);

                // Check and call events and Swap Buffers
                Glfw.SwapBuffers(window);
                Glfw.PollEvents();
            }

            Gl.DeleteVertexArrays(vao);
            Gl.DeleteBuffers(vbo);
            Gl.DeleteBuffers(ebo);
            Gl.DeleteProgram(shaderProgram);

            Glfw.Terminate();
        }
 public void Bind()
 {
     Gl.BindVertexArray(_id);
 }
 public void Unbind()
 {
     Gl.BindVertexArray(0);
 }
Beispiel #4
0
 public void DrawRaw()
 {
     Gl.BindVertexArray(vao);
     Gl.DrawElements(PrimitiveType.Triangles, Indices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);
     Gl.BindVertexArray(0);
 }
Beispiel #5
0
 public override void Unbind()
 {
     Gl.BindVertexArray(0);
 }
Beispiel #6
0
 public override void Bind()
 {
     Gl.BindVertexArray(ID);
 }
Beispiel #7
0
 public void Bind()
 {
     Gl.BindVertexArray(m_RendererID);
 }
 private void unbindVao()
 {
     Gl.BindVertexArray(0);
 }