//================================================================================================================================================================
//================================================================================================================================================================
void OceanHeightfield::CreateHeightfield(D3DXVECTOR2* out_h0, float* out_omega)
{
	int i, j;
	D3DXVECTOR2 K, Kn;

	D3DXVECTOR2 wind_dir;
	D3DXVec2Normalize(&wind_dir, &ocean_params.wind_dir);
	float a = ocean_params.wave_amplitude * 1e-7f;	// It is too small. We must scale it for editing.
	float v = ocean_params.wind_speed;
	float dir_depend = ocean_params.wind_dependency;

	int height_map_dim = ocean_params.displacement_map_dim;
	float patch_length = ocean_params.patch_length;

	// initialize random generator.
	srand(0);

	for (i = 0; i <= height_map_dim; i++)
	{
		// K is wave-vector, range [-|DX/W, |DX/W], [-|DY/H, |DY/H]
		K.y = (-height_map_dim / 2.0f + i) * (2 * D3DX_PI / patch_length);

		for (j = 0; j <= height_map_dim; j++)
		{
			K.x = (-height_map_dim / 2.0f + j) * (2 * D3DX_PI / patch_length);

			float phil = (K.x == 0 && K.y == 0) ? 0 : sqrtf(Phillips(K, wind_dir, v, a, dir_depend));

			out_h0[i * (height_map_dim + 4) + j].x = float(phil * GaussRandom() * HALF_SQRT_2);
			out_h0[i * (height_map_dim + 4) + j].y = float(phil * GaussRandom() * HALF_SQRT_2);

			// The angular frequency is following the dispersion relation:
			//            out_omega^2 = g*k
			// The equation of Gerstner wave:
			//            x = x0 - K/k * A * sin(dot(K, x0) - sqrt(g * k) * t), x is a 2D vector.
			//            z = A * cos(dot(K, x0) - sqrt(g * k) * t)
			// Gerstner wave shows that a point on a simple sinusoid wave is doing a uniform circular
			// motion with the center (x0, y0, z0), radius A, and the circular plane is parallel to
			// vector K.
			out_omega[i * (height_map_dim + 4) + j] = sqrtf(GRAV_ACCEL * sqrtf(K.x * K.x + K.y * K.y));
		}
	}
}
//=============================================
int GaussianNoiseGenerator::Execute(void)
{
  int is;
  float *noise_sig_ptr;
  float noise_sigma;
  float rand_var;
  float noise_sig_val;

  //--------------------------------------------
  noise_sigma = Noise_Sigma;
  long seed = Seed;

  //---------------------------------------------

  noise_sig_ptr = GET_OUTPUT_PTR(Noise_Sig);

  //-------------------------------------------------
  //  main loop

  for(is=0; is<Proc_Block_Size; is++)
    {
    // generate gaussian RV
    GaussRandom(&seed, &rand_var);

    noise_sig_val = noise_sigma * rand_var;

    *noise_sig_ptr++ = noise_sig_val;
    }// end of main loop

  // put back variables that have changed
  Seed = seed;

  //----------------------------------------------

  return(_MES_AOK);
};