Exemple #1
0
            public LodControl(Formats.Msts.Models.LodControl MSTSlod_control, Helpers.TextureFlags textureFlags, ShapeFile sFile, SharedShape sharedShape)
            {
#if DEBUG_SHAPE_HIERARCHY
                Trace.WriteLine("  LOD control:");
#endif
                DistanceLevels = (from Formats.Msts.Models.DistanceLevel level in MSTSlod_control.DistanceLevels
                                  select new DistanceLevel(level, textureFlags, sFile, sharedShape)).ToArray();
                if (DistanceLevels.Length == 0)
                {
                    throw new InvalidDataException("Shape file missing distance_level");
                }
            }
Exemple #2
0
            public SubObject(Formats.Msts.Models.SubObject sub_object, ref int totalPrimitiveIndex, int[] hierarchy, Helpers.TextureFlags textureFlags, ShapeFile sFile, SharedShape sharedShape)
#endif
            {
#if DEBUG_SHAPE_HIERARCHY
                var debugShapeHierarchy = new StringBuilder();
                debugShapeHierarchy.AppendFormat("      Sub object {0}:\n", subObjectIndex);
#endif
                var vertexBufferSet = new VertexBufferSet(sub_object, sFile, viewer.RenderProcess.GraphicsDevice);
#if DEBUG_SHAPE_NORMALS
                var debugNormalsMaterial = viewer.MaterialManager.Load("DebugNormals");
#endif

#if OPTIMIZE_SHAPES_ON_LOAD
                var primitiveMaterials = sub_object.primitives.Cast <primitive>().Select((primitive) =>
#else
                var primitiveIndex = 0;
#if DEBUG_SHAPE_NORMALS
                ShapePrimitives = new ShapePrimitive[sub_object.primitives.Count * 2];
#else
                ShapePrimitives = new ShapePrimitive[sub_object.Primitives.Count];
#endif
                foreach (Primitive primitive in sub_object.Primitives)
#endif
                {
                    var primitiveState          = sFile.Shape.PrimaryStates[primitive.PrimitiveStateIndex];
                    var vertexState             = sFile.Shape.VertexStates[primitiveState.VertexStateIndex];
                    var lightModelConfiguration = sFile.Shape.LightModelConfigs[vertexState.LightConfigIndex];
                    var options = SceneryMaterialOptions.None;

                    // Validate hierarchy position.
                    var hierarchyIndex = vertexState.MatrixIndex;
                    while (hierarchyIndex != -1)
                    {
                        if (hierarchyIndex < 0 || hierarchyIndex >= hierarchy.Length)
                        {
                            var hierarchyList = new List <int>();
                            hierarchyIndex    = vertexState.MatrixIndex;
                            while (hierarchyIndex >= 0 && hierarchyIndex < hierarchy.Length)
                            {
                                hierarchyList.Add(hierarchyIndex);
                                hierarchyIndex = hierarchy[hierarchyIndex];
                            }
                            hierarchyList.Add(hierarchyIndex);
                            Trace.TraceWarning("Ignored invalid primitive hierarchy {1} in shape {0}", sharedShape.FilePath, String.Join(" ", hierarchyList.Select(hi => hi.ToString()).ToArray()));
                            break;
                        }
                        hierarchyIndex = hierarchy[hierarchyIndex];
                    }

                    if (lightModelConfiguration.UVOperations.Count > 0)
                    {
                        if (lightModelConfiguration.UVOperations[0].TextureAddressMode - 1 >= 0 && lightModelConfiguration.UVOperations[0].TextureAddressMode - 1 < UVTextureAddressModeMap.Length)
                        {
                            options |= UVTextureAddressModeMap[lightModelConfiguration.UVOperations[0].TextureAddressMode - 1];
                        }
                        else if (!shapeWarnings.Contains("texture_addressing_mode:" + lightModelConfiguration.UVOperations[0].TextureAddressMode))
                        {
                            Trace.TraceInformation("Skipped unknown texture addressing mode {1} first seen in shape {0}", sharedShape.FilePath, lightModelConfiguration.UVOperations[0].TextureAddressMode);
                            shapeWarnings.Add("texture_addressing_mode:" + lightModelConfiguration.UVOperations[0].TextureAddressMode);
                        }
                    }

                    if (primitiveState.AlphaTestMode == 1)
                    {
                        options |= SceneryMaterialOptions.AlphaTest;
                    }

                    if (ShaderNames.ContainsKey(sFile.Shape.ShaderNames[primitiveState.ShaderIndex]))
                    {
                        options |= ShaderNames[sFile.Shape.ShaderNames[primitiveState.ShaderIndex]];
                    }
                    else if (!shapeWarnings.Contains("shader_name:" + sFile.Shape.ShaderNames[primitiveState.ShaderIndex]))
                    {
                        Trace.TraceInformation("Skipped unknown shader name {1} first seen in shape {0}", sharedShape.FilePath, sFile.Shape.ShaderNames[primitiveState.ShaderIndex]);
                        shapeWarnings.Add("shader_name:" + sFile.Shape.ShaderNames[primitiveState.ShaderIndex]);
                    }

                    if (12 + vertexState.LightMatrixIndex >= 0 && 12 + vertexState.LightMatrixIndex < VertexLightModeMap.Length)
                    {
                        options |= VertexLightModeMap[12 + vertexState.LightMatrixIndex];
                    }
                    else if (!shapeWarnings.Contains("lighting_model:" + vertexState.LightMatrixIndex))
                    {
                        Trace.TraceInformation("Skipped unknown lighting model index {1} first seen in shape {0}", sharedShape.FilePath, vertexState.LightMatrixIndex);
                        shapeWarnings.Add("lighting_model:" + vertexState.LightMatrixIndex);
                    }

                    if ((textureFlags & Helpers.TextureFlags.Night) != 0)
                    {
                        options |= SceneryMaterialOptions.NightTexture;
                    }

                    if ((textureFlags & Helpers.TextureFlags.Underground) != 0)
                    {
                        options |= SceneryMaterialOptions.UndergroundTexture;
                    }

                    Material material;
                    if (primitiveState.TextureIndices.Length != 0)
                    {
                        var texture   = sFile.Shape.Textures[primitiveState.TextureIndices[0]];
                        var imageName = sFile.Shape.ImageNames[texture.ImageIndex];
                        if (String.IsNullOrEmpty(sharedShape.ReferencePath))
                        {
                            material = viewer.MaterialManager.Load("Scenery", Helpers.GetRouteTextureFile(viewer.Simulator, textureFlags, imageName), (int)options, texture.MipMapLODBias);
                        }
                        else
                        {
                            material = viewer.MaterialManager.Load("Scenery", Helpers.GetTextureFile(viewer.Simulator, textureFlags, sharedShape.ReferencePath, imageName), (int)options, texture.MipMapLODBias);
                        }
                    }
                    else
                    {
                        material = viewer.MaterialManager.Load("Scenery", null, (int)options);
                    }

#if DEBUG_SHAPE_HIERARCHY
                    debugShapeHierarchy.AppendFormat("        Primitive {0,-2}: pstate={1,-2} vstate={2,-2} lstate={3,-2} matrix={4,-2}", primitiveIndex, primitive.prim_state_idx, primitiveState.ivtx_state, vertexState.LightCfgIdx, vertexState.imatrix);
                    var debugMatrix = vertexState.imatrix;
                    while (debugMatrix >= 0)
                    {
                        debugShapeHierarchy.AppendFormat(" {0}", sharedShape.MatrixNames[debugMatrix]);
                        debugMatrix = hierarchy[debugMatrix];
                    }
                    debugShapeHierarchy.Append("\n");
#endif

#if OPTIMIZE_SHAPES_ON_LOAD
                    return(new { Key = material.ToString() + "/" + vertexState.imatrix.ToString(), Primitive = primitive, Material = material, HierachyIndex = vertexState.imatrix });
                }).ToArray();
Exemple #3
0
 public SubObject(sub_object sub_object, ref int totalPrimitiveIndex, int[] hierarchy, Helpers.TextureFlags textureFlags, int subObjectIndex, SFile sFile, SharedShape sharedShape)
Exemple #4
0
            public DistanceLevel(Formats.Msts.Models.DistanceLevel MSTSdistance_level, Helpers.TextureFlags textureFlags, ShapeFile sFile, SharedShape sharedShape)
            {
#if DEBUG_SHAPE_HIERARCHY
                Trace.WriteLine("    Distance level {0}: hierarchy={1}", MSTSdistance_level.distance_level_header.dlevel_selection, String.Join(" ", MSTSdistance_level.distance_level_header.hierarchy.Select(i => i.ToString()).ToArray()));
#endif
                ViewingDistance = MSTSdistance_level.DistanceLevelHeader.DistanceLevelSelection;
                // TODO, work out ViewShereRadius from all sub_object radius and centers.
                if (sFile.Shape.Volumes.Count > 0)
                {
                    ViewSphereRadius = sFile.Shape.Volumes[0].Radius;
                }
                else
                {
                    ViewSphereRadius = 100;
                }

                var index = 0;
#if DEBUG_SHAPE_HIERARCHY
                var subObjectIndex = 0;
                SubObjects = (from sub_object obj in MSTSdistance_level.sub_objects
                              select new SubObject(obj, ref index, MSTSdistance_level.distance_level_header.hierarchy, textureFlags, subObjectIndex++, sFile, sharedShape)).ToArray();
#else
                SubObjects = (from Formats.Msts.Models.SubObject obj in MSTSdistance_level.SubObjects
                              select new SubObject(obj, ref index, MSTSdistance_level.DistanceLevelHeader.Hierarchy, textureFlags, sFile, sharedShape)).ToArray();
#endif
                if (SubObjects.Length == 0)
                {
                    throw new InvalidDataException("Shape file missing sub_object");
                }
            }