Recap
Basically, I wanted to create a scene with random terrain and light the scene with some different illumination models. Then I wanted to throw in dynamic shadow casting and, if time permitted, some fancier effects like soft shadows and
volumetric shadows. The best way to go about all of this was with the use of shaders; specifically GLSL.
Status
According to my timeline in the
previous post I expected to be further. In all honesty I knew I would get distracted and fall behind. Progress has actually been made though; I'm only a week behind!
- Random scene generation is done
- Basic shader understanding/implementation is done
- Phong illumination model is implemented
Terrain Generation
I began the project with my basic terrain generation algorithm I started a few months ago. It used the simple recursive
Diamond-Square Algorithm to generate a 2-dimensional height map. From there I could create a basic polygonal mesh and render the scene using OpenGL. It gets some pretty nice results and isn't too difficult to implement, but can be a bit intensive when generating larger maps.
|
Diamond-Square Terrain |
|
Distraction 1: Minecraftify it! |
Random Planet Generation
I got distracted on a bit of a terrain generation kick and felt like taking it a step further. I can not for the life of me find the paper and website where I got this algorithm from, but it is awesome:
- Generate a tessellated sphere (I use recursive icosahedron subdivision)
- For an arbitrary number of iterations:
- Generate a random plane normal
- For every point on sphere
- Determine side of plane the point is on
- If in front, raise a random amount
- If behind, lower a random amount
It's fast (for reasonably selected tessellation and iteration factors), simple to understand, straightforward to implement and makes some pretty interesting looking planets. All these points on the sphere are then loaded into a vertex buffer object and passed over to the GPU (with an accompanying index buffer) where my shaders take over.
Shaders
I ended up following an object-oriented approach similar to what's outlined on the
Swiftless Tutorials
website, but modified to make things a bit more logical for me. This
made loading, compiling and linking shaders extremely easy and let me
focus on actually writing the shaders.
My shaders are still a tad basic, but they do have a few noteworthy characteristics:
- Implement all the basics of GLSL usage, such as varying, uniform, and attribute types
- The vertex shader adjusts vertices based on a distance calculation (any points below sea-level are adjusted to preserve a more rounded planet)
- The fragment shader determines pixel color based on their position, and implements the Phong Illumination models (Per-pixel lighting, yeah!)
Results
Originally being made in C++, all of this has been ported over to Java and runs on Android 4.0.3 (should work on 2.2+) with OpenGL ES 2.0. I got a tablet and figured this would be a good way to break it in.
|
Point Cloud - 0 offsets |
|
|
Point Cloud - 1 offset |
|
|
Wire-frame Mesh - 1 offset |
|
|
Solid - 1 offset |
|
|
Solid - 100 offsets |
|
|
Solid - 1000 offsets |
|