PDA

View Full Version : how to add texture to a ray tracer



brett01
11-03-2008, 12:15 AM
I created a basic ray tracer using planes,triangles and spheres.I want to know how to add textures to it.can some one help me please.I need to add a noise function called perlin noise fn to it.
Thanks in advance.

trinitrotoluene
11-03-2008, 07:46 AM
If you need procedural textures you can use libnoise (http://libnoise.sourceforge.net/).

Edit: It is very easy to use Perlin noise to generate a procedural texture for a surface. Take the hit coordinates of the ray and pass it to the noise generator. A very simple procedural texture can be defined like this:



red = (noise.GetValue(hit.x,hit.y,hit.z)+1.0)*0.5
//Add a constant number for having a different value
green = (noise.GetValue(hit.x+M_PI,hit.y-M_PI,hit.z)+1.0)*0.5
blue = (noise.GetValue(hit.x-M_PI,hit.y+M_PI,hit.z+0.56756)+1.0)*0.5

brett01
11-03-2008, 12:26 PM
Thank you dude where can i get "noise" from perlins fn???and M_PI.Hi if i want to add texture of a checker board to my plane how can i create it??

trinitrotoluene
11-03-2008, 12:35 PM
M_PI come from #include <cmath> header. noise is an object of type noise::module::Perlin from libnoise.You can find other noise generator source code on the internet.


Edit: Marc Olano web site (http://www.csee.umbc.edu/~olano/papers/) made a Perlin noise generator with source code available.

Reedit: Stefan Gustavson paper about Perlin simplex noise (http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf) is also interesting.

brett01
11-03-2008, 12:42 PM
if i want to add texture of a checker board to my plane how can i create it??

trinitrotoluene
11-03-2008, 12:45 PM
For a plane in the xy plane => the normal = (0,0,1) you can take this algorithm:

if the sum of floor(x) + floor(y) coordinate is odd the color is black else if the sum is even the color is white.



if( (int(x) + int(y))%2 == 1)
{
color = black;
}
else
{
color = white;
}