2009-02-22 10:43:35 +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.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
2009-08-14 13:13:28 +00:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using OpenTK.Platform;
|
2009-11-04 10:44:56 +00:00
|
|
|
|
using System.Drawing;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
2010-10-09 19:59:06 +00:00
|
|
|
|
[Example("VBO Static", ExampleCategory.OpenGL, "1.x", 3, Documentation = "VBOStatic")]
|
2009-02-22 10:43:35 +00:00
|
|
|
|
public class T08_VBO : GameWindow
|
|
|
|
|
{
|
2009-11-04 10:44:56 +00:00
|
|
|
|
const float rotation_speed = 180.0f;
|
|
|
|
|
float angle;
|
2010-10-09 19:59:06 +00:00
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
struct Vbo { public int VboID, EboID, NumElements; }
|
2009-02-22 10:43:35 +00:00
|
|
|
|
Vbo[] vbo = new Vbo[2];
|
2010-10-09 19:59:06 +00:00
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
VertexPositionColor[] CubeVertices = new VertexPositionColor[]
|
|
|
|
|
{
|
|
|
|
|
new VertexPositionColor(-1.0f, -1.0f, 1.0f, Color.DarkRed),
|
|
|
|
|
new VertexPositionColor( 1.0f, -1.0f, 1.0f, Color.DarkRed),
|
|
|
|
|
new VertexPositionColor( 1.0f, 1.0f, 1.0f, Color.Gold),
|
|
|
|
|
new VertexPositionColor(-1.0f, 1.0f, 1.0f, Color.Gold),
|
|
|
|
|
new VertexPositionColor(-1.0f, -1.0f, -1.0f, Color.DarkRed),
|
|
|
|
|
new VertexPositionColor( 1.0f, -1.0f, -1.0f, Color.DarkRed),
|
|
|
|
|
new VertexPositionColor( 1.0f, 1.0f, -1.0f, Color.Gold),
|
|
|
|
|
new VertexPositionColor(-1.0f, 1.0f, -1.0f, Color.Gold)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
readonly short[] CubeElements = new short[]
|
|
|
|
|
{
|
|
|
|
|
0, 1, 2, 2, 3, 0, // front face
|
|
|
|
|
3, 2, 6, 6, 7, 3, // top face
|
|
|
|
|
7, 6, 5, 5, 4, 7, // back face
|
|
|
|
|
4, 0, 3, 3, 7, 4, // left face
|
|
|
|
|
0, 1, 5, 5, 4, 0, // bottom face
|
|
|
|
|
1, 5, 6, 6, 2, 1, // right face
|
|
|
|
|
};
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
public T08_VBO() : base(800, 600) { }
|
|
|
|
|
|
2009-10-21 13:33:00 +00:00
|
|
|
|
protected override void OnLoad(EventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
2009-08-17 09:56:16 +00:00
|
|
|
|
string version = GL.GetString(StringName.Version);
|
|
|
|
|
int major = (int)version[0];
|
|
|
|
|
int minor = (int)version[2];
|
|
|
|
|
if (major <= 1 && minor < 5)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
System.Windows.Forms.MessageBox.Show("You need at least OpenGL 1.5 to run this example. Aborting.", "VBOs not supported",
|
|
|
|
|
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
|
|
|
|
|
this.Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
GL.ClearColor(System.Drawing.Color.MidnightBlue);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
vbo[0] = LoadVBO(CubeVertices, CubeElements);
|
|
|
|
|
vbo[1] = LoadVBO(CubeVertices, CubeElements);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnResize(EventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2009-11-04 10:44:56 +00:00
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
|
|
2009-08-14 13:13:28 +00:00
|
|
|
|
float aspect_ratio = Width / (float)Height;
|
2009-08-15 17:52:49 +00:00
|
|
|
|
Matrix4 perpective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 64);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
2009-08-14 13:13:28 +00:00
|
|
|
|
GL.LoadMatrix(ref perpective);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2009-11-04 10:44:56 +00:00
|
|
|
|
base.OnUpdateFrame(e);
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
if (Keyboard[OpenTK.Input.Key.Escape])
|
|
|
|
|
this.Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
|
protected override void OnRenderFrame(FrameEventArgs e)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
base.OnRenderFrame(e);
|
|
|
|
|
|
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
|
|
|
|
|
2009-08-14 13:13:28 +00:00
|
|
|
|
Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
2009-08-14 13:13:28 +00:00
|
|
|
|
GL.LoadMatrix(ref lookat);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
angle += rotation_speed * (float)e.Time;
|
|
|
|
|
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
Draw(vbo[0]);
|
|
|
|
|
|
|
|
|
|
SwapBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
Vbo LoadVBO<TVertex>(TVertex[] vertices, short[] elements) where TVertex : struct
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
Vbo handle = new Vbo();
|
|
|
|
|
int size;
|
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
// To create a VBO:
|
|
|
|
|
// 1) Generate the buffer handles for the vertex and element buffers.
|
|
|
|
|
// 2) Bind the vertex buffer handle and upload your vertex data. Check that the buffer was uploaded correctly.
|
|
|
|
|
// 3) Bind the element buffer handle and upload your element data. Check that the buffer was uploaded correctly.
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
GL.GenBuffers(1, out handle.VboID);
|
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID);
|
2009-11-04 10:44:56 +00:00
|
|
|
|
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
BufferUsageHint.StaticDraw);
|
|
|
|
|
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
|
2009-11-04 10:44:56 +00:00
|
|
|
|
if (vertices.Length * BlittableValueType.StrideOf(vertices) != size)
|
|
|
|
|
throw new ApplicationException("Vertex data not uploaded correctly");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
GL.GenBuffers(1, out handle.EboID);
|
|
|
|
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID);
|
2009-11-04 10:44:56 +00:00
|
|
|
|
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(elements.Length * sizeof(short)), elements,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
BufferUsageHint.StaticDraw);
|
|
|
|
|
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
|
2009-11-04 10:44:56 +00:00
|
|
|
|
if (elements.Length * sizeof(short) != size)
|
|
|
|
|
throw new ApplicationException("Element data not uploaded correctly");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
handle.NumElements = elements.Length;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Draw(Vbo handle)
|
|
|
|
|
{
|
2009-11-04 10:44:56 +00:00
|
|
|
|
// To draw a VBO:
|
|
|
|
|
// 1) Ensure that the VertexArray client state is enabled.
|
|
|
|
|
// 2) Bind the vertex and element buffer handles.
|
|
|
|
|
// 3) Set up the data pointers (vertex, normal, color) according to your vertex format.
|
|
|
|
|
// 4) Call DrawElements. (Note: the last parameter is an offset into the element buffer
|
|
|
|
|
// and will usually be IntPtr.Zero).
|
|
|
|
|
|
2010-10-09 19:56:33 +00:00
|
|
|
|
GL.EnableClientState(ArrayCap.ColorArray);
|
|
|
|
|
GL.EnableClientState(ArrayCap.VertexArray);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID);
|
|
|
|
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID);
|
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(CubeVertices), new IntPtr(0));
|
|
|
|
|
GL.ColorPointer(4, ColorPointerType.UnsignedByte, BlittableValueType.StrideOf(CubeVertices), new IntPtr(12));
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-11-04 10:44:56 +00:00
|
|
|
|
GL.DrawElements(BeginMode.Triangles, handle.NumElements, DrawElementsType.UnsignedShort, IntPtr.Zero);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Entry point of this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
using (T08_VBO example = new T08_VBO())
|
|
|
|
|
{
|
2009-11-04 10:44:56 +00:00
|
|
|
|
Utilities.SetWindowTitle(example);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
example.Run(30.0, 0.0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|