#region --- License --- /* This source file is released under the MIT license. See License.txt for more information. * Coded by Stephen Apostolopoulos. */ #endregion #region --- Using Directives --- using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using OpenTK.OpenGL; using Enums = OpenTK.OpenGL.Enums; #endregion --- Using Directives --- namespace OpenTK.Examples.OpenGL.GLSL { public class Cube : Framework { #region --- Variables --- #region Shaders string[] vertex_shader_source = { "void main() {", "gl_FrontColor = gl_Color;", "gl_Position = ftransform();", "}", }; string[] fragment_shader_source = { "void main() { gl_FragColor = gl_Color; }\0" }; #endregion static float angle; #endregion --- Variables --- #region --- Methods --- #region Entry point [STAThread] static void Main() { new Cube().Run(); } #endregion #region DrawCube public void DrawCube() { GL.Begin(Enums.BeginMode.QUADS); GL.Color3f(1, 0, 0); GL.Vertex3f(-1.0f, -1.0f, -1.0f); GL.Vertex3f(-1.0f, 1.0f, -1.0f); GL.Vertex3f(1.0f, 1.0f, -1.0f); GL.Vertex3f(1.0f, -1.0f, -1.0f); GL.Color3f(1, 1, 0); GL.Vertex3f(-1.0f, -1.0f, -1.0f); GL.Vertex3f(1.0f, -1.0f, -1.0f); GL.Vertex3f(1.0f, -1.0f, 1.0f); GL.Vertex3f(-1.0f, -1.0f, 1.0f); GL.Color3f(1, 0, 1); GL.Vertex3f(-1.0f, -1.0f, -1.0f); GL.Vertex3f(-1.0f, -1.0f, 1.0f); GL.Vertex3f(-1.0f, 1.0f, 1.0f); GL.Vertex3f(-1.0f, 1.0f, -1.0f); GL.Color3f(0, 1, 0); GL.Vertex3f(-1.0f, -1.0f, 1.0f); GL.Vertex3f(1.0f, -1.0f, 1.0f); GL.Vertex3f(1.0f, 1.0f, 1.0f); GL.Vertex3f(-1.0f, 1.0f, 1.0f); GL.Color3f(0, 0, 1); GL.Vertex3f(-1.0f, 1.0f, -1.0f); GL.Vertex3f(-1.0f, 1.0f, 1.0f); GL.Vertex3f(1.0f, 1.0f, 1.0f); GL.Vertex3f(1.0f, 1.0f, -1.0f); GL.Color3f(0, 1, 1); GL.Vertex3f(1.0f, -1.0f, -1.0f); GL.Vertex3f(1.0f, 1.0f, -1.0f); GL.Vertex3f(1.0f, 1.0f, 1.0f); GL.Vertex3f(1.0f, -1.0f, 1.0f); GL.End(); } #endregion #endregion --- Methods --- #region --- Event Handlers --- #region OnLoad protected override void OnLoad(object sender, EventArgs e) { base.OnLoad(sender, e); Text = GL.GetString(Enums.StringName.VENDOR) + " " + GL.GetString(Enums.StringName.RENDERER) + " " + GL.GetString(Enums.StringName.VERSION); GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(Enums.EnableCap.DEPTH_TEST); int vertex_shader_object, fragment_shader_object; int status; int shader_program; vertex_shader_object = GL.CreateShader(Enums.VERSION_2_0.VERTEX_SHADER); fragment_shader_object = GL.CreateShader(Enums.VERSION_2_0.FRAGMENT_SHADER); GL.ShaderSource(vertex_shader_object, vertex_shader_source.Length, vertex_shader_source, null); GL.CompileShader(vertex_shader_object); GL.GetShaderiv(vertex_shader_object, Enums.VERSION_2_0.COMPILE_STATUS, out status); if (status != (int)Enums.Boolean.TRUE) { StringBuilder info = new StringBuilder(1024); GL.GetShaderInfoLog(vertex_shader_object, info.MaxCapacity, null, info); throw new Exception(info.ToString()); } GL.ShaderSource(fragment_shader_object, fragment_shader_source.Length, fragment_shader_source, null); GL.CompileShader(fragment_shader_object); GL.GetShaderiv(fragment_shader_object, Enums.VERSION_2_0.COMPILE_STATUS, out status); if (status != (int)Enums.Boolean.TRUE) { StringBuilder info = new StringBuilder(1024); GL.GetShaderInfoLog(fragment_shader_object, 1024, null, info); throw new Exception(info.ToString()); } shader_program = GL.CreateProgram(); GL.AttachShader(shader_program, fragment_shader_object); GL.AttachShader(shader_program, vertex_shader_object); GL.LinkProgram(shader_program); GL.UseProgram(shader_program); OnResize(sender, e); } #endregion #region OnResize protected override void OnResize(object sender, EventArgs e) { base.OnResize(sender, e); if (ClientSize.Height == 0) ClientSize = new System.Drawing.Size(ClientSize.Width, 1); GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height); double ratio = 0.0; ratio = ClientSize.Width / (double)ClientSize.Height; GL.MatrixMode(Enums.MatrixMode.PROJECTION); GL.LoadIdentity(); Glu.Perspective(45.0, ratio, 1.0, 64.0); } #endregion #region OnPaint protected override void OnPaint() { base.OnPaint(); GL.MatrixMode(Enums.MatrixMode.MODELVIEW); GL.LoadIdentity(); Glu.LookAt( 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); GL.Rotatef(angle, 0.0f, 1.0f, 0.0f); angle += 0.05f; GL.Clear(Enums.ClearBufferMask.COLOR_BUFFER_BIT | Enums.ClearBufferMask.DEPTH_BUFFER_BIT); DrawCube(); ActiveContext.SwapBuffers(); } #endregion #region OnKeyDown protected override void OnKeyDown(object sender, KeyEventArgs e) { base.OnKeyDown(sender, e); switch (e.KeyData) { case Keys.Escape: Application.Exit(); break; case Keys.F1: this.SetResolution(this.Width, this.Height, this.ColorDepth, !this.IsFullscreen); break; } } #endregion #endregion --- Event Handlers --- } }