2009-10-24 10:07:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
using OpenTK.Input;
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
|
|
|
using Examples.Shapes;
|
|
|
|
|
|
|
|
namespace Examples.Tutorial
|
|
|
|
{
|
|
|
|
|
2010-10-02 18:52:34 +00:00
|
|
|
[Example("Stencil CSG", ExampleCategory.OpenGL, "1.x", Documentation = "StencilCSG")]
|
2010-10-09 19:59:06 +00:00
|
|
|
partial class StencilCSG : GameWindow
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
|
|
|
#region Model Related
|
|
|
|
DrawableShape OperandB;
|
|
|
|
DrawableShape OperandA;
|
|
|
|
float MySphereZOffset = 0f;
|
|
|
|
float MySphereXOffset = 0f;
|
|
|
|
|
|
|
|
int Texture;
|
|
|
|
#endregion Model Related
|
|
|
|
|
|
|
|
string WindowTitle;
|
|
|
|
bool ShowDebugWireFrame = true;
|
|
|
|
|
|
|
|
float CameraZoom;
|
|
|
|
float CameraRotX;
|
|
|
|
float CameraRotY;
|
2010-10-09 19:59:06 +00:00
|
|
|
Vector3 EyePosition = new Vector3(0f, 0f, 15f);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
|
|
|
#region Window
|
|
|
|
public StencilCSG()
|
2010-10-09 19:59:06 +00:00
|
|
|
: base(800, 600, new GraphicsMode(new ColorFormat(8, 8, 8, 8), 24, 8)) // request 8-bit stencil buffer
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
|
|
|
base.VSync = VSyncMode.Off;
|
2009-11-04 20:48:31 +00:00
|
|
|
Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Escape: this.Exit(); break;
|
|
|
|
case Key.Space: ShowDebugWireFrame = !ShowDebugWireFrame; break;
|
|
|
|
}
|
|
|
|
};
|
2009-10-24 10:07:43 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
protected override void OnResize(EventArgs e)
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Viewport(0, 0, Width, Height);
|
|
|
|
GL.MatrixMode(MatrixMode.Projection);
|
2009-11-04 20:48:31 +00:00
|
|
|
Matrix4 p = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Width / (float)Height, 0.1f, 64.0f);
|
2009-10-24 10:07:43 +00:00
|
|
|
GL.LoadMatrix(ref p);
|
|
|
|
}
|
|
|
|
#endregion Window
|
|
|
|
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
|
|
{
|
|
|
|
#region Abort on platforms which will not be able to execute the ops properly
|
|
|
|
/*
|
|
|
|
if (!GL.SupportsExtension("VERSION_1_2"))
|
|
|
|
{
|
|
|
|
Trace.WriteLine("Aborting. OpenGL 1.2 or later required.");
|
|
|
|
this.Exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] t = new int[2];
|
|
|
|
GL.GetInteger(GetPName.MajorVersion, out t[0]);
|
|
|
|
GL.GetInteger(GetPName.MinorVersion, out t[1]);
|
|
|
|
Trace.WriteLine("OpenGL Context Version: " + t[0] + "." + t[1]);
|
|
|
|
|
|
|
|
GL.GetInteger(GetPName.DepthBits, out t[0]);
|
|
|
|
Trace.WriteLine("Depth Bits: " + t[0]);
|
|
|
|
GL.GetInteger(GetPName.StencilBits, out t[1]);
|
|
|
|
Trace.WriteLine("Stencil Bits: " + t[1]);
|
|
|
|
|
|
|
|
if (t[0] < 16)
|
|
|
|
{
|
|
|
|
Trace.WriteLine("Aborting. Need at least 16 depth bits, only " + t[0] + " available.");
|
|
|
|
this.Exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t[1] < 1)
|
|
|
|
{
|
|
|
|
Trace.WriteLine("Aborting. Need at least 1 stencil bit, only " + t[1] + " available.");
|
|
|
|
this.Exit();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
#endregion Abort on platforms which will not be able to execute the ops properly
|
|
|
|
|
|
|
|
WindowTitle = "Cube-Sphere Stencil CSG " + GL.GetString(StringName.Renderer) + " (GL " + GL.GetString(StringName.Version) + ")";
|
|
|
|
|
|
|
|
#region GL States
|
|
|
|
GL.ClearColor(.08f, .12f, .16f, 1f);
|
|
|
|
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
GL.DepthFunc(DepthFunction.Less);
|
|
|
|
GL.ClearDepth(1.0);
|
|
|
|
|
|
|
|
GL.Enable(EnableCap.StencilTest);
|
|
|
|
GL.ClearStencil(0);
|
|
|
|
GL.StencilMask(0xFFFFFFFF); // read&write
|
|
|
|
|
|
|
|
GL.Enable(EnableCap.CullFace);
|
|
|
|
GL.FrontFace(FrontFaceDirection.Ccw);
|
|
|
|
GL.CullFace(CullFaceMode.Back);
|
|
|
|
|
|
|
|
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
|
|
|
|
|
|
|
|
GL.Color4(1f, 1f, 1f, 1f);
|
|
|
|
|
|
|
|
GL.Enable(EnableCap.Lighting);
|
|
|
|
GL.Enable(EnableCap.Light0);
|
|
|
|
GL.ShadeModel(ShadingModel.Smooth);
|
|
|
|
|
|
|
|
#endregion GL States
|
|
|
|
|
|
|
|
#region Load Texture
|
|
|
|
Bitmap bitmap = new Bitmap("Data/Textures/logo-dark.jpg");
|
|
|
|
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
|
|
|
|
|
|
|
GL.GenTextures(1, out Texture);
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Texture);
|
|
|
|
|
2009-11-02 09:37:18 +00:00
|
|
|
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
2009-10-24 10:07:43 +00:00
|
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
|
|
|
|
GL.Finish();
|
|
|
|
bitmap.UnlockBits(data);
|
|
|
|
#endregion Load Texture
|
|
|
|
|
|
|
|
OperandA = new ChamferCube(1.5, 2.0, 2.5, ChamferCube.SubDivs.Four, 0.42, true);
|
|
|
|
OperandB = new SlicedSphere(2.0f, Vector3d.Zero,
|
|
|
|
SlicedSphere.eSubdivisions.Three,
|
|
|
|
new SlicedSphere.eDir[] { SlicedSphere.eDir.All },
|
|
|
|
true);
|
|
|
|
|
|
|
|
#region Invert Operand B's Normals
|
|
|
|
// only the inside of the operand is ever drawn to color buffers and lighting requires this.
|
|
|
|
BeginMode tempPrimMode;
|
|
|
|
VertexT2dN3dV3d[] tempVertices;
|
|
|
|
uint[] tempIndices;
|
|
|
|
|
|
|
|
OperandB.GetArraysforVBO(out tempPrimMode, out tempVertices, out tempIndices);
|
|
|
|
OperandB.Dispose();
|
|
|
|
|
|
|
|
for (int i = 0; i < tempVertices.Length; i++)
|
|
|
|
{
|
2009-11-04 20:48:31 +00:00
|
|
|
tempVertices[i].Normal *= -1.0;
|
2009-10-24 10:07:43 +00:00
|
|
|
tempVertices[i].Normal.Normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
OperandB = new VboShape(ref tempPrimMode, ref tempVertices, ref tempIndices, true);
|
|
|
|
#endregion Invert Operand B's Normals
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnUnload(EventArgs e)
|
|
|
|
{
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.DeleteTextures(1, ref Texture);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
|
|
|
OperandA.Dispose();
|
|
|
|
OperandB.Dispose();
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
base.OnUnload(e);
|
2009-10-24 10:07:43 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
|
|
|
#region Magic numbers for camera
|
|
|
|
CameraRotX = -Mouse.X * .5f;
|
|
|
|
CameraRotY = Mouse.Y * .5f;
|
|
|
|
CameraZoom = Mouse.Wheel * .2f;
|
|
|
|
#endregion Magic numbers for camera
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawOperandB()
|
|
|
|
{
|
|
|
|
GL.PushMatrix();
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Translate(Math.Cos(MySphereXOffset), -1f, Math.Cos(MySphereZOffset));
|
2009-10-24 10:07:43 +00:00
|
|
|
OperandB.Draw();
|
|
|
|
GL.PopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawOperandA()
|
|
|
|
{
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Enable(EnableCap.Texture2D);
|
2009-10-24 10:07:43 +00:00
|
|
|
OperandA.Draw();
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Disable(EnableCap.Texture2D);
|
2009-10-24 10:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RenderCsg()
|
|
|
|
{
|
|
|
|
// first pass
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Disable(EnableCap.StencilTest);
|
|
|
|
|
|
|
|
GL.ColorMask(false, false, false, false);
|
|
|
|
GL.CullFace(CullFaceMode.Front);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandB();// draw front-faces into depth buffer
|
|
|
|
|
|
|
|
// use stencil plane to find parts of b in a
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.DepthMask(false);
|
|
|
|
GL.Enable(EnableCap.StencilTest);
|
|
|
|
GL.StencilFunc(StencilFunction.Always, 0, 0);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr);
|
|
|
|
GL.CullFace(CullFaceMode.Back);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandA(); // increment the stencil where the front face of a is drawn
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr);
|
|
|
|
GL.CullFace(CullFaceMode.Front);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandA(); // decrement the stencil buffer where the back face of a is drawn
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.DepthMask(true);
|
|
|
|
GL.Disable(EnableCap.DepthTest);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.ColorMask(true, true, true, true);
|
|
|
|
GL.StencilFunc(StencilFunction.Notequal, 0, 1);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandB(); // draw the part of b that's in a
|
|
|
|
|
|
|
|
// fix depth
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.ColorMask(false, false, false, false);
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
GL.Disable(EnableCap.StencilTest);
|
|
|
|
GL.DepthFunc(DepthFunction.Always);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandA();
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.DepthFunc(DepthFunction.Less);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
|
|
|
// second pass
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.CullFace(CullFaceMode.Back);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandA();
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.DepthMask(false);
|
|
|
|
GL.Enable(EnableCap.StencilTest);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.StencilFunc(StencilFunction.Always, 0, 0);
|
|
|
|
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandB(); // increment the stencil where the front face of b is drawn
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr);
|
|
|
|
GL.CullFace(CullFaceMode.Front);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandB(); // decrement the stencil buffer where the back face of b is drawn
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.DepthMask(true);
|
|
|
|
GL.Disable(EnableCap.DepthTest);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.ColorMask(true, true, true, true);
|
|
|
|
GL.StencilFunc(StencilFunction.Equal, 0, 1);
|
|
|
|
GL.CullFace(CullFaceMode.Back);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandA(); // draw the part of a that's in b
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Enable(EnableCap.DepthTest);
|
2009-10-24 10:07:43 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
protected override void OnRenderFrame(FrameEventArgs e)
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
2010-10-09 19:59:06 +00:00
|
|
|
this.Title = WindowTitle + " FPS: " + (1f / e.Time).ToString("0.");
|
2009-10-24 10:07:43 +00:00
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
MySphereZOffset += (float)(e.Time * 3.1);
|
|
|
|
MySphereXOffset += (float)(e.Time * 4.2);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
|
|
|
#region Transform setup
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
|
2009-10-24 10:07:43 +00:00
|
|
|
|
|
|
|
// Camera
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.MatrixMode(MatrixMode.Modelview);
|
|
|
|
Matrix4 mv = Matrix4.LookAt(EyePosition, Vector3.Zero, Vector3.UnitY);
|
2009-10-24 10:07:43 +00:00
|
|
|
GL.LoadMatrix(ref mv);
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Translate(0f, 0f, CameraZoom);
|
|
|
|
GL.Rotate(CameraRotX, Vector3.UnitY);
|
|
|
|
GL.Rotate(CameraRotY, Vector3.UnitX);
|
2009-10-24 10:07:43 +00:00
|
|
|
#endregion Transform setup
|
|
|
|
|
|
|
|
RenderCsg();
|
|
|
|
|
|
|
|
// ---------------------------------
|
|
|
|
|
2010-10-09 19:59:06 +00:00
|
|
|
if (ShowDebugWireFrame)
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
2009-11-04 20:48:31 +00:00
|
|
|
GL.Color3(System.Drawing.Color.LightGray);
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.Disable(EnableCap.StencilTest);
|
|
|
|
GL.Disable(EnableCap.Lighting);
|
2009-11-04 20:48:31 +00:00
|
|
|
//GL.Disable( EnableCap.DepthTest );
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
|
2009-10-24 10:07:43 +00:00
|
|
|
DrawOperandB();
|
2010-10-09 19:59:06 +00:00
|
|
|
GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
|
|
|
|
GL.Enable(EnableCap.DepthTest);
|
|
|
|
GL.Enable(EnableCap.Lighting);
|
|
|
|
GL.Enable(EnableCap.StencilTest);
|
2009-10-24 10:07:43 +00:00
|
|
|
}
|
|
|
|
this.SwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
[STAThread]
|
|
|
|
static void Main()
|
|
|
|
{
|
2010-10-09 19:59:06 +00:00
|
|
|
using (StencilCSG example = new StencilCSG())
|
2009-10-24 10:07:43 +00:00
|
|
|
{
|
|
|
|
Utilities.SetWindowTitle(example);
|
2010-10-09 19:59:06 +00:00
|
|
|
example.Run(30.0, 0.0);
|
2009-10-24 10:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|