Fixed line endings.

This commit is contained in:
the_fiddler 2009-02-12 23:43:06 +00:00
parent d79a9ca5d3
commit 8919350730

View file

@ -1,298 +1,298 @@
#region --- License --- #region --- License ---
/* Licensed under the MIT/X11 license. /* Licensed under the MIT/X11 license.
* Copyright (c) 2006-2008 the OpenTK Team. * Copyright (c) 2006-2008 the OpenTK Team.
* This notice may not be removed from any source distribution. * This notice may not be removed from any source distribution.
* See license.txt for licensing details. * See license.txt for licensing details.
*/ */
#endregion #endregion
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using OpenTK; using OpenTK;
using OpenTK.Input; using OpenTK.Input;
using OpenTK.Math; using OpenTK.Math;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics.OpenGL;
namespace Examples.Tutorial namespace Examples.Tutorial
{ {
[Example("Simple FrameBuffer Object.", ExampleCategory.OpenGL)] [Example("Simple FrameBuffer Object.", ExampleCategory.OpenGL)]
public class SimpleFBO : GameWindow public class SimpleFBO : GameWindow
{ {
public SimpleFBO() public SimpleFBO()
: base(800, 600) : base(800, 600)
{ {
} }
TextureFont sans = new TextureFont(new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 14.0f)); TextureFont sans = new TextureFont(new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 14.0f));
ITextPrinter text = new TextPrinter(); ITextPrinter text = new TextPrinter();
uint ColorTexture; uint ColorTexture;
uint DepthTexture; uint DepthTexture;
uint FBOHandle; uint FBOHandle;
const int TextureSize = 512; const int TextureSize = 512;
#region Randoms #region Randoms
Random rnd = new Random(); Random rnd = new Random();
public const float scale = 3f; public const float scale = 3f;
/// <summary>Returns a random Float in the range [-0.5*scale..+0.5*scale]</summary> /// <summary>Returns a random Float in the range [-0.5*scale..+0.5*scale]</summary>
public float GetRandom() public float GetRandom()
{ {
return (float)(rnd.NextDouble() - 0.5) * scale; return (float)(rnd.NextDouble() - 0.5) * scale;
} }
/// <summary>Returns a random Float in the range [0..1]</summary> /// <summary>Returns a random Float in the range [0..1]</summary>
public float GetRandom0to1() public float GetRandom0to1()
{ {
return (float)rnd.NextDouble(); return (float)rnd.NextDouble();
} }
#endregion Randoms #endregion Randoms
public override void OnLoad(EventArgs e) public override void OnLoad(EventArgs e)
{ {
GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.DepthTest);
GL.ClearDepth(1.0f); GL.ClearDepth(1.0f);
GL.DepthFunc(DepthFunction.Lequal); GL.DepthFunc(DepthFunction.Lequal);
GL.Disable(EnableCap.CullFace); GL.Disable(EnableCap.CullFace);
GL.PolygonMode(MaterialFace.Back, PolygonMode.Line); GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);
// Create Color Tex // Create Color Tex
GL.GenTextures(1, out ColorTexture); GL.GenTextures(1, out ColorTexture);
GL.BindTexture(TextureTarget.Texture2D, ColorTexture); GL.BindTexture(TextureTarget.Texture2D, ColorTexture);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, TextureSize, TextureSize, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, TextureSize, TextureSize, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
// GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D ); // GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D );
// Create Depth Tex // Create Depth Tex
GL.GenTextures(1, out DepthTexture); GL.GenTextures(1, out DepthTexture);
GL.BindTexture(TextureTarget.Texture2D, DepthTexture); GL.BindTexture(TextureTarget.Texture2D, DepthTexture);
GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)All.DepthComponent32, TextureSize, TextureSize, 0, PixelFormat.DepthComponent, PixelType.UnsignedInt, IntPtr.Zero); GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)All.DepthComponent32, TextureSize, TextureSize, 0, PixelFormat.DepthComponent, PixelType.UnsignedInt, IntPtr.Zero);
// things go horribly wrong if DepthComponent's Bitcount does not match the main Framebuffer's Depth // things go horribly wrong if DepthComponent's Bitcount does not match the main Framebuffer's Depth
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
// GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D ); // GL.Ext.GenerateMipmap( GenerateMipmapTarget.Texture2D );
// Create a FBO and attach the textures // Create a FBO and attach the textures
GL.Ext.GenFramebuffers(1, out FBOHandle); GL.Ext.GenFramebuffers(1, out FBOHandle);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FBOHandle); GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FBOHandle);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture, 0); GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture, 0);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, TextureTarget.Texture2D, DepthTexture, 0); GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, TextureTarget.Texture2D, DepthTexture, 0);
#region Test for Error #region Test for Error
switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt)) switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
{ {
case FramebufferErrorCode.FramebufferCompleteExt: case FramebufferErrorCode.FramebufferCompleteExt:
{ {
Console.WriteLine("FBO: The framebuffer is complete and valid for rendering."); Console.WriteLine("FBO: The framebuffer is complete and valid for rendering.");
break; break;
} }
case FramebufferErrorCode.FramebufferIncompleteAttachmentExt: case FramebufferErrorCode.FramebufferIncompleteAttachmentExt:
{ {
Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean theres no texture attached or the format isnt renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume."); Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean theres no texture attached or the format isnt renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume.");
break; break;
} }
case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt: case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt:
{ {
Console.WriteLine("FBO: There are no attachments."); Console.WriteLine("FBO: There are no attachments.");
break; break;
} }
/* case FramebufferErrorCode.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT: /* case FramebufferErrorCode.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
{ {
Console.WriteLine("FBO: An object has been attached to more than one attachment point."); Console.WriteLine("FBO: An object has been attached to more than one attachment point.");
break; break;
}*/ }*/
case FramebufferErrorCode.FramebufferIncompleteDimensionsExt: case FramebufferErrorCode.FramebufferIncompleteDimensionsExt:
{ {
Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height."); Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height.");
break; break;
} }
case FramebufferErrorCode.FramebufferIncompleteFormatsExt: case FramebufferErrorCode.FramebufferIncompleteFormatsExt:
{ {
Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format."); Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format.");
break; break;
} }
case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt: case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt:
{ {
Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesnt have an attachment."); Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesnt have an attachment.");
break; break;
} }
case FramebufferErrorCode.FramebufferIncompleteReadBufferExt: case FramebufferErrorCode.FramebufferIncompleteReadBufferExt:
{ {
Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesnt have an attachment."); Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesnt have an attachment.");
break; break;
} }
case FramebufferErrorCode.FramebufferUnsupportedExt: case FramebufferErrorCode.FramebufferUnsupportedExt:
{ {
Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation."); Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation.");
break; break;
} }
default: default:
{ {
Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)"); Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)");
break; break;
} }
} }
// using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering // using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
int[] queryinfo = new int[6]; int[] queryinfo = new int[6];
GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]); GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]); GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]); GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
GL.GetInteger(GetPName.Stereo, out queryinfo[3]); GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
GL.GetInteger(GetPName.Samples, out queryinfo[4]); GL.GetInteger(GetPName.Samples, out queryinfo[4]);
GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]); GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] + Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
"\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]); "\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);
Console.WriteLine("Last GL Error: " + GL.GetError()); Console.WriteLine("Last GL Error: " + GL.GetError());
#endregion Test for Error #endregion Test for Error
GL.PushAttrib(AttribMask.ViewportBit); GL.PushAttrib(AttribMask.ViewportBit);
{ {
GL.Viewport(0, 0, TextureSize, TextureSize); GL.Viewport(0, 0, TextureSize, TextureSize);
// clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer // clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
GL.ClearColor(1f, 0f, 0f, 0f); GL.ClearColor(1f, 0f, 0f, 0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// smack 50 random triangles into the FBO's textures // smack 50 random triangles into the FBO's textures
GL.Begin(BeginMode.Triangles); GL.Begin(BeginMode.Triangles);
{ {
for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
{ {
GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1()); GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1());
GL.Vertex3(GetRandom(), GetRandom(), GetRandom()); GL.Vertex3(GetRandom(), GetRandom(), GetRandom());
GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1()); GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1());
GL.Vertex3(GetRandom(), GetRandom(), GetRandom()); GL.Vertex3(GetRandom(), GetRandom(), GetRandom());
GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1()); GL.Color3(GetRandom0to1(), GetRandom0to1(), GetRandom0to1());
GL.Vertex3(GetRandom(), GetRandom(), GetRandom()); GL.Vertex3(GetRandom(), GetRandom(), GetRandom());
} }
} }
GL.End(); GL.End();
} }
GL.PopAttrib(); GL.PopAttrib();
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO
GL.ClearColor(.1f, .2f, .3f, 0f); GL.ClearColor(.1f, .2f, .3f, 0f);
GL.Color3(1f, 1f, 1f); GL.Color3(1f, 1f, 1f);
GL.Enable(EnableCap.Texture2D); // enable Texture Mapping GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture
} }
public override void OnUnload(EventArgs e) public override void OnUnload(EventArgs e)
{ {
// Clean up what we allocated before exiting // Clean up what we allocated before exiting
GL.DeleteTextures(1, ref ColorTexture); GL.DeleteTextures(1, ref ColorTexture);
GL.DeleteTextures(1, ref DepthTexture); GL.DeleteTextures(1, ref DepthTexture);
GL.Ext.DeleteFramebuffers(1, ref FBOHandle); GL.Ext.DeleteFramebuffers(1, ref FBOHandle);
base.OnUnload(e); base.OnUnload(e);
} }
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{ {
GL.Viewport(0, 0, Width, Height); GL.Viewport(0, 0, Width, Height);
GL.MatrixMode(MatrixMode.Projection); GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity(); GL.LoadIdentity();
Glu.Perspective(60.0, Width / (double)Height, 1.0, 5.0); Glu.Perspective(60.0, Width / (double)Height, 1.0, 5.0);
GL.MatrixMode(MatrixMode.Modelview); GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity(); GL.LoadIdentity();
Glu.LookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); Glu.LookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
base.OnResize(e); base.OnResize(e);
} }
public override void OnUpdateFrame(UpdateFrameEventArgs e) public override void OnUpdateFrame(UpdateFrameEventArgs e)
{ {
base.OnUpdateFrame(e); base.OnUpdateFrame(e);
if (Keyboard[Key.Space]) if (Keyboard[Key.Space])
{ {
ErrorCode err = GL.GetError(); ErrorCode err = GL.GetError();
Console.WriteLine(err + " " + Glu.ErrorString((GluErrorCode)err)); Console.WriteLine(err + " " + Glu.ErrorString((GluErrorCode)err));
} }
if (Keyboard[Key.Escape]) if (Keyboard[Key.Escape])
this.Exit(); this.Exit();
} }
public override void OnRenderFrame(RenderFrameEventArgs e) public override void OnRenderFrame(RenderFrameEventArgs e)
{ {
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
text.Begin(); text.Begin();
text.Draw((1.0 / e.Time).ToString("F2"), sans); text.Draw((1.0 / e.Time).ToString("F2"), sans);
text.End(); text.End();
GL.PushMatrix(); GL.PushMatrix();
{ {
// Draw the Color Texture // Draw the Color Texture
GL.Translate(-1.1f, 0f, 0f); GL.Translate(-1.1f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, ColorTexture); GL.BindTexture(TextureTarget.Texture2D, ColorTexture);
GL.Begin(BeginMode.Quads); GL.Begin(BeginMode.Quads);
{ {
GL.TexCoord2(0f, 1f); GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f); GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f); GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f); GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f); GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f); GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f); GL.Vertex2(1.0f, 1.0f);
} }
GL.End(); GL.End();
// Draw the Depth Texture // Draw the Depth Texture
GL.Translate(+2.2f, 0f, 0f); GL.Translate(+2.2f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, DepthTexture); GL.BindTexture(TextureTarget.Texture2D, DepthTexture);
GL.Begin(BeginMode.Quads); GL.Begin(BeginMode.Quads);
{ {
GL.TexCoord2(0f, 1f); GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f); GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f); GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f); GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f); GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f); GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f); GL.Vertex2(1.0f, 1.0f);
} }
GL.End(); GL.End();
} }
GL.PopMatrix(); GL.PopMatrix();
this.SwapBuffers(); this.SwapBuffers();
} }
#region public static void Main() #region public static void Main()
/// <summary> /// <summary>
/// Entry point of this example. /// Entry point of this example.
/// </summary> /// </summary>
[STAThread] [STAThread]
public static void Main() public static void Main()
{ {
using (SimpleFBO example = new SimpleFBO()) using (SimpleFBO example = new SimpleFBO())
{ {
Utilities.SetWindowTitle(example); Utilities.SetWindowTitle(example);
example.Run(30.0, 0.0); example.Run(30.0, 0.0);
} }
} }
#endregion #endregion
} }
} }