English 中文(简体)
My "perlin" noise effect shader produces either all-white or all-black
原标题:

I m trying to code a "perlin" noise shader in NVidia FX Composer. However, no matter how I tweak the noise function, it returns either 100% white or 100% black. I have no clue how to solve this or even where the problem is.

Edit: If you ve seen This page, you probably know where I got the code. Figured I d start with a method I d already gotten working on a CPU.

Help, please.

float Noise1(int x, int y)
{
    int n = x + y * 57;
// int n = x + y * 1376312627;
// n = n * n;
// n = (int)pow(n * pow(2, 13), n);
// return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) + 0x7fffffff) / 1073741824.0);    
// return abs( ( (n * (n * n * 15731 + 789221) + 1376312589) + 0x7fffffff) / 2147483647.0);
// return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) + 0x7fffffff) / 2147483647.0);    
// return ( n / 2147483647.0);    
return ( ((float)n) / 500.0 );
// return n = 2147483647.0;
}

float SmoothNoise_1(int x, int y)
{
    float corners = ( Noise1(x-1, y-1) + Noise1(x+1, y-1) + Noise1(x-1, y+1) + Noise1(x+1, y+1) ) / 16.0;
    float sides   = ( Noise1(x-1, y)   + Noise1(x+1, y)   + Noise1(x,   y-1) + Noise1(x, y+1) ) /  8.0;
    float center  =  Noise1(x, y) / 4.0;
    return corners + sides + center;
}

float Cosine_Interpolate(float a, float b, float x)
{
float ft = x * 3.1415927;
float f = (1 - cos(ft)) * 0.5;

return  a*(1-f) + b*f;
}

float InterpolatedNoise_1(float x, float y)
{
int integer_X    = (int)x;
float fractional_X = x - integer_X;

int integer_Y    = (int)y;
float fractional_Y = y - integer_Y;

float v1 = SmoothNoise_1(integer_X,     integer_Y);
float v2 = SmoothNoise_1(integer_X + 1, integer_Y);
float v3 = SmoothNoise_1(integer_X,     integer_Y + 1);
float v4 = SmoothNoise_1(integer_X + 1, integer_Y + 1);

float i1 = Cosine_Interpolate(v1 , v2 , fractional_X);
float i2 = Cosine_Interpolate(v3 , v4 , fractional_X);

return Cosine_Interpolate(i1 , i2 , fractional_Y);
}


int width = 512;
int height = 512;


float4 PerlinNoise_2D(float2 xy : TEXCOORD0) : COLOR0
{
float4 total = 0;
// int p = persistence;
float p = 1.0;
// int n = Number_Of_Octaves - 1;
int n = 2;

for(int i = 0; i < n; ++i)
{
    float frequency = pow(2, i);
    float amplitude = pow(p, i);

    /* total.a = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude;
    total.r = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude;
    total.g = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude;
    total.b = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude; */
    /* total.a = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude;
    total.r = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude;
    total.g = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude;
    total.b = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude; */
    total.a = InterpolatedNoise_1(xy.x * width, xy.y * height);
    total.r = InterpolatedNoise_1(xy.x * width, xy.y * height);
    total.g = InterpolatedNoise_1(xy.x * width, xy.y * height);
    total.b = InterpolatedNoise_1(xy.x * width, xy.y * height);
}

return clamp(total, 0.0, 1.0);
// return (float)(int)(2147483647 + 2147483647 + 2147483647 / 2) / 2147483647.0;
}

technique Perlin
{
pass p0
{
        VertexShader = null;
        PixelShader = compile ps_3_0 PerlinNoise_2D();
}
}

Thanks.

问题回答

In short form, because a GeForce 8800GT doesn t do Bitwise and pow() returns a float. So no spinning and wobbling integer bits. (Very technical explanation, that).





相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

Why and when should you use hoops 3d graphics? [closed]

My company needs 3D visualization for our commercial applications (CAD, mesh manipulation, computational geometry). We are tired of true vision 3D (tv3d), which we ve been using for years (poor ...

Prerequisite for learning directx

I am from .net C# background and I want to learn DirectX. I have knowledge of C++ but I am fairly new to graphic world. I am little confused about how to start learning directx, should I start ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Determine VRAM size on windows

I need to determine roughly how much VRAM a system s graphics card has. I know all the reasons why I shouldn t but I do. It doesn t need to be perfect (some cards lie etc.) but I need a ballpark. On ...

Mask white color out of PNG image on iPhone

I know that there are already 2 relevant posts on that, but it s not clear... So the case is this...: I have a UIImage which consists of a png file requested from a url on the net. Is it possible to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签