Esempio n. 1
0
 public override void Draw(ICamera camera, CommandBuffer commands, SystemLighting lights, NebulaRenderer nr)
 {
     Init();
     if (Dfm != null)
     {
         Dfm.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         var center = VectorMath.Transform(Vector3.Zero, World);
         //var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, 20, nr, LitAmbient, LitDynamic, NoFog);
         Dfm.DrawBuffer(commands, World, ref Lighting.Empty);
     }
     if (Model != null)
     {
         if (Model.Levels.Length > 0)
         {
             Model.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
             var center = VectorMath.Transform(Model.Levels[0].Center, World);
             var lvl    = GetLevel(Model, center, camera.Position);
             if (lvl == null)
             {
                 return;
             }
             var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, Model.Levels[0].Radius, nr, LitAmbient, LitDynamic, NoFog);
             var r        = Model.Levels [0].Radius + lighting.FogRange.Y;
             if (lighting.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, center) <= (r * r))
             {
                 Model.DrawBufferLevel(lvl, commands, World, ref lighting);
             }
         }
     }
     else if (Cmp != null)
     {
         Cmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         foreach (Part p in Cmp.Parts)
         {
             var     model = p.Model;
             Matrix4 w     = World;
             if (p.Construct != null)
             {
                 w = p.Construct.Transform * World;
             }
             if (model.Levels.Length > 0)
             {
                 var center = VectorMath.Transform(model.Levels[0].Center, w);
                 var lvl    = GetLevel(model, center, camera.Position);
                 if (lvl == null)
                 {
                     continue;
                 }
                 var bsphere = new BoundingSphere(
                     center,
                     model.Levels[0].Radius
                     );
                 if (camera.Frustum.Intersects(bsphere))
                 {
                     var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, model.Levels[0].Radius, nr, LitAmbient, LitDynamic, NoFog);
                     var r        = model.Levels [0].Radius + lighting.FogRange.Y;
                     if (lighting.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, center) <= (r * r))
                     {
                         model.DrawBufferLevel(lvl, commands, w, ref lighting);
                     }
                 }
             }
         }
     }
     else if (CmpParts != null)
     {
         _parentCmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         foreach (Part p in CmpParts)
         {
             p.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
             var     model = p.Model;
             Matrix4 w     = World;
             if (p.Construct != null)
             {
                 w = p.Construct.Transform * World;
             }
             if (model.Levels.Length > 0)
             {
                 var center = VectorMath.Transform(model.Levels[0].Center, w);
                 var lvl    = GetLevel(model, center, camera.Position);
                 if (lvl == null)
                 {
                     continue;
                 }
                 var bsphere = new BoundingSphere(
                     center,
                     model.Levels[0].Radius
                     );
                 if (camera.Frustum.Intersects(bsphere))
                 {
                     var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, model.Levels[0].Radius, nr, LitAmbient, LitDynamic, NoFog);
                     var r        = model.Levels[0].Radius + lighting.FogRange.Y;
                     if (lighting.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, center) <= (r * r))
                     {
                         model.DrawBufferLevel(lvl, commands, w, ref lighting);
                     }
                 }
             }
         }
     }
     else if (Sph != null)
     {
         Sph.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         var l = RenderHelpers.ApplyLights(lights, LightGroup, pos, Sph.Radius, nr, LitAmbient, LitDynamic, NoFog);
         var r = Sph.Radius + l.FogRange.Y;
         if (l.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, pos) <= (r * r))
         {
             Sph.DrawBuffer(commands, World, ref l);
         }
     }
 }
Esempio n. 2
0
 public static Vector3 Transform(this Matrix4 mat, Vector3 toTransform)
 {
     return(VectorMath.Transform(toTransform, mat));
 }
Esempio n. 3
0
 public override void DepthPrepass(ICamera camera, RenderState rstate)
 {
     Init();
     if (Model != null)
     {
         Model.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         if (Model.Levels.Length != 0)
         {
             var center = VectorMath.Transform(Model.Levels[0].Center, World);
             var lvl    = GetLevel(Model, center, camera.Position);
             if (lvl == null)
             {
                 return;
             }
             Model.DepthPrepassLevel(lvl, rstate, World);
         }
     }
     else if (Cmp != null || CmpParts != null)
     {
         if (Cmp != null)
         {
             Cmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         }
         else
         {
             _parentCmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         }
         //Check if -something- renders
         var partCol = (IEnumerable <Part>)CmpParts ?? Cmp.Parts;
         foreach (Part p in partCol)
         {
             var     model = p.Model;
             Matrix4 w     = World;
             if (p.Construct != null)
             {
                 w = p.Construct.Transform * World;
             }
             if (model.Levels.Length > 0)
             {
                 var center = VectorMath.Transform(model.Levels[0].Center, w);
                 var lvl    = GetLevel(model, center, camera.Position);
                 if (lvl == null)
                 {
                     continue;
                 }
                 var bsphere = new BoundingSphere(
                     center,
                     model.Levels[0].Radius
                     );
                 if (camera.Frustum.Intersects(bsphere))
                 {
                     model.DepthPrepassLevel(lvl, rstate, w);
                 }
             }
         }
     }
     else if (Sph != null)
     {
         Sph.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
         Sph.DepthPrepass(rstate, World);
     }
 }
Esempio n. 4
0
        public override bool PrepareRender(ICamera camera, NebulaRenderer nr, SystemRenderer sys)
        {
            this.sysr = sys;
            if (Nebula != null && nr != Nebula)
            {
                return(false);
            }
            var dsq = VectorMath.DistanceSquared(pos, camera.Position);

            if (LODRanges != null)             //Fastest cull
            {
                var maxd = LODRanges[LODRanges.Length - 1] * sysr.LODMultiplier;
                maxd *= maxd;
                if (dsq > maxd)
                {
                    return(false);
                }
            }
            if (Model != null)
            {
                if (Model.Levels.Length != 0)
                {
                    var center = VectorMath.Transform(Model.Levels[0].Center, World);
                    var lvl    = GetLevel(Model, center, camera.Position);
                    if (lvl == null)
                    {
                        return(false);
                    }
                    var bsphere = new BoundingSphere(
                        center,
                        Model.Levels[0].Radius
                        );
                    if (!camera.Frustum.Intersects(bsphere))
                    {
                        return(false);                                     //Culled
                    }
                    sys.AddObject(this);
                    return(true);
                }
            }
            else if (Cmp != null || CmpParts != null)
            {
                //Check if -something- renders
                var  partCol  = (IEnumerable <Part>)CmpParts ?? Cmp.Parts;
                bool cmpParts = CmpParts != null;
                foreach (Part p in partCol)
                {
                    if (cmpParts)
                    {
                        p.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime));
                    }
                    var     model = p.Model;
                    Matrix4 w     = World;
                    if (p.Construct != null)
                    {
                        w = p.Construct.Transform * World;
                    }
                    if (model.Levels.Length > 0)
                    {
                        var center = VectorMath.Transform(model.Levels[0].Center, w);
                        var lvl    = GetLevel(model, center, camera.Position);
                        if (lvl == null)
                        {
                            continue;
                        }
                        var bsphere = new BoundingSphere(
                            center,
                            model.Levels[0].Radius
                            );
                        if (camera.Frustum.Intersects(bsphere))
                        {
                            sys.AddObject(this);
                            return(true);
                        }
                    }
                }
                return(false);
            }
            else if (Sph != null)
            {
                var bsphere = new BoundingSphere(
                    pos,
                    Math.Max(Sph.Radius, radiusAtmosphere));
                if (!camera.Frustum.Intersects(bsphere))
                {
                    return(false);
                }
                sys.AddObject(this);
                return(true);
            }
            return(false);
        }
Esempio n. 5
0
 public override bool OutOfView(ICamera camera)
 {
     if (Model != null)
     {
         if (Model.Levels.Length != 0)
         {
             var center = VectorMath.Transform(Model.Levels[0].Center, World);
             var lvl    = GetLevel(Model, center, camera.Position);
             if (lvl == null)
             {
                 return(true);
             }
             var bsphere = new BoundingSphere(
                 center,
                 Model.Levels[0].Radius
                 );
             if (!camera.Frustum.Intersects(bsphere))
             {
                 return(true);                                                         //Culled
             }
         }
     }
     else if (Cmp != null || CmpParts != null)
     {
         //Check if -something- renders
         bool doCull  = true;
         var  partCol = (IEnumerable <Part>)CmpParts ?? Cmp.Parts;
         foreach (Part p in partCol)
         {
             var     model = p.Model;
             Matrix4 w     = World;
             if (p.Construct != null)
             {
                 w = p.Construct.Transform * World;
             }
             if (model.Levels.Length > 0)
             {
                 var center = VectorMath.Transform(model.Levels[0].Center, w);
                 var lvl    = GetLevel(model, center, camera.Position);
                 if (lvl == null)
                 {
                     continue;
                 }
                 var bsphere = new BoundingSphere(
                     center,
                     model.Levels[0].Radius
                     );
                 if (camera.Frustum.Intersects(bsphere))
                 {
                     doCull = false;
                     break;
                 }
             }
         }
         return(doCull);
     }
     else if (Sph != null)
     {
         var bsphere = new BoundingSphere(
             pos,
             Math.Max(Sph.Radius, radiusAtmosphere));
         if (!camera.Frustum.Intersects(bsphere))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 6
0
        void AddAsteroidToBuffer(StaticAsteroid ast, Material mat)
        {
            var model     = ast.Drawable as ModelFile;
            var l0        = model.Levels[0];
            var vertType  = l0.Mesh.VertexBuffer.VertexType.GetType();
            var transform = ast.RotationMatrix * Matrix4.CreateTranslation(ast.Position * field.CubeSize);
            var norm      = transform;

            norm.Invert();
            norm.Transpose();
            int vertOffset = verts.Count;

            for (int i = 0; i < l0.Mesh.VertexCount; i++)
            {
                VertexPositionNormalColorTexture vert;
                if (vertType == typeof(VertexPositionNormalColorTexture))
                {
                    vert = l0.Mesh.verticesVertexPositionNormalColorTexture[i];
                }
                else if (vertType == typeof(VertexPositionNormalTexture))
                {
                    var v = l0.Mesh.verticesVertexPositionNormalTexture[i];
                    vert = new VertexPositionNormalColorTexture(
                        v.Position,
                        v.Normal,
                        Color4.White,
                        v.TextureCoordinate);
                }
                else if (vertType == typeof(VertexPositionNormalTextureTwo))
                {
                    var v = l0.Mesh.verticesVertexPositionNormalTextureTwo[i];
                    vert = new VertexPositionNormalColorTexture(
                        v.Position,
                        v.Normal,
                        Color4.White,
                        v.TextureCoordinate);
                }
                else if (vertType == typeof(VertexPositionNormalDiffuseTextureTwo))
                {
                    var v = l0.Mesh.verticesVertexPositionNormalDiffuseTextureTwo[i];
                    vert = new VertexPositionNormalColorTexture(
                        v.Position,
                        v.Normal,
                        v.Diffuse,
                        v.TextureCoordinate);
                }
                else
                {
                    throw new NotImplementedException("Asteroids: " + vertType.FullName);
                }
                vert.Position = VectorMath.Transform(vert.Position, transform);
                vert.Normal   = (norm * new Vector4(vert.Normal, 0)).Xyz;
                verts.Add(vert);
            }
            for (int i = l0.StartMesh; i < l0.StartMesh + l0.MeshCount; i++)
            {
                var m          = l0.Mesh.Meshes[i];
                var baseVertex = vertOffset + l0.StartVertex + m.StartVertex;
                int indexStart = m.TriangleStart;
                int indexCount = m.NumRefVertices;
                for (int j = indexStart; j < indexStart + indexCount; j++)
                {
                    var idx = baseVertex + l0.Mesh.Indices[j];
                    if (idx > ushort.MaxValue)
                    {
                        throw new Exception();
                    }
                    indices.Add((ushort)idx);
                }
            }
        }
Esempio n. 7
0
        public void DrawPerspective(
            Texture2D texture,
            Vector3 pos,
            Matrix4 world,
            Vector2 size,
            Color4 color,
            Vector2 topleft,
            Vector2 topright,
            Vector2 bottomleft,
            Vector2 bottomright,
            Vector3 normal,
            float angle,
            int layer,
            BlendMode blend = BlendMode.Normal
            )
        {
            var upref = Vector3.UnitY;

            if ((Vector3.UnitY - normal).Length < float.Epsilon)
            {
                upref = Vector3.UnitZ;
            }
            var srcright = Vector3.Cross(normal, upref);
            var srcup    = Vector3.Cross(srcright, normal);

            srcup.Normalize();
            srcright.Normalize();
            Vector3 up, right;

            if (Math.Abs(angle) < float.Epsilon)
            {
                up    = srcup;
                right = srcright;
            }
            else
            {
                var s = (float)Math.Sin(angle);
                var c = (float)Math.Cos(angle);
                up    = c * srcright - s * srcup;
                right = s * srcright + c * srcup;
            }
            rendat[billboardCount] = new RenderData(
                texture,
                blend,
                RenderKind.Rect,
                (ushort)vertexCountRect
                );
            //
            var sz = 0.5f * size;

            verticesRect[vertexCountRect++] = new VertexPositionColorTexture(
                VectorMath.Transform(
                    pos - right * sz.X - up * sz.Y, world
                    ),
                color,
                bottomleft
                );
            verticesRect[vertexCountRect++] = new VertexPositionColorTexture(
                VectorMath.Transform(
                    pos + right * sz.X - up * sz.Y, world
                    ),
                color,
                topleft
                );
            verticesRect[vertexCountRect++] = new VertexPositionColorTexture(
                VectorMath.Transform(
                    pos - right * sz.X + up * sz.Y, world
                    ),
                color,
                bottomright
                );
            verticesRect[vertexCountRect++] = new VertexPositionColorTexture(
                VectorMath.Transform(
                    pos + right * sz.X + up * sz.Y, world
                    ),
                color,
                topright
                );
            //
            var z = RenderHelpers.GetZ(camera.Position, pos);

            buffer.AddCommand(
                this,
                rendat[billboardCount].GetHashCode(),
                billboardCount,
                layer,
                z
                );
            billboardCount++;
        }
Esempio n. 8
0
		public Vector3 InverseTransformPoint(Vector3 input)
		{
			var tf = GetTransform();
			tf.Invert();
			return VectorMath.Transform(input, tf);
		}
Esempio n. 9
0
 public void Transform(ref Matrix4 matrix, out BoundingSphere result)
 {
     result.Center = VectorMath.Transform(this.Center, matrix);
     result.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33)))));
 }