Ray Tracing explained 1

March 1st, 2010

I recently had an in class test on ray tracing (which I got full marks on). In order to get maximum use from all the revision I did I’ll also be flogging the subject here. So this is a series of straight forward articles on ray tracing, but first just explaining the general idea.

So what is it?

It is a method of rendering scenes usually characterised with a checkerboard floor, ultra-shiny surfaces and all perfectly sharp and crisp. The idea is very much like how light works in the real world, but in reverse. The 3d has a camera from which rays travel from.

The above diagram shows a simple ray tracing scene. The Projection Plane is the display which the rays intersect as they fly into the world. A ray intersects this plane for each pixel on your screen. The Projection Reference Point behind it is a single point in space that they originate from. This allows the rays to all fly out at a different angles.

Primary, Secondary

Rays that originate directly from the projection reference point are the primary rays and when they hit an object two secondary rays are created. One is for flying off to record any objects or lights reflecting onto the object that was hit. The other is for going through the object to record any light refracting through the object (but obviously only if the object is transparent).

When a secondary ray hits an object the above process will repeat, and repeat, and repeat slowly building a tree of all of the reflections and refractions of light that can be recorded from the original primary ray.

This process will stop if there are no more objects to hit (in which case it’s just background colour), if the ray hits a light (no point reflecting off them, you’ve just got full light), if you have set limit on the number of sub-rays to create and if the impact that the sub-ray will have on the primary ray is too small for us to notice.

The original primary ray is used to find the colour of a pixel on the screen, and to do this all we need to do is walk up the tree of sub-rays from bottom to top looking at each object in turn. This also needs to take into account the lighting on the object (diffusion, any specula highlights and ambient lighting) and the distance of the object to add fog.

End

That’s the basic model, but next time I will be writing about some more advanced algorithms used in ray tracing for making it more efficient and for making more realistic images.

the guy in red

February 21st, 2010

Last week I was in a book, this week I’m in an Indian newspaper!

No, it’s not about me at all. The guy in the main photos is an Indian friend of mine I met in Hong Kong who wrote sent the article. It’s about his Hong Kong experiences. Instead I am the guy in red standing next to him in front of the giant Tian Tan Buddha in the background, located at the Po Lin Monastery in Ngong Ping. It’s not about me, but at least I’m in it!

If your ever in the Hong Kong area then I highly recommend visiting the monastery. You can get there via cable car with impressive views of the Hong Kong Airport and the surrounding untouched tropical wilderness of Lantau island. Although their cable cars do occasionally crash into the surrounding mountains.

Next week, I’m on the back of an envelope!

A history of Circles

February 15th, 2010

Not too long ago I uploaded my second tech demo, Circles. Very small, very simple and was built also as a tutorial for my framework.
Circles ScreenshotBut it’s history goes back a lot further then that.

The version on my site is built in Java but originally it was built in Ruby using OpenGL. It was something small I’d knocked up in an hour called Physics Balls and built for an easy-going weekly competition called the Wednesday Workshop over at SoCoder. You can find it here.

The original Circles, on SoCoder


Overall it went down like a lead balloon. “Not much of a game” was the description given, and the consensus from most users. But that wasn’t the end!

During my time at university I’ve been very priviledged to be able to work with the Greenfoot team building example scenarios, testing and creating worksheets. The software allows you to easily build games and interactive scenarios with very little code and includes an web portal, the Greenfoot Gallery, where they can be uploaded. One of the projects I uploaded was a newer version of Physics Balls: Circles, and later it was included in a combination scenario I built: the JL235 Collection (which I thought was really cool, but people weren’t that impressed).

Some of my GreenfootGallery scenarios

Last year a book was released for Greenfoot for which I was asked if I could provide my Circles scenario. So I’m in a book, WOOT! Here is the book…

Inside this copy looked as though it had been through a washing machine


and in Chapter 10 on page 157-158, is Circles!

Circles, but my name is wrong! I never call myself 'Joe Lenton'

So is that it? Of course not! For my third year project at uni I’m building a highly concurrent framework in Erlang for which I’ve also made a version for another small tutorial…

In this version each Circle is updating in it's own seperate process

Next week, the magical Square!

isMouseDown() is bad

February 9th, 2010

I’m currently trying to solve a UI issue with Space Snake Galaxies. On the universe map you can bring up highscore and level info for each galaxy where you play.


When you click outside of either they dissappear.

The issue is that there are places where you can both click outside of the highscores and on a button and so both detect that a mouse click has occurred. Why can you do this? Because I’m still using the old basic way of handling controls through simple function primitives. i.e. isMouseDown, isKeyDown, getMouseX and getMouseY. i.e.

if ( getControls().isMouseDown(Controls.LEFT_MOUSE) ) {
    // do something
}

It’s crazy that indie game programmers still recommend doing this. It’s like the GOTO of controls. Dead simple and you can build anything with it, but it’s a real mess to code anything sophisticated (like good buttons!).

In GUIs you don’t do this. When I press a key an event is dispatched to the top level component. If it cannot handle the event it will pass it on to another component (and so on and so forth). This means the GUI is passive; it’s mouse and keybaord handling code is only run when an event occurres. With the basic active code above it will make a check on every frame to see if the mouse is down or not, wasting CPU time.

But there is another difference. If an event can be handled by a component then the event is removed. That sole property is almost impossible to build using basic functions like isMouseDown. You either need to permanently alter the state of the controls (so future checks on isMouseDown return false) or tie all components together globally (have a button check if any other buttons have handled mouse controls first, and if they haven’t then it will).

I think the solution will be to build a passive event system into my framework. To allow easy event based input for buttons and panels whilst keeping direct input for guns and shooting. Because sometimes all you need is isMouseDown.

BOOOO! Oracle

February 5th, 2010

Sun were giving all their products away for free and wasn’t making any money. To help out Oracle buys Sun. At first I thought this was excellent news because both companies have exceptional products, but neither have many flagship products (note flagship) that directly compete with each other. So it seems like a good mix.

This was until I received…

In short, Project Kenai is closing and taking my open source sf-library along with it. BOOOO!

So I’ve had to move the whole project across to Google Code. But it has me annoyed because Project Kenai is clearly prettier then Google Code:

However what really is quite worrying is that the JOGL project (Java OpenGL bindings) will also be closing along with Project Kenai and there are no plans to move it back to it’s old home java.net or to anywhere new (but there are some custom branches around on github). But it annoys me as yet another example of how Java, a very mature and successful language, is failing to capture the desktop market. I did a blog post a month or two ago mentioning WebGL which will allow you to do 3D in the browser, yet you’ve been able to do it for years with Java. It’s crazy it doesn’t get more support for this kind of thing from Sun,.. sorry, Oracle.

BOOOOOO!

What am I working on? SSG!

January 31st, 2010

It’s been a while since I’ve added some proper content to the site, so what have I been up to? Well mainly…

It’s a full complete remake of SpaceSnake. For a while I’ve wanted to do two things for my next project. First make a complete game, not another small 1 level title, and second base it on one of my existing games. SpaceSnake is easily the most popular game on my site. It’s also the only one of my games where someone other then me has blogged about it! Making it the obvious choice.

It’ll have multiple levels placed in a large universe map that you play across. On each level there will be achievements to complete in order to unlock the later levels for building a sense of progression and development. The levels will also get consistently harder as you go through to push your snake skills to the limit.

SSG universe map

an early shot of the universe map


SSG universe map 2

previewing a level on the universe map

When finished I am planning to try and get it released onto the (still quite recent) JavaStore. It might not be Steam or the Apple Store, but having a full game finished and published will give me more credability and gain experience I can use for the next title. In many ways it’s like I’m building my first game again because it needs to meet a standard at of completeness that is far higher then any other game I have built.

SSG 'My Plumbing' level screenshot

levels now overlap across the sides of the screen


There is still lots to do, so I’d better get back to work!

Arts & Crafts

January 8th, 2010

My brother recently asked me if I still had a copy of a video I made years ago. A small proof of concept video when I was building my own green screen (actually it was orange, that was all I could get for free). So I managed to track it down and reposted it on YouTube.

It’s also online at MySpace where amazingly this has almost 4,500 plays!

Next is something more recent. If you’ve played through ColdWar Chaos then you’d have seen the bombs that explode into different cat faces. These were all made using a video of one my cats, Moses. Here are the original images I made for each of her faces in the game:
moses

Java, with no Java!

December 30th, 2009

A few weeks ago I saw this video on channel 9 about the new hardware acceleration features being added to IE9. In reality this is nothing new, I saw news reports of Mozilla talking about adding similar to FireFox at least a year ago. But to see it in action brought home a certain reality.

One of the key selling points for me using Java for building my games is that all of the graphics are hardware accelerated, all thanks to the OpenGL bindings. There is no worry about the number of images I’m using and if they are transparent. With hardware accelerated support in the browser this advantage is just no longer true.

The need for hardware acceleration is apparent if you look at some of the current high-end HTML 5 examples. There are some great ones over at Chrome Experiments.com, my favourites being a partial port of Another World. But some others make my PC grind to a crawl. However even with HTML 5 I’d then have to write all my games in JavaScript; a language far more combersume and limiting then Java.

Java to JavaScript

So next enters GWT, the Google Web Toolkit. A library for building GUIs in JavaScript. But what’s interesting is that it’s written and compiled from Java code.

I’ve been playing around with it over the last few weeks and if you’ve got any experience using something like Swing then you’ll find it a synch to use. They have a nice page showing all of the core widgets included here, but even nicer is their mock mail app example.

The potential here is that I could write a game in Java and not have to rely on the plugin being present. But even if the graphics are hardware accelerated it’s still not the same without the OpenGL bindings. Lots of little effects and tweaks I’d like to perform for which I need them to be able to perform. The solution? WebGL!

GFX FTW!

A new standard in development (here) and already supported by FireFox, Chrome, Opera and Safari (although it seems only via experimental nightly builds on FF). This would be perfect and is what I’d love to be using. It’s just a shame that only one of those browsers has a percentage of users in the double digits, and even then nothing compared to IE. Until that changes it’s just not practical to use. That pains me.

Any JVM bugs users experience automatically get blamed on my games (that is where the user saw them occur). It’s also not as cross-platform as I once thought (although still excellent at this). I’d ultimately love to have the same environment I have right now, writing Java code that uses OpenGL, but with no actual Java backend. That would be heaven.

About time!

December 17th, 2009

I’ve been bogged down with tonnes of academic work over the past few months. Lots of assignments, lectures to attend and my on going third year project. As a result StudioFortress has been on the back burner for a while with the odd minor update.

However it’s now the end of term so that’s changed. Over the last few weeks Space Snake has been getting a lot of plays from visitors via SuperLand.de, a german blog on games. It’s still easily my most popular game, so I’ve finally found enough time to add a feature it’s needed for ages. Highscores!!!

space_snake_highscores

If you go and play today you’ll find the various death screens have been replaced with new ones where you can type in your name and submit a score. A minor improvement in the grand scheme of things, but I personally think it makes a big difference for this game.

Concurrency links

November 2nd, 2009

Currently I am in my fourth year at university, and for that year I am building a highly concurrent game framework in Erlang as my final year project. It’s a nice language that can potentially handle millions of processes, but in practice only 10’s of thousands on a standard home PC. I’m aiming to try to see if you can easily split a game up into thousands of parts that will automatically scale with the number of CPUs your using.

But this post is not about my project. Whilst doing some background research online I’ve found several nice articles and slides about concurrency in games. Here is a compiled list of my favourites:

Background

Game Benchmarks : Part 2: How Many CPU Cores Do You Need
This is the game benchmarks page from a Tom’s Hardware Guide article on how many cores you need to run apps and games. On average 4 cores only give a 0.6% increase (for games) over 3! So concurrency is clearly a problem, today.

Gamasutra Features : Multithreaded Game Engine Architectures
Three concurrent alternatives to the common game loop. I’m personally building the third architecture for my framework.

Info

Designing the Framework of a Parallel Game Engine
An article by a developer at Intel who has designed a highly concurrent game framework. A long but interesting read on how he has achieved it, and probably the best link on this page.

id Tech 5 Challenges, from Texture Virtualization to Massive Parallelization
These are some Id Software slides about some of the technologies they have built behind their upcomming game Rage. As a part of it they have built a Job based concurrency model where all of the games tasks are split into generic Jobs. At the same time they have a thread pool taking the Jobs and running them.

Tim Sweeney Slides

For those who don’t know, Tim Sweeney is one of the founders of Epic Games and worked on the Unreal engine. As someone who works on one of the most popular engines used for games, these slides are a great insight into what we should be seeing in the not-so distant future.

The next mainstream programming language
Some of this is about concurrency, however a large portion are also ideas on how functional languages could increase the code quality of games and game middle wear if used over current languages.

The end of the GPU roadmap
Very interesting set of slides on how the work inside games will (in Tim Sweeney’s eyes) be split up and parallelised in the futrue, and some of the potential speed ups from doing this. I like how the main emphasis is about underlying technologies that will do this for developers, leaving us to just handle building the games.

Valve

GDC 2007, Valve making Source multi-core
This is a more low-level set of slides on concurrency additions to Valve’s Source engine in 2007. It mainly covers a few key points about the importance of lock-free algorithms.

Multi-Threaded Challenges in the Game Space, a Conversation with Tom Leonard of Valve Fame
An interview discussing the ideas and issus behind the Source engines concurrency support. It mainly goes into why they have chosen to implement their libraries themselves rather then re-using existing libraries.