Fixed code formatting.

This commit is contained in:
the_fiddler 2010-10-09 19:59:06 +00:00
parent 0f80ff8f1d
commit 148578b1eb
8 changed files with 137 additions and 136 deletions

View file

@ -43,7 +43,7 @@ using OpenTK.Graphics.OpenGL;
namespace Examples.Tutorial namespace Examples.Tutorial
{ {
[Example("Display Lists", ExampleCategory.OpenGL, "1.x", 2, Documentation="DisplayLists")] [Example("Display Lists", ExampleCategory.OpenGL, "1.x", 2, Documentation = "DisplayLists")]
public class T07_Display_Lists_Flower : GameWindow public class T07_Display_Lists_Flower : GameWindow
{ {
#region --- Fields --- #region --- Fields ---
@ -122,8 +122,8 @@ namespace Examples.Tutorial
float aspect = this.ClientSize.Width / (float)this.ClientSize.Height; float aspect = this.ClientSize.Width / (float)this.ClientSize.Height;
Matrix4 projection_matrix; Matrix4 projection_matrix;
Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspect, 1, 64, out projection_matrix); Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspect, 1, 64, out projection_matrix);
GL.MatrixMode(MatrixMode.Projection); GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection_matrix); GL.LoadMatrix(ref projection_matrix);

View file

@ -17,7 +17,7 @@ using OpenTK.Graphics.OpenGL;
namespace Examples.Tutorial namespace Examples.Tutorial
{ {
[Example("Framebuffer Objects", ExampleCategory.OpenGL, "1.x", Documentation="FramebufferObject")] [Example("Framebuffer Objects", ExampleCategory.OpenGL, "1.x", Documentation = "FramebufferObject")]
public class SimpleFBO : GameWindow public class SimpleFBO : GameWindow
{ {
public SimpleFBO() public SimpleFBO()
@ -26,7 +26,7 @@ namespace Examples.Tutorial
} }
Font sans = new Font(System.Drawing.FontFamily.GenericSansSerif, 16.0f); Font sans = new Font(System.Drawing.FontFamily.GenericSansSerif, 16.0f);
uint ColorTexture; uint ColorTexture;
uint DepthTexture; uint DepthTexture;
uint FBOHandle; uint FBOHandle;
@ -62,7 +62,7 @@ namespace Examples.Tutorial
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
Exit(); Exit();
} }
GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.DepthTest);
GL.ClearDepth(1.0f); GL.ClearDepth(1.0f);
GL.DepthFunc(DepthFunction.Lequal); GL.DepthFunc(DepthFunction.Lequal);
@ -217,7 +217,7 @@ namespace Examples.Tutorial
protected override void OnResize(EventArgs e) protected override void OnResize(EventArgs e)
{ {
GL.Viewport(0, 0, Width, Height); GL.Viewport(0, 0, Width, Height);
double aspect_ratio = Width / (double)Height; double aspect_ratio = Width / (double)Height;
OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64); OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64);
@ -242,7 +242,7 @@ namespace Examples.Tutorial
protected override void OnRenderFrame(FrameEventArgs e) protected override void OnRenderFrame(FrameEventArgs e)
{ {
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.PushMatrix(); GL.PushMatrix();
{ {
// Draw the Color Texture // Draw the Color Texture

View file

@ -25,7 +25,7 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Demonstrates immediate mode rendering. /// Demonstrates immediate mode rendering.
/// </summary> /// </summary>
[Example("Immediate mode", ExampleCategory.OpenGL, "1.x", 1, Documentation="ImmediateMode")] [Example("Immediate mode", ExampleCategory.OpenGL, "1.x", 1, Documentation = "ImmediateMode")]
public class T03_Immediate_Mode_Cube : GameWindow public class T03_Immediate_Mode_Cube : GameWindow
{ {
#region --- Fields --- #region --- Fields ---
@ -37,10 +37,11 @@ namespace Examples.Tutorial
#region --- Constructor --- #region --- Constructor ---
public T03_Immediate_Mode_Cube() : base(800, 600, new GraphicsMode(16, 16)) public T03_Immediate_Mode_Cube()
{ } : base(800, 600, new GraphicsMode(16, 16))
{ }
#endregion #endregion
#region OnLoad #region OnLoad
@ -114,7 +115,7 @@ namespace Examples.Tutorial
Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0); Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
GL.MatrixMode(MatrixMode.Modelview); GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat); GL.LoadMatrix(ref lookat);
angle += rotation_speed * (float)e.Time; angle += rotation_speed * (float)e.Time;
GL.Rotate(angle, 0.0f, 1.0f, 0.0f); GL.Rotate(angle, 0.0f, 1.0f, 0.0f);

View file

@ -14,7 +14,7 @@ namespace Examples.Tutorial
{ {
[Example("Stencil CSG", ExampleCategory.OpenGL, "1.x", Documentation = "StencilCSG")] [Example("Stencil CSG", ExampleCategory.OpenGL, "1.x", Documentation = "StencilCSG")]
partial class StencilCSG: GameWindow partial class StencilCSG : GameWindow
{ {
#region Model Related #region Model Related
DrawableShape OperandB; DrawableShape OperandB;
@ -31,11 +31,11 @@ namespace Examples.Tutorial
float CameraZoom; float CameraZoom;
float CameraRotX; float CameraRotX;
float CameraRotY; float CameraRotY;
Vector3 EyePosition = new Vector3( 0f, 0f, 15f ); Vector3 EyePosition = new Vector3(0f, 0f, 15f);
#region Window #region Window
public StencilCSG() public StencilCSG()
: base( 800, 600, new GraphicsMode( new ColorFormat( 8, 8, 8, 8 ), 24, 8 ) ) // request 8-bit stencil buffer : base(800, 600, new GraphicsMode(new ColorFormat(8, 8, 8, 8), 24, 8)) // request 8-bit stencil buffer
{ {
base.VSync = VSyncMode.Off; base.VSync = VSyncMode.Off;
Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e) Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
@ -48,10 +48,10 @@ namespace Examples.Tutorial
}; };
} }
protected override void OnResize(EventArgs e ) protected override void OnResize(EventArgs e)
{ {
GL.Viewport( 0, 0, Width, Height ); GL.Viewport(0, 0, Width, Height);
GL.MatrixMode( MatrixMode.Projection ); GL.MatrixMode(MatrixMode.Projection);
Matrix4 p = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Width / (float)Height, 0.1f, 64.0f); Matrix4 p = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Width / (float)Height, 0.1f, 64.0f);
GL.LoadMatrix(ref p); GL.LoadMatrix(ref p);
} }
@ -160,15 +160,15 @@ namespace Examples.Tutorial
protected override void OnUnload(EventArgs e) protected override void OnUnload(EventArgs e)
{ {
GL.DeleteTextures( 1, ref Texture ); GL.DeleteTextures(1, ref Texture);
OperandA.Dispose(); OperandA.Dispose();
OperandB.Dispose(); OperandB.Dispose();
base.OnUnload( e ); base.OnUnload(e);
} }
protected override void OnUpdateFrame( FrameEventArgs e ) protected override void OnUpdateFrame(FrameEventArgs e)
{ {
#region Magic numbers for camera #region Magic numbers for camera
CameraRotX = -Mouse.X * .5f; CameraRotX = -Mouse.X * .5f;
@ -180,117 +180,117 @@ namespace Examples.Tutorial
public void DrawOperandB() public void DrawOperandB()
{ {
GL.PushMatrix(); GL.PushMatrix();
GL.Translate( Math.Cos(MySphereXOffset), -1f, Math.Cos(MySphereZOffset) ); GL.Translate(Math.Cos(MySphereXOffset), -1f, Math.Cos(MySphereZOffset));
OperandB.Draw(); OperandB.Draw();
GL.PopMatrix(); GL.PopMatrix();
} }
public void DrawOperandA() public void DrawOperandA()
{ {
GL.Enable( EnableCap.Texture2D ); GL.Enable(EnableCap.Texture2D);
OperandA.Draw(); OperandA.Draw();
GL.Disable( EnableCap.Texture2D ); GL.Disable(EnableCap.Texture2D);
} }
public void RenderCsg() public void RenderCsg()
{ {
// first pass // first pass
GL.Disable( EnableCap.StencilTest ); GL.Disable(EnableCap.StencilTest);
GL.ColorMask( false, false, false, false ); GL.ColorMask(false, false, false, false);
GL.CullFace( CullFaceMode.Front ); GL.CullFace(CullFaceMode.Front);
DrawOperandB();// draw front-faces into depth buffer DrawOperandB();// draw front-faces into depth buffer
// use stencil plane to find parts of b in a // use stencil plane to find parts of b in a
GL.DepthMask( false ); GL.DepthMask(false);
GL.Enable( EnableCap.StencilTest ); GL.Enable(EnableCap.StencilTest);
GL.StencilFunc( StencilFunction.Always, 0, 0 ); GL.StencilFunc(StencilFunction.Always, 0, 0);
GL.StencilOp( StencilOp.Keep, StencilOp.Keep, StencilOp.Incr ); GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr);
GL.CullFace( CullFaceMode.Back ); GL.CullFace(CullFaceMode.Back);
DrawOperandA(); // increment the stencil where the front face of a is drawn DrawOperandA(); // increment the stencil where the front face of a is drawn
GL.StencilOp( StencilOp.Keep, StencilOp.Keep, StencilOp.Decr ); GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr);
GL.CullFace( CullFaceMode.Front ); GL.CullFace(CullFaceMode.Front);
DrawOperandA(); // decrement the stencil buffer where the back face of a is drawn DrawOperandA(); // decrement the stencil buffer where the back face of a is drawn
GL.DepthMask( true ); GL.DepthMask(true);
GL.Disable( EnableCap.DepthTest ); GL.Disable(EnableCap.DepthTest);
GL.ColorMask( true, true, true, true ); GL.ColorMask(true, true, true, true);
GL.StencilFunc( StencilFunction.Notequal, 0, 1 ); GL.StencilFunc(StencilFunction.Notequal, 0, 1);
DrawOperandB(); // draw the part of b that's in a DrawOperandB(); // draw the part of b that's in a
// fix depth // fix depth
GL.ColorMask( false, false, false, false ); GL.ColorMask(false, false, false, false);
GL.Enable( EnableCap.DepthTest ); GL.Enable(EnableCap.DepthTest);
GL.Disable( EnableCap.StencilTest ); GL.Disable(EnableCap.StencilTest);
GL.DepthFunc( DepthFunction.Always ); GL.DepthFunc(DepthFunction.Always);
DrawOperandA(); DrawOperandA();
GL.DepthFunc( DepthFunction.Less ); GL.DepthFunc(DepthFunction.Less);
// second pass // second pass
GL.CullFace( CullFaceMode.Back ); GL.CullFace(CullFaceMode.Back);
DrawOperandA(); DrawOperandA();
GL.DepthMask( false ); GL.DepthMask(false);
GL.Enable( EnableCap.StencilTest ); GL.Enable(EnableCap.StencilTest);
GL.StencilFunc( StencilFunction.Always, 0, 0 ); GL.StencilFunc(StencilFunction.Always, 0, 0);
GL.StencilOp( StencilOp.Keep, StencilOp.Keep, StencilOp.Incr ); GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr);
DrawOperandB(); // increment the stencil where the front face of b is drawn DrawOperandB(); // increment the stencil where the front face of b is drawn
GL.StencilOp( StencilOp.Keep, StencilOp.Keep, StencilOp.Decr ); GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr);
GL.CullFace( CullFaceMode.Front ); GL.CullFace(CullFaceMode.Front);
DrawOperandB(); // decrement the stencil buffer where the back face of b is drawn DrawOperandB(); // decrement the stencil buffer where the back face of b is drawn
GL.DepthMask( true ); GL.DepthMask(true);
GL.Disable( EnableCap.DepthTest ); GL.Disable(EnableCap.DepthTest);
GL.ColorMask( true, true, true, true ); GL.ColorMask(true, true, true, true);
GL.StencilFunc( StencilFunction.Equal, 0, 1 ); GL.StencilFunc(StencilFunction.Equal, 0, 1);
GL.CullFace( CullFaceMode.Back ); GL.CullFace(CullFaceMode.Back);
DrawOperandA(); // draw the part of a that's in b DrawOperandA(); // draw the part of a that's in b
GL.Enable( EnableCap.DepthTest ); GL.Enable(EnableCap.DepthTest);
} }
protected override void OnRenderFrame( FrameEventArgs e ) protected override void OnRenderFrame(FrameEventArgs e)
{ {
this.Title = WindowTitle + " FPS: " + ( 1f / e.Time ).ToString("0."); this.Title = WindowTitle + " FPS: " + (1f / e.Time).ToString("0.");
MySphereZOffset += (float)( e.Time * 3.1 ); MySphereZOffset += (float)(e.Time * 3.1);
MySphereXOffset += (float)( e.Time * 4.2 ); MySphereXOffset += (float)(e.Time * 4.2);
#region Transform setup #region Transform setup
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit ); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
// Camera // Camera
GL.MatrixMode( MatrixMode.Modelview ); GL.MatrixMode(MatrixMode.Modelview);
Matrix4 mv = Matrix4.LookAt( EyePosition, Vector3.Zero, Vector3.UnitY ); Matrix4 mv = Matrix4.LookAt(EyePosition, Vector3.Zero, Vector3.UnitY);
GL.LoadMatrix(ref mv); GL.LoadMatrix(ref mv);
GL.Translate( 0f, 0f, CameraZoom ); GL.Translate(0f, 0f, CameraZoom);
GL.Rotate( CameraRotX, Vector3.UnitY ); GL.Rotate(CameraRotX, Vector3.UnitY);
GL.Rotate( CameraRotY, Vector3.UnitX ); GL.Rotate(CameraRotY, Vector3.UnitX);
#endregion Transform setup #endregion Transform setup
RenderCsg(); RenderCsg();
// --------------------------------- // ---------------------------------
if ( ShowDebugWireFrame ) if (ShowDebugWireFrame)
{ {
GL.Color3(System.Drawing.Color.LightGray); GL.Color3(System.Drawing.Color.LightGray);
GL.Disable( EnableCap.StencilTest ); GL.Disable(EnableCap.StencilTest);
GL.Disable( EnableCap.Lighting ); GL.Disable(EnableCap.Lighting);
//GL.Disable( EnableCap.DepthTest ); //GL.Disable( EnableCap.DepthTest );
GL.PolygonMode( MaterialFace.Front, PolygonMode.Line ); GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
DrawOperandB(); DrawOperandB();
GL.PolygonMode( MaterialFace.Front, PolygonMode.Fill ); GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
GL.Enable( EnableCap.DepthTest ); GL.Enable(EnableCap.DepthTest);
GL.Enable( EnableCap.Lighting ); GL.Enable(EnableCap.Lighting);
GL.Enable( EnableCap.StencilTest ); GL.Enable(EnableCap.StencilTest);
} }
this.SwapBuffers(); this.SwapBuffers();
} }
@ -298,12 +298,11 @@ namespace Examples.Tutorial
[STAThread] [STAThread]
static void Main() static void Main()
{ {
using ( StencilCSG example = new StencilCSG() ) using (StencilCSG example = new StencilCSG())
{ {
Utilities.SetWindowTitle(example); Utilities.SetWindowTitle(example);
example.Run( 30.0, 0.0 ); example.Run(30.0, 0.0);
} }
} }
} }
} }

View file

@ -11,7 +11,7 @@ namespace Examples.Tutorial
{ {
[Example("Texture Matrix Wormhole", ExampleCategory.OpenGL, "1.x", Documentation = "TextureMatrix")] [Example("Texture Matrix Wormhole", ExampleCategory.OpenGL, "1.x", Documentation = "TextureMatrix")]
class TextureMatrix : GameWindow class TextureMatrix : GameWindow
{ {
@ -95,7 +95,7 @@ namespace Examples.Tutorial
ClearBufferMask.DepthBufferBit); ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Texture); GL.MatrixMode(MatrixMode.Texture);
GL.Translate(e.Time/2, -e.Time, 0f); GL.Translate(e.Time / 2, -e.Time, 0f);
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY); Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview); GL.MatrixMode(MatrixMode.Modelview);
@ -136,9 +136,9 @@ namespace Examples.Tutorial
Bitmap bitmap = new Bitmap(filename); Bitmap bitmap = new Bitmap(filename);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(Target, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); GL.TexImage2D(Target, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.Finish(); GL.Finish();
bitmap.UnlockBits(data); bitmap.UnlockBits(data);
if (GL.GetError() != ErrorCode.NoError) if (GL.GetError() != ErrorCode.NoError)
throw new Exception("Error loading texture " + filename); throw new Exception("Error loading texture " + filename);

View file

@ -22,7 +22,7 @@ namespace Examples.Tutorial
/// <summary> /// <summary>
/// Demonstrates simple OpenGL Texturing. /// Demonstrates simple OpenGL Texturing.
/// </summary> /// </summary>
[Example("Texture mapping", ExampleCategory.OpenGL, "1.x", 5, Documentation="Textures")] [Example("Texture mapping", ExampleCategory.OpenGL, "1.x", 5, Documentation = "Textures")]
public class Textures : GameWindow public class Textures : GameWindow
{ {
Bitmap bitmap = new Bitmap("Data/Textures/logo.jpg"); Bitmap bitmap = new Bitmap("Data/Textures/logo.jpg");
@ -42,7 +42,7 @@ namespace Examples.Tutorial
GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.Texture2D);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out texture); GL.GenTextures(1, out texture);
GL.BindTexture(TextureTarget.Texture2D, texture); GL.BindTexture(TextureTarget.Texture2D, texture);
@ -51,7 +51,7 @@ namespace Examples.Tutorial
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data); bitmap.UnlockBits(data);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);

View file

@ -9,11 +9,11 @@ namespace Examples.Tutorial
{ {
[Example("VBO Dynamic", ExampleCategory.OpenGL, "1.x", 4, Documentation = "VBODynamic")] [Example("VBO Dynamic", ExampleCategory.OpenGL, "1.x", 4, Documentation = "VBODynamic")]
class T09_VBO_Dynamic: GameWindow class T09_VBO_Dynamic : GameWindow
{ {
/// <summary>Creates a 800x600 window with the specified title.</summary> /// <summary>Creates a 800x600 window with the specified title.</summary>
public T09_VBO_Dynamic( ) public T09_VBO_Dynamic()
: base( 800, 600 ) : base(800, 600)
{ {
this.VSync = VSyncMode.Off; this.VSync = VSyncMode.Off;
} }
@ -42,48 +42,48 @@ namespace Examples.Tutorial
} }
uint VBOHandle; uint VBOHandle;
#endregion Particles #endregion Particles
/// <summary>Load resources here.</summary> /// <summary>Load resources here.</summary>
/// <param name="e">Not used.</param> /// <param name="e">Not used.</param>
protected override void OnLoad( EventArgs e ) protected override void OnLoad(EventArgs e)
{ {
GL.ClearColor( .1f, 0f, .1f, 0f ); GL.ClearColor(.1f, 0f, .1f, 0f);
GL.Enable( EnableCap.DepthTest ); GL.Enable(EnableCap.DepthTest);
// Setup parameters for Points // Setup parameters for Points
GL.PointSize( 5f ); GL.PointSize(5f);
GL.Enable( EnableCap.PointSmooth ); GL.Enable(EnableCap.PointSmooth);
GL.Hint( HintTarget.PointSmoothHint, HintMode.Nicest ); GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
// Setup VBO state // Setup VBO state
GL.EnableClientState( EnableCap.ColorArray ); GL.EnableClientState(EnableCap.ColorArray);
GL.EnableClientState( EnableCap.VertexArray ); GL.EnableClientState(EnableCap.VertexArray);
GL.GenBuffers( 1, out VBOHandle ); GL.GenBuffers(1, out VBOHandle);
// Since there's only 1 VBO in the app, might aswell setup here. // Since there's only 1 VBO in the app, might aswell setup here.
GL.BindBuffer( BufferTarget.ArrayBuffer, VBOHandle ); GL.BindBuffer(BufferTarget.ArrayBuffer, VBOHandle);
GL.ColorPointer( 4, ColorPointerType.UnsignedByte, VertexC4ubV3f.SizeInBytes, (IntPtr) 0 ); GL.ColorPointer(4, ColorPointerType.UnsignedByte, VertexC4ubV3f.SizeInBytes, (IntPtr)0);
GL.VertexPointer( 3, VertexPointerType.Float, VertexC4ubV3f.SizeInBytes, (IntPtr) (4*sizeof(byte)) ); GL.VertexPointer(3, VertexPointerType.Float, VertexC4ubV3f.SizeInBytes, (IntPtr)(4 * sizeof(byte)));
Random rnd = new Random( ); Random rnd = new Random();
Vector3 temp = Vector3.Zero; Vector3 temp = Vector3.Zero;
// generate some random stuff for the particle system // generate some random stuff for the particle system
for ( uint i = 0 ; i < MaxParticleCount ; i++ ) for (uint i = 0; i < MaxParticleCount; i++)
{ {
VBO[i].R = (byte) rnd.Next( 0, 256 ); VBO[i].R = (byte)rnd.Next(0, 256);
VBO[i].G = (byte) rnd.Next( 0, 256 ); VBO[i].G = (byte)rnd.Next(0, 256);
VBO[i].B = (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].A = (byte)rnd.Next(0, 256); // isn't actually used
VBO[i].Position = Vector3.Zero; // all particles are born at the origin VBO[i].Position = Vector3.Zero; // all particles are born at the origin
// generate direction vector in the range [-0.25f...+0.25f] // generate direction vector in the range [-0.25f...+0.25f]
// that's slow enough so you can see particles 'disappear' when they are respawned // that's slow enough so you can see particles 'disappear' when they are respawned
temp.X = (float) ( ( rnd.NextDouble( ) - 0.5 ) * 0.5f ); temp.X = (float)((rnd.NextDouble() - 0.5) * 0.5f);
temp.Y = (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 ); temp.Z = (float)((rnd.NextDouble() - 0.5) * 0.5f);
ParticleAttributes[i].Direction = temp; // copy ParticleAttributes[i].Direction = temp; // copy
ParticleAttributes[i].Age = 0; ParticleAttributes[i].Age = 0;
} }
@ -94,7 +94,7 @@ namespace Examples.Tutorial
protected override void OnUnload(EventArgs e) protected override void OnUnload(EventArgs e)
{ {
GL.DeleteBuffers( 1, ref VBOHandle ); GL.DeleteBuffers(1, ref VBOHandle);
} }
/// <summary> /// <summary>
@ -120,32 +120,33 @@ namespace Examples.Tutorial
/// Called when it is time to setup the next frame. Add you game logic here. /// Called when it is time to setup the next frame. Add you game logic here.
/// </summary> /// </summary>
/// <param name="e">Contains timing information for framerate independent logic.</param> /// <param name="e">Contains timing information for framerate independent logic.</param>
protected override void OnUpdateFrame( FrameEventArgs e ) protected override void OnUpdateFrame(FrameEventArgs e)
{ {
if ( Keyboard[Key.Escape] ) if (Keyboard[Key.Escape])
{ {
Exit( ); Exit();
} }
// will update particles here. When using a Physics SDK, it's update rate is much higher than // 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. // the framerate and it would be a waste of cycles copying to the VBO more often than drawing it.
if ( VisibleParticleCount < MaxParticleCount ) if (VisibleParticleCount < MaxParticleCount)
VisibleParticleCount++; VisibleParticleCount++;
Vector3 temp; Vector3 temp;
for ( int i = MaxParticleCount - VisibleParticleCount ; i < MaxParticleCount ; i++ ) for (int i = MaxParticleCount - VisibleParticleCount; i < MaxParticleCount; i++)
{ {
if (ParticleAttributes[i].Age >= MaxParticleCount) if (ParticleAttributes[i].Age >= MaxParticleCount)
{ {
// reset particle // reset particle
ParticleAttributes[i].Age = 0; ParticleAttributes[i].Age = 0;
VBO[i].Position = Vector3.Zero; VBO[i].Position = Vector3.Zero;
} else }
else
{ {
ParticleAttributes[i].Age += (uint)Math.Max(ParticleAttributes[i].Direction.LengthFast * 10, 1); ParticleAttributes[i].Age += (uint)Math.Max(ParticleAttributes[i].Direction.LengthFast * 10, 1);
Vector3.Multiply( ref ParticleAttributes[i].Direction, (float) e.Time, out temp ); Vector3.Multiply(ref ParticleAttributes[i].Direction, (float)e.Time, out temp);
Vector3.Add( ref VBO[i].Position, ref temp, out VBO[i].Position ); Vector3.Add(ref VBO[i].Position, ref temp, out VBO[i].Position);
} }
} }
} }
@ -154,27 +155,27 @@ namespace Examples.Tutorial
/// Called when it is time to render the next frame. Add your rendering code here. /// Called when it is time to render the next frame. Add your rendering code here.
/// </summary> /// </summary>
/// <param name="e">Contains timing information.</param> /// <param name="e">Contains timing information.</param>
protected override void OnRenderFrame( FrameEventArgs e ) protected override void OnRenderFrame(FrameEventArgs e)
{ {
this.Title = VisibleParticleCount + " Points. FPS: " + string.Format( "{0:F}", 1.0 / e.Time ); this.Title = VisibleParticleCount + " Points. FPS: " + string.Format("{0:F}", 1.0 / e.Time);
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit ); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.PushMatrix( ); GL.PushMatrix();
GL.Translate( 0f, 0f, -5f ); GL.Translate(0f, 0f, -5f);
// Tell OpenGL to discard old VBO when done drawing it and reserve memory _now_ for a new buffer. // 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 // without this, GL would wait until draw operations on old VBO are complete before writing to it
GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr) ( VertexC4ubV3f.SizeInBytes * MaxParticleCount ), IntPtr.Zero, BufferUsageHint.StreamDraw ); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(VertexC4ubV3f.SizeInBytes * MaxParticleCount), IntPtr.Zero, BufferUsageHint.StreamDraw);
// Fill newly allocated buffer // Fill newly allocated buffer
GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr) ( VertexC4ubV3f.SizeInBytes * MaxParticleCount ), VBO, BufferUsageHint.StreamDraw ); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(VertexC4ubV3f.SizeInBytes * MaxParticleCount), VBO, BufferUsageHint.StreamDraw);
// Only draw particles that are alive // Only draw particles that are alive
GL.DrawArrays( BeginMode.Points, MaxParticleCount - VisibleParticleCount, VisibleParticleCount ); GL.DrawArrays(BeginMode.Points, MaxParticleCount - VisibleParticleCount, VisibleParticleCount);
GL.PopMatrix( ); GL.PopMatrix();
SwapBuffers( ); SwapBuffers();
} }
/// <summary> /// <summary>

View file

@ -22,15 +22,15 @@ using System.Drawing;
namespace Examples.Tutorial namespace Examples.Tutorial
{ {
[Example("VBO Static", ExampleCategory.OpenGL, "1.x", 3, Documentation="VBOStatic")] [Example("VBO Static", ExampleCategory.OpenGL, "1.x", 3, Documentation = "VBOStatic")]
public class T08_VBO : GameWindow public class T08_VBO : GameWindow
{ {
const float rotation_speed = 180.0f; const float rotation_speed = 180.0f;
float angle; float angle;
struct Vbo { public int VboID, EboID, NumElements; } struct Vbo { public int VboID, EboID, NumElements; }
Vbo[] vbo = new Vbo[2]; Vbo[] vbo = new Vbo[2];
VertexPositionColor[] CubeVertices = new VertexPositionColor[] VertexPositionColor[] CubeVertices = new VertexPositionColor[]
{ {
new VertexPositionColor(-1.0f, -1.0f, 1.0f, Color.DarkRed), new VertexPositionColor(-1.0f, -1.0f, 1.0f, Color.DarkRed),