Sign in to follow this  
bengaltgrs

HLSL random function?

Recommended Posts

bengaltgrs    282
Does HLSL have a random number generating function, like rand() in C++? I have looked around at some sites that describe HLSL functions, but I have not seen any functions to produce random numbers. Thanks, -Chris

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Under the Intrinsic Functions listing in the DX-HLSL docs, there does not exist a random number generator. You should be able to implement one yourself using a standard template, but the general complexity of a "good" random number generator would probably exceed the amount of allowed instructions in the lower pixel shader versions. The seed could then be supplied by an externally set parameter, or taken from some input data out of the vertex data, texel data, or a combination of both.

Share this post


Link to post
Share on other sites
bengaltgrs    282
I see, well I was actually hoping to randomly offset the texture coordinates in the vertex shader, but as you said, it would more than likely take too many instructions to create my own random number generator.

-Chris

Share this post


Link to post
Share on other sites
jollyjeffers    1570
Your best bet is to get the CPU to generate some random numbers and then try and find a "random-ish" way of looking at said numbers [smile]

For example, if you're going to heavily use random numbers create a 1024*1024 texture - you can store 4194304 8-bit random offsets created by the CPU.

For each pass, indicate a particular sub-region that it can examine and then do some screen-space look-up (etc..) to pick a value inside that region. Texture filtering could work nicely here as well.

Sure, it's not as good as a proper random number generator but you can probably get close enough...

hth
Jack

Share this post


Link to post
Share on other sites
bengaltgrs    282
That certainly sounds like an interesting idea, I will have to try and figure that out. Also, how would I get a texture to scroll across a plane? I thought adding a delta time to the texture coordinates would do it, but that just stretches it out.

-Chris

Share this post


Link to post
Share on other sites
jollyjeffers    1570
You might want to look into "texture coordinate transformation" - there are a number of articles/threads/resources on it. Basically it's a way of applying a matrix-transform (scaling/rotation/translation etc..) to texture coordinates - might well do what you're after [smile]

Jack

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