im trying to write a cg pixel shader and just want to generate a random number too offset the colour of a pixel

is there something i need to include, i tried using rand() but gave me errors

thanks in advance

im trying to write a cg pixel shader and just want to generate a random number too offset the colour of a pixel

is there something i need to include, i tried using rand() but gave me errors

thanks in advance

rand() and RAND_MAX are both defined in cstdlib, so try adding the following line to your program: #include <cstdlib> :)

BTW, If this doesn't work, could you please post the code then?

this heres my pixel shader and i have #include <cstdlib> in my winMain, can the include be put in the cg file or???

struct OutputStructure {
  float4 colour : COLOR;
};

OutputStructure pixelMain(float4 colour : COLOR,
                            float2 diffuseTexture : TEXCOORD0,
                            uniform sampler2D tex,
                            uniform float amplitude
                            )
{
  OutputStructure OUT;

  float displacement = (rand()%128)+1;
  float height = 512;

  OUT.colour.rgb = tex2D(tex, diffuseTexture + float2(0, displacement/height)).rgb;
  //OUT.colour.rgb = tex2D(tex,diffuseTexture).rgb;

  return OUT;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

>and i have #include <cstdlib> in my winMain
You don't have to include it in a function :P
Add that line at the top of your program's source file where you are using rand() in.

What errors are you getting?

Well you could fill a texture with random noise and pass this to the shader, it would be faster to generate this on a per frame basis than call a randomisation function - if that were possible (see below) - on a per-pixel or per-vertex basis.

So why cant you have one? Remember shader state is persistent for all the pixels it runs over so a random function is going to require a seed which will generate the same random number for every pixel.

sorry for the late reply, ummm, am now sending an array [512][512] to the shader with random values, is there a way i can use these numbers to push colours up with each pixels equivilent place in the array.

struct OutputStructure {
float4 colour : COLOR;
};

OutputStructure pixelMain(float4 colour : COLOR,
float2 diffuseTexture : TEXCOORD0,
float disp[512][512]: TEXCOORD1,
uniform sampler2D tex)
{
OutputStructure OUT;

OUT.colour.rgb = tex2D(tex, diffuseTexture + something that gets the value in the array?????)).rgb;

return OUT;
}

thanks for the help

This article has been dead for over six months. Start a new discussion instead.