Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Java Raytracer

Sign in to follow this  


So I've set myself up a new side project, because between Squarediana (which I never work on), SnakeEngine (which I never work on), and Grad School (which I always work on), I don't have enough things to do. This project is another ray tracer, this time in Java.


1. Because I haven't really done much with java, other than little programs here and there. The largest thing I've done in it was when a friend and I implemented Tetris, and then a genetic algorithm to play Tetris, in it. Sorry, it had no GUI. We could spit out an ascii drawing of the board if we wanted to though.

2. I like ray tracers. I've been itching to trace some rays lately. I also would like to learn Swing better. This seems like a good mix. Games are fun too, but then you have the pressure of being "done" some day. With raytracers, you just get some diffuse light on spheres and you good. Everything else is "extra". Ok, so any self respecting raytracer will have specularity and reflection, because otherwise what's the point.

Why Java, you should use C++.

I've done a ray tracer in C++. Originally for a class at my uni, I developed it in C++ on Linux using Qt for the GUI. It was a lot of fun. If you look at my old journal entries you can see the development of it. I have also ported it to Windows, though I couldn't get Qt compiling correctly here, so it's just command line. I was going to start a new one anyway, without a deadline, and try to make it truely extendable. This is because there was all sorts of things I wanted to implement but didn't get the chance to, and I think it will be easier and more fun to experiment with a more moddable design from the start. Figure why not try Java, with all it's cool GUI and Threadness and 1.5ness (I haven't actually done anything in Java since 1.5 came out).

Also, I think it will be a challenge.

So, what's first? Achieve C++ speed. Or at least close enough.

Right now I have the basics in place, and can render this image:

In actuality, I also have reflection in place, but for my purposes I'm leaving it out. I have it set up (scene files) to render that exact image in both my java implementation and my C++ implementation.

Giving the render thread maximum priority, the java raytracer (JRT) finished that in ~980ms. Slooooooowwwwwwwwwwwww........

C++ verion (CRT) did it in 390ms. Better (not exactly real-time either...).
But that wasn't totally fair, CRT has bounding sphere support, so, turn that off: 312ms. Doh. But of course it's faster...THERE'S ONLY 4 SPHERES ON SCREEN (and 3 lights, FYI, though bounding spheres don't affect those)! The bounding sphere's aren't gaining anything. But I'll keep it off until JRT get's culling methods too.

So much of my problem is, as I implied, I'm not a Java programmer. And for efficiency, I don't know how best to set up classes (OMG EVERYTHING'S A CLASS!). I would like to improve speed, but keep the understandability and extendability of my code. First thing I'm doing is I'm going to remove as many "new"'s as possible. Although passing everything by reference will take a bit of getting used to. So, that's what I'm off to do now.
Sign in to follow this  


Recommended Comments

Guest Anonymous Poster


To increase speed first you have to name your variables short names. The longer the name, the more time it takes to look up in memory.

Second put everything on as few lines as possible, the more lines of code, the more the computer has to read, sucking up precious nano seconds.

Try and do everything in one gigantic loop if possible, and make all your variables global for even faster look up.

Hope that helps and no one takes it seriously :)

Share this comment

Link to comment
Good idea, I'll also get rid of whitespace and newlines. I hear the fewer lines of code your program is the faster it will run.

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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!