Esempio n. 1
0
        private void DrawGlContent(DrawEventArgs e)
        {
            var gcodeOptions = sceneContext.RendererOptions;

            if (gcodeOptions.GCodeModelView)
            {
                modelRenderStyle = ModelRenderStyle.WireframeAndSolid;
            }
            else
            {
                modelRenderStyle = ModelRenderStyle.None;
            }

            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.First))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }

            GLHelper.SetGlContext(this.World, renderSource.TransformToScreenSpace(renderSource.LocalBounds), lighting);

            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.OpaqueContent))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }

            // Draw solid objects, extract transparent
            var transparentMeshes = new List <Object3DView>();

            var  selectedItem       = scene.SelectedItem;
            bool suppressNormalDraw = false;

            if (selectedItem != null)
            {
                // Invoke existing IEditorDraw when iterating items
                if (selectedItem is IEditorDraw editorDraw)
                {
                    // TODO: Putting the drawing code in the IObject3D means almost certain bindings to MatterControl in IObject3D. If instead
                    // we had a UI layer object that used binding to register scene drawing hooks for specific types, we could avoid the bindings
                    editorDraw.DrawEditor(this, transparentMeshes, e, ref suppressNormalDraw);
                }
            }

            foreach (var item in scene.Children)
            {
                if (item.Visible &&
                    (item != selectedItem || suppressNormalDraw == false))
                {
                    DrawObject(item, transparentMeshes, e);
                }
            }

            if (sceneContext.Printer?.Connection?.serialPort is PrinterEmulator.Emulator emulator)
            {
                void NozzlePositionChanged(object s, EventArgs e2)
                {
                    // limit max number of updates per second to 10
                    if (UiThread.CurrentTimerMs > lastEmulatorDrawMs + 100)
                    {
                        UiThread.RunOnIdle(Invalidate);
                        // set it to now
                        lastEmulatorDrawMs = UiThread.CurrentTimerMs;
                    }
                }

                var matrix = Matrix4X4.CreateTranslation(emulator.CurrentPosition + new Vector3(.5, .5, 5));
                GLHelper.Render(emulatorNozzleMesh,
                                MaterialRendering.Color(emulator.ExtruderIndex),
                                matrix,
                                RenderTypes.Shaded,
                                matrix * World.ModelviewMatrix);

                if (!emulatorHooked)
                {
                    emulator.DestinationChanged += NozzlePositionChanged;
                    emulatorHooked = true;
                }

                Closed += (s, e3) => emulator.DestinationChanged -= NozzlePositionChanged;
            }

            transparentMeshes.Sort(BackToFrontXY);

            var bedNormalInViewSpace  = Vector3Ex.TransformNormal(Vector3.UnitZ, World.ModelviewMatrix).GetNormal();
            var pointOnBedInViewSpace = Vector3Ex.Transform(new Vector3(10, 10, 0), World.ModelviewMatrix);
            var lookingDownOnBed      = Vector3Ex.Dot(bedNormalInViewSpace, pointOnBedInViewSpace) < 0;

            floorDrawable.LookingDownOnBed = lookingDownOnBed;

            if (lookingDownOnBed)
            {
                floorDrawable.Draw(this, e, Matrix4X4.Identity, this.World);
            }

            var wireColor = Color.Transparent;

            switch (modelRenderStyle)
            {
            case ModelRenderStyle.Wireframe:
                wireColor = darkWireframe;
                break;

            case ModelRenderStyle.WireframeAndSolid:
                wireColor = lightWireframe;
                break;
            }

            // Draw transparent objects
            foreach (var item in transparentMeshes)
            {
                var object3D = item.Object3D;
                GLHelper.Render(
                    object3D.Mesh,
                    item.Color,
                    object3D.WorldMatrix(),
                    RenderTypes.Outlines,
                    object3D.WorldMatrix() * World.ModelviewMatrix,
                    wireColor,
                    allowBspRendering: transparentMeshes.Count < 1000);
            }

            if (!lookingDownOnBed)
            {
                floorDrawable.Draw(this, e, Matrix4X4.Identity, this.World);
            }

            DrawInteractionVolumes(e);

            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.TransparentContent))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }

            GLHelper.UnsetGlContext();

            // Invoke DrawStage.Last item drawables
            foreach (var item in scene.Children)
            {
                // HACK: Consider how shared code in DrawObject can be reused to prevent duplicate execution
                bool isSelected = selectedItem != null &&
                                  selectedItem.DescendantsAndSelf().Any((i) => i == item);

                foreach (var itemDrawable in itemDrawables.Where(d => d.DrawStage == DrawStage.Last && d.Enabled))
                {
                    itemDrawable.Draw(this, item, isSelected, e, Matrix4X4.Identity, this.World);
                }
            }

            // Invoke DrawStage.Last scene drawables
            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.Last))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }
        }
Esempio n. 2
0
        private void DrawGlContent(DrawEventArgs e)
        {
            var gcodeOptions = sceneContext.RendererOptions;

            if (gcodeOptions.GCodeModelView)
            {
                modelRenderStyle = ModelRenderStyle.WireframeAndSolid;
            }
            else
            {
                modelRenderStyle = ModelRenderStyle.None;
            }

            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.First))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }

            GLHelper.SetGlContext(this.World, renderSource.TransformToScreenSpace(renderSource.LocalBounds), lighting);

            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.OpaqueContent))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }

            // Draw solid objects, extract transparent
            var transparentMeshes = new List <Object3DView>();

            var  selectedItem       = scene.SelectedItem;
            bool suppressNormalDraw = false;

            if (selectedItem != null)
            {
                // Invoke existing IEditorDraw when iterating items
                if (selectedItem is IEditorDraw editorDraw)
                {
                    // TODO: Putting the drawing code in the IObject3D means almost certain bindings to MatterControl in IObject3D. If instead
                    // we had a UI layer object that used binding to register scene drawing hooks for specific types, we could avoid the bindings
                    editorDraw.DrawEditor(this, transparentMeshes, e, ref suppressNormalDraw);
                }
            }

            foreach (var item in scene.Children)
            {
                if (item.Visible &&
                    (item != selectedItem || suppressNormalDraw == false))
                {
                    DrawObject(item, transparentMeshes, e);
                }
            }

            transparentMeshes.Sort(BackToFrontXY);

            var bedNormalInViewSpace  = Vector3Ex.TransformNormal(Vector3.UnitZ, World.ModelviewMatrix).GetNormal();
            var pointOnBedInViewSpace = Vector3Ex.Transform(new Vector3(10, 10, 0), World.ModelviewMatrix);
            var lookingDownOnBed      = Vector3Ex.Dot(bedNormalInViewSpace, pointOnBedInViewSpace) < 0;

            floorDrawable.LookingDownOnBed = lookingDownOnBed;

            if (lookingDownOnBed)
            {
                floorDrawable.Draw(this, e, Matrix4X4.Identity, this.World);
            }

            var wireColor = Color.Transparent;

            switch (modelRenderStyle)
            {
            case ModelRenderStyle.Wireframe:
                wireColor = darkWireframe;
                break;

            case ModelRenderStyle.WireframeAndSolid:
                wireColor = lightWireframe;
                break;
            }

            // Draw transparent objects
            foreach (var item in transparentMeshes)
            {
                var object3D = item.Object3D;
                GLHelper.Render(
                    object3D.Mesh,
                    item.Color,
                    object3D.WorldMatrix(),
                    RenderTypes.Outlines,
                    object3D.WorldMatrix() * World.ModelviewMatrix,
                    wireColor);
            }

            if (!lookingDownOnBed)
            {
                floorDrawable.Draw(this, e, Matrix4X4.Identity, this.World);
            }

            DrawInteractionVolumes(e);

            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.TransparentContent))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }

            GLHelper.UnsetGlContext();

            // Invoke DrawStage.Last item drawables
            foreach (var item in scene.Children)
            {
                // HACK: Consider how shared code in DrawObject can be reused to prevent duplicate execution
                bool isSelected = selectedItem != null &&
                                  (selectedItem.DescendantsAndSelf().Any((i) => i == item) ||
                                   selectedItem.Parents <ModifiedMeshObject3D>().Any((mw) => mw == item));

                foreach (var itemDrawable in itemDrawables.Where(d => d.DrawStage == DrawStage.Last && d.Enabled))
                {
                    itemDrawable.Draw(this, item, isSelected, e, Matrix4X4.Identity, this.World);
                }
            }

            // Invoke DrawStage.Last scene drawables
            foreach (var drawable in drawables.Where(d => d.DrawStage == DrawStage.Last))
            {
                if (drawable.Enabled)
                {
                    drawable.Draw(this, e, Matrix4X4.Identity, this.World);
                }
            }
        }