Beispiel #1
0
        static void SetLight(ShaderVariables shader, bool hasSpotlight, int i, ref RenderLight lt)
        {
            float kind = 0;

            if (lt.Kind == LightKind.Point || lt.Kind == LightKind.Spotlight)
            {
                kind = 1;
            }
            else if (lt.Kind == LightKind.PointAttenCurve)
            {
                kind = 2;
            }
            shader.SetLightsPos(i, new Vector4(lt.Kind == LightKind.Directional ? lt.Direction : lt.Position, kind));
            shader.SetLightsColorRange(i, new Vector4(lt.Color.R, lt.Color.G, lt.Color.B, lt.Range));
            shader.SetLightsAttenuation(i, lt.Attenuation);
            if (hasSpotlight)
            {
                if (lt.Kind == LightKind.Spotlight)
                {
                    shader.SetLightsDir(i, lt.Direction);
                    shader.SetSpotlightParams(i, new Vector3(lt.Falloff, (float)(Math.Cos(lt.Theta / 2.0)), (float)(Math.Cos(lt.Phi / 2.0))));
                }
                else if (lt.Kind == LightKind.Point || lt.Kind == LightKind.PointAttenCurve)
                {
                    shader.SetSpotlightParams(i, Vector3.Zero);
                }
            }
        }
Beispiel #2
0
        public void GetLighting(out bool FogEnabled, out Color4?Ambient, out Vector2 FogRange, out Color4 FogColor, out RenderLight?lightning)
        {
            Ambient    = Nebula.AmbientColor;
            FogEnabled = Nebula.FogEnabled;
            FogColor   = GetFogColor();
            FogRange   = Nebula.FogRange;
            var ex = GetExclusion(camera.Position);

            if (ex != null)
            {
                var factor = CalculateTransition(ex.Zone);
                FogRange.Y = MathHelper.Lerp(FogRange.Y, ex.FogFar, factor);;
            }
            lightning = null;
            if (dynLightningActive)
            {
                var rl = new RenderLight();
                rl.Kind  = LightKind.Point;
                rl.Color = new Color3f(Nebula.DynamicLightningColor.R,
                                       Nebula.DynamicLightningColor.G,
                                       Nebula.DynamicLightningColor.B);
                rl.Position    = dynamicLightningPos;
                rl.Attenuation = new Vector3(1, 0, 0.0000055f);
                rl.Range       = (int)Nebula.FogRange.Y;
                lightning      = rl;
            }
        }
Beispiel #3
0
 public void Add(RenderLight light)
 {
     if (Count == 8)
     {
         throw new Exception("Too many lights!");
     }
     SetLight(ref this, Count++, light);
 }
Beispiel #4
0
            static void SetLight(ref LightsArray lt, int index, RenderLight value)
            {
                fixed(LightsArray *lights = &lt)
                {
                    var ptr = (ulong)lights;

                    ptr += sizeof(int);
                    ptr += (ulong)(Stride * index);
                    *((RenderLight *)ptr) = value;
                }
            }
Beispiel #5
0
        //Returns whether or not a spotlight can be culled
        static bool SpotlightTest(ref RenderLight light, Vector3 objPos, float objRadius)
        {
            var V                = objPos - light.Position;
            var VLenSq           = V.LengthSquared;
            var V1len            = Vector3.Dot(V, light.Direction);
            var distClosestPoint = (float)(Math.Cos(light.Phi) * Math.Sqrt(VLenSq - V1len * V1len) - V1len * Math.Sin(light.Phi));
            var angleCull        = distClosestPoint > objRadius;
            var frontCull        = V1len > objRadius + light.Range;
            var backCull         = V1len < -objRadius;

            return(angleCull || frontCull || backCull);
        }
Beispiel #6
0
        ThnEntity GetEntity(LuaTable table)
        {
            object o;

            var e = new ThnEntity();

            e.Name = (string)table["entity_name"];
            e.Type = ThnEnum.Check <EntityTypes>(table["type"]);
            if (table.TryGetValue("srt_grp", out o))
            {
                e.SortGroup = (int)(float)table["srt_grp"];
            }
            if (table.TryGetValue("usr_flg", out o))
            {
                e.UserFlag = (int)(float)table["usr_flg"];
            }
            if (table.TryGetValue("lt_grp", out o))
            {
                e.LightGroup = (int)(float)table["lt_grp"];
            }

            Vector3 tmp;

            if (table.TryGetVector3("ambient", out tmp))
            {
                e.Ambient = tmp;
            }
            if (table.TryGetVector3("up", out tmp))
            {
                e.Up = tmp;
            }
            if (table.TryGetVector3("front", out tmp))
            {
                e.Front = tmp;
            }

            if (table.TryGetValue("template_name", out o))
            {
                e.Template = (string)o;
            }
            if (table.TryGetValue("flags", out o))
            {
                if (o is float)
                {
                    e.ObjectFlags = ConvertFlags(e.Type, table);
                }
                else
                {
                    e.ObjectFlags = (ThnObjectFlags)o;
                }
            }
            if (table.TryGetValue("userprops", out o))
            {
                var usrprops = (LuaTable)o;
                if (usrprops.TryGetValue("category", out o))
                {
                    e.MeshCategory = (string)o;
                }
                if (usrprops.TryGetValue("nofog", out o))
                {
                    e.NoFog = ThnEnum.Check <bool>(o);
                }
            }

            if (table.TryGetValue("spatialprops", out o))
            {
                var spatialprops = (LuaTable)o;
                if (spatialprops.TryGetVector3("pos", out tmp))
                {
                    e.Position = tmp;
                }
                if (spatialprops.TryGetValue("orient", out o))
                {
                    e.RotationMatrix = GetMatrix((LuaTable)o);
                }
            }

            if (table.TryGetValue("cameraprops", out o))
            {
                var cameraprops = (LuaTable)o;
                if (cameraprops.TryGetValue("fovh", out o))
                {
                    e.FovH = (float)o;
                }
                if (cameraprops.TryGetValue("hvaspect", out o))
                {
                    e.HVAspect = (float)o;
                }
            }
            if (table.TryGetValue("lightprops", out o))
            {
                var lightprops = (LuaTable)o;
                e.LightProps = new ThnLightProps();
                if (lightprops.TryGetValue("on", out o))
                {
                    e.LightProps.On = ThnEnum.Check <bool>(o);
                }
                else
                {
                    e.LightProps.On = true;
                }
                var r = new RenderLight();
                r.Position = e.Position.Value;
                if (lightprops.TryGetValue("type", out o))
                {
                    var tp = ThnEnum.Check <LightTypes>(o);
                    if (tp == LightTypes.Point)
                    {
                        r.Kind = LightKind.Point;
                    }
                    if (tp == LightTypes.Direct)
                    {
                        r.Kind = LightKind.Directional;
                    }
                    if (tp == LightTypes.Spotlight)
                    {
                        r.Kind    = LightKind.Spotlight;
                        r.Falloff = 1f;
                    }
                }
                else
                {
                    throw new Exception("Light without type");
                }
                if (lightprops.TryGetVector3("diffuse", out tmp))
                {
                    r.Color = new Color4(tmp.X, tmp.Y, tmp.Z, 1);
                }
                if (lightprops.TryGetVector3("direction", out tmp))
                {
                    r.Direction = tmp;
                }
                if (lightprops.TryGetValue("range", out o))
                {
                    r.Range = (int)(float)o;
                }
                if (lightprops.TryGetValue("theta", out o))
                {
                    r.Theta = r.Phi = (float)o;
                }
                if (lightprops.TryGetVector3("atten", out tmp))
                {
                    r.Attenuation = new Vector4(tmp, 0);
                }
                e.LightProps.Render = r;
            }
            if (table.TryGetValue("pathprops", out o))
            {
                var pathprops = (LuaTable)o;
                if (pathprops.TryGetValue("path_data", out o))
                {
                    e.Path = new MotionPath((string)o);
                }
            }
            return(e);
        }
Beispiel #7
0
        public GameData.StarSystem GetSystem(string id)
        {
            var legacy = fldata.Universe.FindSystem(id);

            if (fldata.Stars != null)
            {
                foreach (var txmfile in fldata.Stars.TextureFiles)
                {
                    resource.LoadTxm(Compatibility.VFS.GetPath(fldata.Freelancer.DataPath + txmfile));
                }
            }
            var sys = new GameData.StarSystem();

            sys.AmbientColor    = legacy.AmbientColor ?? Color4.White;
            sys.Name            = legacy.StridName;
            sys.Id              = legacy.Nickname;
            sys.BackgroundColor = legacy.SpaceColor ?? Color4.Black;
            sys.MusicSpace      = legacy.MusicSpace;
            sys.FarClip         = legacy.SpaceFarClip ?? 20000f;
            if (legacy.BackgroundBasicStarsPath != null)
            {
                try {
                    sys.StarsBasic = resource.GetDrawable(legacy.BackgroundBasicStarsPath);
                } catch (Exception) {
                    sys.StarsBasic = null;
                    FLLog.Error("System", "Failed to load starsphere " + legacy.BackgroundBasicStarsPath);
                }
            }

            if (legacy.BackgroundComplexStarsPath != null)
            {
                try {
                    sys.StarsComplex = resource.GetDrawable(legacy.BackgroundComplexStarsPath);
                } catch (Exception) {
                    sys.StarsComplex = null;
                    FLLog.Error("System", "Failed to load starsphere " + legacy.BackgroundComplexStarsPath);
                }
            }

            if (legacy.BackgroundNebulaePath != null)
            {
                try {
                    sys.StarsNebula = resource.GetDrawable(legacy.BackgroundNebulaePath);
                } catch (Exception) {
                    sys.StarsNebula = null;
                    FLLog.Error("System", "Failed to load starsphere " + legacy.BackgroundNebulaePath);
                }
            }

            if (legacy.LightSources != null)
            {
                foreach (var src in legacy.LightSources)
                {
                    var lt = new RenderLight();
                    lt.Color       = src.Color.Value;
                    lt.Position    = src.Pos.Value;
                    lt.Range       = src.Range.Value;
                    lt.Direction   = src.Direction ?? new Vector3(0, 0, 1);
                    lt.Kind        = ((src.Type ?? Legacy.Universe.LightType.Point) == Legacy.Universe.LightType.Point) ? LightKind.Point : LightKind.Directional;
                    lt.Attenuation = new Vector4(src.Attenuation ?? Vector3.UnitY, 0);
                    if (src.AttenCurve != null)
                    {
                        lt.Kind        = LightKind.PointAttenCurve;
                        lt.Attenuation = ApproximateCurve.GetCubicFunction(
                            fldata.Graphs.FindFloatGraph(src.AttenCurve).Points.ToArray()
                            );
                    }
                    sys.LightSources.Add(lt);
                }
            }
            foreach (var obj in legacy.Objects)
            {
                sys.Objects.Add(GetSystemObject(obj));
            }
            if (legacy.Zones != null)
            {
                foreach (var zne in legacy.Zones)
                {
                    var z = new GameData.Zone();
                    z.Nickname     = zne.Nickname;
                    z.EdgeFraction = zne.EdgeFraction ?? 0.25f;
                    z.Position     = zne.Pos.Value;
                    if (zne.Rotate != null)
                    {
                        var r = zne.Rotate.Value;

                        var qx = Quaternion.FromEulerAngles(
                            MathHelper.DegreesToRadians(r.X),
                            MathHelper.DegreesToRadians(r.Y),
                            MathHelper.DegreesToRadians(r.Z)
                            );
                        z.RotationMatrix = Matrix4.CreateFromQuaternion(qx);
                        z.RotationAngles = new Vector3(
                            MathHelper.DegreesToRadians(r.X),
                            MathHelper.DegreesToRadians(r.Y),
                            MathHelper.DegreesToRadians(r.Z)
                            );
                    }
                    else
                    {
                        z.RotationMatrix = Matrix4.Identity;
                        z.RotationAngles = Vector3.Zero;
                    }
                    switch (zne.Shape.Value)
                    {
                    case Legacy.Universe.ZoneShape.ELLIPSOID:
                        z.Shape = new GameData.ZoneEllipsoid(z,
                                                             zne.Size.Value.X,
                                                             zne.Size.Value.Y,
                                                             zne.Size.Value.Z
                                                             );
                        break;

                    case Legacy.Universe.ZoneShape.SPHERE:
                        z.Shape = new GameData.ZoneSphere(z,
                                                          zne.Size.Value.X
                                                          );
                        break;

                    case Legacy.Universe.ZoneShape.BOX:
                        z.Shape = new GameData.ZoneBox(z,
                                                       zne.Size.Value.X,
                                                       zne.Size.Value.Y,
                                                       zne.Size.Value.Z
                                                       );
                        break;

                    case Legacy.Universe.ZoneShape.CYLINDER:
                        z.Shape = new GameData.ZoneCylinder(z,
                                                            zne.Size.Value.X,
                                                            zne.Size.Value.Y
                                                            );
                        break;

                    case Legacy.Universe.ZoneShape.RING:
                        z.Shape = new GameData.ZoneRing(z,
                                                        zne.Size.Value.X,
                                                        zne.Size.Value.Y,
                                                        zne.Size.Value.Z
                                                        );
                        break;

                    default:
                        Console.WriteLine(zne.Nickname);
                        Console.WriteLine(zne.Shape.Value);
                        throw new NotImplementedException();
                    }
                    sys.Zones.Add(z);
                }
            }
            if (legacy.Asteroids != null)
            {
                foreach (var ast in legacy.Asteroids)
                {
                    sys.AsteroidFields.Add(GetAsteroidField(sys, ast));
                }
            }

            if (legacy.Nebulae != null)
            {
                foreach (var nbl in legacy.Nebulae)
                {
                    sys.Nebulae.Add(GetNebula(sys, nbl));
                }
            }
            return(sys);
        }
Beispiel #8
0
        ThnEntity GetEntity(LuaTable table)
        {
            object o;

            var e = new ThnEntity();

            e.Name = (string)table["entity_name"];
            e.Type = ThnTypes.Convert <EntityTypes>(table["type"]);
            if (table.TryGetValue("srt_grp", out o))
            {
                e.SortGroup = (int)(float)table["srt_grp"];
            }
            if (table.TryGetValue("usr_flg", out o))
            {
                e.UserFlag = (int)(float)table["usr_flg"];
            }
            if (table.TryGetValue("lt_grp", out o))
            {
                e.LightGroup = (int)(float)table["lt_grp"];
            }

            Vector3 tmp;

            if (table.TryGetVector3("ambient", out tmp))
            {
                e.Ambient = tmp;
            }
            if (table.TryGetVector3("up", out tmp))
            {
                e.Up = tmp;
            }
            if (table.TryGetVector3("front", out tmp))
            {
                e.Front = tmp;
            }

            if (table.TryGetValue("template_name", out o))
            {
                e.Template = (string)o;
            }
            if (table.TryGetValue("flags", out o))
            {
                if (o is float)
                {
                    e.ObjectFlags = ConvertFlags(e.Type, table);
                }
                else
                {
                    e.ObjectFlags = (ThnObjectFlags)o;
                }
            }
            if (table.TryGetValue("userprops", out o))
            {
                var usrprops = (LuaTable)o;
                if (usrprops.TryGetValue("category", out o))
                {
                    e.MeshCategory = (string)o;
                }
                if (usrprops.TryGetValue("nofog", out o))
                {
                    e.NoFog = ThnTypes.Convert <bool>(o);
                }
                if (usrprops.TryGetValue("Actor", out o))
                {
                    e.Actor = o.ToString();
                }
                if (usrprops.TryGetValue("TextString", out o))
                {
                    e.DisplayText         = new ThnDisplayText();
                    e.DisplayText.TextIDS = FuzzyInt(o);
                    if (usrprops.TryGetValue("TextStart", out o))
                    {
                        e.DisplayText.Start = FuzzyFloat(o);
                    }
                }

                if (usrprops.TryGetValue("main_object", out o))
                {
                    e.MainObject = true;
                }
            }
            if (table.TryGetValue("audioprops", out o))
            {
                var aprops = (LuaTable)o;
                e.AudioProps = new ThnAudioProps();
                if (aprops.TryGetValue("rmix", out o))
                {
                    e.AudioProps.Rmix = (float)o;
                }
                if (aprops.TryGetValue("ain", out o))
                {
                    e.AudioProps.Ain = (float)o;
                }
                if (aprops.TryGetValue("dmax", out o))
                {
                    e.AudioProps.Dmax = (float)o;
                }
                if (aprops.TryGetValue("atout", out o))
                {
                    e.AudioProps.Atout = (float)o;
                }
                if (aprops.TryGetValue("pan", out o))
                {
                    e.AudioProps.Pan = (float)o;
                }
                if (aprops.TryGetValue("dmin", out o))
                {
                    e.AudioProps.Dmin = (float)o;
                }
                if (aprops.TryGetValue("aout", out o))
                {
                    e.AudioProps.Aout = (float)o;
                }
                if (aprops.TryGetValue("attenuation", out o))
                {
                    e.AudioProps.Attenuation = (float)o;
                }
            }
            if (table.TryGetValue("spatialprops", out o))
            {
                var spatialprops = (LuaTable)o;
                if (spatialprops.TryGetVector3("pos", out tmp))
                {
                    e.Position = tmp;
                }
                if (spatialprops.TryGetValue("orient", out o))
                {
                    e.RotationMatrix = GetMatrix((LuaTable)o);
                }
            }

            if (table.TryGetValue("cameraprops", out o))
            {
                var cameraprops = (LuaTable)o;
                if (cameraprops.TryGetValue("fovh", out o))
                {
                    e.FovH = (float)o;
                }
                if (cameraprops.TryGetValue("hvaspect", out o))
                {
                    e.HVAspect = (float)o;
                }
                if (cameraprops.TryGetValue("nearplane", out o))
                {
                    e.NearPlane = (float)o;
                }
                if (cameraprops.TryGetValue("farplane", out o))
                {
                    e.FarPlane = (float)o;
                }
            }
            if (table.TryGetValue("lightprops", out o))
            {
                var lightprops = (LuaTable)o;
                e.LightProps = new ThnLightProps();
                if (lightprops.TryGetValue("on", out o))
                {
                    e.LightProps.On = ThnTypes.Convert <bool>(o);
                }
                else
                {
                    e.LightProps.On = true;
                }
                var r = new RenderLight();
                r.Position = e.Position.Value;
                if (lightprops.TryGetValue("type", out o))
                {
                    var tp = ThnTypes.Convert <LightTypes>(o);
                    if (tp == LightTypes.Point)
                    {
                        r.Kind = LightKind.Point;
                    }
                    if (tp == LightTypes.Direct)
                    {
                        r.Kind = LightKind.Directional;
                    }
                    if (tp == LightTypes.Spotlight)
                    {
                        r.Kind    = LightKind.Spotlight;
                        r.Falloff = 1f;
                    }
                }
                else
                {
                    throw new Exception("Light without type");
                }
                if (lightprops.TryGetVector3("diffuse", out tmp))
                {
                    r.Color = new Color3f(tmp.X, tmp.Y, tmp.Z);
                }
                if (lightprops.TryGetVector3("ambient", out tmp))
                {
                    r.Ambient = new Color3f(tmp.X, tmp.Y, tmp.Z);
                }
                if (lightprops.TryGetVector3("direction", out tmp))
                {
                    r.Direction = tmp;
                }
                if (lightprops.TryGetValue("range", out o))
                {
                    r.Range = (int)(float)o;
                }
                if (lightprops.TryGetValue("theta", out o))
                {
                    r.Theta = r.Phi = (float)o;
                }
                if (lightprops.TryGetVector3("atten", out tmp))
                {
                    r.Attenuation = tmp;
                }
                e.LightProps.Render = r;
            }
            if (table.TryGetValue("pathprops", out o))
            {
                var pathprops = (LuaTable)o;
                if (pathprops.TryGetValue("path_data", out o))
                {
                    e.Path = new MotionPath((string)o);
                }
            }
            return(e);
        }