Pages

Saturday, May 28, 2016

Drawing a Sphere... Ready for gameplay!

Due to the nature of cubes not being a very good representation of a rotatable unit, I decided to add a sphere to Fragile's collection of drawable "blocks", and OH MY GOSH I ran into all sorts of difficulties... You wouldn't believe how many hours I spent trying to add a simple sphere to the game. Here's the story.

First, I had to generate a sphere. I tried replicating a UV sphere formula, and it kind of worked, but it was heavily distorted. I figured okay, maybe the formula wasn't designed for Direct3D or something, so after some tinkering I decided to move on to a different algorithm.

Next, I wrote some code to generate it from an icosahedron, which is a 20-faced sphere-like shape that you start with because each face is a triangle - real interesting, actually. You then divide each (equilateral) triangle into 4 equilateral subtriangles (broken at the midpoints of each edge of the bigger triangle), and you can keep repeating this process until your sphere-like shape has enough vertices (points) to satisfy you. You also re-adjust each point along the way to ensure that it has a distance of 1 away from the origin, and then it becomes an isosphere! :)

Well yeah, that was heavily distorted too. So I rechecked the algorithm and each line of code at least a dozen times, and I figured, maybe the vertices can't be duplicates, so I revised the algorithm to get rid of duplicates. I also tried starting from an octahedron (to make an octasphere?). Nope, still heavily distorted.

Then I did some searching around and discovered that maybe the winding order had something to do with it, so I revised the algorithm to take care of that. Nope, still heavily distorted.

This process went on and on and on and on for hours and hours; I did all sorts of problem-solving with all diligence, determined to fix this stupid sphere. I plotted the points in gnuplot (looked like a sphere!), I manually checked and revised a bunch of vertices and indices, I turned features and indices on and off, I talked to my programming friends about the issue, I researched, I debugged the graphics pipeline, I changed memory structures, I tried a few other fixes, and I gave up a few times. Still a distorted sphere:

But, determined not to give up, totally baffled by this weird spear-like point that kept distorting my sphere - yet with perfect vertices and a rather simple algorithm - I finally figured out the problem: there was a byte-alignment property that was improperly set when I duplicated the cube-drawing code... Great.

The bad news is, I lost several days and many hours of development fixing this dumb sphere. The good news is, now it's finally drawing, and I understand the process extremely well and refreshed my memory on how the entire graphics pipeline and all of Fragile's drawing code works! So I'm sure that will have its benefits. Forty first step complete:

Fun fact: this beautiful sphere is made out of 4 levels of the recursive algorithm, and it consists of 1,026 vertices and 454 indices.

Next up: units, blocks, and gameplay!!!

No comments:

Post a Comment