• Advertisement
Sign in to follow this  

PerlinGPU and QuadTree

This topic is 1857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

I use Libnoise.XNA for creating the height of my Terrain.

I have implemented a Quadtree with LOD.

So with Libnoise i Choose the right Position of the Heightmap with:

[CODE]
float[] v = new float[Planet.size * Planet.size];
for (int z = 0; z < Planet.size; z++)
for (int x = 0; x < Planet.size; x++)
{
test = new Vector3(MathHelper.Lerp(-1, 1, (float)x / (Planet.size - 1)), 0, MathHelper.Lerp(-1, 1, (float)z / (Planet.size - 1)));
test = Vector3.Transform(test, world);
test = Vector3.Normalize(test);
v[x + z * Planet.size] = (float)perlin.GetValue(test.X, 0, test.Z); ;
[/CODE]

perlin.GetValue.


So i Implemented the GPU Perlin

[CODE]
float4x4 World;
float3 coord;
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 texCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 texCoord : TEXCOORD0;
float4 wPosition: TEXCOORD1;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = input.Position;
output.wPosition = input.Position;
output.texCoord = input.texCoord;
return output;
}
texture permTexture2d;
texture permGradTexture;
sampler permSampler2d = sampler_state
{
texture = <permTexture2d>;
AddressU = Wrap;
AddressV = Wrap;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = NONE;
};
sampler permGradSampler = sampler_state
{
texture = <permGradTexture>;
AddressU = Wrap;
AddressV = Clamp;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = NONE;
};
float3 fade(float3 t)
{
return t * t * t * (t * (t * 6 - 15) + 10); // new curve
}
float4 perm2d(float2 p)
{
return tex2D(permSampler2d, p);
}
float gradperm(float x, float3 p)
{
float3 sample = tex1D(permGradSampler, x);
return dot(sample, p);
}
float gradPerm(float x, float3 p)
{
return dot(tex1D(permGradSampler, x), p);
}
float inoise(float3 p)
{
//new Vector3(MathHelper.Lerp(-1, 1, (float)x / (Planet.size - 1)), 0, MathHelper.Lerp(-1, 1, (float)z / (Planet.size - 1)));
float3 xx;
xx.x = lerp( -1 ,1, coord.x);
xx.y = 0;
xx.z =lerp( -1 ,1, coord.z);

xx = normalize(xx);
float3 P = fmod(floor(p), 256);
p -= floor(p);
float3 f = fade(p);
P = P /256;
const float one = -11.0 /256;
float4 AA = perm2d(P.xy) + P.z;
return lerp(lerp( lerp( gradPerm(AA.x, p ),
gradPerm(AA.z, p + float3(-1, 0, 0) ), f.x),
lerp( gradPerm(AA.y, p + float3(0, -1, 0) ),
gradPerm(AA.w, p + float3(-1, -1, 0) ), f.x), f.y),

lerp( lerp( gradPerm(AA.x+one, p + float3(0, 0, -1) ),
gradPerm(AA.z+one, p + float3(-1, 0, -1) ), f.x),
lerp( gradPerm(AA.y+one, p + float3(0, -1, -1) ),
gradPerm(AA.w+one, p + float3(-1, -1, -1) ), f.x), f.y), f.z);
}
float turbulence(float3 p, int octaves, float lacunarity = 2.0, float gain = 0.5)
{
float sum = 0;
float freq = 1.0, amp = 1.0;
for(int i=0; i < octaves; i++) {
sum += abs(inoise(p*freq))*amp;
freq *= lacunarity;
amp *= gain;
}
return sum;
}
float ridge(float h, float offset)
{
h = abs(h);
h = offset - h;
h = h * h;
return h;
}
float ridgedmf(float3 p, int octaves, float lacunarity, float gain, float offset = 1.0)
{
float sum = 0;
float freq = 1.3f;
float amp = 0.5;
float prev = 1.0;
for(int i=0; i < octaves; i++)
{
float n = ridge(inoise(p*freq), offset);
sum += n*amp*prev;
prev = n;
freq *= lacunarity;
amp *= gain;
}
return sum;
}
float fBm(float3 p, int octaves, float lacunarity, float gain = 0.7)
{
float freq = 1.3f,
amp = 0.5f;
float sum = 0.6f;
for(int i=0; i < octaves; i++) {
sum += inoise(p*freq)*amp;
freq *= lacunarity;
amp *= gain;
}
return sum;
}
float Interpolate(float x, float y, float a)
{
float val = (1 - cos(a * 3.1415926535f)) * .5;
return x * (1 - val) + y * val;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float3 p = input.wPosition;
float inz = inoise(p)*0.7+0.7;


inz += ridgedmf(p,8,2.0,0.5);
inz *= turbulence(p,8,2.0,0.5);
inz *= fBm(p,8,2.0,0.8);
inz *= Interpolate(0.7,0.7,0.5);


return float4(inz,inz,inz,1);
}
technique PerlinNoise
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}
[/CODE]

But how can i get the Coords similar like perlin.GetValue?

Anyone can help me? :/

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement