Wednesday, July 9, 2008

Raytracing Chaos

I've spent about five hours on my raytracer and I must say, it's been a blast. It's the most fun you can have while programming. Seriously, if you're not writing a raytracer at this very moment, please stop whatever you're doing and start writing one right now.

So far I've been writing doing most of the invisible stuff, like world and camera setup, ray collision detection and so on, but I have already some pictures. I did some kind of mistake and I couldn't figure what. The pictures should have a couple spheres on them, but it looks more like stripes and chaos. However if I wanted, I wouldn't be probably able to create such nice pictures :)

cross

face

lines

Finally I found out where the bug was, I forgot to normalize the vector giving the direction of the casted ray. Since I was counting with the length of the vector being one in the ray collision detection code, the results have been quite strange. After fixing the results finally looks like spheres. There is no occlusion code yet, but I have naive lighting finished.

lines

The next step is to write exporter for Blender, write a parser and render some my of my models.

Thursday, July 3, 2008

Raytracer Rules

Thinking about the Raytracer, I am about to write. I figured that there must be some CS school assignments already setting rules for this subject. Google search revealed some of them:

I like the first assignment the most, so these are the rules, I'm setting for myself

  1. Functional requirements
    1. Reflective spheres
    2. Rectangles (preferably checkered planes as well :) )
    3. Scene parser
    4. Blender exporter of geometry and scene settings
    5. Output in some braindead file format, probably just RGBRGB or PPM
    6. Spatial partitioning
    7. Bonus - Cornell Box
  2. Time and size contraints
    1. Deadline 15th August
    2. Three reflective spheres on checkered background in 1024x768 have to be rendered in less than 30s

The speed constrain is pretty vague, but since I don't have anything to benchmark it against, let's stick with this.

Wednesday, July 2, 2008

Return From Finally

Just today I was helping with some bug in Java and came across interesting problem - what happens if you use return within try/catch statement? Should the finally section fire up or not? I simplified the problem to following code snippet:

What does the following code print out?

class ReturnFromFinally {
 public static int a() {
  try {
   return 1;
  }
  catch (Exception e) {}
  finally{
   return 2;
  }
 }

        public static void main(String[] args) {
  System.out.println(a());
        }
}

My initial guess would be, that it should print out "1", I'm calling return, so I assume, one will be returned. However, it is not the case:

The second return is executed

I understand the logic, finally section has to be executed, but somehow I feel uneasy about this. Let's see what C# does in this case:

class ReturnFromFinally
{
 public static int a()
 {
  try {
          return 1;
  }
  catch (System.Exception e) {}
  finally 
  { 
   return 2;
  }
 }

 public static void Main(string[] args)
 {
  System.Console.WriteLine(a());
 }
}

error CS0157: Control cannot leave the body of a finally clause

I prefer much rather this behavior, control flow cannot be messed with in finally clause, so it prevents us from shooting ourself in the foot. Just for the sake of completeness, let's check what other languages do.

Python:

def a():
 try:
  return 1
 finally:
  return 2

print a()

Python returns 2 as well

JavaScript:

<script>
function ReturnFromFinally()
{
 try
 {
  return 1;
 }
 catch (e)
 {
 }
 finally
 {
  return 2;
 }
}
</script>
<a onclick="alert(ReturnFromFinally());">Click here</a>

JavaScript returns 2 as well

There is no finally clause in C++ and PHP, so I can't try out the last two languages I have compiler/interpreter for.

Our little experiment nicely showed, that C# has the nicest approach to this problem, but I was quite surprised to learn, that all the other languages handle the problem the same way.

Tuesday, July 1, 2008

Tetris Stage One Finished

So, yesterday was the deadline for my Tetris project. How did I do?

I finished a basic Tetris game. The blocks fall, the finished rows disappear. You can rotate the blocks and that's it. It doesn't have any kind of score, levels or anything else. It redraws ugly, because it doesn't use dirty rectangles, but that shouldn't be that hard to add.

I consider this project finished for the time being, although the code is quite ugly. I will now try to go for the raytracer, because I can't wait to try writing one :).

However before going for the compiler, I will schedule it as next little project of mine. Clean up the Tetris game, add local & network multiplayer support and include pretty graphics.

Should someone very inconsiderate be interested in playing this, here you go. (10kB)