Ejemplo n.º 1
0
        private static void WriteMap(string statement, ObjMaterialMap map, StreamWriter stream)
        {
            // TODO: write tests

            stream.Write(statement);

            if (!map.IsHorizontalBlendingEnabled)
            {
                stream.Write(" -blenu off");
            }

            if (!map.IsVerticalBlendingEnabled)
            {
                stream.Write(" -blenv off");
            }

            if (map.BumpMultiplier != 0.0f)
            {
                stream.Write(" -bm ");
                stream.Write(map.BumpMultiplier.ToString("F6", CultureInfo.InvariantCulture));
            }

            if (map.Boost != 0.0f)
            {
                stream.Write(" -boost ");
                stream.Write(map.Boost.ToString("F6", CultureInfo.InvariantCulture));
            }

            if (map.IsColorCorrectionEnabled)
            {
                stream.Write(" -cc on");
            }

            if (map.IsClampingEnabled)
            {
                stream.Write(" -clamp on");
            }

            if (map.ScalarChannel != ObjMapChannel.Luminance)
            {
                stream.Write(" -imfchan ");

                switch (map.ScalarChannel)
                {
                case ObjMapChannel.Red:
                    stream.Write("r");
                    break;

                case ObjMapChannel.Green:
                    stream.Write("g");
                    break;

                case ObjMapChannel.Blue:
                    stream.Write("b");
                    break;

                case ObjMapChannel.Matte:
                    stream.Write("m");
                    break;

                case ObjMapChannel.Depth:
                    stream.Write("z");
                    break;

                default:
                    stream.Write("l");
                    break;
                }
            }

            if (map.ModifierBase != 0.0f || map.ModifierGain != 1.0f)
            {
                stream.Write(" -mm ");
                stream.Write(map.ModifierBase.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(map.ModifierGain.ToString("F6", CultureInfo.InvariantCulture));
            }

            ObjVector3 offset = map.Offset;

            if (offset.X != 0.0f || offset.Y != 0.0f || offset.Z != 0.0f)
            {
                stream.Write(" -o ");
                stream.Write(offset.X.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(offset.Y.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(offset.Z.ToString("F6", CultureInfo.InvariantCulture));
            }

            ObjVector3 scale = map.Scale;

            if (scale.X != 1.0f || scale.Y != 1.0f || scale.Z != 1.0f)
            {
                stream.Write(" -s ");
                stream.Write(scale.X.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(scale.Y.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(scale.Z.ToString("F6", CultureInfo.InvariantCulture));
            }

            ObjVector3 turbulence = map.Turbulence;

            if (turbulence.X != 0.0f || turbulence.Y != 0.0f || turbulence.Z != 0.0f)
            {
                stream.Write(" -t ");
                stream.Write(turbulence.X.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(turbulence.Y.ToString("F6", CultureInfo.InvariantCulture));
                stream.Write(' ');
                stream.Write(turbulence.Z.ToString("F6", CultureInfo.InvariantCulture));
            }

            if (map.TextureResolution != 0)
            {
                stream.Write(" -texres ");
                stream.Write(map.TextureResolution);
            }

            stream.Write(' ');
            stream.WriteLine(map.FileName);
        }
Ejemplo n.º 2
0
        private static ObjMaterialMap ParseMaterialMap(string statement, string[] values)
        {
            var map = new ObjMaterialMap();

            for (int index = 0; index < values.Length;)
            {
                index++;

                if (values.Length - index < 1)
                {
                    throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a filename."));
                }

                switch (values[index].ToLowerInvariant())
                {
                case "-type":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -type option must specify a value."));
                    }

                    index++;
                    break;

                case "-blenu":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify a value."));
                    }

                    if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsHorizontalBlendingEnabled = true;
                    }
                    else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsHorizontalBlendingEnabled = false;
                    }
                    else
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify on or off."));
                    }

                    index++;
                    break;

                case "-blenv":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify a value."));
                    }

                    if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsVerticalBlendingEnabled = true;
                    }
                    else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsVerticalBlendingEnabled = false;
                    }
                    else
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify on or off."));
                    }

                    index++;
                    break;

                case "-bm":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -bm option must specify a value."));
                    }

                    map.BumpMultiplier = float.Parse(values[index + 1], CultureInfo.InvariantCulture);

                    index++;
                    break;

                case "-boost":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -boost option must specify a value."));
                    }

                    map.Boost = float.Parse(values[index + 1], CultureInfo.InvariantCulture);

                    index++;
                    break;

                case "-cc":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify a value."));
                    }

                    if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsColorCorrectionEnabled = true;
                    }
                    else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsColorCorrectionEnabled = false;
                    }
                    else
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify on or off."));
                    }

                    index++;
                    break;

                case "-clamp":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify a value."));
                    }

                    if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsClampingEnabled = true;
                    }
                    else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
                    {
                        map.IsClampingEnabled = false;
                    }
                    else
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify on or off."));
                    }

                    index++;
                    break;

                case "-imfchan":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must specify a value."));
                    }

                    switch (values[index + 1].ToLowerInvariant())
                    {
                    case "r":
                        map.ScalarChannel = ObjMapChannel.Red;
                        break;

                    case "g":
                        map.ScalarChannel = ObjMapChannel.Green;
                        break;

                    case "b":
                        map.ScalarChannel = ObjMapChannel.Blue;
                        break;

                    case "m":
                        map.ScalarChannel = ObjMapChannel.Matte;
                        break;

                    case "l":
                        map.ScalarChannel = ObjMapChannel.Luminance;
                        break;

                    case "z":
                        map.ScalarChannel = ObjMapChannel.Depth;
                        break;

                    default:
                        throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must specify a value in (r, g, b, m, l, z)."));
                    }

                    index++;
                    break;

                case "-mm":
                    if (values.Length - index < 3)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -mm option must specify a base and a gain."));
                    }

                    map.ModifierBase = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
                    map.ModifierGain = float.Parse(values[index + 2], CultureInfo.InvariantCulture);

                    index += 2;
                    break;

                case "-o":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -o option must specify at least 2 values."));
                    }

                    var offset = new ObjVector3();

                    offset.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture);

                    if (values.Length - index > 3)
                    {
                        offset.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture);

                        if (values.Length - index > 4)
                        {
                            offset.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture);
                            index++;
                        }

                        index++;
                    }

                    index++;

                    map.Offset = offset;
                    break;

                case "-s":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -s option must specify at least 2 values."));
                    }

                    var scale = new ObjVector3(1.0f, 1.0f, 1.0f);

                    scale.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture);

                    if (values.Length - index > 3)
                    {
                        scale.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture);

                        if (values.Length - index > 4)
                        {
                            scale.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture);
                            index++;
                        }

                        index++;
                    }

                    index++;

                    map.Scale = scale;
                    break;

                case "-t":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -t option must specify at least 2 values."));
                    }

                    var turbulence = new ObjVector3();

                    turbulence.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture);

                    if (values.Length - index > 3)
                    {
                        turbulence.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture);

                        if (values.Length - index > 4)
                        {
                            turbulence.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture);
                            index++;
                        }

                        index++;
                    }

                    index++;

                    map.Turbulence = turbulence;
                    break;

                case "-texres":
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " -texres option must specify a value."));
                    }

                    map.TextureResolution = int.Parse(values[index + 1], CultureInfo.InvariantCulture);

                    index++;
                    break;

                default:
                    if (!Path.HasExtension(values[index]))
                    {
                        throw new InvalidDataException("A filename must have an extension.");
                    }

                    map.FileName = values[index];
                    index++;

                    if (index != values.Length)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " has too many values."));
                    }

                    break;
                }
            }

            return(map);
        }