1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
// This code was written for the OpenTK library and has been released
// to the Public Domain.
// It is provided "as is" without express or implied warranty of any kind.
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
{
[Example("Stencil CSG", ExampleCategory.OpenGL, "1.x", Documentation = "StencilCSG")]
partial class StencilCSG : GameWindow
{
#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;
Vector3 EyePosition = new Vector3(0f, 0f, 15f);
#region Window
public StencilCSG()
: base(800, 600, new GraphicsMode(new ColorFormat(8, 8, 8, 8), 24, 8)) // request 8-bit stencil buffer
{
base.VSync = VSyncMode.Off;
KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape: this.Exit(); break;
case Key.Space: ShowDebugWireFrame = !ShowDebugWireFrame; break;
}
};
}
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, 64.0f);
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);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
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.
PrimitiveType tempPrimMode;
VertexT2dN3dV3d[] tempVertices;
uint[] tempIndices;
OperandB.GetArraysforVBO(out tempPrimMode, out tempVertices, out tempIndices);
OperandB.Dispose();
for (int i = 0; i < tempVertices.Length; i++)
{
tempVertices[i].Normal *= -1.0;
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)
{
GL.DeleteTextures(1, ref Texture);
OperandA.Dispose();
OperandB.Dispose();
base.OnUnload(e);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
var mouse = OpenTK.Input.Mouse.GetState();
CameraRotX = -mouse.X * .5f;
CameraRotY = mouse.Y * .5f;
CameraZoom = mouse.Wheel * .2f;
}
public void DrawOperandB()
{
GL.PushMatrix();
GL.Translate(Math.Cos(MySphereXOffset), -1f, Math.Cos(MySphereZOffset));
OperandB.Draw();
GL.PopMatrix();
}
public void DrawOperandA()
{
GL.Enable(EnableCap.Texture2D);
OperandA.Draw();
GL.Disable(EnableCap.Texture2D);
}
public void RenderCsg()
{
// first pass
GL.Disable(EnableCap.StencilTest);
GL.ColorMask(false, false, false, false);
GL.CullFace(CullFaceMode.Front);
DrawOperandB();// draw front-faces into depth buffer
// use stencil plane to find parts of b in a
GL.DepthMask(false);
GL.Enable(EnableCap.StencilTest);
GL.StencilFunc(StencilFunction.Always, 0, 0);
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr);
GL.CullFace(CullFaceMode.Back);
DrawOperandA(); // increment the stencil where the front face of a is drawn
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr);
GL.CullFace(CullFaceMode.Front);
DrawOperandA(); // decrement the stencil buffer where the back face of a is drawn
GL.DepthMask(true);
GL.Disable(EnableCap.DepthTest);
GL.ColorMask(true, true, true, true);
GL.StencilFunc(StencilFunction.Notequal, 0, 1);
DrawOperandB(); // draw the part of b that's in a
// fix depth
GL.ColorMask(false, false, false, false);
GL.Enable(EnableCap.DepthTest);
GL.Disable(EnableCap.StencilTest);
GL.DepthFunc(DepthFunction.Always);
DrawOperandA();
GL.DepthFunc(DepthFunction.Less);
// second pass
GL.CullFace(CullFaceMode.Back);
DrawOperandA();
GL.DepthMask(false);
GL.Enable(EnableCap.StencilTest);
GL.StencilFunc(StencilFunction.Always, 0, 0);
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr);
DrawOperandB(); // increment the stencil where the front face of b is drawn
GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr);
GL.CullFace(CullFaceMode.Front);
DrawOperandB(); // decrement the stencil buffer where the back face of b is drawn
GL.DepthMask(true);
GL.Disable(EnableCap.DepthTest);
GL.ColorMask(true, true, true, true);
GL.StencilFunc(StencilFunction.Equal, 0, 1);
GL.CullFace(CullFaceMode.Back);
DrawOperandA(); // draw the part of a that's in b
GL.Enable(EnableCap.DepthTest);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
this.Title = WindowTitle + " FPS: " + (1f / e.Time).ToString("0.");
MySphereZOffset += (float)(e.Time * 3.1);
MySphereXOffset += (float)(e.Time * 4.2);
#region Transform setup
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
// Camera
GL.MatrixMode(MatrixMode.Modelview);
Matrix4 mv = Matrix4.LookAt(EyePosition, Vector3.Zero, Vector3.UnitY);
GL.LoadMatrix(ref mv);
GL.Translate(0f, 0f, CameraZoom);
GL.Rotate(CameraRotX, Vector3.UnitY);
GL.Rotate(CameraRotY, Vector3.UnitX);
#endregion Transform setup
RenderCsg();
// ---------------------------------
if (ShowDebugWireFrame)
{
GL.Color3(System.Drawing.Color.LightGray);
GL.Disable(EnableCap.StencilTest);
GL.Disable(EnableCap.Lighting);
//GL.Disable( EnableCap.DepthTest );
GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
DrawOperandB();
GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Lighting);
GL.Enable(EnableCap.StencilTest);
}
this.SwapBuffers();
}
[STAThread]
static void Main()
{
using (StencilCSG example = new StencilCSG())
{
Utilities.SetWindowTitle(example);
example.Run(30.0, 0.0);
}
}
}
}
|