- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/Water2.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    00017 using System;
    00018 using System.Collections.Generic;
    00019 using System.Text;
    00020 using System.IO;
    00021 using System.Drawing;
    00022 using Microsoft.DirectX;
    00023 using Microsoft.DirectX.Direct3D;
    00024 using Microsoft.Samples.DirectX.UtilityToolkit;
    00025
    00026 namespace DXGfxLib
    00027 {
    00028     public class Water2 : SceneObject
    00029     {
    00030         public VertexBuffer waterVBuff = null;
    00031         public IndexBuffer waterIBuff = null;
    00032         private Texture waterTexture = null;
    00033         private Texture bump = null;
    00034         public int numberVertices = 0;
    00035         public int numberTri = 0;
    00036         private Effect waterEffect = null;
    00037         private float movex = 0;
    00038         private float movey = 1;
    00039         private float R0 = 0.02037f;
    00040         private Frustrum frustrum = null;
    00041
    00042         public float desiredDimension = 1000.0f;
    00043
    00044         public Water2()
    00045         {
    00046             Initialize();
    00047         }
    00048
    00049         public override void Initialize()
    00050         {
    00051             base.Initialize();
    00052         }
    00053
    00054         public Water2(float dim)
    00055         {
    00056             LoadFromFile(DXGfxManager.GetGlobalInstance().d3dDevice, "Water.bmp");
    00057         }
    00058
    00059         public override void GetPrepared()
    00060         {
    00061             base.GetPrepared();
    00062
    00063             movex = 0;
    00064             movey = 1;
    00065             R0 = 0.02037f;
    00066
    00067             desiredDimension = 1000.0f;
    00068
    00069             Device dxDevice = DXGfxManager.GetGlobalInstance().d3dDevice;
    00070
    00071             string currDir = Directory.GetCurrentDirectory();
    00072
    00073             if (!File.Exists(fileName))
    00074             {
    00075                 throw new Exception("The ressource file does not exist! There is a problem somewhere.... did you delete or moved any file?");
    00076             }
    00077             else
    00078             {
    00079                 LoadFromFile(dxDevice, fileName);
    00080             }
    00081         }
    00082
    00088         public override void LoadFromFile(Device d3ddevice, string fileName)
    00089         {
    00090             int numberOfCollRow = 4;
    00091             int numberOfVerticesPerCollRow = numberOfCollRow + 1;
    00092
    00093             numberVertices = numberOfVerticesPerCollRow * numberOfVerticesPerCollRow;
    00094             numberTri = numberOfCollRow * numberOfCollRow * 2;
    00095
    00096             float startingXPos = -desiredDimension / 2;
    00097             float currentXPos = startingXPos;
    00098
    00099             float startingTU = 0.0f;
    00100             float currentTU = startingTU;
    00101             float stepTU = 1.0f / numberOfCollRow;
    00102
    00103             float startingZPos = desiredDimension / 2;
    00104             float currentZPos = startingZPos;
    00105
    00106             float startingTV = 1.0f;
    00107             float currentTV = startingTV;
    00108             float stepTV = 1.0f / numberOfCollRow;
    00109
    00110             float dimensionOfCollRow = desiredDimension / numberOfCollRow;
    00111
    00112             int vertexIndex = 0;
    00113
    00114             CustomVertex.PositionNormalTextured[] vertices = new CustomVertex.PositionNormalTextured[numberVertices];
    00115
    00116             for (int i = 0; i < numberOfVerticesPerCollRow; i++)
    00117             {
    00118                 for (int j = 0; j < numberOfVerticesPerCollRow; j++)
    00119                 {
    00120                     vertices[vertexIndex].X = currentXPos;
    00121                     vertices[vertexIndex].Y = 0.0f;
    00122                     vertices[vertexIndex].Z = currentZPos;
    00123
    00124                     vertices[vertexIndex].Nx = 0.0f;
    00125                     vertices[vertexIndex].Ny = 1.0f;
    00126                     vertices[vertexIndex].Nz = 0.0f;
    00127
    00128                     vertices[vertexIndex].Tu = currentTU;
    00129                     vertices[vertexIndex].Tv = currentTV;
    00130
    00131                     vertexIndex++;
    00132
    00133                     currentTU += stepTU;
    00134                     currentXPos += dimensionOfCollRow;
    00135                 }
    00136                 currentXPos = startingXPos;
    00137                 currentTU = startingTU;
    00138                 currentZPos -= dimensionOfCollRow;
    00139                 currentTV -= stepTV;
    00140             }
    00141
    00142             waterVBuff = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), numberVertices,
    00143                 d3ddevice,
    00144                 Usage.Dynamic, CustomVertex.PositionNormalTextured.Format, Pool.SystemMemory);
    00145
    00146             GraphicsStream vbStream = waterVBuff.Lock(0, 0, LockFlags.NoOverwrite);
    00147             vbStream.Write(vertices);
    00148             waterVBuff.Unlock();
    00149
    00150
    00151             Int16[] indices = new Int16[numberTri * 3];
    00152
    00153             int indiceIndex = 0;
    00154
    00155             for (int n = 0; n < numberOfCollRow; n++)
    00156             {
    00157                 for (int m = 0; m < numberOfCollRow; m++)
    00158                 {
    00159                     indices[indiceIndex + 0] = (Int16)(m + n * (numberOfCollRow + 1));
    00160                     indices[indiceIndex + 1] = (Int16)(m + 1 + (n + 1) * (numberOfCollRow + 1));
    00161                     indices[indiceIndex + 2] = (Int16)(m + (n + 1) * (numberOfCollRow + 1));
    00162
    00163                     indices[indiceIndex + 3] = (Int16)(m + n * (numberOfCollRow + 1));
    00164                     indices[indiceIndex + 4] = (Int16)(m + 1 + n * (numberOfCollRow + 1));
    00165                     indices[indiceIndex + 5] = (Int16)(m + 1 + (n + 1) * (numberOfCollRow + 1));
    00166
    00167                     indiceIndex += 6;
    00168                 }
    00169             }
    00170
    00171             waterIBuff = new IndexBuffer(d3ddevice,
    00172                 sizeof(Int16) * numberTri * 3, Usage.Dynamic, Pool.SystemMemory, true);
    00173
    00174             GraphicsStream ibStream = waterIBuff.Lock(0, 0, LockFlags.NoOverwrite);
    00175
    00176             ibStream.Write(indices);
    00177
    00178             waterIBuff.Unlock();
    00179
    00180             // Load the water texture
    00181             // Load our file
    00182             this.fileName = fileName;
    00183             string textureFile = Utility.FindMediaFile(fileName);
    00184             // Store the current folder, then switch to the folder where the media was found
    00185             string currentFolder = System.IO.Directory.GetCurrentDirectory();
    00186             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(textureFile).Directory.FullName);
    00187             // Now load the mesh hierarchy
    00188             waterTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, textureFile);
    00189
    00190             System.IO.Directory.SetCurrentDirectory(currentFolder);
    00191
    00192             textureFile = Utility.FindMediaFile("waveBumps.dds");
    00193             // Store the current folder, then switch to the folder where the media was found
    00194             currentFolder = System.IO.Directory.GetCurrentDirectory();
    00195             System.IO.Directory.SetCurrentDirectory(new System.IO.FileInfo(textureFile).Directory.FullName);
    00196             // Now load the mesh hierarchy
    00197             bump = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, textureFile);
    00198
    00199             System.IO.Directory.SetCurrentDirectory(currentFolder);
    00200
    00201             worldMat = Matrix.Translation(0.0f, 0.0f, 0.0f);
    00202         }
    00203
    00204         public override void Update(Frustrum frustrum, double appTime, float elapsedTime)
    00205         {
    00206             this.frustrum = frustrum;
    00207             movex = 0.03f * (float)Math.Sin(appTime);
    00208             movey = 0.03f * (float)Math.Cos(appTime);
    00209         }
    00210
    00211         public void Render(Device device, Texture refractionTex, Texture reflectionTex)
    00212         {
    00213             // Water
    00214             device.Transform.World = worldMat;
    00215             device.SetStreamSource(0, waterVBuff, 0);
    00216             device.Indices = waterIBuff;
    00217
    00218             device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
    00219
    00220             if (waterEffect == null)
    00221             {
    00222                 DXGfxManager.GetGlobalInstance().LoadEffect(device, "WaterHLSL.fx", ref waterEffect);
    00223             }
    00224
    00225             Camera camera = DXGfxManager.GetGlobalInstance().CurrentCamera;
    00226             waterEffect.SetValue("g_WorldViewProj", worldMat * frustrum.matView * frustrum.matProj);
    00227             waterEffect.SetValue("g_WaterTexture", waterTexture);
    00228             waterEffect.SetValue("g_BumpTexture", bump);
    00229             waterEffect.SetValue("g_RefractionTexture", refractionTex);
    00230             waterEffect.SetValue("g_ReflectionTexture", reflectionTex);
    00231             waterEffect.SetValue("g_Movex", movex);
    00232             waterEffect.SetValue("g_Movey", movey);
    00233             waterEffect.SetValue("g_R0", R0);
    00234             Vector3 position = MathUtil.ExtractPosition(Matrix.Invert(frustrum.matView));
    00235             waterEffect.SetValue("g_Eye", new Vector4(position.X, position.Z, position.Y, 1));
    00236             waterEffect.Technique = "RenderWater20";
    00237
    00238             // Apply the technique contained in the effect
    00239             int passes = waterEffect.Begin(0);
    00240             for (int pass = 0; pass < passes; pass++)
    00241             {
    00242                 waterEffect.BeginPass(pass);
    00243
    00244                 device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numberVertices, 0, numberTri);
    00245
    00246                 waterEffect.EndPass();
    00247             }
    00248             waterEffect.End();
    00249         }
    00250     }
    00251 }