You ever get that speed bug bite? You write code that every time you want to run it or test it it takes 15 or more seconds and you feel like nothing is getting done? That's how I was feeling with the python voronoi map generator. every time I tried to tweak something to make it a little bit better it would take anywhere from 15 to 30 seconds depending on what I added or what size I had the map at. I've started to learn a bit of Java recently and I figured this might be an interesting place to start. Taking notes from Minecraft I settled on LWJGL (Light-Weight Java Game Library), an OpenGL-based library that can be used for  2D or 3D (and I guess 1D if you're into that kinda thing) games. after recreating the map generator in java (had to find a way around the lambda keyword, that was a bunch of fun), I watched these tutorials. If you are interested in game development in Java, this is a great place to start, as it has really helped me.

Anyways back to this kickin' project. 

So I create a 2D version exactly like the python version without any interactiveness other than moving the map around and without a hint of AI.

 It's quite bland, and I don't really like it all that much, and the speed of the map generator (which is effectively instant at this point) is cancelled out by the speed it would take to add anything. So instead of recreating the whole project (AI and buildings in all) I've decided to create a new project just in java, all about the world building and procedural content. 

Because of LWJGL being based in OpenGL, it is just as easy (if not easier) to create a 3D program. So since I'm curious what the world would look like if I added a third dimension (without a cheap 1 directional stationary shadow trick), I create a simple world with flat quads floating at their height generated by the map generator.

Bam. Alright, I'll admit, I really like the way that looks, unfortunately the major downside is the black you can see between each tile where you can occasionally see the tile behind it which looks pretty ugly. Also there is this other bug where if you have a tile on the left side of the screen that is closer than another tile, the closer tile will draw BEHIND the further ones (and yes this only happens on the left side for some reason). This is unacceptable. only pure photorealism will be accepted! So to get rid of the ugly black holes I take every 'tile' and stretch it downwards to create a rectangular prism, giving a Minecraft-y look... kinda.

Okay so it doesn't looks that great, but what is missing? ah right, lighting. So to add lighting in LWJGL it's pretty straight forward. enable lighting, choose a shading model, create a light, give it properties and a position, enable that light. Boom. all the lighting you will ever need is done. or perhaps not. What's wrong? even though the light is set up nothing gets shaded! obviously there is a problem!

*10 minutes of searching later...*

Normals. Normals. Normals. In case anyone doesn't know what a normal is, Shamus Young has a great post on them you can read here. Basically a tl;dr would be they tell the program which way something is facing so that it knows how to light that object. each face or vertex can have a normal and you can light accordingly. Because everything in this simple world is just a simple box, the normals are basically binary 'this is facing exactly left' or 'this is facing exactly down', done with simple positive or negative 1s in the x,y,or z coordinate.

That looks much, much better already. You can tell where everything is, and things pop out more. We are now one step closer to photorealism. But what is this? if I rotate around to the back of the world, something very odd happens. The boxes get all warped and don't render correctly. You can see through some of them as if they are too small, and others just seem deformed and are being lighted terribly.

I can't figure out why this isn't working. after spending some time trying to fix it I decide to write a blog post about it to let the math part of my brain cool down. Tell me what you think in the comments, time to get back to work!

Code on.