Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 24 Jan 2013
Offline Last Active Yesterday, 05:48 PM

Topics I've Started

Random numbers in a pixel shader

26 October 2014 - 11:33 AM

I have a path tracing program which I created a while ago and I thought it would be an awesome project to convert it to work on the GPU using a HLSL pixel shader. However, path tracing requires thousands of random numbers for each pixel of the final image, and there is no random number generator in HLSL. So, I came up with an idea to create my own:


1) Create an array of random numbers in the main program, with the resolution of the final path traced image. Send it into the shader as a texture.


2) At the top of the pixel shader, create a variable which keeps track of how many random number calls have been made for this pixel. Let's call it nRandomCalls. Every time the path tracer asks for a new random number, increment it by 1.


3) Sample the noise texture using the location of the current pixel. This assigns a value to each pixel which is random, but always the same for this particular pixel. Use the obtained number to generate a set of texture coordinates.


Now, here is my idea for how to get the random numbers themselves. Every time a new random number is needed...


4) Increment nRandomCalls by 1.


5) Sample the noise texture again using the texture coordinates from part (3) plus an offset which is determined using nRandomCalls. This produces our final random number.


As long as the initial array is random enough, this should act as a good source of noise, right? Is there any reason why this wouldn't work? Thanks smile.png

Spherified cube

26 October 2014 - 06:33 AM



Just a quick question regarding the process of converting a cube to a sphere, like this:#




...am I right in guessing that this could be done in a vertex shader by taking each vertex, finding a vector from the vertex to the centre of the cube and moving the vertex along that vector until its length matches the desired sphere radius? Or is it more complicated than that?


Thanks a lot :)

Scenes with large and small elements

21 October 2014 - 11:47 AM



At the moment I'm working on a simple simulation of an Earth-orbiting spacecraft. This involves centring the camera on my spacecraft which is orbiting a few hundred kilometres above Earth and simultaneously rendering the planet itself out to a distance of around 12000km. Of course, it's unfeasible to render such a scene using metres as my base unit, as I have to specify the spacecraft's position in hundreds of thousands of metres relative to the centre of Earth, and using such massive numbers to position objects in Direct3D seems to cause problems.


So, my question is: how would you approach the problem of rendering small scenes embedded in much larger ones? Should I render the Earth first followed by the spacecraft using different units each time? If you know of any useful articles or tutorials, that would be great :)



n-dimensional noise generation

04 October 2014 - 05:43 AM

Hi everyone!


I'm trying to make a Perlin noise program but I'm having a bit of trouble coming up with a good noise function. I'm trying to create 2D noise to start with. I'm using the default_random_engine from <random.h> to fill a 2D array with noise, like so:

//Generate random floats between 0 and 1
default_random_engine gen(0);
uniform_real_distribution <float> line(0, 1);
//Fill the array image[][] using the random generator
void fill_noise()
    for (unsigned y = 0; y < yres; y++) 
        for (unsigned x = 0; x < xres; x++)
            float shade = line(gen);
            image[x][y] = Vec3(shade); //RGB are the same to give grayscale value

This works fine but I'd like to create a noise function which gives me the same result for a given x and y coordinate in the image every time I call it, i.e. something like this:

float noise_2d(int x, int y)
    //Generate a random number between 0 and 1 which is always the same for a given x, y

I hope that's clear enough! So how can I use <random.h> to create such a function? Thanks a lot smile.png

Stretched pixels in windowed mode

04 June 2014 - 05:15 PM

Hi all,


I've created a simple program using DX9 in windowed mode. It uses a simple diffuse shader to render a mesh in a 640x480 window. The program seems to work fine but there's a strange array of stretched pixels which distort the image. Here's a shot:


Attached File  pix1.png   36.7KB   4 downloads


If you can't see them, here's the same image after edge-detect:


Attached File  pix2.png   15.9KB   3 downloads


Is there anything I'm overlooking that needs to be done to render in windowed mode (I've been doing fullscreen stuff until now). Thanks!