public override void Resolve(List <ushort> indices, List <VertexPositionNormalTexture> vertices, List <Vector2> vertexTextureMap, List <Vector3> vertexPositions) { List <UInt16> indices2 = new List <UInt16>(); foreach (Polygon poly in Polygons) { poly.NbrPrims = 1; indices2.Add(poly.Vertex1); indices2.Add(poly.Vertex2); indices2.Add(poly.Vertex3); } _originalPositions = new Vector3[VertexCount]; _repairPoisitons = new Vector3[VertexCount]; vertexPositions.CopyTo(VertexBaseIndex, _originalPositions, 0, VertexCount); Polygon currentPoly = null; foreach (Polygon poly in Polygons) { poly.NbrPrims = 1; if (poly.MaterialIndex >= 0 && MaterialNames != null) { CMaterial material = ResourceCache.GetMaterial(MaterialNames[poly.MaterialIndex]); if (material != null) { poly.DoubleSided = material.DoubleSided; poly.Material = material; } if (currentPoly != null && poly.MaterialIndex == currentPoly.MaterialIndex) { poly.Skip = true; currentPoly.NbrPrims = currentPoly.NbrPrims + 1; } else { currentPoly = poly; } } } _localVertices = new VertexPositionNormalTexture[VertexCount]; for (int i = 0; i < VertexCount; i++) { Vector3 normal = Polygons[i / 3].Normal; if (TextureMapCount > 0) { _localVertices[i] = new VertexPositionNormalTexture(vertexPositions[i + VertexBaseIndex], normal, vertexTextureMap[i + VertexBaseIndex]); } else { _localVertices[i] = new VertexPositionNormalTexture(vertexPositions[i + VertexBaseIndex], normal, Vector2.Zero); } _originalPositions[i] = _localVertices[i].Position; } // link up vertices which share the same position so when we crush we avoid gaps _vertexLinks = new List <int> [_originalPositions.Length]; for (int i = 0; i < _originalPositions.Length; i++) { for (int j = 0; j < _originalPositions.Length; j++) { if (i == j) { continue; } if (_originalPositions[i] == _originalPositions[j]) { if (_vertexLinks[i] == null) { _vertexLinks[i] = new List <int>(); } _vertexLinks[i].Add(j); } } } for (int i = 0; i < _localVertices.Length; i++) { _localVertices[i].Normal = Vector3.Zero; } for (int i = 0; i < indices2.Count / 3; i++) { Vector3 firstvec = _localVertices[indices2[i * 3 + 1]].Position - _localVertices[indices2[i * 3]].Position; Vector3 secondvec = _localVertices[indices2[i * 3]].Position - _localVertices[indices2[i * 3 + 2]].Position; Vector3 normal = Vector3.Cross(firstvec, secondvec); normal.Normalize(); _localVertices[indices2[i * 3]].Normal += normal; _localVertices[indices2[i * 3 + 1]].Normal += normal; _localVertices[indices2[i * 3 + 2]].Normal += normal; } for (int i = 0; i < _localVertices.Length; i++) { _localVertices[i].Normal.Normalize(); } int size = VertexPositionNormalTexture.SizeInBytes * _localVertices.Length; _vertexBuffer = new VertexBuffer(Engine.Device, size, BufferUsage.WriteOnly); _vertexBuffer.SetData(_localVertices); _indexBuffer = new IndexBuffer(Engine.Device, typeof(UInt16), indices2.Count, BufferUsage.WriteOnly); _indexBuffer.SetData(indices2.ToArray()); }
public virtual void Resolve(List <UInt16> indices, List <VertexPositionNormalTexture> vertices, List <Vector2> vertexTextureMap, List <Vector3> vertexPositions) { bool injectHardEdges = true; Polygon currentPoly = null; foreach (Polygon poly in Polygons) { poly.NbrPrims = 1; indices.Add(poly.Vertex1); indices.Add(poly.Vertex2); indices.Add(poly.Vertex3); if (injectHardEdges) { Vector2 uv = Vector2.Zero; if (TextureMapCount > 0 && poly.Vertex1 + VertexBaseIndex < vertexTextureMap.Count) { uv = vertexTextureMap[poly.Vertex1 + VertexBaseIndex]; } vertices.Add(new VertexPositionNormalTexture(vertexPositions[poly.Vertex1 + VertexBaseIndex], poly.Normal, uv)); if (TextureMapCount > 0 && poly.Vertex2 + VertexBaseIndex < vertexTextureMap.Count) { uv = vertexTextureMap[poly.Vertex2 + VertexBaseIndex]; } vertices.Add(new VertexPositionNormalTexture(vertexPositions[poly.Vertex2 + VertexBaseIndex], poly.Normal, uv)); if (TextureMapCount > 0 && poly.Vertex3 + VertexBaseIndex < vertexTextureMap.Count) { uv = vertexTextureMap[poly.Vertex3 + VertexBaseIndex]; } vertices.Add(new VertexPositionNormalTexture(vertexPositions[poly.Vertex3 + VertexBaseIndex], poly.Normal, uv)); } if (MaterialNames != null) { CMaterial material; if (poly.MaterialIndex < 0) { material = ResourceCache.GetMaterial("drkcurb.mat"); } else { material = ResourceCache.GetMaterial(MaterialNames[poly.MaterialIndex]); } if (material != null) { poly.DoubleSided = material.DoubleSided; poly.Material = material; } if (currentPoly != null && poly.MaterialIndex == currentPoly.MaterialIndex) { poly.Skip = true; currentPoly.NbrPrims = currentPoly.NbrPrims + 1; } else { currentPoly = poly; } } } if (!injectHardEdges) { //for (int i = 0; i < VertexCount; i++) //{ // Vector3 normal = Polygons[i / 3].Normal; // if (TextureMapCount > 0) // vertices.Add(new VertexPositionNormalTexture(vertexPositions[i + VertexBaseIndex], normal, vertexTextureMap[i + VertexBaseIndex])); // else // vertices.Add(new VertexPositionNormalTexture(vertexPositions[i + VertexBaseIndex], normal, Vector2.Zero)); //} //for (int i = 0; i < indices.Count / 3; i++) //{ // Vector3 firstvec = vertices[indices[i * 3 + 1]].Position - vertices[indices[i * 3]].Position; // Vector3 secondvec = vertices[indices[i * 3]].Position - vertices[indices[i * 3 + 2]].Position; // Vector3 normal = Vector3.Cross(firstvec, secondvec); // normal.Normalize(); // VertexPositionNormalTexture vpnt = vertices[indices[i * 3]]; // vpnt.Normal += normal; // vpnt = vertices[indices[i * 3 + 1]]; // vpnt.Normal += normal; // vpnt = vertices[indices[i * 3 + 2]]; // vpnt.Normal += normal; //} //for (int i = 0; i < vertices.Count; i++) // vertices[i].Normal.Normalize(); } }
public VehicleModel(VehicleFile file, bool forDisplayOnly) { Config = file; if (file.DrivenWheelRefs.Count == 0 || file.NonDrivenWheelRefs.Count == 0) { throw new Exception("No wheel refs specified"); } foreach (string pixFileName in file.PixFiles) { PixFile pixFile = new PixFile(pixFileName); ResourceCache.Add(pixFile); } foreach (string matFileName in file.MaterialFiles) { MatFile matFile = new MatFile(matFileName); ResourceCache.Add(matFile); } foreach (string matFileName in file.CrashMaterialFiles) { MatFile matFile = new MatFile(matFileName); ResourceCache.Add(matFile); } ResourceCache.ResolveMaterials(); _grooves = new List <BaseGroove>(); foreach (BaseGroove g in file.Grooves) { if (!g.IsWheelActor) { _grooves.Add(g); } } ActFile actFile = new ActFile(file.ActorFile); _actors = actFile.Hierarchy; DatFile modelFile = new DatFile(_actors.Root.ModelName, !forDisplayOnly); ModelName = _actors.Root.ModelName; _actors.AttachModels(modelFile.Models); _actors.ResolveTransforms(!forDisplayOnly, _grooves); foreach (BaseGroove g in _grooves) { g.SetActor(_actors.GetByName(g.ActorName)); } // link the funks and materials foreach (BaseFunk f in file.Funks) { f.Resolve(); } Vector3 tireWidth = new Vector3(0.034f, 0, 0) * GameVars.Scale; foreach (int id in file.DrivenWheelRefs) { BaseGroove g = file.Grooves.Find(a => a.Id == id); if (g == null) { continue; } CActor actor = _actors.GetByName(g.ActorName); CWheelActor ca = new CWheelActor(actor, true, false); ca.Position = actor.Matrix.Translation + (ca.IsLeft ? -1 * tireWidth : tireWidth); file.WheelActors.Add(ca); } foreach (int id in file.NonDrivenWheelRefs) { BaseGroove g = file.Grooves.Find(a => a.Id == id); CActor actor = _actors.GetByName(g.ActorName); if (actor == null) { continue; //BUSTER.TXT does some weird shit for cockpit view of the front wheels } CWheelActor ca = new CWheelActor(actor, false, true); ca.Position = actor.Matrix.Translation + (ca.IsLeft ? -1 * tireWidth : tireWidth); file.WheelActors.Add(ca); } if (forDisplayOnly) { _actors.RenderWheelsSeparately = false; } }
public Race(string filename, string playerVehicleFile) { Race.Current = this; Logger.Log("Starting race " + Path.GetFileName(filename)); ConfigFile = new RaceFile(filename); foreach (string matFileName in ConfigFile.MaterialFiles) { MatFile matFile = new MatFile(matFileName); ResourceCache.Add(matFile); } foreach (string pixFileName in ConfigFile.PixFiles) { PixFile pixFile = new PixFile(pixFileName); ResourceCache.Add(pixFile); } if (GameVars.Emulation == EmulationMode.Demo) { ResourceCache.Add(new CMaterial("drkcurb.mat", 226)); //demo doesn't have this file, I guess the color is hard-coded } else { ResourceCache.Add(new MatFile("drkcurb.mat")); } ResourceCache.ResolveMaterials(); if (filename.Contains("TESTMAP")) //nasty hack... { GameVars.Scale.Y *= 0.5f; } DatFile modelFile = new DatFile(ConfigFile.ModelFile); ActFile actFile = new ActFile(ConfigFile.ActorFile); _actors = actFile.Hierarchy; _actors.AttachModels(modelFile.Models); _actors.ResolveTransforms(false, ConfigFile.Grooves); if (filename.Contains("TESTMAP")) //nasty hack... { GameVars.Scale.Y *= 2f; } // link the actors and grooves foreach (BaseGroove g in ConfigFile.Grooves) { g.SetActor(_actors.GetByName(g.ActorName)); } // link the funks and materials foreach (BaseFunk f in ConfigFile.Funks) { f.Resolve(); } if (ConfigFile.SkyboxTexture != "none") { PixFile horizonPix = new PixFile(ConfigFile.SkyboxTexture); _skybox = SkyboxGenerator.Generate(horizonPix.PixMaps[0].Texture, ConfigFile.SkyboxRepetitionsX - 3f, ConfigFile.DepthCueMode); _skybox.HeightOffset = -220 + ConfigFile.SkyboxPositionY * 1.5f; } Physics.TrackProcessor.GenerateTrackActor(ConfigFile, _actors, out _nonCars); Logger.Log("NonCars: " + _nonCars.Count); GridPlacer.Reset(); List <int> opponentIds = new List <int>(); List <int> pickedNbrs = new List <int>(); for (int i = 0; i < 5; i++) { int index = 0; while (true) { index = GameEngine.Random.Next(1, OpponentsFile.Instance.Opponents.Count); if (!pickedNbrs.Contains(index)) { pickedNbrs.Add(index); break; } } try { Opponents.Add(new Opponent(OpponentsFile.Instance.Opponents[index].FileName, ConfigFile.GridPosition, ConfigFile.GridDirection)); NbrOpponents++; } catch (Exception ex) { Logger.Log("Error while loading opponent " + OpponentsFile.Instance.Opponents[index].FileName + ", " + ex.Message); } } foreach (CopStartPoint point in ConfigFile.CopStartPoints) { Opponents.Add(new Opponent(point.IsSpecialForces ? "bigapc.txt" : "apc.txt", point.Position, 0, new CopDriver())); } foreach (Opponent o in Opponents) { Drivers.Add(o.Driver); } OpponentController.Nodes = ConfigFile.OpponentPathNodes; PlayerVehicle = new Vehicle(GameVars.BasePath + @"cars\" + playerVehicleFile, new PlayerDriver()); PlayerVehicle.PlaceOnGrid(ConfigFile.GridPosition, ConfigFile.GridDirection); Drivers.Add(PlayerVehicle.Driver); Peds = new PedestrianController(ConfigFile.Peds); _map = new RaceMap(this); RaceTime = new RaceTimeController(); PhysX.Instance.Scene.SetActorGroupPairFlags(PhysXConsts.TrackId, PhysXConsts.VehicleId, ContactPairFlag.Forces | ContactPairFlag.OnStartTouch | ContactPairFlag.OnTouch); PhysX.Instance.Scene.SetActorGroupPairFlags(PhysXConsts.VehicleId, PhysXConsts.NonCarId, ContactPairFlag.Forces | ContactPairFlag.OnStartTouch | ContactPairFlag.OnTouch); PhysX.Instance.Scene.SetActorGroupPairFlags(PhysXConsts.TrackId, PhysXConsts.NonCarId, ContactPairFlag.OnTouch); PhysX.Instance.Scene.SetActorGroupPairFlags(PhysXConsts.VehicleId, PhysXConsts.VehicleId, ContactPairFlag.Forces | ContactPairFlag.OnTouch | ContactPairFlag.OnStartTouch | ContactPairFlag.OnEndTouch); }