Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ColoredTriangleVaoOpenTK4OpenGL31 (Added shaders to Program.cs)
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
using OpenToolkit.Graphics.OpenGL; using OpenToolkit.Windowing.Desktop; using OpenToolkit.Mathematics; using System.IO; using System; namespace ColoredTriangleVaoOpenTK4OpenGL31 { class Program { static void Main(string[] args) { GameWindowSettings gameWindowSettings = new GameWindowSettings(); NativeWindowSettings nativeWindow = new NativeWindowSettings(); nativeWindow.Size = new Vector2i(270, 270); nativeWindow.APIVersion = System.Version.Parse("3.1"); nativeWindow.Profile = OpenToolkit.Windowing.Common.ContextProfile.Any; using (var window = new Window(gameWindowSettings, nativeWindow)) { window.Run(); } } } class Window : GameWindow { private int _program; private int _vao; public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindow) : base(gameWindowSettings, nativeWindow) { } protected override void OnLoad() { Title = "Colored Triangle"; GL.ClearColor(0f, 0.1f, 0.2f, 1f); _program = LoadShaders(); float[] positions = new float[] { 0f, 1f, 0f, -1f, 0f, 0f, 1f, 0f, 0f }; float[] colors = new float[] { 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f }; // int vao = GL.GenVertexArray(); GL.GenVertexArrays(1, out _vao); GL.BindVertexArray(_vao); int vboPos; GL.GenBuffers(1, out vboPos); GL.BindBuffer(BufferTarget.ArrayBuffer, vboPos); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * positions.Length, positions, BufferUsageHint.StaticDraw); int vboColor; GL.GenBuffers(1, out vboColor); GL.BindBuffer(BufferTarget.ArrayBuffer, vboColor); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * colors.Length, colors, BufferUsageHint.StaticDraw); int aPositionLoc = GL.GetAttribLocation(_program, "aPosition"); int aColorLoc = GL.GetAttribLocation(_program, "aColor"); GL.VertexAttribPointer(aPositionLoc, 3, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(aPositionLoc); GL.VertexAttribPointer(aColorLoc, 3, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(aColorLoc); GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } protected override void OnRenderFrame(OpenToolkit.Windowing.Common.FrameEventArgs args) { GL.Clear(ClearBufferMask.ColorBufferBit); GL.BindVertexArray(_vao); GL.DrawArrays(OpenToolkit.Graphics.OpenGL.PrimitiveType.Triangles, 0, 3); SwapBuffers(); } private static int LoadShaders() { int vShader = GL.CreateShader(ShaderType.VertexShader); int fShader = GL.CreateShader(ShaderType.FragmentShader); #region Vertex Shader Loading // string source = File.ReadAllText("Shaders/vShader.glsl"); string vSource = string.Join( Environment.NewLine, "#version 140", "in vec3 aPosition;", "in vec3 aColor;", "out vec3 vColor;", "void main()", "{", " gl_Position = vec4(aPosition, 1.0);", " vColor = aColor;", "}"); GL.ShaderSource(vShader, vSource); GL.CompileShader(vShader); int status; GL.GetShader(vShader, ShaderParameter.CompileStatus, out status); if (status == 0) { string info = GL.GetShaderInfoLog(vShader); Console.WriteLine("Vertex Shader Error: " + info); // return -1; } #endregion #region Fragment Shader Loading // source = File.ReadAllText("Shaders/fShader.glsl"); string fSource = string.Join( Environment.NewLine, "#version 140", "in vec3 vColor;", "out vec4 fragColor;", "void main()", "{", " fragColor = vec4(vColor, 1.0);", "}"); GL.ShaderSource(fShader, fSource); GL.CompileShader(fShader); GL.GetShader(fShader, ShaderParameter.CompileStatus, out status); if (status == 0) { string info = GL.GetShaderInfoLog(fShader); Console.WriteLine("Fragment Shader Error: " + info); // return -1; } #endregion #region Creating Shader Program int program = GL.CreateProgram(); GL.AttachShader(program, vShader); GL.AttachShader(program, fShader); GL.LinkProgram(program); GL.GetProgram(program, GetProgramParameterName.LinkStatus, out status); if (status == 0) { string info = GL.GetProgramInfoLog(program); Console.WriteLine("Program Error: " + info); // return -1; } #endregion GL.UseProgram(program); // GL.DeleteShader(vShader); // GL.DeleteShader(fShader); return program; } } }
Show compiler warnings
[
+
]
Show input
edit mode
|
history
|
discussion