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.Text;
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.OpenGL;
|
|
|
|
|
using OpenTK.Platform;
|
2007-08-06 14:13:50 +02:00
|
|
|
|
using System.Threading;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
2007-08-10 11:27:13 +02:00
|
|
|
|
public class T08_VBO : OpenTK.GameWindow /*, IExample */
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
#region --- Private Fields ---
|
|
|
|
|
|
|
|
|
|
int vbo, ibo, nbo; // vertex, index and normal buffer objects.
|
|
|
|
|
float angle;
|
|
|
|
|
|
|
|
|
|
// Cube vertices
|
|
|
|
|
float[] vdata =
|
|
|
|
|
{
|
|
|
|
|
-1.0f, -1.0f, 1.0f ,
|
|
|
|
|
1.0f, -1.0f, 1.0f ,
|
|
|
|
|
1.0f, 1.0f, 1.0f ,
|
|
|
|
|
-1.0f, 1.0f, 1.0f ,
|
|
|
|
|
-1.0f, -1.0f, -1.0f ,
|
|
|
|
|
1.0f, -1.0f, -1.0f ,
|
|
|
|
|
1.0f, 1.0f, -1.0f ,
|
|
|
|
|
-1.0f, 1.0f, -1.0f ,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Cube normals
|
|
|
|
|
float[,] ndata =
|
|
|
|
|
{
|
|
|
|
|
{ 1.0f, 0.0f, 0.0f },
|
|
|
|
|
{ 0.0f, 1.0f, 0.0f },
|
|
|
|
|
{ 0.0f, 0.0f, 1.0f },
|
|
|
|
|
{ -1.0f, 0.0f, 0.0f },
|
|
|
|
|
{ 0.0f, -1.0f, 0.0f },
|
|
|
|
|
{ 0.0f, 0.0f, -1.0f },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Indices
|
|
|
|
|
ushort[] idata =
|
|
|
|
|
{
|
|
|
|
|
// front face
|
|
|
|
|
0, 1, 2, 3,
|
|
|
|
|
// top face
|
|
|
|
|
3, 4, 6, 7,
|
|
|
|
|
// back face
|
|
|
|
|
7, 6, 5, 4,
|
|
|
|
|
// left face
|
|
|
|
|
4, 0, 3, 7,
|
|
|
|
|
// bottom face
|
|
|
|
|
0, 1, 5, 4,
|
|
|
|
|
// right face
|
|
|
|
|
1, 5, 6, 2,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructor ---
|
|
|
|
|
|
|
|
|
|
public T08_VBO()
|
|
|
|
|
{
|
2007-08-04 14:09:58 +02:00
|
|
|
|
this.CreateWindow(new DisplayMode(800, 600));
|
2007-08-06 13:22:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OnLoad
|
2007-08-04 14:09:58 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
public override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
2007-08-20 12:46:37 +02:00
|
|
|
|
if (!GL.SupportsExtension("VERSION_1_4"))
|
2007-08-06 13:22:18 +02:00
|
|
|
|
{
|
|
|
|
|
System.Windows.Forms.MessageBox.Show("You need at least OpenGL 1.4 to run this example. Aborting.", "VBOs not supported",
|
|
|
|
|
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
|
|
|
|
|
this.Exit();
|
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enable(GL.Enums.EnableCap.DEPTH_TEST);
|
|
|
|
|
GL.EnableClientState(GL.Enums.EnableCap.VERTEX_ARRAY);
|
|
|
|
|
GL.EnableClientState(GL.Enums.EnableCap.INDEX_ARRAY);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
LoadCube();
|
|
|
|
|
|
|
|
|
|
this.OnResize(new ResizeEventArgs(this.Width, this.Height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
#region OnResize
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-08-06 13:22:18 +02:00
|
|
|
|
base.OnResize(e);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-21 14:04:01 +02:00
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
double ratio = e.Width / (double)e.Height;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION);
|
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 14:09:58 +02:00
|
|
|
|
#region OnUpdateFrame
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
/// <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>
|
2007-09-23 00:01:43 +02:00
|
|
|
|
public override void OnUpdateFrame(UpdateFrameEventArgs e)
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-09-26 13:47:30 +02:00
|
|
|
|
if (Keyboard[OpenTK.Input.Key.Escape])
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-08-04 14:09:58 +02:00
|
|
|
|
this.Exit();
|
2007-07-23 02:15:18 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
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.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
#region OnRenderFrame
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-09-23 14:09:42 +02:00
|
|
|
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-08-06 13:22:18 +02:00
|
|
|
|
base.OnRenderFrame(e);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
GL.Clear(
|
|
|
|
|
GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT |
|
|
|
|
|
GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ARRAY_BUFFER, vbo);
|
|
|
|
|
GL.VertexPointer(3, GL.Enums.VertexPointerType.FLOAT, 0, 0);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-08-06 13:22:18 +02:00
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, ibo);
|
|
|
|
|
GL.IndexPointer(GL.Enums.IndexPointerType.FLOAT, 0, 0);
|
|
|
|
|
|
|
|
|
|
GL.Color3(1.0f, 1.0f, 1.0f);
|
|
|
|
|
GL.DrawElements(
|
|
|
|
|
GL.Enums.BeginMode.QUADS,
|
|
|
|
|
idata.Length,
|
|
|
|
|
GL.Enums.All.UNSIGNED_SHORT,
|
|
|
|
|
idata);
|
|
|
|
|
|
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ARRAY_BUFFER, 0);
|
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
|
|
Context.SwapBuffers();
|
2007-08-06 14:13:50 +02:00
|
|
|
|
Thread.Sleep(0);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region LoadCube
|
|
|
|
|
|
|
|
|
|
private void LoadCube()
|
|
|
|
|
{
|
|
|
|
|
int size; // To check whether the buffers were uploaded correctly.
|
2007-09-03 00:52:00 +02:00
|
|
|
|
//GL.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
// First, generate the buffer objects
|
|
|
|
|
GL.GenBuffers(1, out vbo);
|
|
|
|
|
GL.GenBuffers(1, out ibo);
|
|
|
|
|
|
|
|
|
|
// Upload the vertex data
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ARRAY_BUFFER, vbo);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
GL.BufferData(
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enums.VERSION_1_5.ARRAY_BUFFER,
|
2007-07-23 02:15:18 +02:00
|
|
|
|
(IntPtr)(vdata.Length * 4),
|
|
|
|
|
vdata,
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enums.VERSION_1_5.STATIC_DRAW);
|
2007-09-03 00:52:00 +02:00
|
|
|
|
GL.GetBufferParameter(
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enums.VERSION_1_5.ARRAY_BUFFER,
|
|
|
|
|
GL.Enums.VERSION_1_5.BUFFER_SIZE,
|
2007-07-23 02:15:18 +02:00
|
|
|
|
out size);
|
|
|
|
|
if (vdata.Length * 4 != size)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("Vertex array not uploaded correctly");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Upload the index data
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, ibo);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
GL.BufferData(
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER,
|
2007-07-23 02:15:18 +02:00
|
|
|
|
(IntPtr)(idata.Length * 2),
|
|
|
|
|
idata,
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enums.VERSION_1_5.STATIC_DRAW
|
2007-07-23 02:15:18 +02:00
|
|
|
|
);
|
2007-09-03 00:52:00 +02:00
|
|
|
|
GL.GetBufferParameter(
|
2007-08-01 11:32:49 +02:00
|
|
|
|
GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER,
|
|
|
|
|
GL.Enums.VERSION_1_5.BUFFER_SIZE,
|
2007-07-23 02:15:18 +02:00
|
|
|
|
out size);
|
|
|
|
|
if (idata.Length * 2 != size)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("Index array not uploaded correctly");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 02:14:31 +02:00
|
|
|
|
#region public void Launch()
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
2007-08-03 02:14:31 +02:00
|
|
|
|
public void Launch()
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-09-23 14:49:38 +02:00
|
|
|
|
Run(60.0, 60.0);
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|