Opentk/Source/Examples/OpenGL/1.x/VBODynamic.cs

197 lines
7.7 KiB
C#
Raw Normal View History

using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace Examples.Tutorial
{
2010-10-02 18:52:34 +00:00
[Example("VBO Dynamic", ExampleCategory.OpenGL, "1.x", 4, Documentation = "VBODynamic")]
2010-10-09 19:59:06 +00:00
class T09_VBO_Dynamic : GameWindow
{
/// <summary>Creates a 800x600 window with the specified title.</summary>
2010-10-09 19:59:06 +00:00
public T09_VBO_Dynamic()
: base(800, 600)
{
this.VSync = VSyncMode.Off;
}
#region Particles
static int MaxParticleCount = 2000;
int VisibleParticleCount;
VertexC4ubV3f[] VBO = new VertexC4ubV3f[MaxParticleCount];
ParticleAttribut[] ParticleAttributes = new ParticleAttribut[MaxParticleCount];
// this struct is used for drawing
struct VertexC4ubV3f
{
public byte R, G, B, A;
public Vector3 Position;
public static int SizeInBytes = 16;
}
// this struct is used for updates
struct ParticleAttribut
{
public Vector3 Direction;
public uint Age;
// more stuff could be here: Rotation, Radius, whatever
}
uint VBOHandle;
2010-10-09 19:59:06 +00:00
#endregion Particles
/// <summary>Load resources here.</summary>
/// <param name="e">Not used.</param>
2010-10-09 19:59:06 +00:00
protected override void OnLoad(EventArgs e)
{
2010-10-09 19:59:06 +00:00
GL.ClearColor(.1f, 0f, .1f, 0f);
GL.Enable(EnableCap.DepthTest);
// Setup parameters for Points
2010-10-09 19:59:06 +00:00
GL.PointSize(5f);
GL.Enable(EnableCap.PointSmooth);
GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
// Setup VBO state
2010-10-09 19:59:06 +00:00
GL.EnableClientState(EnableCap.ColorArray);
GL.EnableClientState(EnableCap.VertexArray);
2010-10-09 19:59:06 +00:00
GL.GenBuffers(1, out VBOHandle);
// Since there's only 1 VBO in the app, might aswell setup here.
2010-10-09 19:59:06 +00:00
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOHandle);
GL.ColorPointer(4, ColorPointerType.UnsignedByte, VertexC4ubV3f.SizeInBytes, (IntPtr)0);
GL.VertexPointer(3, VertexPointerType.Float, VertexC4ubV3f.SizeInBytes, (IntPtr)(4 * sizeof(byte)));
2010-10-09 19:59:06 +00:00
Random rnd = new Random();
Vector3 temp = Vector3.Zero;
2010-10-09 19:59:06 +00:00
// generate some random stuff for the particle system
2010-10-09 19:59:06 +00:00
for (uint i = 0; i < MaxParticleCount; i++)
{
2010-10-09 19:59:06 +00:00
VBO[i].R = (byte)rnd.Next(0, 256);
VBO[i].G = (byte)rnd.Next(0, 256);
VBO[i].B = (byte)rnd.Next(0, 256);
VBO[i].A = (byte)rnd.Next(0, 256); // isn't actually used
VBO[i].Position = Vector3.Zero; // all particles are born at the origin
// generate direction vector in the range [-0.25f...+0.25f]
// that's slow enough so you can see particles 'disappear' when they are respawned
2010-10-09 19:59:06 +00:00
temp.X = (float)((rnd.NextDouble() - 0.5) * 0.5f);
temp.Y = (float)((rnd.NextDouble() - 0.5) * 0.5f);
temp.Z = (float)((rnd.NextDouble() - 0.5) * 0.5f);
ParticleAttributes[i].Direction = temp; // copy
ParticleAttributes[i].Age = 0;
}
VisibleParticleCount = 0;
}
protected override void OnUnload(EventArgs e)
{
2010-10-09 19:59:06 +00:00
GL.DeleteBuffers(1, ref VBOHandle);
}
/// <summary>
/// Called when your window is resized. Set your viewport here. It is also
/// a good place to set up your projection matrix (which probably changes
/// along when the aspect ratio of your window).
/// </summary>
/// <param name="e">Contains information on the new Width and Size of the GameWindow.</param>
protected override void OnResize(EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
GL.MatrixMode(MatrixMode.Projection);
Matrix4 p = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Width / (float)Height, 0.1f, 50.0f);
GL.LoadMatrix(ref p);
GL.MatrixMode(MatrixMode.Modelview);
Matrix4 mv = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY);
GL.LoadMatrix(ref mv);
}
/// <summary>
/// Called when it is time to setup the next frame. Add you game logic here.
/// </summary>
/// <param name="e">Contains timing information for framerate independent logic.</param>
2010-10-09 19:59:06 +00:00
protected override void OnUpdateFrame(FrameEventArgs e)
{
2010-10-09 19:59:06 +00:00
if (Keyboard[Key.Escape])
{
2010-10-09 19:59:06 +00:00
Exit();
}
// will update particles here. When using a Physics SDK, it's update rate is much higher than
// the framerate and it would be a waste of cycles copying to the VBO more often than drawing it.
2010-10-09 19:59:06 +00:00
if (VisibleParticleCount < MaxParticleCount)
VisibleParticleCount++;
Vector3 temp;
2010-10-09 19:59:06 +00:00
for (int i = MaxParticleCount - VisibleParticleCount; i < MaxParticleCount; i++)
{
if (ParticleAttributes[i].Age >= MaxParticleCount)
{
// reset particle
ParticleAttributes[i].Age = 0;
VBO[i].Position = Vector3.Zero;
2010-10-09 19:59:06 +00:00
}
else
{
ParticleAttributes[i].Age += (uint)Math.Max(ParticleAttributes[i].Direction.LengthFast * 10, 1);
2010-10-09 19:59:06 +00:00
Vector3.Multiply(ref ParticleAttributes[i].Direction, (float)e.Time, out temp);
Vector3.Add(ref VBO[i].Position, ref temp, out VBO[i].Position);
}
}
}
/// <summary>
/// Called when it is time to render the next frame. Add your rendering code here.
/// </summary>
/// <param name="e">Contains timing information.</param>
2010-10-09 19:59:06 +00:00
protected override void OnRenderFrame(FrameEventArgs e)
{
2010-10-09 19:59:06 +00:00
this.Title = VisibleParticleCount + " Points. FPS: " + string.Format("{0:F}", 1.0 / e.Time);
2010-10-09 19:59:06 +00:00
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
2010-10-09 19:59:06 +00:00
GL.PushMatrix();
2010-10-09 19:59:06 +00:00
GL.Translate(0f, 0f, -5f);
// Tell OpenGL to discard old VBO when done drawing it and reserve memory _now_ for a new buffer.
// without this, GL would wait until draw operations on old VBO are complete before writing to it
2010-10-09 19:59:06 +00:00
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(VertexC4ubV3f.SizeInBytes * MaxParticleCount), IntPtr.Zero, BufferUsageHint.StreamDraw);
// Fill newly allocated buffer
2010-10-09 19:59:06 +00:00
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(VertexC4ubV3f.SizeInBytes * MaxParticleCount), VBO, BufferUsageHint.StreamDraw);
// Only draw particles that are alive
2010-10-09 19:59:06 +00:00
GL.DrawArrays(BeginMode.Points, MaxParticleCount - VisibleParticleCount, VisibleParticleCount);
2010-10-09 19:59:06 +00:00
GL.PopMatrix();
2010-10-09 19:59:06 +00:00
SwapBuffers();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// The 'using' idiom guarantees proper resource cleanup.
// We request 30 UpdateFrame events per second, and unlimited
// RenderFrame events (as fast as the computer can handle).
using (T09_VBO_Dynamic example = new T09_VBO_Dynamic())
{
Utilities.SetWindowTitle(example);
example.Run(60.0, 0.0);
}
}
}
}