Ejemplo n.º 1
0
        public void GetObjectPositionOnWater(ObjectGeometry obj, WaterShader shader)
        {
            PositionNormal p11 = new PositionNormal();
            PositionNormal p12 = new PositionNormal();
            PositionNormal p13 = new PositionNormal();
            PositionNormal p21 = new PositionNormal();
            PositionNormal p22 = new PositionNormal();
            PositionNormal p23 = new PositionNormal();
            PositionNormal p31 = new PositionNormal();
            PositionNormal p32 = new PositionNormal();
            PositionNormal p33 = new PositionNormal();

            Vector3 position = obj.ModelMatrix.Translation;
            position.Y = 20;

            p11.Position = CalculatePosition(Vector3.Transform(position + new Vector3(-5, 0, 5), obj.RotationMatrix), shader);
            p12.Position = CalculatePosition(Vector3.Transform(position + new Vector3(0, 0, 5), obj.RotationMatrix), shader);
            p13.Position = CalculatePosition(Vector3.Transform(position + new Vector3(5, 0, 5), obj.RotationMatrix), shader);

            p21.Position = CalculatePosition(Vector3.Transform(position + new Vector3(-5, 0, 0), obj.RotationMatrix), shader);
            p22.Position = CalculatePosition(Vector3.Transform(position + new Vector3(0, 0, 0), obj.RotationMatrix), shader);
            p23.Position = CalculatePosition(Vector3.Transform(position + new Vector3(5, 0, 0), obj.RotationMatrix), shader);

            p31.Position = CalculatePosition(Vector3.Transform(position + new Vector3(-5, 0, -5), obj.RotationMatrix), shader);
            p32.Position = CalculatePosition(Vector3.Transform(position + new Vector3(0, 0, -5), obj.RotationMatrix), shader);
            p33.Position = CalculatePosition(Vector3.Transform(position + new Vector3(5, 0, -5), obj.RotationMatrix), shader);

            CalculateNormal(p11, p22, p23);
            CalculateNormal(p11, p12, p23);

            CalculateNormal(p12, p22, p23);
            CalculateNormal(p12, p13, p23);

            CalculateNormal(p21, p31, p32);
            CalculateNormal(p21, p22, p32);

            CalculateNormal(p22, p32, p33);
            CalculateNormal(p22, p23, p33);

            obj.Translate(new Vector3(position.X,p22.Position.Y,position.Z));
            obj.Update();

            obj.UpVector = p22.Normal;
            obj.RightVector = Vector3.Cross(obj.ForwardVector, obj.UpVector);
            obj.RightVector = Vector3.Normalize(obj.RightVector);

            obj.ForwardVector = Vector3.Cross(obj.UpVector, obj.RightVector);
            obj.ForwardVector = Vector3.Normalize(obj.ForwardVector);
        }
Ejemplo n.º 2
0
        private Vector3 CalculatePosition(Vector3 position, WaterShader shader)
        {
            Vector3 P = position;
            for (int i = 0; i < 24; i += 6)
            {
                float A = shader.Waves[i] * shader.Waves[i + 3];
                float omega = 2.0f * 3.14f / shader.Waves[i];
                float phi = shader.Waves[i + 2] * omega;
                float Qi = shader.Waves[i + 1] / (omega * A * 4.0f);

                Vector2 a = new Vector2(shader.Waves[i + 4], shader.Waves[i + 5]);
                Vector2 b = new Vector2(position.X, position.Z);
                float term = omega * Vector2.Dot(a, b) + phi * shader.Time;

                float C = (float)Math.Cos(term);
                float S = (float)Math.Sin(term);
                P += new Vector3(Qi * A * shader.Waves[i + 4] * C, A * S * 10, i * A * shader.Waves[i + 5] * C);
            }
            return P;
        }
Ejemplo n.º 3
0
        public TerrainScreen()
        {
            camera = new FirstPersonCamera(new Vector3(867, 198, -1215));
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1, 10000);

            islandShader = new MultiTextured();
            {
                islandShader.ProjectionMatrix = projectionMatrix;
                islandShader.ViewMatrix = camera.View;
                islandShader.InitParameters();
            }

            mvpshader = new JustMvp();
            {
                mvpshader.ProjectionMatrix = projectionMatrix;
                mvpshader.ViewMatrix = camera.View;
                mvpshader.InitParameters();
            }

            shipShader = new BasicEffect(BaseClass.Device);
            {
                shipShader.Projection = projectionMatrix;
                shipShader.View = camera.View;
            }

            scatteringShader = new Scattaring();
            {
                scatteringShader.ProjectionMatrix = projectionMatrix;
                scatteringShader.ViewMatrix = camera.View;
                scatteringShader.InitParameters();
            }

            waterShader = new WaterShader();
            {
                waterShader.ProjectionMatrix = projectionMatrix;
                waterShader.ViewMatrix = camera.View;
                waterShader.InitParameters();
            }

            cloudShader = new CloudShader();
            {
                cloudShader.ProjectionMatrix = projectionMatrix;
                cloudShader.ViewMatrix = camera.View;
                cloudShader.InitParameters();
            }

            rainShader = new RainShader();
            {
                rainShader.ProjectionMatrix = projectionMatrix;
                rainShader.ViewMatrix = camera.View;
                rainShader.InitParameters();
            }

            rainDropsShader = new RainDropsShader();
            {
                rainDropsShader.InitParameters();
            }

            fogShader = new Pirates.Shaders.Fog();
            {
                fogShader.InitParameters();
            }

            rs = new RasterizerState();
            rs.CullMode = CullMode.None;
            //rs.FillMode = FillMode.WireFrame;

            cloudManager = new CloudManager();
            rainManager = new RainManager();

            Vector3 minBox = new Vector3(-5000f, 300f, -5000f);
            Vector3 maxBox = new Vector3(5000f, 1500f, 5000f);

            int[] allCloudSprites = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            cloudManager.AddCloud(1000, minBox, maxBox, 0.75f, allCloudSprites);
            cloudManager.Instancer.Update();

            minBox = new Vector3(-800, 320f, -800f);
            maxBox = new Vector3(800f, 0f, 800f);
            rainManager.AddDrop(250000, minBox, maxBox);
            cloudManager.Instancer.Update();

            island = new Terrain("island4", 10, 1);

            water = new Terrain("map2", 30, 1);
            water.Translate(0, 40, 0);
            water.Update();
            reflectionPlane = CreatePlane(40, new Vector3(0, -1, 0), true);
            reflectionMatrix = Matrix.CreateReflection(reflectionPlane);

            skydome = new ObjectSkydome(scatteringShader);
            skydome.Scale(3200);
            skydome.Rotate(-90, new Vector3(1, 0, 0));
            skydome.Update();

            Wind.Direction = MathFunctions.AngleToVector(45);
            //Wind.Force = 1000000f;

            ship = new ObjectShip();
            ship.Translate(-288, 20, -486);
            ship.Update();
            ship.Physics = new ObjectPhysics(50, ship.ModelMatrix);

            if (island.IsOnHeightmap(ship.ModelMatrix.Translation))
            {
                ship.Physics.material = MaterialType.Island;
                island.ColisionWithTerrain(ship);
            }
            else
            {
                ship.Physics.material = MaterialType.Water;
                water.GetObjectPositionOnWater(ship, waterShader);
            }
        }
Ejemplo n.º 4
0
        public TerrainScreen(SerializationInfo info, StreamingContext ctxt)
        {
            this.camera = (FirstPersonCamera)info.GetValue("Camera", typeof(FirstPersonCamera));
            this.aspectRatio = (float)info.GetValue("AspectRatio", typeof(float));
            this.projectionMatrix = (Matrix)info.GetValue("ProjectionMatrix", typeof(Matrix));
            this.rs = new RasterizerState();

            islandShader = new MultiTextured();
            {
                islandShader.ProjectionMatrix = projectionMatrix;
                islandShader.ViewMatrix = camera.View;

                islandShader.InitParameters();
            }

            mvpshader = new JustMvp();
            {
                mvpshader.ProjectionMatrix = projectionMatrix;
                mvpshader.ViewMatrix = camera.View;
                mvpshader.InitParameters();
            }

            scatteringShader = new Scattaring();
            {
                scatteringShader.ProjectionMatrix = projectionMatrix;
                scatteringShader.ViewMatrix = camera.View;
                scatteringShader.InitParameters();
            }

            waterShader = new WaterShader();
            {
                waterShader.ProjectionMatrix = projectionMatrix;
                waterShader.ViewMatrix = camera.View;
                waterShader.InitParameters();
            }

            rs = new RasterizerState();
            rs.CullMode = CullMode.None;
            rs.FillMode = FillMode.WireFrame;

            island = new Terrain("island4", 2, 1);
            water = new Terrain("map2", 1, 1);
            water.Translate(0, 30, 0);
            water.Update();

            DepthStencilState depthStencilState = new DepthStencilState();
            depthStencilState.DepthBufferFunction = CompareFunction.LessEqual;
            BaseClass.Device.DepthStencilState = depthStencilState;
        }