using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using System.IO; namespace XNAGame { public class Water { GraphicsDevice device; Effect effect; //Water variables private VertexPositionTexture[] waterVertices; private float waterHeight; //Water bumpmapping variables private Texture2D waterBumpMap; private Matrix worldMatrix; public Water(float waterHeight) { this.waterHeight = waterHeight; } public void Initialize(GraphicsDevice gd, ContentManager cm) { SetUpXNADevice(gd); SetUpEffects(cm); SetUpWaterVertices(); } private void SetUpXNADevice(GraphicsDevice gd) { device = gd; } private void SetUpEffects(ContentManager cm) { effect = cm.Load("effect"); waterBumpMap = cm.Load("Models\\bumpmaps\\water"); } private void SetUpWaterVertices() { int WIDTH = 500; int HEIGHT = 500; waterVertices = new VertexPositionTexture[6]; waterVertices[0] = new VertexPositionTexture(new Vector3(0, 0, waterHeight), new Vector2(0, 1)); waterVertices[2] = new VertexPositionTexture(new Vector3(WIDTH, HEIGHT, waterHeight), new Vector2(1, 0)); waterVertices[1] = new VertexPositionTexture(new Vector3(0, HEIGHT, waterHeight), new Vector2(0, 0)); waterVertices[3] = new VertexPositionTexture(new Vector3(0, 0, waterHeight), new Vector2(0, 1)); waterVertices[5] = new VertexPositionTexture(new Vector3(WIDTH, 0, waterHeight), new Vector2(1, 1)); waterVertices[4] = new VertexPositionTexture(new Vector3(WIDTH, HEIGHT, waterHeight), new Vector2(1, 0)); } public void updateCamera(Matrix viewMatrix, Matrix projectionMatrix) { worldMatrix = Matrix.Identity; effect.Parameters["xCameraViewProjection"].SetValue(viewMatrix * projectionMatrix); effect.Parameters["xView"].SetValue(viewMatrix); effect.Parameters["xProjection"].SetValue(projectionMatrix); effect.Parameters["xWorld"].SetValue(worldMatrix); } private void DrawWater(Matrix viewMatrix, Matrix projectionMatrix, Matrix reflectionViewMatrix, Texture2D reflectionMap, Texture2D refractionMap, Vector3 cameraPosition, float elapsedTime) { effect.CurrentTechnique = effect.Techniques["Water"]; Matrix worldMatrix = Matrix.Identity; effect.Parameters["xWorld"].SetValue(worldMatrix); effect.Parameters["xView"].SetValue(viewMatrix); effect.Parameters["xReflectionView"].SetValue(reflectionViewMatrix); effect.Parameters["xProjection"].SetValue(projectionMatrix); effect.Parameters["xReflectionMap"].SetValue(reflectionMap); effect.Parameters["xRefractionMap"].SetValue(refractionMap); //Set the wavelength and waveheight of the water effect.Parameters["xWaterBumpMap"].SetValue(waterBumpMap); effect.Parameters["xWaveLength"].SetValue(0.1f); effect.Parameters["xWaveHeight"].SetValue(0.3f); //For the Fresnel realization effect.Parameters["xCamPos"].SetValue(cameraPosition); //Set the effect file to let the water move effect.Parameters["xTime"].SetValue(elapsedTime); effect.Parameters["xWindForce"].SetValue(20.0f); Matrix windDirection = Matrix.CreateRotationZ(MathHelper.PiOver2); effect.Parameters["xWindDirection"].SetValue(windDirection); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); device.VertexDeclaration = new VertexDeclaration(device, VertexPositionTexture.VertexElements); device.DrawUserPrimitives(PrimitiveType.TriangleList, waterVertices, 0, 2); pass.End(); } effect.End(); } public void Draw(Matrix viewMatrix, Matrix projectionMatrix, Matrix reflectionViewMatrix, Texture2D reflectionMap, Texture2D refractionMap, Vector3 cameraPosition, float elapsedTime) { DrawWater(viewMatrix, projectionMatrix, reflectionViewMatrix, reflectionMap, refractionMap, cameraPosition, elapsedTime); } } }