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.Text;
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.OpenGL;
|
|
|
|
|
using OpenTK.Platform;
|
2007-08-06 12:13:50 +00:00
|
|
|
|
using System.Threading;
|
2007-11-04 15:32:24 +00:00
|
|
|
|
using OpenTK.OpenGL.Enums;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
|
{
|
2007-11-04 15:32:24 +00:00
|
|
|
|
public class T08_VBO : GameWindow, IExample
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
#region --- Private Fields ---
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
Shapes.Shape shape = new Examples.Shapes.Cube();
|
|
|
|
|
//new Examples.Shapes.Plane(16, 16, 2.0f, 2.0f);
|
|
|
|
|
|
|
|
|
|
int handle_vertex_buffer, ibo, nbo; // vertex, index and normal buffer objects.
|
2007-07-23 00:15:18 +00:00
|
|
|
|
float angle;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructor ---
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
public T08_VBO() : base(new DisplayMode(800, 600), "OpenTK Tutorial 08: Vertex Buffer Objects") { }
|
2007-08-06 11:22:18 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
#region OnLoad override
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
public override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
2007-08-20 10:46:37 +00:00
|
|
|
|
if (!GL.SupportsExtension("VERSION_1_4"))
|
2007-08-06 11:22:18 +00: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 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
|
GL.EnableClientState(EnableCap.VertexArray);
|
|
|
|
|
//GL.EnableClientState(GL.Enums.EnableCap.INDEX_ARRAY);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
// Create the Vertex Buffer Object:
|
|
|
|
|
// 1) Generate the buffer handles.
|
|
|
|
|
// 2) Bind the Vertex Buffer and upload your vertex data. Check that the data was uploaded correctly.
|
|
|
|
|
// 3) Bind the Index Buffer and upload your index data. Check that the data was uploaded correctly.
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
int size; // To check whether the buffers were uploaded correctly.
|
|
|
|
|
|
|
|
|
|
// Generate handles.
|
|
|
|
|
GL.GenBuffers(1, out handle_vertex_buffer);
|
|
|
|
|
//GL.GenBuffers(1, out ibo);
|
|
|
|
|
|
|
|
|
|
// Upload the vertex data.
|
|
|
|
|
GL.BindBuffer(Version15.ArrayBuffer, handle_vertex_buffer);
|
|
|
|
|
GL.BufferData(Version15.ArrayBuffer, (IntPtr)shape.Vertices.Length, shape.Vertices, Version15.StaticDraw);
|
|
|
|
|
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero);
|
|
|
|
|
GL.GetBufferParameter(Version15.ArrayBuffer, Version15.BufferSize, out size);
|
|
|
|
|
if (shape.Vertices.Length != size)
|
|
|
|
|
throw new ApplicationException("Vertex array not uploaded correctly");
|
|
|
|
|
/*
|
|
|
|
|
// Upload the index data.
|
|
|
|
|
GL.BindBuffer(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, ibo);
|
|
|
|
|
GL.BufferData(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, (IntPtr)shape.Indices.Length, shape.Indices, GL.Enums.VERSION_1_5.STATIC_DRAW);
|
|
|
|
|
GL.GetBufferParameter(GL.Enums.VERSION_1_5.ELEMENT_ARRAY_BUFFER, GL.Enums.VERSION_1_5.BUFFER_SIZE, out size);
|
|
|
|
|
if (shape.Indices.Length != size)
|
|
|
|
|
throw new ApplicationException("Index array not uploaded correctly");
|
|
|
|
|
*/
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
#region OnResize override
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
protected override void OnResize(ResizeEventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-21 12:04:01 +00:00
|
|
|
|
GL.Viewport(0, 0, Width, Height);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-06 11:22:18 +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-08-06 11:22:18 +00:00
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.Perspective(45.0, ratio, 1.0, 64.0);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
#region OnUpdateFrame override
|
2007-07-23 00:15:18 +00: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-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-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
#region OnRenderFrame
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-23 12:09:42 +00:00
|
|
|
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-06 11:22:18 +00:00
|
|
|
|
base.OnRenderFrame(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);
|
|
|
|
|
GL.LoadIdentity();
|
|
|
|
|
Glu.LookAt(
|
|
|
|
|
0.0, 5.0, 5.0,
|
|
|
|
|
0.0, 0.0, 0.0,
|
|
|
|
|
0.0, 1.0, 0.0
|
|
|
|
|
);
|
2007-08-06 11:22:18 +00:00
|
|
|
|
|
|
|
|
|
GL.Color3(1.0f, 1.0f, 1.0f);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
GL.DrawElements(BeginMode.Triangles, shape.Indices.Length,
|
|
|
|
|
All.UnsignedInt, shape.Indices);
|
2007-08-06 11:22:18 +00:00
|
|
|
|
|
|
|
|
|
Context.SwapBuffers();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#region public void Launch()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches this example.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Provides a simple way for the example launcher to launch the examples.
|
|
|
|
|
/// </remarks>
|
2007-08-03 00:14:31 +00:00
|
|
|
|
public void Launch()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-09-23 12:49:38 +00:00
|
|
|
|
Run(60.0, 60.0);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-10-20 10:54:40 +00:00
|
|
|
|
|
|
|
|
|
public static readonly int order = 8;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|