Exemplo n.º 1
0
        public bool CollideRayWithSurfaces(Math.Vector3 origin, Math.Vector3 direction, float length, out BspSurface surface)
        {
            if (_surfaces == null)
            {
                surface = null;
                return(false);
            }

            surface = null;
            float distance = float.MaxValue, minDistance = float.MaxValue;

            foreach (BspSurface isurface in _surfaces)
            {
                if (collideRayWithSurface(isurface, origin, direction, length, out distance) == true)
                {
                    if (distance < minDistance && distance > 0)
                    {
                        minDistance = distance;
                        surface     = isurface;
                    }
                }
            }

            if (surface == null)
            {
                return(false);
            }


            return(true);
        }
Exemplo n.º 2
0
        public AxisAlignedBoundingBox(Math.Vector3 min, Math.Vector3 max)
        {
            Min = min;
            Max = max;

            _vertices = null;
        }
Exemplo n.º 3
0
        private bool collideRayWithSurface(BspSurface surface,
                                           Math.Vector3 origin, Math.Vector3 direction, float length, out float distance)
        {
            Math.Vector3?collisionPoint;

            for (int i = 0; i < surface.indices.Length / 3; i++)
            {
                Math.Vector3 v1 = new Math.Vector3(
                    _vertices[surface.indices[i * 3 + 0] * 3 + 0],
                    _vertices[surface.indices[i * 3 + 0] * 3 + 1],
                    _vertices[surface.indices[i * 3 + 0] * 3 + 2]);

                Math.Vector3 v2 = new Math.Vector3(
                    _vertices[surface.indices[i * 3 + 1] * 3 + 0],
                    _vertices[surface.indices[i * 3 + 1] * 3 + 1],
                    _vertices[surface.indices[i * 3 + 1] * 3 + 2]);

                Math.Vector3 v3 = new Math.Vector3(
                    _vertices[surface.indices[i * 3 + 2] * 3 + 0],
                    _vertices[surface.indices[i * 3 + 2] * 3 + 1],
                    _vertices[surface.indices[i * 3 + 2] * 3 + 2]);

                if (Gk3Main.Utils.TestRayTriangleCollision(origin, direction,
                                                           v1, v2, v3, out distance, out collisionPoint) == true)
                {
                    return(true);
                }
            }

            distance = float.NaN;
            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// spawns a vehicle that has the player as destination
        /// </summary>
        public SpawnedDrivingGangMember SpawnAngryVehicle(bool isFriendly)
        {
            Math.Vector3 spawnPos  = SpawnManager.instance.FindGoodSpawnPointForCar(),
                         playerPos = MindControl.CurrentPlayerCharacter.Position;

            if (spawnPos == Vector3.Zero)
            {
                return(null);
            }

            SpawnedDrivingGangMember spawnedVehicle = null;

            if (!isFriendly && spawnedEnemies - 4 < maxSpawnedEnemies)
            {
                spawnedVehicle = SpawnManager.instance.SpawnGangVehicle(enemyGang,
                                                                        spawnPos, playerPos, true, false, IncrementEnemiesCount);
            }
            else if (spawnedAllies - 4 < maxSpawnedAllies)
            {
                spawnedVehicle = SpawnManager.instance.SpawnGangVehicle(GangManager.instance.PlayerGang,
                                                                        spawnPos, playerPos, true, false, IncrementAlliesCount);
            }

            return(spawnedVehicle);
        }
Exemplo n.º 5
0
        public void SetPosition(bool relative, Math.Vector3 position)
        {
            _positionRelative = relative;
            _position         = position;

            AL.Source(_source, ALSourceb.SourceRelative, relative);
            AL.Source(_source, ALSource3f.Position, position.X, position.Y, position.Z);
        }
Exemplo n.º 6
0
        public bool TestRayAABBCollision(Math.Vector3 position, Math.Vector3 origin, Math.Vector3 direction, out float distance)
        {
            // TODO: the code really should be moved in here, rather
            // than in a utility method somewhere else

            float[] aabb = new float[] { Min.X, Min.Y, Min.Z, Max.X, Max.Y, Max.Z };
            return(Gk3Main.Utils.TestRayAABBCollision(position, origin, direction, aabb, out distance));
        }
Exemplo n.º 7
0
        public void AddSun(Math.Vector3 direction, Math.Vector3 color, bool memory)
        {
            const int sunSize = 64;

            Game.Radiosity.GenerateOmniLight(sunSize, color, out _sun, out _sunMask);

            _sunDirection = direction;
        }
Exemplo n.º 8
0
        // TODO: get this to save the orientation! Right now has the same effect as Update(),
        // but the orientation quaternion is out of date!
        internal void LookAt(Math.Vector3 position, Math.Vector3 direction, Math.Vector3 up)
        {
            _position  = position;
            _modelView = Math.Matrix.LookAtLH(position, direction, up);

            Math.Matrix.Multiply(ref _modelView, ref _projection, out _modelViewProjection);

            _frustum = new Frustum(_modelViewProjection);
        }
Exemplo n.º 9
0
        private static void vectorAdd(Math.Vector3 one, Math.Vector3 two, Math.Vector3 three, float scale,
                                      out float r1, out float r2, out float r3)
        {
            Math.Vector3 r = one * scale + two * scale + three * scale;

            r1 = r.X;
            r2 = r.Y;
            r3 = r.Z;
        }
Exemplo n.º 10
0
        public bool IsSphereOutside(Math.Vector3 position, float radius)
        {
            // test near
            if (_near.X * position.X + _near.Y * position.Y + _near.Z * position.Z < -radius)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 11
0
        public bool CollideRay(Math.Vector3 origin, Math.Vector3 direction, float length, out float distance)
        {
            if (_model != null && _model.Loaded)
            {
                return(_model.CollideRay(_position, origin, direction, length, out distance));
            }

            distance = float.MaxValue;
            return(false);
        }
Exemplo n.º 12
0
 private Vector3 VDeriv(double firstParam, double secondParam)
 {
     Math.Vector3 ret = new Math.Vector3
     {
         X = System.Math.Cos(firstParam) * (-smallRadius * System.Math.Sin(secondParam)),
         Y = System.Math.Sin(firstParam) * (-smallRadius * System.Math.Sin(secondParam)),
         Z = smallRadius * System.Math.Cos(secondParam)
     };
     return(ret);
 }
Exemplo n.º 13
0
 private Math.Vector3 CalculatePoint(Real bigAngle, Real smallAngle, Real bigRadius, Real smallRadius)
 {
     Math.Vector3 ret = new Math.Vector3
     {
         X = System.Math.Cos(bigAngle) * (bigRadius + smallRadius * System.Math.Cos(smallAngle)),
         Y = System.Math.Sin(bigAngle) * (bigRadius + smallRadius * System.Math.Cos(smallAngle)),
         Z = smallRadius * System.Math.Sin(smallAngle)
     };
     return(ret);
 }
Exemplo n.º 14
0
        public void SetPosition(bool relative, Math.Vector3 position)
        {
            _position         = position;
            _positionRelative = relative;

            if (isSourceValid())
            {
                _source.SetPosition(relative, position);
            }
        }
Exemplo n.º 15
0
 internal static void AddSun(Math.Vector3 direction, Math.Vector3 color, float radius,
                             BitmapSurface front, BitmapSurface back, BitmapSurface left, BitmapSurface right, BitmapSurface up, bool memory)
 {
     // process each surface of the skybox and figure out how much "sun" is in that texel
     addSunToSurface(direction, color, radius, new Math.Vector3(1.0f, 0, 0), Math.Vector3.Up, front, memory);
     addSunToSurface(direction, color, radius, new Math.Vector3(-1.0f, 0, 0), Math.Vector3.Up, back, memory);
     addSunToSurface(direction, color, radius, new Math.Vector3(0, 0, 1.0f), Math.Vector3.Up, right, memory);
     addSunToSurface(direction, color, radius, new Math.Vector3(0, 0, -1.0f), Math.Vector3.Up, left, memory);
     addSunToSurface(direction, color, radius, new Math.Vector3(0, 1.0f, 0), Math.Vector3.Forward, up, memory);
 }
Exemplo n.º 16
0
        /// <summary>
        /// "Batched" version of RenderAt(). This version is much faster, but
        /// it must be called between BeginBatchRender() and EndBatchRender().
        /// </summary>
        public void RenderAtBatch(Math.Vector3 position, float angle, Camera camera)
        {
            if (!_loaded)
            {
                return;
            }

            Math.Matrix world = Math.Matrix.RotateY(angle)
                                * Math.Matrix.Translate(position);

            foreach (ModMesh mesh in _meshes)
            {
                Math.Matrix worldview;

                if (mesh.AnimatedTransformMatrix.HasValue)
                {
                    if (mesh.AnimatedTransformIsAbsolute)
                    {
                        worldview = mesh.AnimatedTransformMatrix.Value * TempTransform * camera.ViewProjection;
                    }
                    else
                    {
                        worldview = mesh.AnimatedTransformMatrix.Value * world * camera.ViewProjection;
                    }
                }
                else
                {
                    worldview = mesh.TransformMatrix * world * camera.ViewProjection;
                }

                _effect.SetParameter("ModelViewProjection", worldview);

                foreach (ModMeshSection section in mesh.sections)
                {
                    _effect.SetParameter("Diffuse", section.textureResource, 0);
                    _effect.CommitParams();

                    VertexBuffer vertices;
                    if (section.AnimatedVertices != null)
                    {
                        section.AnimatedVertexBuffer.SetData(section.AnimatedVertices, 0, (int)section.numVerts * _elements.Stride / 4);
                        vertices = section.AnimatedVertexBuffer;
                    }
                    else
                    {
                        vertices = section.vertexBuffer;
                    }

                    RendererManager.CurrentRenderer.SetVertexBuffer(vertices);
                    RendererManager.CurrentRenderer.Indices = section.indexBuffer;
                    RendererManager.CurrentRenderer.DrawIndexed(PrimitiveType.Triangles, 0, 0, vertices.NumVertices, 0, section.indices.Length);
                }
            }
        }
Exemplo n.º 17
0
        private static void addSunToSurface(Math.Vector3 direction, Math.Vector3 color, float radius,
                                            Math.Vector3 faceNormal, Math.Vector3 faceUp, BitmapSurface faceSurface, bool memory)
        {
            Math.Vector3 faceRight = faceNormal.Cross(faceUp);
            float        t         = -0.5f / faceNormal.Dot(-direction);

            if (t > 0)
            {
                return;        // the sun never hits this plane
            }
            Math.Vector3 sunP = -direction * t;
            for (int y = 0; y < faceSurface.Height; y++)
            {
                for (int x = 0; x < faceSurface.Width; x++)
                {
                    // calc the distance from this pixel to the sun
                    float u = (float)x / faceSurface.Width - 0.5f;
                    float v = (float)y / faceSurface.Height - 0.5f;

                    Math.Vector3 texelP = faceUp * v + faceRight * u + -faceNormal * 0.5f;

                    float distance = Math.Vector3.Distance(texelP, sunP);

                    if (distance < radius)
                    {
                        if (!memory)
                        {
                            faceSurface.Pixels[(y * faceSurface.Width + x) * 4 + 0] = 255;
                            faceSurface.Pixels[(y * faceSurface.Width + x) * 4 + 1] = 0;
                            faceSurface.Pixels[(y * faceSurface.Width + x) * 4 + 2] = 0;
                        }
                        else
                        {
                            Color c = faceSurface.ReadColorAt(x, y);

                            uint ptr = (uint)c.R | ((uint)c.G << 8) | ((uint)c.B << 16) | ((uint)c.A << 24);

                            UIntPtr ptr2 = (UIntPtr)ptr;

                            unsafe
                            {
                                float *f = (float *)ptr2.ToPointer();
                                f[0] = color.X;
                                f[1] = color.Y;
                                f[2] = color.Z;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 18
0
 public void RenderAABBAt(Math.Vector3 position, float angle, Camera camera)
 {
     foreach (ModMesh mesh in _meshes)
     {
         if (mesh.AnimatedTransformIsAbsolute)
         {
             mesh.UpdatedBoundingBox.Render(camera, Math.Matrix.Identity);
         }
         else
         {
             mesh.UpdatedBoundingBox.Render(camera, Math.Matrix.Translate(position));
         }
     }
 }
Exemplo n.º 19
0
        /// <summary>
        /// returns a random direction with z = 0 or not
        /// </summary>
        /// <returns></returns>
        public static Math.Vector3 RandomDirection(bool zeroZ)
        {
            Math.Vector3 theDirection = Math.Vector3.Zero;
            if (zeroZ)
            {
                theDirection = Math.Vector3.RandomXY();
            }
            else
            {
                theDirection = Math.Vector3.RandomXYZ();
            }

            //theDirection.Normalize();
            return(theDirection);
        }
Exemplo n.º 20
0
        public void Update()
        {
            Math.Vector3 forward = Math.Vector3.Forward;
            Math.Vector3 up      = Math.Vector3.Up;

            forward = _orientation * forward;
            up      = _orientation * up;

            // calculate the ModelViewProjection matrix
            _modelView = Math.Matrix.LookAtLH(_position, forward, up);
            //_modelViewProjection = _modelView * _projection;
            Math.Matrix.Multiply(ref _modelView, ref _projection, out _modelViewProjection);

            _frustum = new Frustum(_modelViewProjection);
        }
Exemplo n.º 21
0
        /// <summary>
        /// compresses the normal stream and fills the current stream
        /// both streams have to be the same size
        /// </summary>
        /// <param name="stream"></param>
        public void CompressAndFill(NormalStream stream)
        {
            System.Diagnostics.Debug.Assert(stream.Size == this.Size);
            const float fracScale = 127.5f;

            for (int i = 0; i < stream.Size; i++)
            {
                Math.Vector3 vec = stream[i];
                IntData[i] = (((uint)(vec.X * fracScale + fracScale) * 1) +
                              ((uint)(vec.Y * fracScale + fracScale) * 256) +
                              ((uint)(vec.Z * fracScale + fracScale) * 65536));

                //16777216
            }
        }
Exemplo n.º 22
0
        public AxisAlignedBoundingBox Transform(Math.Matrix transform)
        {
            Math.Vector3 transformedBBMin = transform * Min;
            Math.Vector3 transformedBBMax = transform * Max;

            AxisAlignedBoundingBox newBBox = new AxisAlignedBoundingBox();

            newBBox.Min.X = System.Math.Min(transformedBBMin.X, transformedBBMax.X);
            newBBox.Min.Y = System.Math.Min(transformedBBMin.Y, transformedBBMax.Y);
            newBBox.Min.Z = System.Math.Min(transformedBBMin.Z, transformedBBMax.Z);
            newBBox.Max.X = System.Math.Max(transformedBBMin.X, transformedBBMax.X);
            newBBox.Max.Y = System.Math.Max(transformedBBMin.Y, transformedBBMax.Y);
            newBBox.Max.Z = System.Math.Max(transformedBBMin.Z, transformedBBMax.Z);

            return(newBBox);
        }
Exemplo n.º 23
0
        public static void Update(Graphics.Camera camera)
        {
            for (int i = 0; i < _playingSounds.Count;)
            {
                if (_playingSounds[i].Instance.State == AudioEngine.SoundState.Stopped)
                {
                    // the sound is stopped, so update the wait handle
                    if (_playingSounds[i].Wait != null)
                    {
                        _playingSounds[i].Wait.Finished = true;
                    }

                    // remove sounds ready to die
                    if (_playingSounds[i].Released)
                    {
                        _playingSounds[i].Dispose();
                        _playingSounds.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
                else
                {
                    i++;
                }
            }

            if (camera != null)
            {
                Math.Vector3 position = camera.Position;
                Math.Vector3 forward  = camera.Orientation * -Math.Vector3.Forward;
                Math.Vector3 up       = camera.Orientation * Math.Vector3.Up;

                AL.Listener(ALListener3f.Position, position.X, position.Y, position.Z);

                _listenerOrientation[0] = forward.X;
                _listenerOrientation[1] = forward.Y;
                _listenerOrientation[2] = forward.Z;
                _listenerOrientation[3] = up.X;
                _listenerOrientation[4] = up.Y;
                _listenerOrientation[5] = up.Z;
                AL.Listener(ALListenerfv.Orientation, ref _listenerOrientation);
            }
        }
Exemplo n.º 24
0
        public void RenderAt(Math.Vector3 position, float angle, Camera camera)
        {
            if (_loaded == true)
            {
                _effect.Bind();
                _effect.Begin();

                RenderAtBatch(position, angle, camera);

                _effect.End();

                if (Settings.ShowBoundingBoxes)
                {
                    RenderAABBAt(position, angle, camera);
                }
            }
        }
Exemplo n.º 25
0
        public FrameTransformation(float[] matrix43)
        {
            _original.M11 = matrix43[0];
            _original.M12 = matrix43[1];
            _original.M13 = matrix43[2];
            _original.M14 = 0;

            _original.M21 = matrix43[3];
            _original.M22 = matrix43[4];
            _original.M23 = matrix43[5];
            _original.M24 = 0;

            _original.M31 = matrix43[6];
            _original.M32 = matrix43[7];
            _original.M33 = matrix43[8];
            _original.M34 = 0;

            _original.M41 = matrix43[9];
            _original.M42 = matrix43[10];
            _original.M43 = matrix43[11];
            _original.M44 = 1.0f;

            // TODO: scale
            _scale = Math.Vector3.One;

            _scale.X = (float)System.Math.Sqrt(
                matrix43[0 * 3 + 0] * matrix43[0 * 3 + 0] +
                matrix43[0 * 3 + 1] * matrix43[0 * 3 + 1] +
                matrix43[0 * 3 + 2] * matrix43[0 * 3 + 2]);
            _scale.Y = (float)System.Math.Sqrt(
                matrix43[1 * 3 + 0] * matrix43[1 * 3 + 0] +
                matrix43[1 * 3 + 1] * matrix43[1 * 3 + 1] +
                matrix43[1 * 3 + 2] * matrix43[1 * 3 + 2]);
            _scale.Z = (float)System.Math.Sqrt(
                matrix43[2 * 3 + 0] * matrix43[2 * 3 + 0] +
                matrix43[2 * 3 + 1] * matrix43[2 * 3 + 1] +
                matrix43[2 * 3 + 2] * matrix43[2 * 3 + 2]);

            // translation
            _translation.X = matrix43[3 * 3 + 0];
            _translation.Y = matrix43[3 * 3 + 1];
            _translation.Z = matrix43[3 * 3 + 2];

            // TODO: rotation
            _rotation = Math.Quaternion.FromAxis(Math.Vector3.Up, 0);
        }
Exemplo n.º 26
0
        bool ReadBones()
        {
            Advance("numbones");
            int boneNum = int.Parse(NextToken);

            bones = new MD5Bone[boneNum];

            for (int i = 0; i < boneNum; i++)
            {
                Advance("bone");

                // sanity check
                int num = int.Parse(NextToken);
                System.Diagnostics.Debug.Assert(num == i, "Invalid bone num!");

                // read bone data - name, bindpos, bindmat and [parent]
                Advance("name");
                string name = NextToken;
                Advance("bindpos");
                Math.Vector3 position = new Math.Vector3(
                    NextFloat, NextFloat, NextFloat);
                Advance("bindmat");
                Math.Matrix4 md5matrix = new Math.Matrix4(
                    NextFloat, NextFloat, NextFloat, 0,
                    NextFloat, NextFloat, NextFloat, 0,
                    NextFloat, NextFloat, NextFloat, 0,
                    position.X, position.Y, position.Z, 0);
                Math.Matrix4 matrix = Math.Matrix4.Zero;
                matrix.Column1 = md5matrix.Column1;
                matrix.Column2 = md5matrix.Column3;
                matrix.Column3 = md5matrix.Column2;
                matrix.Column4 = md5matrix.Column4;
                Advance("parent", "bone");
                string parentName = null;
                if (CurrentToken == "parent")
                {
                    parentName = NextToken;
                }

                bones[i] = new MD5Bone(name, matrix, parentName);
            }

            return(true);
        }
Exemplo n.º 27
0
        public Math.Vector3 Unproject(Math.Vector3 source,
                                      ref Math.Matrix projection, ref Math.Matrix view, ref Math.Matrix world)
        {
            Math.Vector4 result;
            result.X = ((source.X - X) * 2 / Width) - 1;
            result.Y = 1 - ((source.Y - Y) * 2 / Height);
            result.Z = source.Z;
            result.W = 1.0f;

            Math.Matrix invProj, invView, invWorld;
            Math.Matrix.Invert(ref projection, out invProj);
            Math.Matrix.Invert(ref view, out invView);
            Math.Matrix.Invert(ref world, out invWorld);

            result = invProj * result;
            result = invView * result;
            result = invWorld * result;
            result = result / result.W;

            return(new Math.Vector3(result.X, result.Y, result.Z));
        }
Exemplo n.º 28
0
        public static void GenerateOmniLight(int radius, Math.Vector3 color, out Graphics.TextureResource memoryTex, out Graphics.TextureResource alphaMask)
        {
            memoryTex = GenerateMemoryTexture(radius * 2, radius * 2, color.X, color.Y, color.Z);

            Graphics.BitmapSurface alphaSurface = new Graphics.BitmapSurface(radius * 2, radius * 2, null);
            for (int y = 0; y < radius * 2; y++)
            {
                for (int x = 0; x < radius * 2; x++)
                {
                    if ((x - radius) * (x - radius) + (y - radius) * (y - radius) > radius * radius)
                    {
                        alphaSurface.Pixels[(y * radius * 2 + x) * 4 + 3] = 0;
                    }
                    else
                    {
                        alphaSurface.Pixels[(y * radius * 2 + x) * 4 + 3] = 255;
                    }
                }
            }
            alphaMask = Graphics.RendererManager.CurrentRenderer.CreateTexture("alphaMask", alphaSurface, false, false);
        }
Exemplo n.º 29
0
        public static void AddBillboard(Math.Vector3 position, float width, float height,
                                        TextureResource texture)
        {
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            if (_numBillboards < _maxBillboards - 1)
            {
                _billboards[_numBillboards].Position = position;
                _billboards[_numBillboards].Width    = width;
                _billboards[_numBillboards].Height   = height;
                _billboards[_numBillboards].Texture  = texture;
                _billboards[_numBillboards].U        = 0;
                _billboards[_numBillboards].V        = 0;
                _billboards[_numBillboards].USize    = 1.0f;
                _billboards[_numBillboards].VSize    = 1.0f;

                _numBillboards++;
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// spawns a vehicle that has the player as destination
        /// </summary>
        public void SpawnAngryVehicle(bool isFriendly)
        {
            Math.Vector3 spawnPos       = GangManager.instance.FindGoodSpawnPointForCar();
            Vehicle      spawnedVehicle = null;

            if (!isFriendly && spawnedEnemies - 4 < maxSpawnedEnemies)
            {
                spawnedVehicle = GangManager.instance.SpawnGangVehicle(enemyGang,
                                                                       spawnPos, GangManager.instance.FindGoodSpawnPointForCar(), true, false, true, IncrementEnemiesCount);
            }
            else if (spawnedAllies - 4 < maxSpawnedAllies)
            {
                spawnedVehicle = GangManager.instance.SpawnGangVehicle(GangManager.instance.PlayerGang,
                                                                       spawnPos, GangManager.instance.FindGoodSpawnPointForCar(), true, false, true, IncrementAlliesCount);
            }

            if (spawnedVehicle != null)
            {
                spawnedVehicle.GetPedOnSeat(VehicleSeat.Driver).Task.DriveTo(spawnedVehicle, Game.Player.Character.Position, 25, 100);
                Function.Call(Hash.SET_DRIVE_TASK_DRIVING_STYLE, spawnedVehicle.GetPedOnSeat(VehicleSeat.Driver), 4457020); //ignores roads, avoids obstacles
            }
        }