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;
|
2007-11-04 15:32:24 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using OpenTK.OpenGL.Enums;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
#endregion --- Using Directives ---
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
2007-09-26 11:47:30 +00:00
|
|
|
|
/// <summary>
|
2007-09-26 15:39:32 +00:00
|
|
|
|
/// Demonstrates how to load and use a simple OpenGL shader program. Example is incomplete (documentation).
|
2007-09-26 11:47:30 +00:00
|
|
|
|
/// </summary>
|
2007-11-11 18:44:10 +00:00
|
|
|
|
[Example("First shader", ExampleCategory.GLSL, 1)]
|
|
|
|
|
public class T10_GLSL_Cube : GameWindow
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
#region --- Fields ---
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
static float angle = 0.0f, rotation_speed = 3.0f;
|
2007-09-25 21:11:16 +00:00
|
|
|
|
int vertex_shader_object, fragment_shader_object, shader_program;
|
2007-11-04 15:32:24 +00:00
|
|
|
|
int vertex_buffer_object, color_buffer_object, element_buffer_object;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
Shapes.Shape shape = new Examples.Shapes.Cube();
|
2007-09-30 12:36:41 +00:00
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
public T10_GLSL_Cube()
|
2007-11-11 18:44:10 +00:00
|
|
|
|
: base(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)
|
|
|
|
|
{
|
|
|
|
|
// 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-11-10 23:31:01 +00:00
|
|
|
|
GL.ClearColor(Color.SteelBlue);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
CreateVBO();
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
using (StreamReader vs = new StreamReader("Data/Shaders/Simple_VS.glsl"))
|
|
|
|
|
using (StreamReader fs = new StreamReader("Data/Shaders/Simple_FS.glsl"))
|
|
|
|
|
CreateShaders(vs.ReadToEnd(), fs.ReadToEnd(),
|
|
|
|
|
out vertex_shader_object, out fragment_shader_object,
|
|
|
|
|
out shader_program);
|
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region CreateShaders
|
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
void CreateShaders(string vs, string fs,
|
|
|
|
|
out int vertexObject, out int fragmentObject,
|
|
|
|
|
out int program)
|
|
|
|
|
{
|
|
|
|
|
int status_code;
|
|
|
|
|
string info;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2008-01-02 01:01:40 +00:00
|
|
|
|
vertexObject = GL.CreateShader(ShaderObjectType.VertexShader);
|
|
|
|
|
fragmentObject = GL.CreateShader(ShaderObjectType.FragmentShader);
|
2007-11-10 23:31:01 +00:00
|
|
|
|
|
|
|
|
|
// Compile vertex shader
|
|
|
|
|
GL.ShaderSource(vertexObject, vs);
|
|
|
|
|
GL.CompileShader(vertexObject);
|
|
|
|
|
GL.GetShaderInfoLog(vertexObject, out info);
|
|
|
|
|
GL.GetShader(vertexObject, Version20.CompileStatus, out status_code);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
if (status_code != 1)
|
2007-11-10 23:31:01 +00:00
|
|
|
|
throw new ApplicationException(info);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
// Compile vertex shader
|
|
|
|
|
GL.ShaderSource(fragmentObject, fs);
|
|
|
|
|
GL.CompileShader(fragmentObject);
|
|
|
|
|
GL.GetShaderInfoLog(fragmentObject, out info);
|
|
|
|
|
GL.GetShader(fragmentObject, Version20.CompileStatus, out status_code);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
|
|
|
|
|
if (status_code != 1)
|
2007-11-10 23:31:01 +00:00
|
|
|
|
throw new ApplicationException(info);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
program = GL.CreateProgram();
|
|
|
|
|
GL.AttachShader(program, fragmentObject);
|
|
|
|
|
GL.AttachShader(program, vertexObject);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
GL.LinkProgram(program);
|
|
|
|
|
GL.UseProgram(program);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-11-10 23:31:01 +00:00
|
|
|
|
#region private void CreateVBO()
|
|
|
|
|
|
|
|
|
|
void CreateVBO()
|
2007-11-04 15:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
int size;
|
|
|
|
|
|
|
|
|
|
GL.GenBuffers(1, out vertex_buffer_object);
|
|
|
|
|
GL.GenBuffers(1, out color_buffer_object);
|
|
|
|
|
GL.GenBuffers(1, out element_buffer_object);
|
|
|
|
|
|
|
|
|
|
// Upload the vertex data.
|
2008-01-02 01:01:40 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, vertex_buffer_object);
|
|
|
|
|
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.Vertices.Length * 3 * sizeof(float)), shape.Vertices, BufferUsageHint.StaticDraw);
|
|
|
|
|
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
if (size != shape.Vertices.Length * 3 * sizeof(Single))
|
|
|
|
|
throw new ApplicationException("Problem uploading vertex data to VBO");
|
|
|
|
|
|
|
|
|
|
// Upload the color data.
|
2008-01-02 01:01:40 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer_object);
|
|
|
|
|
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.Colors.Length * sizeof(int)), shape.Colors, BufferUsageHint.StaticDraw);
|
|
|
|
|
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
if (shape.Colors.Length * sizeof(int) != size)
|
|
|
|
|
throw new ApplicationException("Problem uploading color data to VBO");
|
|
|
|
|
|
|
|
|
|
// Upload the index data (elements inside the vertex data, not color indices as per the IndexPointer function!)
|
2008-01-02 01:01:40 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, element_buffer_object);
|
|
|
|
|
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(shape.Indices.Length * sizeof(Int32)), shape.Indices, BufferUsageHint.StaticDraw);
|
|
|
|
|
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
if (shape.Indices.Length * 4 != size)
|
|
|
|
|
throw new ApplicationException("Problem uploading index data to VBO");
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-26 23:31:53 +00:00
|
|
|
|
#region OnUnload
|
|
|
|
|
|
|
|
|
|
public override void OnUnload(EventArgs e)
|
|
|
|
|
{
|
2007-11-10 23:31:01 +00:00
|
|
|
|
GL.DeleteProgram(shader_program);
|
|
|
|
|
GL.DeleteShader(fragment_shader_object);
|
|
|
|
|
GL.DeleteShader(vertex_shader_object);
|
|
|
|
|
GL.DeleteBuffers(1, ref vertex_buffer_object);
|
|
|
|
|
GL.DeleteBuffers(1, ref element_buffer_object);
|
2007-09-26 23:31:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#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)
|
|
|
|
|
{
|
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-11-04 15:32:24 +00:00
|
|
|
|
GL.MatrixMode(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-09-26 11:47:30 +00:00
|
|
|
|
if (Keyboard[OpenTK.Input.Key.Escape])
|
2007-08-04 12:09:58 +00:00
|
|
|
|
this.Exit();
|
2007-09-23 13:38:03 +00:00
|
|
|
|
|
2007-09-26 11:47:30 +00:00
|
|
|
|
if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) &&
|
|
|
|
|
Keyboard[OpenTK.Input.Key.Enter])
|
2007-09-23 13:38:03 +00:00
|
|
|
|
Fullscreen = !Fullscreen;
|
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-11-04 15:32:24 +00:00
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit |
|
|
|
|
|
ClearBufferMask.DepthBufferBit);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
2007-09-23 12:49:38 +00:00
|
|
|
|
GL.LoadIdentity();
|
2007-11-04 15:32:24 +00:00
|
|
|
|
Glu.LookAt(0.0, 5.0, 5.0,
|
|
|
|
|
0.0, 0.0, 0.0,
|
|
|
|
|
0.0, 1.0, 0.0);
|
|
|
|
|
|
|
|
|
|
angle += rotation_speed * (float)e.ScaleFactor;
|
2007-09-23 12:49:38 +00:00
|
|
|
|
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.EnableClientState(EnableCap.VertexArray);
|
|
|
|
|
GL.EnableClientState(EnableCap.ColorArray);
|
|
|
|
|
|
2008-01-02 01:01:40 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, vertex_buffer_object);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero);
|
2008-01-02 01:01:40 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer_object);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.ColorPointer(4, ColorPointerType.UnsignedByte, 0, IntPtr.Zero);
|
2008-01-02 01:01:40 +00:00
|
|
|
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, element_buffer_object);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
|
|
|
|
|
GL.DrawElements(BeginMode.Triangles, shape.Indices.Length,
|
|
|
|
|
All.UnsignedInt, IntPtr.Zero);
|
|
|
|
|
|
|
|
|
|
//GL.DrawArrays(GL.Enums.BeginMode.POINTS, 0, shape.Vertices.Length);
|
|
|
|
|
|
|
|
|
|
GL.DisableClientState(EnableCap.VertexArray);
|
|
|
|
|
GL.DisableClientState(EnableCap.ColorArray);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//int error = GL.GetError();
|
|
|
|
|
//if (error != 0)
|
|
|
|
|
// Debug.Print(Glu.ErrorString(Glu.Enums.ErrorCode.INVALID_OPERATION));
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
Context.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
#region public static void Main()
|
2007-09-02 00:07:40 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-11-11 18:44:10 +00:00
|
|
|
|
/// Entry point of this example.
|
2007-09-02 00:07:40 +00:00
|
|
|
|
/// </summary>
|
2007-11-11 18:44:10 +00:00
|
|
|
|
[STAThread]
|
|
|
|
|
public static void Main()
|
2007-09-02 00:07:40 +00:00
|
|
|
|
{
|
2007-11-11 18:44:10 +00:00
|
|
|
|
using (T10_GLSL_Cube example = new T10_GLSL_Cube())
|
|
|
|
|
{
|
|
|
|
|
// Get the title and category of this example using reflection.
|
|
|
|
|
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
|
|
|
|
|
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
|
|
|
|
|
example.Run(30.0, 0.0);
|
|
|
|
|
}
|
2007-09-02 00:07:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|