/// <summary>
        /// Draw a bounding frustrum representation
        /// </summary>
        /// <param name="frustum">Frustrum</param>
        /// <param name="camera">Camera</param>
        /// <param name="color">Color</param>
        public static void Draw(BoundingFrustum frustum, BaseCamera camera, Color color)
        {
            if (effect == null)
            {
                effect = new BasicEffect(YnG.GraphicsDevice);
                effect.VertexColorEnabled = true;
                effect.LightingEnabled = false;
            }

            Vector3[] corners = frustum.GetCorners();
            for (int i = 0; i < 8; i++)
            {
                vertices[i].Position = corners[i];
                vertices[i].Color = color;
            }

            effect.View = camera.View;
            effect.Projection = camera.Projection;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                YnG.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, vertices, 0, 8, indices, 0, indices.Length / 2);
            }
        }
示例#2
0
        /// <summary>
        /// Update matrix world, bounding volumes if the mesh is dynamic and update the material.
        /// </summary>
        public virtual void PreDraw(BaseCamera camera)
        {
            UpdateMatrix();
			
            if (_dynamic)
                UpdateBoundingVolumes();

            _material.Update(camera, ref _world);
        }
 /// <summary>
 /// Make a bounding frustrum of the scene.
 /// </summary>
 /// <param name="frustum"></param>
 /// <param name="camera">Camera used on the scene.</param>
 /// <returns>The bounding frustrum.</returns>
 public static BoundingFrustum BuildBoundingFrustum(this BoundingFrustum frustum, BaseCamera camera)
 {
     Matrix view = Matrix.CreateLookAt(camera.Position, camera.Target, camera.VectorUp);
     Matrix projection = Matrix.CreatePerspectiveFieldOfView(camera.FieldOfView, camera.AspectRatio, camera.Near, camera.Far);
     Quaternion rotation = Quaternion.CreateFromRotationMatrix(Matrix.Invert(view));
     Matrix matrix = Matrix.CreateFromQuaternion(rotation);
     matrix.Translation = camera.Position;
     view = Matrix.Invert(matrix);
     return new BoundingFrustum(Matrix.Multiply(view, projection));
 }
示例#4
0
        public override void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
        {
            PreDraw(camera);

            device.BlendState = BlendState.AlphaBlend;

            _geometry.Draw(device, _material);

            device.BlendState = BlendState.Opaque;
        }
示例#5
0
        /// <summary>
        /// Create a state with a 3D scene and a camera.
        /// </summary>
        /// <param name="camera">Camera to use on this scene.</param>
        public YnState3D(BaseCamera camera)
            : base()
        {
            if (camera != null)
                _cameraManager = new CameraManager(camera);
            else
                _cameraManager = new CameraManager();

            _scene = new YnScene3D();
            _basicObjects = new YnBasicCollection();
        }
示例#6
0
        /// <summary>
        /// Get the distance between the mouse cursor and an object3D
        /// </summary>
        /// <param name="camera">Camera to use</param>
        /// <param name="object3D">Object3D</param>
        /// <returns>The distance between the object and the mouse cursor, -1 if not collide</returns>
        public static float MouseCollideWithObject(BaseCamera camera, YnEntity3D object3D)
        {
            float? distance = null;

            if (object3D.Dynamic)
                object3D.UpdateBoundingVolumes();

            distance = GetMouseRay(camera).Intersects(object3D.BoundingBox);

            return distance != null ? (float)distance : -1.0f;
        }
示例#7
0
        /// <summary>
        /// Get a Ray from the mouse coordinate
        /// </summary>
        /// <param name="camera">Camera to use</param>
        /// <returns>A ray</returns>
        public static Ray GetMouseRay(BaseCamera camera)
        {
            Vector3 nearPoint = new Vector3(YnG.Mouse.Position, 0);
            Vector3 farPoint = new Vector3(YnG.Mouse.Position, 1);

            nearPoint = YnG.GraphicsDevice.Viewport.Unproject(nearPoint, camera.Projection, camera.View, Matrix.Identity);
            farPoint = YnG.GraphicsDevice.Viewport.Unproject(farPoint, camera.Projection, camera.View, Matrix.Identity);

            // Get the direction
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            return new Ray(nearPoint, direction);
        }
示例#8
0
        public override void PreDraw(BaseCamera camera)
        {
            if (_isFixed)
            {
                _world = Matrix.CreateScale(_scale) *
                    Matrix.CreateFromYawPitchRoll(_rotation.Y, _rotation.X, _rotation.Z) *
                    Matrix.CreateTranslation(_position);
            }

            if (_dynamic)
                UpdateBoundingVolumes();

            _material.Update(camera, ref _world);
        }
示例#9
0
        public void UpdateRotation(BaseCamera camera)
        {
            Vector3 reference = Vector3.Backward;
            Vector3 lookDirection = Vector3.Normalize(camera.Position - camera.Target);
            float dot = Vector3.Dot(reference, lookDirection);
            float angle = (float)Math.Acos(dot / (reference.Length() * lookDirection.Length()));

            if (lookDirection.X < reference.X)
                angle = -angle;

            _world = Matrix.CreateScale(_scale) * 
                Matrix.CreateFromYawPitchRoll(angle + _rotation.Y, _rotation.X, _rotation.Z) * 
                Matrix.CreateTranslation(_position);
        }
示例#10
0
        public override void Update(BaseCamera camera, ref Matrix world)
        {
            // Update matrices
            base.Update(camera, ref world);

            DualTextureEffect dualTextureEffect = (DualTextureEffect)_effect;
			
            // Fog
            UpdateFog(dualTextureEffect);

            // Textures
            dualTextureEffect.Texture = _texture;
            dualTextureEffect.Texture2 = _secondTexture;
            
            // Lights
            dualTextureEffect.DiffuseColor = new Vector3(_diffuseColor.X, _diffuseColor.Y, _diffuseColor.Z) * _diffuseIntensity;
            dualTextureEffect.Alpha = _alphaColor;
        }
示例#11
0
        public static void Draw(Ray ray, float length, BaseCamera camera, Color color)
        {
            if (effect == null)
            {
                effect = new BasicEffect(YnG.GraphicsDevice);
                effect.VertexColorEnabled = false;
                effect.LightingEnabled = false;
            }

            verts[0] = new VertexPositionColor(ray.Position, Color.White);
            verts[1] = new VertexPositionColor(ray.Position + (ray.Direction * length), Color.White);

            effect.DiffuseColor = color.ToVector3();
            effect.Alpha = (float)color.A / 255f;

            effect.World = Matrix.Identity;
            effect.View = camera.View;
            effect.Projection = camera.Projection;

            YnG.GraphicsDevice.RasterizerState = new RasterizerState() { CullMode = CullMode.None };

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                YnG.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, verts, 0, 1);

                effect.World = Matrix.Invert(Matrix.CreateLookAt(
                    verts[1].Position,
                    verts[0].Position,
                    (ray.Direction != Vector3.Up) ? Vector3.Up : Vector3.Left));

                YnG.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, arrowVerts, 0, 5, arrowIndexs, 0, 4);
            }

            YnG.GraphicsDevice.RasterizerState = new RasterizerState() { CullMode = CullMode.CullClockwiseFace };
        }
示例#12
0
        public override void Update(BaseCamera camera, ref Matrix world)
        {
            // Update matrices
            base.Update(camera, ref world);

            BasicEffect basicEffect = (BasicEffect)_effect;

            // Texture
            basicEffect.TextureEnabled = _enableMainTexture;
            basicEffect.Texture = _texture;

            // Fog
            UpdateFog(basicEffect);

            // Lights
            if (UpdateLights(basicEffect))
            {
                basicEffect.PreferPerPixelLighting = _enablePerPixelLighting;
                basicEffect.EmissiveColor = _emissiveColor;
                basicEffect.DiffuseColor = _diffuseColor * _diffuseIntensity;
                basicEffect.SpecularColor = _specularColor * _specularIntensity;
                basicEffect.Alpha = _alphaColor;
            }
        }
示例#13
0
 /// <summary>
 /// Create a state with a 3D scene and a camera.
 /// </summary>
 /// <param name="name">State name.</param>
 /// <param name="camera">Camera to use on this scene.</param>
 public YnState3D(string name, BaseCamera camera)
     : this(camera)
 {
     _name = name;
 }
示例#14
0
        /// <summary>
        /// Draw the model.
        /// </summary>
        /// <param name="device">GraphicsDevice</param>
        public override void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
        {
            UpdateMatrix();
            _material.Update(camera, ref _world);

            _model.CopyAbsoluteBoneTransformsTo(_bonesTransforms);

            foreach (ModelMesh mesh in _model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    UpdateModelEffects(effect);
                    effect.World = _bonesTransforms[mesh.ParentBone.Index] * World;
                    effect.View = camera.View;
                    effect.Projection = camera.Projection;
                }
                mesh.Draw();
            }
        }
示例#15
0
 /// <summary>
 /// Remove a camera of the scene
 /// </summary>
 /// <param name="cmaera">Camera to remove.</param>
 public bool Remove(BaseCamera camera)
 {
     return _cameraManager.Remove(camera);
 }
示例#16
0
 /// <summary>
 /// Add a camera to the scene.
 /// </summary>
 /// <param name="camera">Camera to add.</param>
 /// <returns>Return false if the camera is already added otherwise return true.</returns>
 public bool Add(BaseCamera camera)
 {
     _cameraManager.Add(camera);
     return true;
 }
示例#17
0
 /// <summary>
 /// Draw members.
 /// </summary>
 /// <param name="device"></param>
 public override void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
 {
     _members.Draw(gameTime, device, camera, _sceneLight);
 }
示例#18
0
 public override void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
 {
     base.Draw(gameTime, device, camera);
 }
示例#19
0
 public BaseControl(BaseCamera camera, PlayerIndex index)
     : this(camera)
 {
     _playerIndex = index;
 }
示例#20
0
 /// <summary>
 /// Draw members on screen if visible.
 /// </summary>
 /// <param name="gameTime">GameTime object.</param>
 /// <param name="device">GraphicsDevice object</param>
 public override void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
 {
     if (Visible)
         _members.Draw(gameTime, device, camera, null);
 }
示例#21
0
        public static YnEntity3D[] CubeCollide(BaseCamera camera, YnGroup3D group)
        {
            List<YnEntity3D> collides = new List<YnEntity3D>();

            int groupSize = group.Count;

            for (int i = 0; i < groupSize; i++)
            {
                if (camera.BoundingBox.Intersects(group[i].BoundingBox))
                    collides.Add(group[i]);
            }

            return collides.ToArray();
        }
示例#22
0
        /// <summary>
        /// Get 2D position (on screen) from 3D position (on world)
        /// </summary>
        /// <param name="camera">Camera to use</param>
        /// <param name="position">Position 3D</param>
        /// <returns>The screen position</returns>
        public static Vector2 GetWorldToScreenPosition(BaseCamera camera, ref Vector3 position)
        {
            Vector3 p2d = YnG.GraphicsDevice.Viewport.Project(position, camera.Projection, camera.View, Matrix.Identity);

            return new Vector2(p2d.X, p2d.Y);
        }
示例#23
0
 public override void Update(BaseCamera camera, ref Matrix world)
 {
     // Matrices
     IEffectMatrices effectMatrices = (IEffectMatrices)_effect;
     effectMatrices.World = world;
     effectMatrices.View = camera.View;
     effectMatrices.Projection = camera.Projection;
 }
示例#24
0
        /// <summary>
        /// Get 3D world position of from the 2D position (on screen)
        /// </summary>
        /// <param name="camera">Camera to use</param>
        /// <param name="position">Position on world</param>
        /// <returns>Position on 3D world</returns>
        public static Vector3 GetScreenToWorldPosition(BaseCamera camera, ref Vector2 position)
        {
            Vector3 p3d = YnG.GraphicsDevice.Viewport.Unproject(new Vector3(position, 0.0f), camera.Projection, camera.View, Matrix.Identity);

            return p3d;
        }
示例#25
0
 /// <summary>
 /// Update the shader values
 /// </summary>
 /// <param name="camera">Camera to use.</param>
 /// <param name="world">Entity World matrix</param>
 public abstract void Update(BaseCamera camera, ref Matrix world);
示例#26
0
 /// <summary>
 /// Draw mesh.
 /// </summary>
 /// <param name="device"></param>
 public override void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
 {
     PreDraw(camera);
     _geometry.Draw(device, _material);
 }
示例#27
0
        /// <summary>
        /// A base control with a camera.
        /// </summary>
        /// <param name="camera"></param>
        public BaseControl(BaseCamera camera)
        {
            _camera = camera;

            // Physics
            PhysicsPosition = new PhysicsController()
            {
                Acceleration = Vector3.One,
                Velocity = Vector3.Zero,
                MaxVelocity = 0.5f
            };

            PhysicsRotation = new PhysicsController()
            {
                Acceleration = Vector3.One,
                Velocity = Vector3.Zero,
                MaxVelocity = 0.5f
            };

            // Movement
            _moveSpeed = 0.3f;
            _strafeSpeed = 0.2f;
            _pitchSpeed = 0.2f;
            _rotationSpeed = 0.3f;
            _playerIndex = PlayerIndex.One;

            // Flags
            _enableKeyboard = true;
            _enableGamepad = true;
            _enableMouse = false;

            // Invert
            _xDirection = 1;
            _yDirection = 1;
            _zDirection = 1;
            _xRotation = 1;
            _yRotation = 1;
            _zRotation = 1;
        }
示例#28
0
 public CameraChangedEventArgs()
 {
     ActiveCamera = null;
 }
示例#29
0
        public virtual void Draw(GameTime gameTime, GraphicsDevice device, BaseCamera camera)
        {

        }
示例#30
0
 public CameraChangedEventArgs(BaseCamera camera)
 {
     ActiveCamera = camera;
 }
        public override void Update(BaseCamera camera, ref Matrix world)
        {
            if (!_effectLoaded) return;

            // Update matrices
            base.Update(camera, ref world);

            EnvironmentMapEffect environmentMapEffect = (EnvironmentMapEffect)_effect;

            // Texture
            environmentMapEffect.Texture = _texture;
            environmentMapEffect.EnvironmentMap = _environmentTexture;
            environmentMapEffect.EnvironmentMapAmount = _environmentAmount;
            environmentMapEffect.EnvironmentMapSpecular = _specularColor * _specularIntensity;
            environmentMapEffect.FresnelFactor = _fresnelFactor;
            // Fog
            UpdateFog(environmentMapEffect);

            // Lights
            if (UpdateLights(environmentMapEffect))
            {
                environmentMapEffect.EmissiveColor = _emissiveColor;
                environmentMapEffect.DiffuseColor = _diffuseColor * _diffuseIntensity;
                environmentMapEffect.Alpha = _alphaColor;
            }
        }
示例#32
0
        /// <summary>
        /// Check if the mouse cursor collide with a group of object on the scene
        /// </summary>
        /// <param name="camera">Active camera</param>
        /// <param name="group">Group of object</param>
        /// <returns></returns>
        public static CollideInformation[] MouseCollideWithGroup(BaseCamera camera, YnGroup3D group)
        {
            List<CollideInformation> collides = new List<CollideInformation>();

            int groupSize = group.Count;

            for (int i = 0; i < groupSize; i++)
            {
                float distance = MouseCollideWithObject(camera, group[i]);

                if (distance > -1)
                    collides.Add(new CollideInformation() { Object3D = group[i], Distance = distance });
            }

            return collides.ToArray();
        }