Opentk/Source/Examples/OpenGL/GLSL/Lesson01/Cube.cs

236 lines
6.7 KiB
C#
Raw Normal View History

2006-11-05 11:50:08 +00:00
#region --- License ---
/* This source file is released under the MIT license. See License.txt for more information.
* Coded by Stephen Apostolopoulos.
*/
#endregion
2006-11-05 11:50:08 +00:00
#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;
2006-11-05 11:50:08 +00:00
using OpenTK.OpenGL;
using Enums = OpenTK.OpenGL.Enums;
2006-11-05 11:50:08 +00:00
#endregion --- Using Directives ---
2006-10-08 18:26:43 +00:00
namespace OpenTK.Examples.OpenGL.GLSL
{
2006-11-05 11:50:08 +00:00
public class Cube : Framework
{
2006-11-05 11:50:08 +00:00
#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;
2006-11-05 11:50:08 +00:00
#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
2006-11-02 21:40:36 +00:00
protected override void OnLoad(object sender, EventArgs e)
{
2006-11-02 21:40:36 +00:00
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);
2006-11-02 21:40:36 +00:00
OnResize(sender, e);
}
2006-11-05 11:50:08 +00:00
#endregion
2006-11-05 11:50:08 +00:00
#region OnResize
2006-11-02 21:40:36 +00:00
protected override void OnResize(object sender, EventArgs e)
{
2006-11-02 21:40:36 +00:00
base.OnResize(sender, e);
if (ClientSize.Height == 0)
ClientSize = new System.Drawing.Size(ClientSize.Width, 1);
2006-10-09 22:35:25 +00:00
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);
}
2006-11-05 11:50:08 +00:00
#endregion
2006-11-05 11:50:08 +00:00
#region OnPaint
2006-11-02 21:40:36 +00:00
protected override void OnPaint()
{
2006-11-02 21:40:36 +00:00
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();
2006-11-02 21:40:36 +00:00
ActiveContext.SwapBuffers();
}
#endregion
2006-11-05 11:50:08 +00:00
#region OnKeyDown
2006-11-02 21:40:36 +00:00
protected override void OnKeyDown(object sender, KeyEventArgs e)
{
2006-11-02 21:40:36 +00:00
base.OnKeyDown(sender, e);
switch (e.KeyData)
{
case Keys.Escape:
Application.Exit();
break;
case Keys.F1:
2006-11-02 21:40:36 +00:00
this.SetResolution(this.Width, this.Height, this.ColorDepth, !this.IsFullscreen);
break;
}
}
#endregion
2006-11-05 11:50:08 +00:00
#endregion --- Event Handlers ---
}
}