/// <summary>Creates a new ObjectState</summary> public ObjectState(StaticObject prototype) : this() { Prototype = prototype; }
/// <summary>Applies the MeshBuilder's data to a StaticObject</summary> public void Apply(ref StaticObject Object, bool EnableHacks = false) { if (TransformMatrix != Matrix4D.NoTransformation) { for (int i = 0; i < Vertices.Length; i++) { Vertices[i].Coordinates.Transform(TransformMatrix); } } if (Faces.Length != 0) { int mf = Object.Mesh.Faces.Length; int mm = Object.Mesh.Materials.Length; int mv = Object.Mesh.Vertices.Length; Array.Resize <MeshFace>(ref Object.Mesh.Faces, mf + Faces.Length); Array.Resize <MeshMaterial>(ref Object.Mesh.Materials, mm + Materials.Length); Array.Resize <VertexTemplate>(ref Object.Mesh.Vertices, mv + Vertices.Length); for (int i = 0; i < Vertices.Length; i++) { Object.Mesh.Vertices[mv + i] = Vertices[i]; } for (int i = 0; i < Faces.Length; i++) { Object.Mesh.Faces[mf + i] = Faces[i]; for (int j = 0; j < Object.Mesh.Faces[mf + i].Vertices.Length; j++) { Object.Mesh.Faces[mf + i].Vertices[j].Index += (ushort)mv; } Object.Mesh.Faces[mf + i].Material += (ushort)mm; } for (int i = 0; i < Materials.Length; i++) { if (EnableHacks && !string.IsNullOrEmpty(Materials[i].DaytimeTexture)) { if (!string.IsNullOrEmpty(Materials[i].NighttimeTexture)) { if (Materials[i].EmissiveColorUsed == false) { /* * Versions of openBVE prior to 1.7.0 rendered polygons with two defined textures as unlit * The new GL 3.2 renderer corrects this behaviour * Horrid workaround.... */ Materials[i].EmissiveColorUsed = true; Materials[i].EmissiveColor = Color24.White; } } } Object.Mesh.Materials[mm + i].Flags = new MaterialFlags(); if (Materials[i].EmissiveColorUsed) { Object.Mesh.Materials[mm + i].Flags |= MaterialFlags.Emissive; } if (Materials[i].TransparentColorUsed) { Object.Mesh.Materials[mm + i].Flags |= MaterialFlags.TransparentColor; } Object.Mesh.Materials[mm + i].Color = Materials[i].Color; Object.Mesh.Materials[mm + i].TransparentColor = Materials[i].TransparentColor; if (Materials[i].DaytimeTexture != null || Materials[i].Text != null) { Textures.Texture tday; if (Materials[i].Text != null) { Bitmap bitmap = null; if (Materials[i].DaytimeTexture != null) { bitmap = new Bitmap(Materials[i].DaytimeTexture); } Bitmap texture = TextOverlay.AddTextToBitmap(bitmap, Materials[i].Text, Materials[i].Font, 12, Materials[i].BackgroundColor, Materials[i].TextColor, Materials[i].TextPadding); currentHost.RegisterTexture(texture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tday); } else { if (Materials[i].TransparentColorUsed) { currentHost.RegisterTexture(Materials[i].DaytimeTexture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tday); } else { currentHost.RegisterTexture(Materials[i].DaytimeTexture, new TextureParameters(null, null), out tday); } } Object.Mesh.Materials[mm + i].DaytimeTexture = tday; } else { Object.Mesh.Materials[mm + i].DaytimeTexture = null; } Object.Mesh.Materials[mm + i].EmissiveColor = Materials[i].EmissiveColor; if (Materials[i].NighttimeTexture != null) { Textures.Texture tnight; if (Materials[i].TransparentColorUsed) { currentHost.RegisterTexture(Materials[i].NighttimeTexture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tnight); } else { currentHost.RegisterTexture(Materials[i].NighttimeTexture, new TextureParameters(null, null), out tnight); } Object.Mesh.Materials[mm + i].NighttimeTexture = tnight; } else { Object.Mesh.Materials[mm + i].NighttimeTexture = null; } Object.Mesh.Materials[mm + i].DaytimeNighttimeBlend = 0; Object.Mesh.Materials[mm + i].BlendMode = Materials[i].BlendMode; Object.Mesh.Materials[mm + i].GlowAttenuationData = Materials[i].GlowAttenuationData; Object.Mesh.Materials[mm + i].WrapMode = Materials[i].WrapMode; } } }
/// <summary>Applies the MeshBuilder's data to a StaticObject</summary> public void Apply(ref StaticObject Object) { if (TransformMatrix != Matrix4D.NoTransformation) { for (int i = 0; i < Vertices.Length; i++) { Vertices[i].Coordinates.Transform(TransformMatrix); } } if (Faces.Length != 0) { int mf = Object.Mesh.Faces.Length; int mm = Object.Mesh.Materials.Length; int mv = Object.Mesh.Vertices.Length; Array.Resize <MeshFace>(ref Object.Mesh.Faces, mf + Faces.Length); Array.Resize <MeshMaterial>(ref Object.Mesh.Materials, mm + Materials.Length); Array.Resize <VertexTemplate>(ref Object.Mesh.Vertices, mv + Vertices.Length); for (int i = 0; i < Vertices.Length; i++) { Object.Mesh.Vertices[mv + i] = Vertices[i]; } for (int i = 0; i < Faces.Length; i++) { Object.Mesh.Faces[mf + i] = Faces[i]; for (int j = 0; j < Object.Mesh.Faces[mf + i].Vertices.Length; j++) { Object.Mesh.Faces[mf + i].Vertices[j].Index += (ushort)mv; } Object.Mesh.Faces[mf + i].Material += (ushort)mm; } for (int i = 0; i < Materials.Length; i++) { Object.Mesh.Materials[mm + i].Flags = (byte)((Materials[i].EmissiveColorUsed ? MeshMaterial.EmissiveColorMask : 0) | (Materials[i].TransparentColorUsed ? MeshMaterial.TransparentColorMask : 0)); Object.Mesh.Materials[mm + i].Color = Materials[i].Color; Object.Mesh.Materials[mm + i].TransparentColor = Materials[i].TransparentColor; if (Materials[i].DaytimeTexture != null || Materials[i].Text != null) { Textures.Texture tday; if (Materials[i].Text != null) { Bitmap bitmap = null; if (Materials[i].DaytimeTexture != null) { bitmap = new Bitmap(Materials[i].DaytimeTexture); } Bitmap texture = TextOverlay.AddTextToBitmap(bitmap, Materials[i].Text, Materials[i].Font, 12, Materials[i].BackgroundColor, Materials[i].TextColor, Materials[i].TextPadding); currentHost.RegisterTexture(texture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tday); } else { if (Materials[i].TransparentColorUsed) { currentHost.RegisterTexture(Materials[i].DaytimeTexture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tday); } else { currentHost.RegisterTexture(Materials[i].DaytimeTexture, new TextureParameters(null, null), out tday); } } Object.Mesh.Materials[mm + i].DaytimeTexture = tday; } else { Object.Mesh.Materials[mm + i].DaytimeTexture = null; } Object.Mesh.Materials[mm + i].EmissiveColor = Materials[i].EmissiveColor; if (Materials[i].NighttimeTexture != null) { Textures.Texture tnight; if (Materials[i].TransparentColorUsed) { currentHost.RegisterTexture(Materials[i].NighttimeTexture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tnight); } else { currentHost.RegisterTexture(Materials[i].NighttimeTexture, new TextureParameters(null, null), out tnight); } Object.Mesh.Materials[mm + i].NighttimeTexture = tnight; } else { Object.Mesh.Materials[mm + i].NighttimeTexture = null; } Object.Mesh.Materials[mm + i].DaytimeNighttimeBlend = 0; Object.Mesh.Materials[mm + i].BlendMode = Materials[i].BlendMode; Object.Mesh.Materials[mm + i].GlowAttenuationData = Materials[i].GlowAttenuationData; Object.Mesh.Materials[mm + i].WrapMode = Materials[i].WrapMode; } } }
/// <summary>Creates a clone of this object.</summary> /// <param name="DaytimeTexture">The replacement daytime texture</param> /// <param name="NighttimeTexture">The replacement nighttime texture</param> /// <returns></returns> public StaticObject Clone(Textures.Texture DaytimeTexture, Textures.Texture NighttimeTexture) //Prefix is required or else MCS barfs { StaticObject Result = new StaticObject(currentHost) { StartingDistance = StartingDistance, EndingDistance = EndingDistance, Dynamic = Dynamic, Mesh = { Vertices = new VertexTemplate[Mesh.Vertices.Length] } }; // vertices for (int j = 0; j < Mesh.Vertices.Length; j++) { if (Mesh.Vertices[j] is ColoredVertex) { Result.Mesh.Vertices[j] = new ColoredVertex((ColoredVertex)Mesh.Vertices[j]); } else { Result.Mesh.Vertices[j] = new Vertex((Vertex)Mesh.Vertices[j]); } } // faces Result.Mesh.Faces = new MeshFace[Mesh.Faces.Length]; for (int j = 0; j < Mesh.Faces.Length; j++) { Result.Mesh.Faces[j].Flags = Mesh.Faces[j].Flags; Result.Mesh.Faces[j].Material = Mesh.Faces[j].Material; Result.Mesh.Faces[j].Vertices = new MeshFaceVertex[Mesh.Faces[j].Vertices.Length]; for (int k = 0; k < Mesh.Faces[j].Vertices.Length; k++) { Result.Mesh.Faces[j].Vertices[k] = Mesh.Faces[j].Vertices[k]; } } // materials Result.Mesh.Materials = new MeshMaterial[Mesh.Materials.Length]; for (int j = 0; j < Mesh.Materials.Length; j++) { Result.Mesh.Materials[j] = Mesh.Materials[j]; if (DaytimeTexture != null) { Result.Mesh.Materials[j].DaytimeTexture = DaytimeTexture; } else { Result.Mesh.Materials[j].DaytimeTexture = Mesh.Materials[j].DaytimeTexture; } if (NighttimeTexture != null) { Result.Mesh.Materials[j].NighttimeTexture = NighttimeTexture; } else { Result.Mesh.Materials[j].NighttimeTexture = Mesh.Materials[j].NighttimeTexture; } } return(Result); }
/// <inheritdoc/> public override UnifiedObject Transform(double NearDistance, double FarDistance) { StaticObject Result = (StaticObject)this.Clone(); int n = 0; double x2 = 0.0, x3 = 0.0, x6 = 0.0, x7 = 0.0; for (int i = 0; i < Result.Mesh.Vertices.Length; i++) { if (n == 2) { x2 = Result.Mesh.Vertices[i].Coordinates.X; } else if (n == 3) { x3 = Result.Mesh.Vertices[i].Coordinates.X; } else if (n == 6) { x6 = Result.Mesh.Vertices[i].Coordinates.X; } else if (n == 7) { x7 = Result.Mesh.Vertices[i].Coordinates.X; } n++; if (n == 8) { break; } } if (n >= 4) { int m = 0; for (int i = 0; i < Result.Mesh.Vertices.Length; i++) { if (m == 0) { Result.Mesh.Vertices[i].Coordinates.X = NearDistance - x3; } else if (m == 1) { Result.Mesh.Vertices[i].Coordinates.X = FarDistance - x2; if (n < 8) { break; } } else if (m == 4) { Result.Mesh.Vertices[i].Coordinates.X = NearDistance - x7; } else if (m == 5) { Result.Mesh.Vertices[i].Coordinates.X = NearDistance - x6; break; } m++; if (m == 8) { break; } } } return(Result); }
/// <summary>Applies world data to create a transformed model</summary> public void ApplyData(StaticObject Prototype, Vector3 Position, Transformation BaseTransformation, Transformation AuxTransformation, bool AccurateObjectDisposal, double AccurateObjectDisposalZOffset, double startingDistance, double endingDistance, double BlockLength, double TrackPosition, double Brightness, double ViewingDistance) { StartingDistance = float.MaxValue; EndingDistance = float.MinValue; Mesh.Vertices = new VertexTemplate[Prototype.Mesh.Vertices.Length]; // vertices for (int j = 0; j < Prototype.Mesh.Vertices.Length; j++) { if (Prototype.Mesh.Vertices[j] is ColoredVertex) { Mesh.Vertices[j] = new ColoredVertex((ColoredVertex)Prototype.Mesh.Vertices[j]); } else { Mesh.Vertices[j] = new Vertex((Vertex)Prototype.Mesh.Vertices[j]); } if (AccurateObjectDisposal) { Mesh.Vertices[j].Coordinates.Rotate(AuxTransformation); if (Mesh.Vertices[j].Coordinates.Z < StartingDistance) { StartingDistance = (float)Mesh.Vertices[j].Coordinates.Z; } if (Mesh.Vertices[j].Coordinates.Z > EndingDistance) { EndingDistance = (float)Mesh.Vertices[j].Coordinates.Z; } Mesh.Vertices[j].Coordinates = Prototype.Mesh.Vertices[j].Coordinates; } Mesh.Vertices[j].Coordinates.Rotate(AuxTransformation); Mesh.Vertices[j].Coordinates.Rotate(BaseTransformation); Mesh.Vertices[j].Coordinates += Position; } if (AccurateObjectDisposal) { StartingDistance += (float)AccurateObjectDisposalZOffset; EndingDistance += (float)AccurateObjectDisposalZOffset; } // faces Mesh.Faces = new MeshFace[Prototype.Mesh.Faces.Length]; for (int j = 0; j < Prototype.Mesh.Faces.Length; j++) { Mesh.Faces[j].Flags = Prototype.Mesh.Faces[j].Flags; Mesh.Faces[j].Material = Prototype.Mesh.Faces[j].Material; Mesh.Faces[j].Vertices = new MeshFaceVertex[Prototype.Mesh.Faces[j].Vertices.Length]; for (int k = 0; k < Prototype.Mesh.Faces[j].Vertices.Length; k++) { Mesh.Faces[j].Vertices[k] = Prototype.Mesh.Faces[j].Vertices[k]; if (Mesh.Faces[j].Vertices[k].Normal.NormSquared() != 0.0) { Mesh.Faces[j].Vertices[k].Normal.Rotate(AuxTransformation); Mesh.Faces[j].Vertices[k].Normal.Rotate(BaseTransformation); } } } // materials Mesh.Materials = new MeshMaterial[Prototype.Mesh.Materials.Length]; for (int j = 0; j < Prototype.Mesh.Materials.Length; j++) { Mesh.Materials[j] = Prototype.Mesh.Materials[j]; Mesh.Materials[j].Color *= Brightness; } const double minBlockLength = 20.0; if (BlockLength < minBlockLength) { BlockLength = BlockLength * System.Math.Ceiling(minBlockLength / BlockLength); } if (AccurateObjectDisposal) { StartingDistance += (float)TrackPosition; EndingDistance += (float)TrackPosition; double z = BlockLength * System.Math.Floor(TrackPosition / BlockLength); startingDistance = System.Math.Min(z - BlockLength, (double)StartingDistance); endingDistance = System.Math.Max(z + 2.0 * BlockLength, (double)EndingDistance); StartingDistance = (float)(BlockLength * System.Math.Floor(startingDistance / BlockLength)); EndingDistance = (float)(BlockLength * System.Math.Ceiling(endingDistance / BlockLength)); } else { StartingDistance = (float)startingDistance; EndingDistance = (float)endingDistance; } if (BlockLength != 0.0) { checked { GroupIndex = (short)NumberFormats.Mod(System.Math.Floor(StartingDistance / BlockLength), System.Math.Ceiling(ViewingDistance / BlockLength)); } } }
/// <summary>Creates a new AnimatedObjectState</summary> /// <param name="stateObject">The 3D model</param> /// <param name="position">The relative position</param> public AnimatedObjectState(StaticObject stateObject, Vector3 position) { Object = stateObject; Position = position; }