Ejemplo n.º 1
0
        /// <summary>
        /// Get model matrix that transform model from model space to world space.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static mat4 GetModelMatrix(this IModelSpace model)
        {
            mat4 matrix = glm.translate(mat4.identity(), model.WorldPosition);

            matrix = glm.scale(matrix, model.Scale);
            matrix = glm.rotate(matrix, model.RotationAngleDegree, model.RotationAxis);
            return(matrix);
        }
Ejemplo n.º 2
0
        private INamedElement TryCreateAgeModelElement(IModelSpace modelSpace = null)
        {
            var constructor = new PropertyConstructor();
            var context = this.GetConstructionContext(modelSpace);
            var propertyInfo = typeof(TestModelElement).AsRuntimeTypeInfo().Properties["Age"];
            var modelElement = constructor.TryCreateModelElement(context, propertyInfo);

            return modelElement;
        }
Ejemplo n.º 3
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.º 4
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.º 5
0
        /// <summary>
        /// Copy <see cref="IModelSpace"/> state from specified <paramref name="source"/>.
        /// </summary>
        /// <param name="dest"></param>
        /// <param name="source"></param>
        public static void CopyModelSpaceStateFrom(this IModelSpace dest, IModelSpace source)
        {
            if (dest == null || source == null) { throw new System.ArgumentNullException(); }

            dest.ModelSize = source.ModelSize;
            dest.RotationAngleDegree = source.RotationAngleDegree;
            dest.RotationAxis = source.RotationAxis;
            dest.Scale = source.Scale;
            dest.WorldPosition = source.WorldPosition;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets max and min position of the AABB box that wraps specified <paramref name="model"/>.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="maxPosition"></param>
        /// <param name="minPosition"></param>
        public static void GetMaxMinPosition(this IModelSpace model, out vec3 maxPosition, out vec3 minPosition)
        {
            if (model == null)
            {
                throw new System.ArgumentNullException();
            }

            vec3 lengths = model.Lengths * model.Scale;

            maxPosition = model.WorldPosition + lengths / 2.0f;
            minPosition = model.WorldPosition - lengths / 2.0f;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Rotate this model based on all previous rotation actions.
        /// Thus all rotations will take part in model's rotation result.
        /// <para>在目前的旋转状态下继续旋转一次,即所有的旋转操作都会(按照发生顺序)生效。</para>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="angleDegree">Angle in Degree.</param>
        /// <param name="axis"></param>
        public static void Rotate(this IModelSpace model, float angleDegree, vec3 axis)
        {
            mat4       currentRotationMatrix = glm.rotate(model.RotationAngleDegree, model.RotationAxis);
            mat4       newRotationMatrix     = glm.rotate(angleDegree, axis);
            mat4       latestRotationMatrix  = newRotationMatrix * currentRotationMatrix;
            Quaternion quaternion            = latestRotationMatrix.ToQuaternion();
            float      latestAngle;
            vec3       latestAxis;

            quaternion.Parse(out latestAngle, out latestAxis);
            model.RotationAngleDegree = latestAngle;
            model.RotationAxis        = latestAxis;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Copy <see cref="IModelSpace"/> state from specified <paramref name="source"/>.
        /// </summary>
        /// <param name="dest"></param>
        /// <param name="source"></param>
        public static void CopyModelSpaceStateFrom(this IModelSpace dest, IModelSpace source)
        {
            if (dest == null || source == null)
            {
                throw new System.ArgumentNullException();
            }

            dest.Lengths             = source.Lengths;
            dest.RotationAngleDegree = source.RotationAngleDegree;
            dest.RotationAxis        = source.RotationAxis;
            dest.Scale         = source.Scale;
            dest.WorldPosition = source.WorldPosition;
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static BoundingBox GetBoundingBox(this IModelSpace model)
        {
            vec3 max, min;

            {
                vec3 position = model.WorldPosition + model.Lengths * model.Scale / 2;
                max = position;
            }
            {
                vec3 position = model.WorldPosition - model.Lengths * model.Scale / 2;
                min = position;
            }

            return(new BoundingBox(min, max));
        }
Ejemplo n.º 10
0
        protected override void DoUpdate()
        {
            if (this.self == null)
            {
                this.self = this.BindingObject.Renderer as IModelSpace;
            }

            //this.self.ModelMatrix = glm.translate(mat4.identity(), new vec3());
            if (this.target != null)
            {
                vec4 position = this.target.GetModelMatrix() * new vec4(this.labelPosition.Position, 1.0f);
                this.self.WorldPosition = new vec3(position);
            }
            else
            {
                vec3 position = this.labelPosition.Position;
                this.self.WorldPosition = position;
            }
        }
Ejemplo n.º 11
0
        protected override void DoUpdate()
        {
            if (this.renderer == null)
            {
                this.renderer = this.BindingObject.Renderer as BuildInRenderer;
                this.planetTransform = this.BindingObject.Parent.Renderer as IModelSpace;
            }

            double deltaAngle = Time.DeltaTime.TotalMilliseconds * Math.PI * 2 / this.RevolutionPeriod;
            double newAngle = this.currentAngle + deltaAngle;
            var position = new vec3(
                (float)(this.RevolutionRadius * Math.Cos(newAngle)),
                0,
                (float)(this.RevolutionRadius * Math.Sin(newAngle)))
                + this.planetTransform.WorldPosition;
            this.currentAngle = newAngle;

            this.renderer.WorldPosition = position;
        }
Ejemplo n.º 12
0
        protected override void DoUpdate()
        {
            if (this.self == null)
            {
                this.self = this.BindingObject.Renderer as IModelSpace;
            }

            //this.self.ModelMatrix = glm.translate(mat4.identity(), new vec3());
            if (this.target != null)
            {
                vec4 position = this.target.GetModelMatrix() * new vec4(this.labelPosition.Position, 1.0f);
                this.self.WorldPosition = new vec3(position);
            }
            else
            {
                vec3 position = this.labelPosition.Position;
                this.self.WorldPosition = position;
            }
        }
Ejemplo n.º 13
0
        protected override void DoUpdate()
        {
            if (this.renderer == null)
            {
                this.renderer        = this.BindingObject.Renderer as BuildInRenderer;
                this.planetTransform = (this.BindingObject.Parent as SceneObject).Renderer as IModelSpace;
            }

            double deltaAngle = Time.DeltaTime.TotalMilliseconds * Math.PI * 2 / this.RevolutionPeriod;
            double newAngle   = this.currentAngle + deltaAngle;
            var    position   = new vec3(
                (float)(this.RevolutionRadius * Math.Cos(newAngle)),
                0,
                (float)(this.RevolutionRadius * Math.Sin(newAngle)))
                                + this.planetTransform.WorldPosition;

            this.currentAngle = newAngle;

            this.renderer.WorldPosition = position;
        }
Ejemplo n.º 14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="labelPosition"></param>
 public LabelTargetScript(ILabelPosition labelPosition)
 {
     // TODO: Complete member initialization
     this.labelPosition = labelPosition;
     this.target        = labelPosition as IModelSpace;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="Kephas.Model.Elements.Annotations.RequiredAnnotation"/> class.
 /// </summary>
 /// <param name="elementInfo">Information describing the element.</param>
 /// <param name="modelSpace"> The model space.</param>
 public RequiredAnnotation(IAnnotationInfo elementInfo, IModelSpace modelSpace)
     : base(elementInfo, modelSpace)
 {
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Initializes the service asynchronously by loading the model space.
        /// </summary>
        /// <param name="context">An optional context for initialization.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// An awaitable task.
        /// </returns>
        public async Task InitializeAsync(IContext context = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            this.initialization.Start();

            var constructionContext = new ModelConstructionContext { RuntimeModelElementFactory = this.runtimeModelElementFactory };
            var modelSpace = this.CreateModelSpace(constructionContext);
            constructionContext.ModelSpace = modelSpace;

            try
            {
                var elementInfosCollectorTask = Task.WhenAll(this.ModelInfoProviders.Select(p => p.GetElementInfosAsync(constructionContext, cancellationToken)));
                var elementInfos = (await elementInfosCollectorTask.PreserveThreadContext()).SelectMany(e => e).ToList();

                constructionContext[nameof(IModelConstructionContext.ElementInfos)] = elementInfos;
                ((IWritableNamedElement)modelSpace).CompleteConstruction(constructionContext);

                this.modelSpace = modelSpace;

                this.initialization.Complete();
            }
            catch (Exception exception)
            {
                this.initialization.Fault(exception);
                throw;
            }
        }
Ejemplo n.º 17
0
 public UpdateBillboardPosition(IModelSpace targetRenderer, SceneObject obj = null)
     : base(obj)
 {
     this.TargetRenderer = targetRenderer;
 }
Ejemplo n.º 18
0
 public UpdateLabelPosition(IModelSpace targetRenderer, SceneObject obj = null)
     : base(obj)
 {
     this.TargetRenderer = targetRenderer;
 }
Ejemplo n.º 19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="labelPosition"></param>
 public LabelTargetScript(ILabelPosition labelPosition)
 {
     // TODO: Complete member initialization
     this.labelPosition = labelPosition;
     this.target = labelPosition as IModelSpace;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Run legacy model transform.(from model space to world space)
 /// </summary>
 /// <param name="model"></param>
 public static void LegacyTransform(this IModelSpace model)
 {
     OpenGL.Translate(model.WorldPosition.x, model.WorldPosition.y, model.WorldPosition.z);
     OpenGL.Scale(model.Scale.x, model.Scale.y, model.Scale.z);
     OpenGL.Rotate(model.RotationAngleDegree, model.RotationAxis.x, model.RotationAxis.y, model.RotationAxis.z);
 }