2007-07-23 00:15:18 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#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 OpenTK;
|
|
|
|
|
|
|
|
|
|
#endregion --- Using Directives ---
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
|
|
|
|
public class T10_GLSL_Cube : GameWindow, IExample
|
|
|
|
|
{
|
|
|
|
|
#region --- Fields ---
|
|
|
|
|
|
|
|
|
|
#region Shaders
|
|
|
|
|
|
|
|
|
|
string[] vertex_shader_source =
|
2007-09-23 13:38:03 +00:00
|
|
|
|
{@"
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
gl_FrontColor = gl_Color;
|
|
|
|
|
gl_Position = ftransform();
|
|
|
|
|
}
|
|
|
|
|
"};
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
string[] fragment_shader_source =
|
2007-09-23 13:38:03 +00:00
|
|
|
|
{@"
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
gl_FragColor = gl_Color;
|
|
|
|
|
}
|
|
|
|
|
"};
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
static float angle;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
|
|
|
|
public T10_GLSL_Cube()
|
|
|
|
|
{
|
2007-09-02 00:07:40 +00:00
|
|
|
|
this.CreateWindow(new DisplayMode(800, 600));
|
2007-08-06 11:22:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnLoad
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is the place to load resources that change little
|
|
|
|
|
/// during the lifetime of the GameWindow. In this case, we
|
|
|
|
|
/// check for GLSL support, and load the shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">Not used.</param>
|
|
|
|
|
public override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
2007-09-02 22:52:00 +00:00
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
// Check for necessary capabilities:
|
2007-08-20 10:46:37 +00:00
|
|
|
|
if (!GL.SupportsExtension("VERSION_2_0"))
|
2007-08-06 11:22:18 +00:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("You need at least OpenGL 2.0 to run this example. Aborting.", "GLSL not supported",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
|
|
this.Exit();
|
2007-09-02 08:25:10 +00:00
|
|
|
|
return;
|
2007-08-06 11:22:18 +00:00
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
GL.ClearColor(Color.MidnightBlue);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY);
|
|
|
|
|
GL.EnableClientState(GL.Enums.EnableCap.COLOR_ARRAY);
|
|
|
|
|
GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, Shapes.Cube.Vertices);
|
|
|
|
|
GL.ColorPointer(4, GL.Enums.ColorPointerType.UNSIGNED_BYTE, 0, Shapes.Cube.Colors);
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
uint vertex_shader_object, fragment_shader_object;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
int status;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
uint shader_program;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
vertex_shader_object = (uint)GL.CreateShader(GL.Enums.VERSION_2_0.VERTEX_SHADER);
|
|
|
|
|
fragment_shader_object = (uint)GL.CreateShader(GL.Enums.VERSION_2_0.FRAGMENT_SHADER);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-02 13:34:44 +00:00
|
|
|
|
unsafe { GL.ShaderSource(vertex_shader_object, vertex_shader_source.Length, vertex_shader_source, (int*)null); }
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GL.CompileShader(vertex_shader_object);
|
2007-09-02 00:40:43 +00:00
|
|
|
|
GL.GetShader(vertex_shader_object, GL.Enums.VERSION_2_0.COMPILE_STATUS, out status);
|
2007-08-04 23:44:41 +00:00
|
|
|
|
if (status != (int)GL.Enums.Boolean.TRUE)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-04 23:39:56 +00:00
|
|
|
|
int length = 0;
|
2007-09-02 00:40:43 +00:00
|
|
|
|
GL.GetShader(vertex_shader_object, GL.Enums.VERSION_2_0.INFO_LOG_LENGTH, out length);
|
2007-08-04 23:39:56 +00:00
|
|
|
|
StringBuilder info = new StringBuilder(length);
|
|
|
|
|
GL.GetShaderInfoLog(vertex_shader_object, info.Capacity, out length, info);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
throw new Exception(info.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 13:34:44 +00:00
|
|
|
|
unsafe { GL.ShaderSource(fragment_shader_object, fragment_shader_source.Length, fragment_shader_source, (int*)null); }
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GL.CompileShader(fragment_shader_object);
|
2007-09-02 00:40:43 +00:00
|
|
|
|
GL.GetShader(fragment_shader_object, GL.Enums.VERSION_2_0.COMPILE_STATUS, out status);
|
2007-08-04 23:44:41 +00:00
|
|
|
|
if (status != (int)GL.Enums.Boolean.TRUE)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-04 23:39:56 +00:00
|
|
|
|
int length;
|
2007-09-02 00:40:43 +00:00
|
|
|
|
GL.GetShader(vertex_shader_object, GL.Enums.VERSION_2_0.INFO_LOG_LENGTH, out length);
|
2007-08-04 23:39:56 +00:00
|
|
|
|
StringBuilder info = new StringBuilder(length);
|
|
|
|
|
GL.GetShaderInfoLog(fragment_shader_object, info.Capacity, out length, info);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
throw new Exception(info.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
shader_program = (uint)GL.CreateProgram();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GL.AttachShader(shader_program, fragment_shader_object);
|
|
|
|
|
GL.AttachShader(shader_program, vertex_shader_object);
|
|
|
|
|
|
|
|
|
|
GL.LinkProgram(shader_program);
|
|
|
|
|
GL.UseProgram(shader_program);
|
|
|
|
|
|
|
|
|
|
OnResize(new OpenTK.Platform.ResizeEventArgs(this.Width, this.Height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnResize
|
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the user resizes the window.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">Contains the new width/height of the window.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// You want the OpenGL viewport to match the window. This is the place to do it!
|
|
|
|
|
/// </remarks>
|
2007-07-23 00:15:18 +00:00
|
|
|
|
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
2007-08-21 12:04:01 +00:00
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
double ratio = e.Width / (double)e.Height;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region OnUpdateFrame
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
/// <summary>
|
2007-09-23 13:38:03 +00:00
|
|
|
|
/// Prepares the next frame for rendering.
|
2007-08-06 11:22:18 +00:00
|
|
|
|
/// </summary>
|
2007-09-23 13:38:03 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Place your control logic here. This is the place to respond to user input,
|
|
|
|
|
/// update object positions etc.
|
|
|
|
|
/// </remarks>
|
2007-09-22 22:01:43 +00:00
|
|
|
|
public override void OnUpdateFrame(UpdateFrameEventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-05 16:04:39 +00:00
|
|
|
|
if (Keyboard[0][OpenTK.Input.Key.Escape])
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
this.Exit();
|
2007-09-23 13:38:03 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((Keyboard[0][OpenTK.Input.Key.AltLeft] || Keyboard[0][OpenTK.Input.Key.AltRight]) &&
|
|
|
|
|
Keyboard[0][OpenTK.Input.Key.Enter])
|
|
|
|
|
{
|
|
|
|
|
Fullscreen = !Fullscreen;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
//angle += 180.0f * (float)e.Time;
|
2007-09-23 12:49:38 +00:00
|
|
|
|
angle += 3.0f;
|
|
|
|
|
if (angle > 720.0f)
|
|
|
|
|
angle -= 720.0f;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region OnRenderFrame
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Place your rendering code here.
|
|
|
|
|
/// </summary>
|
2007-09-23 12:09:42 +00:00
|
|
|
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-03 00:14:31 +00:00
|
|
|
|
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-23 12:49:38 +00:00
|
|
|
|
GL.MatrixMode(GL.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.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
|
2007-09-23 13:38:03 +00:00
|
|
|
|
GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, Shapes.Cube.Indices.Length,
|
|
|
|
|
GL.Enums.All.UNSIGNED_SHORT, Shapes.Cube.Indices);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
Context.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-02 00:07:40 +00:00
|
|
|
|
#region public void Launch()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void Launch()
|
|
|
|
|
{
|
2007-09-23 13:38:03 +00:00
|
|
|
|
// Lock UpdateFrame and RenderFrame at 60Hz.
|
2007-09-23 12:49:38 +00:00
|
|
|
|
Run(60.0, 60.0);
|
2007-09-02 00:07:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|