Sign in to follow this  
zulbaric

Converting some code?

Recommended Posts

Hi would it be possible to convert some DirectX code to XNA C#? float Noise(int x) { x = (x<<13) ^ x; return (1.0 - ((x * (x*x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); } float CosInterpolate(float v1, float v2, float a) { float angle = a * D3DX_PI; float prc = (1.0f - cos(angle)) * 0.5f; return v1*(1.0f - prc) + v2*prc; } HRESULT HEIGHTMAP::CreateRandomHeightMap(int seed, float noiseSize, float persistence, int octaves) { //For each map node for(int y=0;y<m_size.y;y++) for(int x=0;x<m_size.x;x++) { //Scale x & y to the range of 0.0 - m_size float xf = ((float)x / (float)m_size.x) * noiseSize; float yf = ((float)y / (float)m_size.y) * noiseSize; float total = 0; // For each octave for(int i=0;i<octaves;i++) { //Calculate frequency and amplitude (different for each octave) float freq = pow(2, i); float amp = pow(persistence, i); //Calculate the x,y noise coordinates float tx = xf * freq; float ty = yf * freq; int tx_int = tx; int ty_int = ty; //Calculate the fractions of x & y float fracX = tx - tx_int; float fracY = ty - ty_int; //Get the noise of this octave for each of these 4 points float v1 = Noise(tx_int + ty_int * 57 + seed); float v2 = Noise(tx_int+ 1 + ty_int * 57 + seed); float v3 = Noise(tx_int + (ty_int+1) * 57 + seed); float v4 = Noise(tx_int + 1 + (ty_int+1) * 57 + seed); //Smooth in the X-axis float i1 = CosInterpolate(v1 , v2 , fracX); float i2 = CosInterpolate(v3 , v4 , fracX); //Smooth in the Y-axis total += CosInterpolate(i1 , i2 , fracY) * amp; } int b = 128 + total * 128.0f; if(b < 0)b = 0; if(b > 255)b = 255; //Save to heightMap m_pHeightMap[x + y * m_size.x] = (b / 255.0f) * m_maxHeight; } return S_OK; } thanks

Share this post


Link to post
Share on other sites
This isn't really DirectX specific code apart from the pi constant and to a lesser extent the hresult enumeration.
I haven't tested it, but replacing them with a C# equivalent (Math.PI and functions in the Math namespace, your own defined return type) should probably make most of it working in XNA.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this