Ejemplo n.º 1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (!this.DesignMode)
            {
                MakeCurrent();
                GL.Viewport(ClientRectangle);
                //float aspect = this.ClientSize.Width / (float)this.ClientSize.Height;

                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();

                // 3D Setup
                Matrix4 projection = viewport.ProjectionMatrix;
                GL.LoadMatrix(ref projection);
                GL.MatrixMode(MatrixMode.Modelview);

                GL.Enable(EnableCap.Blend);                                                    // glEnable(GL_BLEND);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

                GL.ClearColor(Color.White);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.LoadIdentity();

                GL.Enable(EnableCap.Lighting);
                GL.Enable(EnableCap.ColorMaterial);
                GL.Light(LightName.Light0, LightParameter.Position, new Vector4(0, 0, 1, 0));
                GL.Light(LightName.Light0, LightParameter.Diffuse, new Vector4(.8f, .8f, .8f, 1));
                GL.Light(LightName.Light0, LightParameter.Ambient, new Vector4(.3f, .3f, .3f, 1));
                //GL.Light(LightName.Light0, LightParameter.Specular, new Vector4(1.0f, 1.0f, 1.0f, 1));
                GL.Enable(EnableCap.Light0);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);
                //GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.AmbientAndDiffuse);
                GL.Enable(EnableCap.ColorMaterial);

                GL.LoadMatrix(ref viewport.viewMatrix);
                GL.Normal3(new Vector3(0, 0, 1));
                GL.Enable(EnableCap.Normalize);

                GL.DepthMask(true);
                GL.Enable(EnableCap.DepthTest);
                GL.DepthFunc(DepthFunction.Lequal);
                //GL.Enable(EnableCap.CullFace);
                //GL.CullFace(CullFaceMode.Back);
                GL.PolygonOffset(1.0f, 2.0f);
                GL.Enable(EnableCap.PolygonOffsetFill);

                viewport.Draw();

                //GL.PushMatrix();
                foreach (object o in objects)
                {
                    if (o is IOpenGLDrawable)
                    {
                        var drawable = o as IOpenGLDrawable;
                        drawable.Draw();
                    }
                }
                //GL.PopMatrix();

                SwapBuffers();
            }
        }
Ejemplo n.º 2
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (!this.DesignMode)
            {
                MakeCurrent();


                GL.Viewport(ClientRectangle);

                float aspect = this.ClientSize.Width / (float)this.ClientSize.Height;

                //Matrix4 projection_matrix;
                //Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspect, 1, 64, out projection_matrix);

                //GL.MatrixMode(MatrixMode.Projection);
                //GL.LoadMatrix(ref projection_matrix);

                //GL.Viewport(0, 0, Width, Height);

                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();

                // 2D Setup
                //GL.Ortho(viewport.Box.Left, viewport.Box.Right, viewport.Box.Bottom, viewport.Box.Top, 1, -1);

                // 3D Setup
                Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(OpenTK.MathHelper.PiOver4, aspect, 0.1f, 10000.0f);
                GL.LoadMatrix(ref perspective);

                GL.DepthMask(true);
                GL.Enable(EnableCap.DepthTest);
                GL.DepthFunc(DepthFunction.Lequal);


                GL.MatrixMode(MatrixMode.Modelview);

                GL.Enable(EnableCap.Blend);                                                    // glEnable(GL_BLEND);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

                GL.ClearColor(Color.White);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.LoadIdentity();

                GL.Enable(EnableCap.Lighting);
                GL.Enable(EnableCap.ColorMaterial);
                GL.Light(LightName.Light0, LightParameter.Position, new Vector4(0, 0, 1, 0));
                GL.Light(LightName.Light0, LightParameter.Diffuse, new Vector4(.5f, .5f, .5f, 1));
                GL.Light(LightName.Light0, LightParameter.Ambient, new Vector4(.3f, .3f, .3f, 1));
                //GL.Light(LightName.Light0, LightParameter.Specular, new Vector4(0.5f, 0.5f, 0.5f, 1));
                GL.Enable(EnableCap.Light0);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);
                GL.Enable(EnableCap.ColorMaterial);

                GL.LoadMatrix(ref viewport.viewMatrix);

                viewport.Draw();
                GL.Scale(.001, .001, .001);
                GL.Enable(EnableCap.Normalize);
                //GL.Translate(0, 0, -1000);



                GL.Color3(Color.Black);

                //GL.Color3(Color.Black);
                //DrawCrosshairs(mouseLocation, gridScale);
                GL.Color3(Color.LightGray);
                //DrawGrid(viewport.Box, gridScale);

                if (DrawScene2D != null)
                {
                    DrawScene2D(this, EventArgs.Empty);
                }



                //DateTime t = DateTime.Now;

                //for (int f = 0; f < 10; f++)
                //{
                for (int i = objects.Count() - 1; i >= 0; i--)
                {
                    IIsUseful useful = objects[i] as IIsUseful;
                    if (useful != null && !useful.IsUseful())
                    {
                        objects.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        IOpenGLDrawable d = objects[i] as IOpenGLDrawable;
                        if (d != null)
                        {
                            GL.Color3(Color.White);
                            GL.PointSize(1);
                            GL.LineWidth(1);
                            d.Draw();
                        }
                    }
                }
                //}
                //double time = (DateTime.Now - t).TotalMilliseconds;
                //Console.WriteLine("Drawing Took " + time + " Milliseconds");



                GL.Begin(BeginMode.Quads);
                GL.Vertex3(0, 0, 0);
                GL.Vertex3(0, 1, 0);
                GL.Vertex3(1, 1, 0);
                GL.Vertex3(1, 0, 0);
                GL.End();
                //Console.WriteLine("OnPaint Called!");
                SwapBuffers();
            }
        }