Exemple #1
0
        private void Render(object sender, PaintEventArgs e)
        {
            if (!loaded)
            {
                return;
            }
            MakeCurrent();
            SetupViewport();
            //double size = Scene.Size();

            double size = Scene.Max.Y - Scene.Min.Y;

            double SceneYOffset = -size / 2f;

            double defaultzoom = (double)Math.Sqrt(Math.Pow(TableRadius, 2) + Math.Pow(TableHeight, 2));

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadIdentity();
            var aspect_ratio = Width / (double)Height;
            var projection   = Matrix4d.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4, double.Parse(aspect_ratio.ToString()), 1f, 1024);



            //            var modelview = Matrix4d.LookAt(0, size / 2 , - DragZoom * defaultzoom * 2f,
            //                                            0,TableHeight/2, 0,  0, size, 0);
            var viewHeight = (TableHeight / 2.50) + SceneYOffset;
            var modelview  = Matrix4d.LookAt(0, viewHeight, Math.Max(0.00000001f, -(-Drag.Zoom * defaultzoom * 1.5f)),
                                             0, viewHeight, 0, 0, size, 0);

            GL.MatrixMode(MatrixMode.Projection);
            if (Projection)
            {
                GL.LoadMatrix(ref projection);
            }
            else
            {
                GL.LoadIdentity();
                double x = Math.Max(TableRadius * 2, TableHeight) * Drag.Zoom / 2.0;
                double y = x * ((double)Height / (double)Width);
                GL.Ortho(-x, x, -y, y, 0.0, 10000.0);
            }
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);

            #region LIGHT
            //if (Lightning)
            {
                float[] lightPos = new float[] { (float)(-TableRadius * 2f), (float)TableRadius * 2f, (float)TableHeight * 2f, 1.0f };
                GL.PointSize(5);
                GL.Disable(EnableCap.Lighting);
                GL.Begin(PrimitiveType.Points);
                GL.Color3(Color.Yellow);
                GL.Vertex3(lightPos);
                GL.End();
                GL.PointSize(1);

                float[] light_ambient  = { 0.2f, 0.2f, 0.2f, 1.0f };
                float[] light_diffuse  = { 1.0f, 1.0f, 1.0f, 1.0f };
                float[] light_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
                float[] spotdirection  = { 0.5f, -1.0f, -1.0f };

                GL.Light(LightName.Light0, LightParameter.Ambient, light_ambient);
                GL.Light(LightName.Light0, LightParameter.Diffuse, light_diffuse);
                GL.Light(LightName.Light0, LightParameter.Specular, light_specular);

                GL.Enable(EnableCap.Lighting);
                GL.Light(LightName.Light0, LightParameter.Position, lightPos);
                GL.Enable(EnableCap.Light0);
                //GL.Light(LightName.Light0, LightParameter.SpotDirection, spotdirection);
                //GL.Light(LightName.Light0, LightParameter.SpotCutoff, 20);
            }
            #endregion

            GL.Translate(Drag.Pane.X, -Drag.Pane.Y, 0);
            Matrix4d m = MatrixFromYawPitchRoll(-Math.PI * Drag.Angle.X / 360.0f, -Math.PI * Drag.Angle.Y / 360.0f, 0);
            GL.MultMatrix(ref m);
            // Model space
            #region Box/Axis
            GL.Disable(EnableCap.Lighting);
            Vector3d scannerCenter = new Vector3d(0, -size / 2f, 0);
            Axis(new Vector3d(scannerCenter.X - TableRadius, scannerCenter.Y, scannerCenter.Z + TableRadius), 10);
            #endregion ;

            GL.Translate(0, SceneYOffset, 0);

            if (BoundingBox)
            {
                DrawBoundingBox(Color.Red, Scene.Min, Scene.Max);
            }
            if (Lightning)
            {
                GL.Enable(EnableCap.Lighting);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);
                GL.Enable(EnableCap.ColorMaterial);
            }


            GL.PointSize(2);
            GL.LineWidth(2);

            RenderingContext context = RenderingContext.From(ViewerConfig);
            context.ApplyFaceDefault();
            GL.ShadeModel(Smooth ? ShadingModel.Smooth : ShadingModel.Flat);
            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            Scene.Render(ref context);
            GL.Translate(0, -SceneYOffset, 0);
            DrawScannerTable(scannerCenter);
            SwapBuffers();
            using (Graphics g = Graphics.FromHwnd(this.Handle))
            {
                using (SolidBrush bg = new SolidBrush(this.BackColor))
                {
                    using (SolidBrush bf = new SolidBrush(this.ForeColor))
                    {
                        string     text = String.Format("{0} points", Scene.GetNumVertices().ToString("N0"));
                        SizeF      s    = g.MeasureString(text, this.Font);
                        RectangleF rect = new RectangleF(0, 0, s.Width, s.Height);
                        rect.Y = this.Height - s.Height;
                        g.FillRectangle(bg, rect);
                        g.DrawString(text, this.Font, bf, rect.Location);
                    }
                }
            }
//            this.Label.Text = String.Format("{0} points", Scene.GetNumVertices());
        }