Exemplo n.º 1
0
        public virtual void Render(CMaterial actorMaterial)
        {
            GraphicsDevice device = Engine.Device;

            CMaterial currentMaterial = null;
            int baseVert = VertexBaseIndex;
            int indexBufferStart = IndexBufferStart;

            for (int i = 0; i < Polygons.Count; i++)
            {
                Polygon poly = Polygons[i];
                if (poly.Skip) continue;

                if (GameVars.CullingOff != poly.DoubleSided)
                {
                    device.RenderState.CullMode = (poly.DoubleSided ? CullMode.None : CullMode.CullClockwiseFace);
                    GameVars.CullingOff = poly.DoubleSided;
                }

                if (poly.Material != null)
                {
                    if (currentMaterial != poly.Material)
                    {
                        device.Textures[0] = poly.Material.Texture;
                        currentMaterial = poly.Material;
                    }
                }
                else if (actorMaterial != null)
                {
                    if (currentMaterial != actorMaterial)
                    {
                        device.Textures[0] = actorMaterial.Texture;
                        currentMaterial = actorMaterial;
                    }
                }
                else
                {
                    device.Textures[0] = null; currentMaterial = null;
                }

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.BeforeRender();
                }
                GameVars.NbrDrawCalls++;
                if (!HardEdgesInserted)
                    Engine.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, baseVert, 0, 3 * poly.NbrPrims, indexBufferStart, poly.NbrPrims);
                else
                    Engine.Device.DrawPrimitives(PrimitiveType.TriangleList, indexBufferStart, poly.NbrPrims);
                indexBufferStart += poly.NbrPrims * 3;

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.AfterRender();
                }
            }
        }
Exemplo n.º 2
0
        public Vehicle(string filename, IDriver driver)
        {
            Driver         = driver;
            Driver.Vehicle = this;

            Config = new VehicleFile(filename);

            if (driver is PlayerDriver)
            {
                if (Config.WindscreenMaterial != "none")
                {
                    Config.Funks.Add(new WindscreenFunk(Config.WindscreenMaterial, this));
                }
            }

            _model = new VehicleModel(Config, false);

            Audio = new VehicleAudio(this);

            Chassis = new VehicleChassis(this);

            CActor actor2 = _model.GetActor(Path.GetFileNameWithoutExtension(_model.ModelName));

            if (actor2 != null)
            {
                _deformableModel          = (CDeformableModel)actor2.Model;
                _deformableModel._actor   = Chassis.Actor;
                _deformableModel._carFile = Config;
            }

            _crushSection = Config.CrushSections[1];

            CMaterial crashMat = ResourceCache.GetMaterial(Config.CrashMaterialFiles[0]);

            _vehicleBitsEmitter = new ParticleEmitter(new VehicleBitsParticleSystem(crashMat), 3, Vector3.Zero);
            _vehicleBitsEmitter.DumpsPerSecond = 0.7f;

            DamageSmokeEmitter         = new ParticleEmitter(new DamageSmokeParticleSystem(Color.Gray), 5, Vector3.Zero);
            DamageSmokeEmitter.Enabled = false;

            _flames        = new PixmapBillboard(new Vector2(0.7f, 0.25f), "flames.pix");
            SkidMarkBuffer = new SkidMarkBuffer(this, 150);
        }
Exemplo n.º 3
0
        public override void Render(CMaterial actorMaterial)
        {
            if (_actor == null)
            {
                return;
            }

            //for (int i = _nbr; i < _carFile.CrushSections[1].Data.Count; i++)
            //{
            //    CrushData data = _carFile.CrushSections[1].Data[i];
            //    if (!_lastHitPts.Exists(a => a == data.RefVertex)) continue;

            //    Vector3 crushPoint = Vector3.Transform(_originalPositions[data.RefVertex], GameVariables.ScaleMatrix * _actor.GlobalPose);

            //    Engine.DebugRenderer.AddWireframeCube(
            //        Matrix.CreateScale(0.09f)
            //        * Matrix.CreateTranslation(crushPoint)
            //        , Color.White);

            //    crushPoint = Vector3.Transform(_localVertices[data.RefVertex].Position, GameVariables.ScaleMatrix * _actor.GlobalPose);

            //    Engine.DebugRenderer.AddWireframeCube(
            //        Matrix.CreateScale(0.09f)
            //        * Matrix.CreateTranslation(crushPoint)
            //        , Color.Yellow);

            //    //Engine.DebugRenderer.AddAxis(
            //    //    Matrix.CreateTranslation(Vector3.Transform(data.Box.Max, GameVariables.ScaleMatrix * _actor.GlobalPose))
            //    //    , 10);

            //    //Engine.DebugRenderer.AddAxis(
            //    //    Matrix.CreateTranslation(Vector3.Transform(data.Box.Min, GameVariables.ScaleMatrix * _actor.GlobalPose))
            //    //    , 10);



            //    foreach (CrushPoint point in data.Points)
            //    {
            //        Engine.DebugRenderer.AddWireframeCube(
            //        Matrix.CreateScale(0.05f)
            //        * Matrix.CreateTranslation(Vector3.Transform(_localVertices[point.VertexIndex].Position, GameVariables.ScaleMatrix * _actor.GlobalPose))

            //        , Color.Blue);
            //    }
            //    break;
            //}

            if (_repairing)
            {
                if (_repairingFactor > 1)
                {
                    _repairingFactor = 1;
                }
                for (int i = 0; i < _localVertices.Length; i++)
                {
                    _localVertices[i].Position = Vector3.Lerp(_repairPoisitons[i], _originalPositions[i], _repairingFactor);
                }
                _repairingFactor += Engine.ElapsedSeconds;
                _changed          = true;

                if (_repairingFactor >= 1)
                {
                    _repairing = false;
                }
            }

            if (_changed)
            {
                _vertexBuffer.SetData(_localVertices);
                _changed = false;
            }


            GraphicsDevice device = Engine.Device;

            //VertexBuffer oldVertBuffer = device.Vertices[0].VertexBuffer;
            //IndexBuffer oldIndexBuffer = device.Indices;

            device.Vertices[0].SetSource(_vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
            device.Indices = _indexBuffer;

            CMaterial currentMaterial  = null;
            int       baseVert         = 0; // VertexBaseIndex;
            int       indexBufferStart = 0; // IndexBufferStart;

            for (int i = 0; i < Polygons.Count; i++)
            {
                Polygon poly = Polygons[i];
                if (poly.Skip)
                {
                    continue;
                }

                if (GameVars.CullingOff != poly.DoubleSided)
                {
                    device.RenderState.CullMode = (poly.DoubleSided ? CullMode.None : CullMode.CullClockwiseFace);
                    GameVars.CullingOff         = poly.DoubleSided;
                }

                if (poly.Material != null)
                {
                    if (currentMaterial != poly.Material)
                    {
                        device.Textures[0] = poly.Material.Texture;
                        currentMaterial    = poly.Material;
                    }
                }
                else
                {
                    device.Textures[0] = null; currentMaterial = null;
                }

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.BeforeRender();
                }
                GameVars.NbrDrawCalls++;
                Engine.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, baseVert, 0, _localVertices.Length, indexBufferStart, poly.NbrPrims);

                indexBufferStart += poly.NbrPrims * 3;


                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.AfterRender();
                }
            }

            //device.Vertices[0].SetSource(oldVertBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
            //device.Indices = oldIndexBuffer;
        }
Exemplo n.º 4
0
        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());
        }
Exemplo n.º 5
0
 public static void Add(CMaterial material)
 {
     _materials.Add(material);
 }
Exemplo n.º 6
0
Arquivo: CModel.cs Projeto: q4a/OpenC1
        public virtual void Render(CMaterial actorMaterial)
        {
            GraphicsDevice device = GameEngine.Device;

            CMaterial currentMaterial  = null;
            int       baseVert         = VertexBaseIndex;
            int       indexBufferStart = IndexBufferStart;

            for (int i = 0; i < Polygons.Count; i++)
            {
                Polygon poly = Polygons[i];
                if (poly.Skip)
                {
                    continue;
                }

                if (GameVars.CullingOff != poly.DoubleSided)
                {
                    device.RasterizerState.CullMode = (poly.DoubleSided ? CullMode.None : CullMode.CullClockwiseFace);
                    GameVars.CullingOff             = poly.DoubleSided;
                }


                if (poly.Material != null)
                {
                    if (currentMaterial != poly.Material)
                    {
                        device.Textures[0] = poly.Material.Texture;
                        currentMaterial    = poly.Material;
                    }
                }
                else if (actorMaterial != null)
                {
                    if (currentMaterial != actorMaterial)
                    {
                        device.Textures[0] = actorMaterial.Texture;
                        currentMaterial    = actorMaterial;
                    }
                }
                else
                {
                    device.Textures[0] = null; currentMaterial = null;
                }

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.BeforeRender();
                }
                GameVars.NbrDrawCalls++;
                if (!HardEdgesInserted)
                {
                    GameEngine.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, baseVert, 0, 3 * poly.NbrPrims, indexBufferStart, poly.NbrPrims);
                }
                else
                {
                    GameEngine.Device.DrawPrimitives(PrimitiveType.TriangleList, indexBufferStart, poly.NbrPrims);
                }
                indexBufferStart += poly.NbrPrims * 3;

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.AfterRender();
                }
            }
        }
Exemplo n.º 7
0
        public override void Render(CMaterial actorMaterial)
        {
            if (_actor == null) return;

            //for (int i = _nbr; i < _carFile.CrushSections[1].Data.Count; i++)
            //{
            //    CrushData data = _carFile.CrushSections[1].Data[i];
            //    if (!_lastHitPts.Exists(a => a == data.RefVertex)) continue;

            //    Vector3 crushPoint = Vector3.Transform(_originalPositions[data.RefVertex], GameVariables.ScaleMatrix * _actor.GlobalPose);

            //    Engine.DebugRenderer.AddWireframeCube(
            //        Matrix.CreateScale(0.09f)
            //        * Matrix.CreateTranslation(crushPoint)
            //        , Color.White);

            //    crushPoint = Vector3.Transform(_localVertices[data.RefVertex].Position, GameVariables.ScaleMatrix * _actor.GlobalPose);

            //    Engine.DebugRenderer.AddWireframeCube(
            //        Matrix.CreateScale(0.09f)
            //        * Matrix.CreateTranslation(crushPoint)
            //        , Color.Yellow);

            //    //Engine.DebugRenderer.AddAxis(
            //    //    Matrix.CreateTranslation(Vector3.Transform(data.Box.Max, GameVariables.ScaleMatrix * _actor.GlobalPose))
            //    //    , 10);

            //    //Engine.DebugRenderer.AddAxis(
            //    //    Matrix.CreateTranslation(Vector3.Transform(data.Box.Min, GameVariables.ScaleMatrix * _actor.GlobalPose))
            //    //    , 10);

            //    foreach (CrushPoint point in data.Points)
            //    {
            //        Engine.DebugRenderer.AddWireframeCube(
            //        Matrix.CreateScale(0.05f)
            //        * Matrix.CreateTranslation(Vector3.Transform(_localVertices[point.VertexIndex].Position, GameVariables.ScaleMatrix * _actor.GlobalPose))

            //        , Color.Blue);
            //    }
            //    break;
            //}

            if (_repairing)
            {
                if (_repairingFactor > 1) _repairingFactor = 1;
                for (int i = 0; i < _localVertices.Length; i++)
                {
                    _localVertices[i].Position = Vector3.Lerp(_repairPoisitons[i], _originalPositions[i], _repairingFactor);
                }
                _repairingFactor += Engine.ElapsedSeconds;
                _changed = true;

                if (_repairingFactor >= 1) _repairing = false;
            }

            if (_changed)
            {
                _vertexBuffer.SetData(_localVertices);
                _changed = false;
            }

            GraphicsDevice device = Engine.Device;

            //VertexBuffer oldVertBuffer = device.Vertices[0].VertexBuffer;
            //IndexBuffer oldIndexBuffer = device.Indices;

            device.Vertices[0].SetSource(_vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
            device.Indices = _indexBuffer;

            CMaterial currentMaterial = null;
            int baseVert = 0; // VertexBaseIndex;
            int indexBufferStart = 0; // IndexBufferStart;

            for (int i = 0; i < Polygons.Count; i++)
            {
                Polygon poly = Polygons[i];
                if (poly.Skip) continue;

                if (GameVars.CullingOff != poly.DoubleSided)
                {
                    device.RenderState.CullMode = (poly.DoubleSided ? CullMode.None : CullMode.CullClockwiseFace);
                    GameVars.CullingOff = poly.DoubleSided;
                }

                if (poly.Material != null)
                {
                    if (currentMaterial != poly.Material)
                    {
                        device.Textures[0] = poly.Material.Texture;
                        currentMaterial = poly.Material;
                    }
                }
                else
                {
                    device.Textures[0] = null; currentMaterial = null;
                }

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.BeforeRender();
                }
                GameVars.NbrDrawCalls++;
                Engine.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, baseVert, 0, _localVertices.Length, indexBufferStart, poly.NbrPrims);

                indexBufferStart += poly.NbrPrims * 3;

                if (currentMaterial != null && currentMaterial.Funk != null)
                {
                    currentMaterial.Funk.AfterRender();
                }
            }

            //device.Vertices[0].SetSource(oldVertBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
            //device.Indices = oldIndexBuffer;
        }