Sign in to follow this  
  • entries
    94
  • comments
    127
  • views
    83162

ZOMG Update! (raytracer part 1)

Sign in to follow this  

132 views

Yepps, I'm still alive. So everyone that's claimed any of my belongings, SHOO!

So I'm here in Brazil, studying.. Why would you otherwise visit Brazil? >_<

Anywho! I'm reading the following courses:
? Introduction to Graphics programming
? Compilers and languages
? Online businesses
? Introduction to Automisation

oh they're all so fun!! *cries*

Anyway, the current and last assignment for Graphics programming is a raytracer. (OK we've got a theory paper to answer and another written test but this is the last code assignment)

I got the basic part down on the raytracer. Ray / sphere collision and rendering a scene with 4 spheres, pretty simple. Now when I wanted to move on to multiple light sources and reflections, I realize that I have to do some stupid crap things for the assignment. Namely parse a file and build a scene from that and render to a .ppm image, I'll do the image thing laaaater.


A screeny!

Notice how the green ball is darker, because it has a lower diffuse material than the other balls =D. The white is just there to test my depth testing, since it's in front of the red ball it should be rendered just like in the pic.

OK now I'm off to create a triangle and maybe the assignment optional disc. *procrastinating the read config file - set up scene part*
Sign in to follow this  


3 Comments


Recommended Comments

Very nice screeny. I've always wanted to write a ray tracer or caster, but I've never understood any of the articles on the subject. I tried reading the gfx portion of the Doom source code, and it made my head hurt.

Anyways, keep up the good work.[smile]

Share this comment


Link to comment
Raycaster, I can handle. I even wrote one for the GBA once (though, it was horribly, horribly slow). Raytracer however, still makes my brain hurt.

You wouldn't happen to have any good articles, would you? Like, that are not in the GDNet library?

Share this comment


Link to comment
the basics is so simple I was actually chocked!

I'll explain in the way that made me understand.

Imagine you're looking at your screen *it's hard I know*

Imagine your screen is a big texture *even harder to grasp*

Imagine your superman and you're shooting rays from your eyes *this I can imagine*

Now, shoot one ray from your eyes (one ray for both eyes that is) to each pixel on the screen. What you've actually done is pick a origin position, your eyes, and a direction (the pixels). A ray is defined as R(t) = origin + t * direction, where both origin and directon are vectors (origin is a point vector and direction is a normalized direction vector). t is the value you scale the direction with to move along the ray. A ray doesn't go backwards so t isn't allowed to be negative.

Ok back to the ray. Each ray from your eyes travels to a pixel on the screen, and continues to the 3D world behind it. Untill it actually hits something.

In my screenshot I'm shooting one ray for each pixel on that image, all rays start at (0,0,-5) and then I do a collision detection on all spheres in the scene. If there's a hit, I calculate the position. Which isn't hard since most collision detection ray/sphere gives you the 't' you need, just insert it into the ray equation and you've got your intersection point.

Now the fun part, lighting it! From the intersection point on my sphere I calculate a vector to the light source (I currently only have one). Then I dot product that vector with the surface normal of the sphere to see where the light is. If Dot returns < 0 then the light is on the "wrong side of the sphere" and thus the pixel can't be seen. If Dot returns 1 that means that the light is right above the pixel, giving it max light.

See where I'm going with this?.. that means that if the pixel is visible, then Dot will return a value between 0 and 1. Where 1 we defined as max light. Thus... scale the sphere color according to that dot product!

cColor color = dot * sphere->color * light->color();

yay!

That's the easy part =) now, you can add diffuse to the spheres. that's what I have on that screenshot. diffuse is basicly how much color it "doesn't absorb". High diffuse (1.0) makes it "normal", no diffuse (0.0) makes it invisible (i.e. doesn't reflect any color. This is not to be mixed up with reflection, something that comes later.

Where were we? oh yeah, playing superman. Well now comes the tricky part, each ray you shoot from your eye through a pixel and hits a surface. Unless you think my screenshot looks photorealistic, you'll have to react to the surface! What happens to your ray when it hits a shiny object? It reflects. What happens to your ray when it hits a transparent object? It refracts.

So basicly, for each ray that goes into the scene and hits something it'll turn into 3 rays. 1) to the light, 2) reflecting of the object 3) refracting through the object. Each of these rays can in turn be turned into the same type of ray that entered the scene, i.e. hit something and turn into 3 more rays. Ad infinitum... Needless to say, you have to put a depth limit on how far a ray can go. Or at least, how many times it can bounce ^_^

There's also alot to be done in the lighting part. Like Phong lighting to give that nice shiny highlight on the balls. And I can't forget to add shadows!

Well thanx for listening, I'm gonna stop pretend I know something now :P

My resources are:
Mathworld
Real-Time Rendering by Thomas Möller
Raytracing Topics & Techniques (7 part tutorial on flipcode)

That tutorial helped alot for me. I had read a bunch of theory but just seeing a for-loop and some int's made stuff so much clearer.
Note: he explains the ideas and what needs to be done, but kinda leaves it up to you to implement ray/sphere intersection code as well as basic vector math.

I'm thinking of uploading my math lib under MIT license just as a base for people too look at. It comes with a Unit Test too! ;) If there's any interest for it that is?..

Share this comment


Link to comment

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