Esempio n. 1
0
        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.
            BeginMode 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
        }