Renders a bounding box.
Inheritance: Renderer, IBoundingBox
Ejemplo n.º 1
0
        /// <summary>
        /// Gets a <see cref="BoundingBoxRenderer"/> that wraps specified <paramref name="model"/>.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static BoundingBoxRenderer GetBoundingBoxRenderer(this IModelSpace model)
        {
            BoundingBoxRenderer boxRenderer = BoundingBoxRenderer.Create(model.Lengths);

            boxRenderer.SwitchList.Add(new LineWidthSwitch(lineWidth: 1.0f));
            boxRenderer.CopyModelSpaceStateFrom(model);

            return(boxRenderer);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a <see cref="BoundingBoxRenderer"/> that wraps specified <paramref name="model"/>.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static BoundingBoxRenderer GetBoundingBoxRenderer(this IModelSpace model)
        {
            BoundingBoxRenderer boxRenderer = BoundingBoxRenderer.Create(model.ModelSize);

            boxRenderer.StateList.Add(new LineWidthState(lineWidth: 1.0f));
            //boxRenderer.CopyModelSpaceStateFrom(model);
            boxRenderer.ModelSize = model.ModelSize;

            return(boxRenderer);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Render this bounding box.
 /// </summary>
 /// <param name="boundingBox"></param>
 /// <param name="color"></param>
 /// <param name="arg"></param>
 public static void Render(this IBoundingBox boundingBox, Color color, RenderEventArgs arg)
 {
     if (renderer == null)
     {
         var lengths = new vec3(1, 1, 1);
         renderer = BoundingBoxRenderer.Create(lengths);
         renderer.Initialize();
     }
     renderer.WorldPosition    = boundingBox.MaxPosition / 2 + boundingBox.MinPosition / 2;
     renderer.Scale            = boundingBox.MaxPosition - boundingBox.MinPosition;
     renderer.BoundingBoxColor = color;
     renderer.Render(arg);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Render this bounding box.
 /// </summary>
 /// <param name="boundingBox"></param>
 /// <param name="color"></param>
 /// <param name="arg"></param>
 public static void Render(this IBoundingBox boundingBox, Color color, RenderEventArgs arg)
 {
     if (renderer == null)
     {
         var lengths = new vec3(1, 1, 1);
         renderer = BoundingBoxRenderer.Create(lengths);
         renderer.Initialize();
     }
     renderer.WorldPosition = boundingBox.MaxPosition / 2 + boundingBox.MinPosition / 2;
     renderer.Scale = boundingBox.MaxPosition - boundingBox.MinPosition;
     renderer.BoundingBoxColor = color;
     renderer.Render(arg);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Render this bounding box.
        /// </summary>
        /// <param name="boundingBox"></param>
        /// <param name="color"></param>
        /// <param name="arg"></param>
        public static void Render(this IBoundingBox boundingBox, Color color, RenderEventArgs arg)
        {
            if (renderer == null)
            {
                var lengths = new vec3(1, 1, 1);
                renderer = BoundingBoxRenderer.Create(lengths);
                renderer.Initialize();
            }
            mat4 model = glm.translate(mat4.identity(), boundingBox.MaxPosition / 2 + boundingBox.MinPosition / 2);

            model = glm.scale(model, boundingBox.MaxPosition - boundingBox.MinPosition);
            renderer.ModelMatrix      = model;
            renderer.BoundingBoxColor = color;
            renderer.Render(arg);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// get a bounding box renderer.
        /// </summary>
        /// <param name="lengths">bounding box's length at x, y, z direction.</param>
        /// <returns></returns>
        public static BoundingBoxRenderer Create(vec3 lengths)
        {
            var model       = new BoundingBoxModel(lengths);
            var shaderCodes = new ShaderCode[2];

            shaderCodes[0] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
                                                @"Resources\BoundingBox.vert"), ShaderType.VertexShader);
            shaderCodes[1] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
                                                @"Resources\BoundingBox.frag"), ShaderType.FragmentShader);
            var map = new AttributeMap();

            map.Add("in_Position", BoundingBoxModel.strPosition);
            var result = new BoundingBoxRenderer(model, shaderCodes, map, new PolygonModeState(PolygonMode.Line), new PolygonOffsetFillState());

            result.ModelSize = lengths;
            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// get a bounding box renderer.
        /// </summary>
        /// <param name="lengths">bounding box's length at x, y, z direction.</param>
        /// <param name="originalWorldPosition"></param>
        /// <returns></returns>
        public static BoundingBoxRenderer Create(vec3 lengths, vec3 originalWorldPosition = new vec3())
        {
            var bufferable  = new BoundingBoxModel(lengths);
            var shaderCodes = new ShaderCode[2];

            shaderCodes[0] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
                                                @"Resources\BoundingBox.vert"), ShaderType.VertexShader);
            shaderCodes[1] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
                                                @"Resources\BoundingBox.frag"), ShaderType.FragmentShader);
            var map = new PropertyNameMap();

            map.Add("in_Position", BoundingBoxModel.strPosition);
            var result = new BoundingBoxRenderer(bufferable, shaderCodes, map, new PolygonModeSwitch(PolygonModes.Lines), new PolygonOffsetFillSwitch());

            result.halfLengths           = new vec4(lengths / 2, 1.0f);
            result.OriginalWorldPosition = originalWorldPosition;
            result.ModelMatrix           = glm.translate(mat4.identity(), originalWorldPosition);
            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets a <see cref="SceneObject"/> that contains this renderer.
        /// </summary>
        /// <param name="renderer"></param>
        /// <param name="name"></param>
        /// <param name="generateBoundingBox"></param>
        /// <param name="scripts"></param>
        /// <returns></returns>
        public static SceneObject WrapToSceneObject(
            this RendererBase renderer,
            string name,
            bool generateBoundingBox,
            params Script[] scripts)
        {
            var obj = new SceneObject();

            obj.Renderer = renderer;
            obj.Name     = name;
            obj.Scripts.AddRange(scripts);
            if (generateBoundingBox)
            {
                BoundingBoxRenderer box = renderer.GetBoundingBoxRenderer();
                box.BoundingBoxColor = Color.Gray;
                var boxObj = new SceneObject();
                boxObj.Renderer = box;
                boxObj.Name     = string.Format("Box of [{0}]", name);
                obj.Children.Add(boxObj);
            }

            return(obj);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets a <see cref="BoundingBoxRenderer"/> that wraps specified <paramref name="models"/>.
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static BoundingBoxRenderer GetBoundingBoxRenderer(this IModelSpace[] models)
        {
            if (models == null)
            {
                throw new System.ArgumentNullException();
            }

            vec3 max = new vec3();
            vec3 min = new vec3();
            {
                bool initialized = false;
                foreach (var model in models)
                {
                    if (!initialized)
                    {
                        model.GetMaxMinPosition(out max, out min);
                        initialized = true;
                    }
                    else
                    {
                        vec3 tmpMax, tmpMin;
                        model.GetMaxMinPosition(out tmpMax, out tmpMin);
                        tmpMax.UpdateMax(ref max);
                        tmpMin.UpdateMin(ref min);
                    }
                }
            }

            vec3 lengths       = (max - min);
            vec3 worldPosition = max / 2.0f + min / 2.0f;
            BoundingBoxRenderer boxRenderer = BoundingBoxRenderer.Create(lengths);

            boxRenderer.SwitchList.Add(new LineWidthSwitch(lineWidth: 1.0f));
            boxRenderer.WorldPosition = worldPosition;

            return(boxRenderer);
        }
Ejemplo n.º 10
0
        private bool UpdateCurrentNode(TreeNode node)
        {
            bool updated = false;
            var stack = new Stack<TreeNode>();
            stack.Push(node);
            BoundingBoxRenderer selectedRenderer = null;
            while (stack.Count > 0)
            {
                TreeNode current = stack.Pop();
                var obj = current.Tag as SceneObject;
                if (obj != null)
                {
                    var renderer = obj.Renderer as BoundingBoxRenderer;
                    if (renderer != null)
                    {
                        selectedRenderer = renderer;
                        break;
                    }
                }
                foreach (TreeNode item in current.Nodes)
                {
                    stack.Push(item);
                }
            }

            if (selectedRenderer != lastselectedBoxRenderer)
            {
                if (selectedRenderer != null)
                {
                    selectedRenderer.BoundingBoxColor = Color.Aqua;
                    var glState = selectedRenderer.StateList.Find(x => x is LineWidthState) as LineWidthState;
                    glState.LineWidth = 3;
                }

                if (lastselectedBoxRenderer != null)
                {
                    lastselectedBoxRenderer.BoundingBoxColor = Color.White;
                    var glState = lastselectedBoxRenderer.StateList.Find(x => x is LineWidthState) as LineWidthState;
                    glState.LineWidth = 1;
                }

                this.lastselectedBoxRenderer = selectedRenderer;
                updated = true;
            }

            return updated;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// get a bounding box renderer.
 /// </summary>
 /// <param name="lengths">bounding box's length at x, y, z direction.</param>
 /// <returns></returns>
 public static BoundingBoxRenderer Create(vec3 lengths)
 {
     var model = new BoundingBoxModel(lengths);
     var shaderCodes = new ShaderCode[2];
     shaderCodes[0] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
         @"Resources\BoundingBox.vert"), ShaderType.VertexShader);
     shaderCodes[1] = new ShaderCode(ManifestResourceLoader.LoadTextFile(
         @"Resources\BoundingBox.frag"), ShaderType.FragmentShader);
     var map = new AttributeMap();
     map.Add("in_Position", BoundingBoxModel.strPosition);
     var result = new BoundingBoxRenderer(model, shaderCodes, map, new PolygonModeState(PolygonMode.Line), new PolygonOffsetFillState());
     result.ModelSize = lengths;
     return result;
 }