Last active
September 24, 2015 13:05
-
-
Save RichardB01/e8bbeed07c57cc188b64 to your computer and use it in GitHub Desktop.
Halp plox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #version 330 | |
| out vec4 outColour; | |
| void main() | |
| { | |
| outColour = vec4(1.0, 0.0, 0.0, 1.0); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #version 330 | |
| uniform mat4 projection; | |
| uniform mat4 view; | |
| uniform mat4 model; | |
| in vec3 vertex_position; | |
| in vec3 vertex_colour; | |
| in vec2 vertex_texcoord; | |
| void main() | |
| { | |
| gl_Position = projection*view*model * vec4(vertex_position, 0.0); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using OpenTK; | |
| using OpenTK.Graphics; | |
| using OpenTK.Graphics.OpenGL; | |
| namespace OTKBoilerplate | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| using (MainWindow win = new MainWindow()) | |
| { | |
| win.Run(); | |
| } | |
| } | |
| } | |
| public class MainWindow : GameWindow | |
| { | |
| private int _vao; | |
| private int _vbo; | |
| private int _vertexShader; | |
| private int _fragShader; | |
| private int _program; | |
| protected override void OnLoad(EventArgs e) | |
| { | |
| Title = "OpenTK Boilerplate"; | |
| Width = 800; | |
| Height = 600; | |
| GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
| GL.Enable(EnableCap.DepthTest); | |
| GL.Enable(EnableCap.CullFace); | |
| // load shader source. | |
| string vertexSource; | |
| using (StreamReader reader = new StreamReader("default.vert")) | |
| { | |
| vertexSource = reader.ReadToEnd(); | |
| } | |
| string fragSource; | |
| using (StreamReader reader = new StreamReader("default.frag")) | |
| { | |
| fragSource = reader.ReadToEnd(); | |
| } | |
| // create and compile shaders. | |
| _vertexShader = GL.CreateShader(ShaderType.VertexShader); | |
| GL.ShaderSource(_vertexShader, vertexSource); | |
| GL.CompileShader(_vertexShader); | |
| _fragShader = GL.CreateShader(ShaderType.FragmentShader); | |
| GL.ShaderSource(_fragShader, fragSource); | |
| GL.CompileShader(_fragShader); | |
| // link shaders into a program. | |
| _program = GL.CreateProgram(); | |
| GL.AttachShader(_program, _vertexShader); | |
| GL.AttachShader(_program, _fragShader); | |
| GL.BindFragDataLocation(_program, 0, "outColour"); | |
| GL.LinkProgram(_program); | |
| GL.UseProgram(_program); | |
| // set up projection mvp. | |
| int uniModel = GL.GetUniformLocation(_program, "model"); | |
| Matrix4 model = Matrix4.CreateScale(10, 10, 10); | |
| GL.UniformMatrix4(uniModel, false, ref model); | |
| Matrix4 view = Matrix4.LookAt( | |
| 1f, 1f, 1f, | |
| 0f, 0f, 0f, | |
| 0f, 0f, 1f | |
| ); | |
| int uniView = GL.GetUniformLocation(_program, "view"); | |
| GL.UniformMatrix4(uniView, false, ref view); | |
| Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView( | |
| MathHelper.DegreesToRadians(45f), | |
| Width / (float)Height, | |
| 1.0f, | |
| 100.0f | |
| ); | |
| int uniProj = GL.GetUniformLocation(_program, "projection"); | |
| GL.UniformMatrix4(uniProj, false, ref proj); | |
| // create VBO and copy data to it. | |
| GL.GenBuffers(1, out _vbo); | |
| // raw data. | |
| float[] vertices = { | |
| -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, | |
| 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, | |
| 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, | |
| 0.5f, 0.5f, -0.5f, 1.0f, 1.0f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0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, | |
| -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, | |
| 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, | |
| 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, | |
| 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, | |
| -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, | |
| -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, | |
| -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, | |
| 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, | |
| 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, | |
| 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, | |
| -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, | |
| -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f | |
| }; | |
| GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo); | |
| GL.BufferData(BufferTarget.ArrayBuffer, | |
| (IntPtr)(vertices.Length * sizeof(float)), | |
| vertices, | |
| BufferUsageHint.StaticDraw); | |
| // check for data mismatch. | |
| int size; | |
| GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); | |
| if (vertices.Length * sizeof(float) != size) | |
| { | |
| throw new ApplicationException("Vertex data not uploaded correctly."); | |
| } | |
| GL.BindBuffer(BufferTarget.ArrayBuffer, 0); | |
| // create VAO | |
| GL.GenVertexArrays(1, out _vao); | |
| GL.BindVertexArray(_vao); | |
| // specify layout of data. | |
| int posAttrib = GL.GetAttribLocation(_program, "vertex_position"); | |
| GL.EnableVertexAttribArray(posAttrib); | |
| GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo); | |
| GL.VertexAttribPointer(posAttrib, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0); | |
| } | |
| protected override void OnUpdateFrame(FrameEventArgs e) | |
| { | |
| base.OnUpdateFrame(e); | |
| } | |
| protected override void OnRenderFrame(FrameEventArgs e) | |
| { | |
| GL.Viewport(0, 0, Width, Height); | |
| GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); | |
| GL.BindVertexArray(_vao); | |
| GL.DrawArrays(PrimitiveType.Triangles, 0, 36); | |
| GL.BindVertexArray(0); | |
| SwapBuffers(); | |
| } | |
| protected override void OnResize(EventArgs e) | |
| { | |
| base.OnResize(e); | |
| } | |
| protected override void OnUnload(EventArgs e) | |
| { | |
| GL.DeleteProgram(_program); | |
| GL.DeleteShader(_fragShader); | |
| GL.DeleteShader(_vertexShader); | |
| GL.DeleteBuffer(_vbo); | |
| GL.DeleteVertexArray(_vao); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks really really really good