Showing posts with label graphics. Show all posts
Showing posts with label graphics. Show all posts

Friday, March 26, 2010

The Mystery of the Window into the World

This week saw the due date for what I'm calling "the worst graphics assignment ever." I saw some of the weirdest renders in this assignment. The first mistakes I made were little ones, but they had some very artistic effects. Later, I had one of the most mysterious problems I've ever encountered.

I'll start the story from the beginning: The project was to create a KD Tree from a triangle-based scene using Surface Area Heuristics to choose the best dividing planes. Then, using my KD Tree, I was to render the scene using ray casting (non-recursive ray tracing). And after that, add phong lighting. What could go wrong, right?

I decided to approach the assignment by ignoring the KD Tree altogether. I attempted ray casting by testing ray intersection against every triangle in the scene. Very, very slow, but at least I could isolate problems more easily.

The first render looked like a pre-school paint smear. After realizing that I was performing an integer division when getting my aspect ratio, I got a slightly more intelligible paint smear. The left side of the picture looked correct, but the right half looked like my program just stopped trying. Then, I considered the merits of resetting my color between casting rays, and got what must be the cheapest version of shadow casting I've ever implemented:

(Quick, tell the NPR graphics guys! I got comic book shading!)

Finally, I thought of also resetting my minimum time of intersection between casting rays. I rendered and waited extremely patiently for the picture to form. It looked correct! The slow speed of ray casting without a KD Tree was getting to me, though, so I decided to cast a ray for only every other pixel to speed it up. So, I got the newsprint version:

Then, I implemented my KD Tree. I waited with bated breath as it built the tree and rendered. I was sure it was going to draw something like a sparse spattering of random triangles... But it didn't! It looked perfect. Except for the fact that it only rendered a cube-shaped portion of my scene:

Excuse me, but huh? I moved the camera around, and the rendered cube moved with it, almost like I had a three-dimensional cube-shaped "window" into the world of my scene. Amazing. After I showed everybody I could find, I sat back down to figure it out. Was I miscalculating the area I should be ray casting over? No. Was my Axis-aligned bounding box (AABB) for the whole scene too small? No. I asked other classmates if they had any ideas. No. I asked Seniors if they had any ideas. They thought it was cool, but they were as puzzled as I was.

Finally, I e-mailed my teacher for a consult. He commented out some functions to isolate the problem. (Why didn't I think of that? Oh well, next time.) It turned out the scene rendered completely correctly if I skipped the function that clips the rays against the AABB's. I looked over my algorithm and couldn't find anything wrong with it, so I scrapped the function and wrote it over again using a similar but slightly simpler algorithm, so I would be able to find problems more easily. And then, of course, because the graphics gods are fickle, it worked:

Here is the ray clipping algorithm for those who are curious:

So, I never did truly find out the answer to the Mystery of the Window into the World. If anyone has any ideas I haven't thought of, I would be all ears!

Monday, March 8, 2010

Transparent Water: Mission Complete

Success! The water is transparent, the blocks show through, and everything draws in the correct order. I even put in a ground plane to give something for the puzzle blocks to rest on. Now the caustics actually look like caustics on the ground plane.

But, you fix one problem and see a dozen others. There are no shadows, so the blocks still look a little floaty. I should put in some kind of shadow mapping. The caustics don't bend where they overlay the sides of the blocks, which looks a little odd. I could fix that by actually projecting the caustics textures, but I don't think that's a high priority. Also, now that the caustics look like caustics and not highlights on the water, the water looks like it's missing lighting. I wonder if I can use the caustics texture, which I'm already passing into the shader, as a height map to generate normal mapping. Then my water would have ripples!

Sunday, March 7, 2010

Water!

Challenge: to get good-looking water into my game in one week.

The game I'm working on with my team at DigiPen is called Runeshift. It's a relaxing slide-puzzle computer game. I want to put water in the 2nd level, because I think it would be a pleasing backdrop. However, the semester is already half-way over, so if I can't get it in quickly, it threatens to eat up time better spent on polishing what we've already got.

Therefore, I need a quick and dirty way of rendering water. Actually, I've been researching methods of simulating real-time water on the side now for nearly a year. It's a subject that fascinates me. So, I have an idea of what to do. Kjell Andersson makes an awesome real-time caustics generator that creates animated textures that are both tileable and loopable. The interface has tons of options, like how many frames per animation, what resolution, what background color to use, and then lots of slider bars for adjusting the caustics patterns themselves. (Caustics are the patterns of light created by reflection and refraction with an uneven surface, as in water ripples.) For my first iteration, I simply loaded the bitmap frames of a black and white caustics animation and rendered them blended with a color, so that I could easily adjust the color of the water in my water shader. It worked surprisingly well:

One noticeable problem is that the puzzle blocks don't look like they are sitting in the water but sort of floating above it, because the water isn't transparent:

So, my first mission: transparent water!

Friday, February 19, 2010

My, How Things Have Changed...

As I consider my new blog, I realize I've come a very long way from where I was a decade ago. Ten years ago, I was absolutely certain that those tempting computer animation classes at my college were a dead end and that I should choose a more practical career, like teaching. Of course I had a couple of programming classes, and I wrote my own website from scratch in HTML and made a game of checkers, but I didn't take it seriously. I wanted to be able to work anywhere in the world, and this dark little cackling voice in the back of my consciousness told me that programming wasn't a way to do that. (Maybe the cackling should have clued me in to the lies.) Besides, my parents were both teachers, I was good at teaching, and I was convinced, as most women are, that I was bad at math.

Now, look at me. I eat linear algebra for breakfast. With no sugary coating. And love it. Yum. I also make really pretty mistakes in my graphics projects. I mean, seriously. Did a circus clown go ballistic on my triangles when I wasn't looking? Maybe I should sell it as abstract art.

Anyway, I'm now starting a technical blog. And it's technical because that's my daily life. That's what I want to talk about. I want to talk about my hilarious, frustrating, cool, and bizarre programming mistakes. I want to talk about the amazing and terrifying technological advances that are being made (like computer peripherals that literally read your mind). That's me now. Wow.

So, welcome to my blog. I hope you enjoy the ride.