1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
// This code was written for the OpenTK library and has been released
// to the Public Domain.
// It is provided "as is" without express or implied warranty of any kind.
#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 System.Diagnostics;
using System.IO;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
#endregion --- Using Directives ---
namespace Examples.Tutorial
{
/// <summary>
/// Demonstrates how to load and use a simple OpenGL shader program. Example is incomplete (documentation).
/// </summary>
[Example("First shader", ExampleCategory.OpenGL, "2.x", Documentation = "SimpleGLSL")]
public class T10_GLSL_Cube : GameWindow
{
#region --- Fields ---
static float angle = 0.0f, rotation_speed = 3.0f;
int vertex_shader_object, fragment_shader_object, shader_program;
int vertex_buffer_object, color_buffer_object, element_buffer_object;
Shapes.Shape shape = new Examples.Shapes.Cube();
#endregion
#region --- Constructors ---
public T10_GLSL_Cube()
: base(800, 600, GraphicsMode.Default)
{ }
#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>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Check for necessary capabilities:
Version version = new Version(GL.GetString(StringName.Version).Substring(0, 3));
Version target = new Version(2, 0);
if (version < target)
{
throw new NotSupportedException(String.Format(
"OpenGL {0} is required (you only have {1}).", target, version));
}
GL.ClearColor(Color.MidnightBlue);
GL.Enable(EnableCap.DepthTest);
CreateVBO();
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);
}
#endregion
#region CreateShaders
void CreateShaders(string vs, string fs,
out int vertexObject, out int fragmentObject,
out int program)
{
int status_code;
string info;
vertexObject = GL.CreateShader(ShaderType.VertexShader);
fragmentObject = GL.CreateShader(ShaderType.FragmentShader);
// Compile vertex shader
GL.ShaderSource(vertexObject, vs);
GL.CompileShader(vertexObject);
GL.GetShaderInfoLog(vertexObject, out info);
GL.GetShader(vertexObject, ShaderParameter.CompileStatus, out status_code);
if (status_code != 1)
throw new ApplicationException(info);
// Compile vertex shader
GL.ShaderSource(fragmentObject, fs);
GL.CompileShader(fragmentObject);
GL.GetShaderInfoLog(fragmentObject, out info);
GL.GetShader(fragmentObject, ShaderParameter.CompileStatus, out status_code);
if (status_code != 1)
throw new ApplicationException(info);
program = GL.CreateProgram();
GL.AttachShader(program, fragmentObject);
GL.AttachShader(program, vertexObject);
GL.LinkProgram(program);
GL.UseProgram(program);
}
#endregion
#region private void CreateVBO()
void CreateVBO()
{
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 buffer.
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);
if (size != shape.Vertices.Length * 3 * sizeof(Single))
throw new ApplicationException(String.Format(
"Problem uploading vertex buffer to VBO (vertices). Tried to upload {0} bytes, uploaded {1}.",
shape.Vertices.Length * 3 * sizeof(Single), size));
// Upload the color buffer.
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);
if (size != shape.Colors.Length * sizeof(int))
throw new ApplicationException(String.Format(
"Problem uploading vertex buffer to VBO (colors). Tried to upload {0} bytes, uploaded {1}.",
shape.Colors.Length * sizeof(int), size));
// Upload the index buffer (elements inside the vertex buffer, not color indices as per the IndexPointer function!)
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);
if (size != shape.Indices.Length * sizeof(int))
throw new ApplicationException(String.Format(
"Problem uploading vertex buffer to VBO (offsets). Tried to upload {0} bytes, uploaded {1}.",
shape.Indices.Length * sizeof(int), size));
}
#endregion
#region OnUnload
protected override void OnUnload(EventArgs e)
{
if (shader_program != 0)
GL.DeleteProgram(shader_program);
if (fragment_shader_object != 0)
GL.DeleteShader(fragment_shader_object);
if (vertex_shader_object != 0)
GL.DeleteShader(vertex_shader_object);
if (vertex_buffer_object != 0)
GL.DeleteBuffers(1, ref vertex_buffer_object);
if (element_buffer_object != 0)
GL.DeleteBuffers(1, ref element_buffer_object);
}
#endregion
#region OnResize
/// <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>
protected override void OnResize(EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
float aspect_ratio = Width / (float)Height;
Matrix4 perpective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 64);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref perpective);
}
#endregion
#region OnUpdateFrame
/// <summary>
/// Prepares the next frame for rendering.
/// </summary>
/// <remarks>
/// Place your control logic here. This is the place to respond to user input,
/// update object positions etc.
/// </remarks>
protected override void OnUpdateFrame(FrameEventArgs e)
{
var keyboard = OpenTK.Input.Keyboard.GetState();
if (keyboard[OpenTK.Input.Key.Escape])
this.Exit();
if (keyboard[OpenTK.Input.Key.F11])
if (WindowState != WindowState.Fullscreen)
WindowState = WindowState.Fullscreen;
else
WindowState = WindowState.Normal;
}
#endregion
#region OnRenderFrame
/// <summary>
/// Place your rendering code here.
/// </summary>
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit);
Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat);
angle += rotation_speed * (float)e.Time;
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertex_buffer_object);
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer_object);
GL.ColorPointer(4, ColorPointerType.UnsignedByte, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, element_buffer_object);
GL.DrawElements(PrimitiveType.Triangles, shape.Indices.Length,
DrawElementsType.UnsignedInt, IntPtr.Zero);
//GL.DrawArrays(GL.Enums.BeginMode.POINTS, 0, shape.Vertices.Length);
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.ColorArray);
//int error = GL.GetError();
//if (error != 0)
// Debug.Print(Glu.ErrorString(Glu.Enums.ErrorCode.INVALID_OPERATION));
SwapBuffers();
}
#endregion
#region public static void Main()
/// <summary>
/// Entry point of this example.
/// </summary>
[STAThread]
public static void Main()
{
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);
}
}
#endregion
}
}
|