Aug 29

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.

Tags: ,

Aug 14

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.

Tags: ,

Aug 12

So, finally, here’s the code I wrote for my Boid simulator in C# using the XNA framework. Keep in mind: not everything is optimized, documented, and so on and so forth. The code is probably ugly in general, and I’m not using the best C# coding practices. I understand how to code 80% of C# because it’s in the same family of languages as Java, C++, etc., but I don’t know the true ins and outs. If you are a C# aficionado with 1337 C# skillz or any other programming skillz for that matter and would like to pass on helpful information pertaining to coding in C#, why I would be very glad to listen and absorb your expertise.

I suppose I should release this software under some license. So I will. Not to screw you, the end-user, over but to generally absolve me anything detrimental that might happen to you, your loved ones, your pets, and your computer if this software horrendously goes wrong and say, kills you instead of performing the flocking algorithm. It’s a very slim, minute and improbable probability that such an event will happen but I can do nothing about the aberrant possibilities that may bring about these circumstances. So you are thusly forewarned.

The Boid Simulation code for XNA/C# is released under the “Courne Supremacy Licensing Agreement.” By clicking on the download link below you agree to everything I said in the second paragraph of this blog post:

You can download my boid simulation code here.

Oh, now you’re complaining that you can’t run it. Well…you might also need to get the Microsoft XNA Framework Redistributable 1.0 Refresh download.

Or you can get the XNA Game Studio Express 1.0 Refresh and start check out the project workspace and modify the code and get yo learn on.

Happy flocking.

Tags: , , , ,

Aug 11

As my good friend, Scrabcake, make a record of saying, I am a nerd. Here’s proof: I took the day off from work, where I am a software engineer, to stay at home, and…engineer software. Why engineer software at home for free, when the company in which I work for, would gladly pay me to engineer software for them? Well that’s the difference between me and them. I code, that’s what like doing. I also happen to enjoy drawing, writing, and so on, and the Courne Supremacy, which I thought would be a repository of all my short stories, has now become just whatever cool piece of shit I’m working on at the moment.

This week’s cool shit: Flocking/Herding Simulation.

For those who don’t know what it is check out Craig Reynold’s website. For those of you who can’t be bothered to click on a link, the idea is that you have a bunch of little bots and using a simple set of rules you can govern how they herd or flock together. There are three rules to having these autonomous boids flock:

1 . Center of Mass. For boids to flock together, each individual member of the flock, must move towards the center of mass.

2. Keep Apart. Everybody needs their personal space, and boids are no different. Each boid needs their room, so this test is to make sure that two boids don’t collide when herding/flocking together.

3. Maintain Flock Velocity. If a boid is slowing down, then he needs to speed up, if he’s going to fast, then he needs to slow down.

These three rules work with all the members of a flock and the emergent behavior is that the individual boids move together in a herd. For each of these rules, you use some vector manipulation and summation between the entirety of the herd, and come up average velocities for each of those rules. You then integrate the velocity to figure out your boids’ new positions. Of course, you can also add your own custom rules. I’ve added code to have boids flee at the sight of predators and collision avoidance with the sides of the screen to keep the boids in bounds, and there’s code to have them follow a mouse cursor around.

Why are they called boids? I can’t remember, I believe it has to do with the concatenation and shortening of the word “android” and “birds” since these are artificial birds. That’s just a guess, I’m sure there’s another, more correct, wikipedia, mother-approved reason why they are named boids.

The second part of this week’s cool shit: I wrote the whole thing in one day using XNA.

XNA is Microsoft’s XBox360, XP, and Vista C# platform for hobbyist game development. You can download it here and start making games. If you’re new to programming, XNA will be a steep learning curve. You’ll need to learn C#. For a veteran coder, someone who’s done their rounds with C++, Java, and the ilk, you’re 80% there with C#, the extra 20% is getting down the syntactical sugar and wrapping your head around some of C#’s more unique features. I have, what I will consider, a very primitive knowledge of C#, and yet within a day I was able to build a very simple 2D demo, but then again, I’ve been coding for maybe a decade now.

If you want to make a quickie 2D game, XNA will get you up to speed very fast. Oh, and you might want to pick up an XBox360 controller. It’s very easy, almost braindead (once you get past the whole learning C# thing) to interface and start using the gamepad. So far every time I’ve run into something I’ve needed to do for my simulator, XNA has had the answer. Need a container to hold variables for a plane, XNA’s got you covered. Need to import sprites, no problem. XNA supports png, jpeg, and a plethora of other types. You just add it as a resource to your project and you’re on your way. Getting something 2D up and running is a piece of cake.

Now 3D…well, there’s a lot more of a learning curve there, but there are a lot of neat features involved: pixel and vertex shaders without the hassle of setting up pixel and vertex shaders. My paltry single day of coding couldn’t scale that cliff, but that will be left for another day…maybe when I decide to take my Boids into the dreaded 3rd dimension.

And now, the picture. Some explanation. The happy rabbits (albinos) are flocking. Red Albinos mean that they are currently resting. I gave them an energy meter, but since XNA is notoriously bad at displaying text (okay, yeah, XNA has it’s draw backs…), I’m left with tinting sprites to show me the state of each boid. The big spiders (whorses, as we like to call them in these neck of the woods) are predators. They mainly wander around and scatter the flock. Click to enlarge:

Boids Simulation XNA project

BTW, I’ll post the source code, once I figure out how I’m going to do that via the blog.

Tags: , , , ,

Aug 06

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!

Tags: ,