2007-07-23 02:15:18 +02: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;
|
2007-08-01 11:32:49 +02:00
|
|
|
|
using Enums = OpenTK.OpenGL.GL.Enums;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
|
|
#endregion --- Using Directives ---
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
|
|
|
|
public class T10_GLSL_Cube : GameWindow, IExample
|
|
|
|
|
{
|
|
|
|
|
#region --- Fields ---
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
|
|
|
|
public T10_GLSL_Cube()
|
|
|
|
|
{
|
|
|
|
|
Context.MakeCurrent();
|
|
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.ShaderSource(vertex_shader_object, vertex_shader_source.Length, vertex_shader_source, (int[])null);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
GL.CompileShader(vertex_shader_object);
|
2007-08-01 23:14:39 +02:00
|
|
|
|
GL.GetShader(vertex_shader_object, Enums.VERSION_2_0.COMPILE_STATUS, out status);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
if (status != (int)Enums.Boolean.TRUE)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder info = new StringBuilder(1024);
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.GetShaderInfoLog(vertex_shader_object, info.MaxCapacity, (int[])null, info);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
throw new Exception(info.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.ShaderSource(fragment_shader_object, fragment_shader_source.Length, fragment_shader_source, (int[])null);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
GL.CompileShader(fragment_shader_object);
|
2007-08-01 23:14:39 +02:00
|
|
|
|
GL.GetShader(fragment_shader_object, Enums.VERSION_2_0.COMPILE_STATUS, out status);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
if (status != (int)Enums.Boolean.TRUE)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder info = new StringBuilder(1024);
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.GetShaderInfoLog(fragment_shader_object, 1024, (int[])null, info);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
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(new OpenTK.Platform.ResizeEventArgs(this.Width, this.Height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region static public void Launch()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
static public void Launch()
|
|
|
|
|
{
|
|
|
|
|
using (T10_GLSL_Cube ex = new T10_GLSL_Cube())
|
|
|
|
|
{
|
|
|
|
|
ex.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnResize
|
|
|
|
|
|
|
|
|
|
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
|
|
|
|
GL.Viewport(0, 0, this.Width, this.Height);
|
|
|
|
|
|
|
|
|
|
double ratio = 0.0;
|
|
|
|
|
ratio = this.Width / (double)this.Height;
|
|
|
|
|
|
|
|
|
|
GL.MatrixMode(Enums.MatrixMode.PROJECTION);
|
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region UpdateFrame
|
|
|
|
|
|
|
|
|
|
public override void UpdateFrame()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateFrame();
|
|
|
|
|
|
2007-07-27 00:56:55 +02:00
|
|
|
|
if (Key[OpenTK.Input.Keys.Escape])
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
this.Quit = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region RenderFrame
|
|
|
|
|
|
|
|
|
|
public override void RenderFrame()
|
|
|
|
|
{
|
|
|
|
|
base.RenderFrame();
|
|
|
|
|
|
|
|
|
|
GL.Clear(Enums.ClearBufferMask.COLOR_BUFFER_BIT | Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
DrawCube();
|
|
|
|
|
|
|
|
|
|
Context.SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region DrawCube
|
|
|
|
|
|
|
|
|
|
public void DrawCube()
|
|
|
|
|
{
|
|
|
|
|
GL.Begin(Enums.BeginMode.QUADS);
|
|
|
|
|
|
2007-08-01 23:14:39 +02:00
|
|
|
|
GL.Color3(1, 0, 0);
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, 1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, -1.0f, -1.0f);
|
|
|
|
|
|
|
|
|
|
GL.Color3(1, 1, 0);
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, -1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, -1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
GL.Color3(1, 0, 1);
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, -1.0f);
|
|
|
|
|
|
|
|
|
|
GL.Color3(0, 1, 0);
|
|
|
|
|
GL.Vertex3(-1.0f, -1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, -1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, 1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
GL.Color3(0, 0, 1);
|
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(-1.0f, 1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, 1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, 1.0f, -1.0f);
|
|
|
|
|
|
|
|
|
|
GL.Color3(0, 1, 1);
|
|
|
|
|
GL.Vertex3(1.0f, -1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, 1.0f, -1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, 1.0f, 1.0f);
|
|
|
|
|
GL.Vertex3(1.0f, -1.0f, 1.0f);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
GL.End();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|