Cleaned up sample, added color and motion.

This commit is contained in:
the_fiddler 2009-11-04 10:44:56 +00:00
parent 3ca21ed1b6
commit 74657d94f8

View file

@ -16,6 +16,7 @@ using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
using System.Drawing;
#endregion
@ -24,28 +25,54 @@ namespace Examples.Tutorial
[Example("Static Vertex Buffer Objects", ExampleCategory.OpenGL, "VBO", Documentation="VertexBufferObject")]
public class T08_VBO : GameWindow
{
#region --- Private Fields ---
const float rotation_speed = 180.0f;
float angle;
Shapes.Shape cube = new Examples.Shapes.Cube();
struct Vbo
{
public int VboID, EboID, NumElements;
}
struct Vbo { public int VboID, EboID, NumElements; }
Vbo[] vbo = new Vbo[2];
public static readonly int order = 8;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct VertexPositionColor
{
public Vector3 Position;
public uint Color;
#endregion
public VertexPositionColor(float x, float y, float z, Color color)
{
Position = new Vector3(x, y, z);
Color = ToRgba(color);
}
#region --- Constructor ---
static uint ToRgba(Color color)
{
return (uint)color.A << 24 | (uint)color.B << 16 | (uint)color.G << 8 | (uint)color.R;
}
}
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
};
public T08_VBO() : base(800, 600) { }
#endregion
#region OnLoad override
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
@ -60,24 +87,17 @@ namespace Examples.Tutorial
this.Exit();
}
GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f);
GL.ClearColor(System.Drawing.Color.MidnightBlue);
GL.Enable(EnableCap.DepthTest);
// Create the Vertex Buffer Object:
// 1) Generate the buffer handles.
// 2) Bind the Vertex Buffer and upload your vertex buffer. Check that the buffer was uploaded correctly.
// 3) Bind the Index Buffer and upload your index buffer. Check that the buffer was uploaded correctly.
vbo[0] = LoadVBO(cube.Vertices, cube.Indices);
vbo[1] = LoadVBO(cube.Vertices, cube.Indices);
vbo[0] = LoadVBO(CubeVertices, CubeElements);
vbo[1] = LoadVBO(CubeVertices, CubeElements);
}
#endregion
#region OnResize override
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, Width, Height);
float aspect_ratio = Width / (float)Height;
@ -86,27 +106,14 @@ namespace Examples.Tutorial
GL.LoadMatrix(ref perpective);
}
#endregion
#region OnUpdateFrame override
/// <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)
{
base.OnUpdateFrame(e);
if (Keyboard[OpenTK.Input.Key.Escape])
this.Exit();
}
#endregion
#region OnRenderFrame
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
@ -117,68 +124,65 @@ namespace Examples.Tutorial
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat);
GL.Color4(System.Drawing.Color.Black);
angle += rotation_speed * (float)e.Time;
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
Draw(vbo[0]);
SwapBuffers();
}
#endregion
Vbo LoadVBO(Vector3[] vertices, int[] indices)
Vbo LoadVBO<TVertex>(TVertex[] vertices, short[] elements) where TVertex : struct
{
Vbo handle = new Vbo();
int size;
// 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.
GL.GenBuffers(1, out handle.VboID);
GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices,
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices,
BufferUsageHint.StaticDraw);
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
if (vertices.Length * Vector3.SizeInBytes != size)
throw new ApplicationException("Vertex array not uploaded correctly");
//GL.BindBuffer(Version15.ArrayBuffer, 0);
if (vertices.Length * BlittableValueType.StrideOf(vertices) != size)
throw new ApplicationException("Vertex data not uploaded correctly");
GL.GenBuffers(1, out handle.EboID);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices,
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(elements.Length * sizeof(short)), elements,
BufferUsageHint.StaticDraw);
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
if (indices.Length * sizeof(int) != size)
throw new ApplicationException("Element array not uploaded correctly");
//GL.BindBuffer(Version15.ElementArrayBuffer, 0);
if (elements.Length * sizeof(short) != size)
throw new ApplicationException("Element data not uploaded correctly");
handle.NumElements = indices.Length;
handle.NumElements = elements.Length;
return handle;
}
void Draw(Vbo handle)
{
//GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit);
// 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).
//GL.EnableClientState(EnableCap.TextureCoordArray);
GL.EnableClientState(EnableCap.ColorArray);
GL.EnableClientState(EnableCap.VertexArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID);
//GL.TexCoordPointer(2, TexCoordPointerType.Float, vector2_size, (IntPtr)vector2_size);
GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero);
GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(CubeVertices), new IntPtr(0));
GL.ColorPointer(4, ColorPointerType.UnsignedByte, BlittableValueType.StrideOf(CubeVertices), new IntPtr(12));
GL.DrawElements(BeginMode.Triangles, handle.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero);
//GL.DrawArrays(BeginMode.LineLoop, 0, vbo.element_count);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
GL.DisableClientState(EnableCap.VertexArray);
//GL.DisableClientState(EnableCap.TextureCoordArray);
//GL.PopClientAttrib();
GL.DrawElements(BeginMode.Triangles, handle.NumElements, DrawElementsType.UnsignedShort, IntPtr.Zero);
}
#region public static void Main()
/// <summary>
/// Entry point of this example.
/// </summary>
@ -187,13 +191,9 @@ namespace Examples.Tutorial
{
using (T08_VBO example = new T08_VBO())
{
// 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);
Utilities.SetWindowTitle(example);
example.Run(30.0, 0.0);
}
}
#endregion
}
}