예제 #1
0
        void game_InitializeEvent(object sender, EventArgs e)
        {
            Matrix mWaterWorld = Matrix.Identity;// r* t;

            /*--------------------------------------------------------------------------
             * Create the water and fill in the info structure
             *--------------------------------------------------------------------------*/
            WaterInfoTest info = new WaterInfoTest();

            info.fxFilename = System.Windows.Forms.Application.StartupPath + @"\Shaders\Water.fx";
            info.vertRows   = 513;
            info.vertCols   = 513;
            info.dx         = 5.25f; //spaccing of grid lines - big distance
            info.dz         = 5.25f;
            //info.dx = 0.5f; // small distance
            //info.dz = 0.5f;
            info.waveMapFileName0  = System.Windows.Forms.Application.StartupPath + @"\Content\wave0.dds";
            info.waveMapFileName1  = System.Windows.Forms.Application.StartupPath + @"\Content\wave1.dds";
            info.waveNMapVelocity0 = new Vector2(0.03f, 0.05f);
            info.waveNMapVelocity1 = new Vector2(-0.01f, 0.03f);
            info.texScale          = 24.0f;
            info.toWorld           = mWaterWorld;

            TextureCube mEnvMap = TextureCube.FromFile(game.GraphicsDevice, System.Windows.Forms.Application.StartupPath + @"\Content\EnvMap.dds");

            water = new Water(game, info, mEnvMap);

            //mWaterColor = new ColorValue( ( 255 * .13f ), ( 255 * .19f ), ( 255 * .22f ) );

            //use a sky model with a Hoffman-Preethem scattering method
            //mSkyModel = new SkyModel( 2000, mGDevice, typeof( Hoffman_Preethem ), mTerrain.Textures );

            //mGlarePostProcess = new GlarePostProcess( mGDevice, mGDevice.Viewport.Width, mGDevice.Viewport.Height );
            //mEnablePostProcess = true;
        }
예제 #2
0
        public Water(IXNAGame _game, WaterInfoTest waterInfo, TextureCube envMap)
        {
            numLeaves = 0;
            game      = _game;
            mGDevice  = game.GraphicsDevice;

            mInfo = waterInfo;

            mWidth  = (waterInfo.vertCols - 1) * waterInfo.dx;
            mHeight = (waterInfo.vertRows - 1) * waterInfo.dz;

            mWaveNMapOffset0 = new Vector2(0, 0);
            mWaveNMapOffset1 = new Vector2(0, 0);

            mWaveDMapOffset0 = new Vector2(0, 0);
            mWaveDMapOffset1 = new Vector2(0, 0);

            mNumTris     = (waterInfo.vertRows - 1) * (waterInfo.vertCols - 1) * 2;
            mNumVertices = waterInfo.vertRows * waterInfo.vertCols;

            WorldMatrix = waterInfo.toWorld;

            #region Build the Mesh

            VertexElement[] decleration = new VertexElement[]
            { new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0),
              new VertexElement(0, 12, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0),
              new VertexElement(0, 20, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1) };

            VertexElement[] elems = WaterDMapVertex.CreateVertexElements();

            // Generate the water grid and write to water mesh

            //mMesh = new Mesh( mNumTris, mNumVertices, MeshFlags.Managed | MeshFlags.Use32Bit, elems, mGDevice );
            mMesh = new Mesh(game, mNumTris, mNumVertices, WaterDMapVertex.StrideSize
                             , Graphics.AttributeSystem.CreateVertexDeclaration(game.GraphicsDevice, typeof(WaterDMapVertex)));

            //Mesh temp = mMesh.Clone(MeshFlags.Managed, decleration, mGDevice);
            //mesh.Dispose();

            Vector3[] verts;
            int[]     indices;

            Utils.GenTriGridOld(mInfo.vertRows, mInfo.vertCols, mInfo.dx, mInfo.dz, new Vector3(0, 0, 0), out verts, out indices);

            WaterDMapVertex[] v = new WaterDMapVertex[mNumVertices];
            for (int i = 0; i < mInfo.vertRows; ++i)
            {
                for (int j = 0; j < mInfo.vertCols; ++j)
                {
                    int index = i * mInfo.vertCols + j;
                    v[index].pos        = verts[index];
                    v[index].scaledTexC = new Vector2((float)j / mInfo.vertCols,
                                                      (float)i / mInfo.vertRows) * mInfo.texScale;
                    v[index].normalizedTexC = new Vector2((float)j / mInfo.vertCols,
                                                          (float)i / mInfo.vertRows);
                }
            }

            //set the vertexbuffer data
            mMesh.SetVertexBufferData(v);

            //set the index data
            mMesh.SetIndexBufferData(indices);

            int[] adjac = new int[mMesh.NumberFaces * 3];
            mMesh.GenerateAdjacency(0.001f, adjac);
            //mMesh.OptimizeInPlace( MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeAttributeSort, adjac );
            mMesh.OptimizeInPlace(0, adjac);
            adjac = null;

            #endregion

            /*--------------------------------------------------------------------
             * Create the Textures
             */
            int m = mInfo.vertRows;
            int n = mInfo.vertCols;

            mWaveMap0 = Texture2D.FromFile(mGDevice, mInfo.waveMapFileName0);
            mWaveMap1 = Texture2D.FromFile(mGDevice, mInfo.waveMapFileName1);

            //mReflectMap = new RenderToTexture( mGDevice, 1024, 1024, 0, Format.A8R8G8B8, true, DepthFormat.D24X8, true );
            //mRefractMap = new RenderToTexture( mGDevice, 1024, 1024, 0, Format.A8R8G8B8, true, DepthFormat.D24X8, true );
            mReflectMap = new RenderTarget2D(game.GraphicsDevice, 1024, 1024, 0, SurfaceFormat.Color);
            mRefractMap = new RenderTarget2D(game.GraphicsDevice, 1024, 1024, 0, SurfaceFormat.Color);
            DepthBuffer = new DepthStencilBuffer(game.GraphicsDevice,
                                                 1024,
                                                 1024,
                                                 game.GraphicsDevice.DepthStencilBuffer.Format);

            mVisibleSubgrids = new List <SubGrid>();

            buildGeometryHeirarchy();
            buildEffect(game, envMap);
        }