Archive for the ‘Computer Graphics’ Category

That’s not a moon…

Wednesday, August 29th, 2007

That's not a moon!

…it’s a partially reflective UV texture mapped sphere with the image of the moon textured on it. And that one behind it is the place the Battlestar Galactica is going next season. And there’s a big fucking crate, because what wouldn’t a “Hello World” raytraced image be without a big fucking crate. :) Before that I had a nice ranchy image of Ino from Guilty Gear XX, but I figured, who would want to see that when you could be looking at a crate?

So what does this image prove?

a. I’m a big nerd and have too much free time — and I do, cause I’m on vacation (or “vacation?”) which means I get to do whatever the hell I want, and yes, I choose to work on my raytracer, because I enjoy it. Put that in your pipe and smoke it;

b. I got rudimentary UV texture maps working for spheres and triangle meshes;

c. I can load arbitrary triangle meshes, so obviously I would load a cube because there’s nothing more interesting than a cube;

d. The moon is actually partially reflective and would in reality reflect a red and blue infinite checkerboard plane that stretches far and wide across the universe;

e. All of the above.

My raytracer’s actually come to the point where it takes a couple of minutes to raytrace a triangle mesh. Such as this image:

MMmmm Donut

So what’s next on the agenda? Well…I fixed some of the horkiness of the raytracer, but it’s still horked in other ways. There are still issues with the reflection, refraction doesn’t work and so on and so forth…a lot of little quibbles with the mathematics that I need to iron out. It’ll get there. If you’re curious how long it took to add texture mapping and mesh loading, on my part not too long… a couple of hours each. For mesh loading, I had an OBJ loader cooked up for our old game engine Bushido. The tricky part was to understand how to find ray-triangle intersections — and to find a good one at that. If you’re looking for a good resource for barycentric ray-triangle intersection testing then google search for “Fast, Minimum Storage Ray/Triangle Intersection” by Tomas Moeller and Ben Trumbore. They also wrote a book called Real-Time Rendering which I need to get my hands on. As one senior engineer where I work told me, it’s the new bible for computer graphics.

For texture mapping. I had also done a TGA loader for Bushido, so I imported that and made it much more portable. There are also great UV coordinate finding algorithms online for spheres. The 3D mesh UV coordinate algorithm is just based off of the barycentric intersection test. Nice and easy and it gives me much more flexibility in what I can load into my raytracer.

For even further down the road I’m looking at implementing a kd-tree to speed up my raytracing especially with all these triangle meshes. I also need to consider anti-aliasing. I should also think about a transformation pipeline — that’s right, I got away without one for as long as I could! Multiple lights might be fun. Soft shadows. I’d really like to get into the procedural texturing.

Above all…having a GUI interface to set up a raytracer scene would really help. One of my goals is to keep my raytracer code portable, so it’s not dependent on OGL or DirectX. That way I can implement a GUI in the future so I can get a wireframe view of everything before I hit the big ole render button.

More to come…but dinner beckons, and then Rapture.

Horked but Working!

Tuesday, August 14th, 2007

Here’s an image from the current state of my raytracer. The big win from all of this is that my raytracer is now recursive so I can cast reflection and refraction rays from my incident ray. It’s still buggy as hell and not showing correct results.

Raytracing with Reflections

I believe the issue I’m facing here is a z-depth one. I’m noticing that a lot of my calculations are the inverse of what I expect. I don’t believe I’m supposed to see the shadow like that, and definitely I should not be seeing the reflection of those two smaller spheres. Now that I have a fairly decent raytracer working it’s time to go back and correct all the little details.

Raytracing II

Monday, August 6th, 2007

After another day of work here’s the result:

Raytracing II

With the help of Foley, et al. I fixed my illumination code. The specular highlight you see here works more correctly then in the previous image. I’m not so hot about the washed out look of the sphere, but that’s mainly due to the white ambient light I’m using. I also have shadows. I’m not 100% sure if the shadows are correct, but it’s a start.

Three spheres, one checkerboard infinite plane…I have a very simple scene list now and I can add spheres and planes at will. Maybe oneday even arbitrary 3D models. How could would that be?

I’m excited.

There’s so much I can do now. The next major refactoring/addition will be to create a recursive raytracing algorithm which will provide me with refraction and reflection. Chrome spheres here I comes!

Raytracing

Sunday, August 5th, 2007

I’m taking a bit of a hiatus from perspective drawing, but I’ve learned some more interesting perspective techniques that I can share with you later, along with a really good book on the subject, but I’ve started on yet another project I’ve had on the back-burner for a while now: a raytracing engine.

At work we’ve got a little bit of downtime coming up, and one of the senior engineering guys, Bill, and myself started talking about raytracing. I don’t recall exactly how we began discussing the topic — something to do with Pixar, Ed Catmull, and him trying to remember the name of the guy that invented raytracing, and then there was a short discussing I initiated about this guy: Ron Fedkiw. Do yourself a favor and look at the videos on his site. Be prepared to change your pants and/or underpants (if you are the kind that wears underpants) during the viewing of these animations.

Our discussions on computer graphics rekindled my interest to write a raytracer. During my undergrad at the University of Southern California I took a graphics course and the last assignment was to put one together. I only got mine partially working, and had no more time during the semester to go back to the raytracer. So, I gave it up. Plus, through my over-zealous use of object oriented programming, OpenGL, and MFC, it was a horrible mess of partially functioning shit. Moral of that story: keep it simple, stupid.

Last night and all of today, I’ve been working on remedying that epic failure in my college career. Raytracing and all that it entails is an expansive subject. At it’s core, raytracing is firing (out) rays into a 3D scene and intersecting objects to return their color values. You have a picture plane (defined as a grid of pixels), and you use some vector mathematics to fire rays into a 3D scene. A ray is a point in space along with a direction. In this case, you use a point camera, and fire rays from the point through each pixel in the grid into the 3D scene. You test to see if the rays hit things in the scene…spheres, boxes, planes, what have you, and you return the color value and write it to the grid. We can get into discussing things like local illumination models, shadow casting, radiosity, super-sampling, and a whole slew of mathematically challenging topics related to image-based rendering, which I haven’t touched in a couple of years, but we won’t. Time for me to dust that portion of my brain off anyway.

The “Hello World” of raytracing is a checkerboard floor with any grouping of chrome spheres in which everything is reflecting everything else and casting shadows all around. That’s my first major goal, and this time, being a bit wiser, I’ll try and keep it as simple as I can be.

The work-in-progress of my version 0.1 raytracer:

Glenn’s Version 0.1 Asstastic Raytraced Image

Click on it to see it in it’s full 1920×1200 pixel glory.

Features so far: well it works, don’t it? It’s fairly fast. I can raytrace an image of any size. It has a simple illumination model and I can do plane and sphere intersections. All the bread and butter.

How did I go about building this program with my busy work schedule and only having scant hours available on weekends and weekdays? I broke it down into milestones that I thought were doable in a couple of hour chunks.

The first milestone was just to get the project setup. In that case, if I sudden got incredibly lazy, I could have at least the basic project setup in Visual Studio for me to continue on later. This included some math code I jacked from the Cloud game engine, Bushido and a TGA exporter I wrote a long while back. My idea is that this program will draw everything into a pixel buffer (huge ass array of RGBA values), and then spit it out as an image that I can view in Photoshop.

The second milestone was just to build a raycaster. This is the first step in a raytracer…just being able to fire rays into the scene and hit things. I kept true to my word and kept it simple, there’s no complicated scene manager or lighting models, etc. It’s currently all hardcoded. I wrote ray-plane and ray-sphere intersection tests.

The next milestone was to do a simple illumination model. Since I started writing my code willy-nilly, hardcoding things here and there, and such, you can see the lights don’t match. The purple and white plane has a light source of it’s own and the sphere’s light isn’t the same, but that’s fine, I’m messing around with phong shading, and right now it works, and it produces a pretty damn cool (and asstastic) result.

I though this would take me at least a couple of days to do…but it’s 1 am, Sunday morning, and after only at least, I’d say, 10 hours of coding between Friday and Saturday, I’ve got quite a lot of what I wanted to do.

My next milestone is to make everything a little more organized so I can do some recursive raytracing — this will allow me to get the refections, refraction, and shadows, that I’ve been dying to see in my own raytraced imagine.

Once I have that, I think I will have the start of a scene list, which I can expand to include ray-triangle intersection and generic 3D models I can import into the raytracing engine. What I’d love to do is to raytrace a Dyadin level or a the islands in Cloud. That would be pretty damn schweet.

And from there…well the sky’s the limit, and I’d love to start learning the more complicated mathematics behind all the cool stuff you can do. I have a book on procedural texturing and modeling, that would be awesome to infuse into my raytracer. You’ll be seeing more of this on the Courne Supremacy.

Oh, and I saw the Courne Ultimatum. I suggest you do the same, it’s an excellent movie.