Friday, October 22, 2010

Project 1 - Drawing a sphere

Let's get started on this then.

Drawing a sphere should not be something too fundamental to the process of creating this game, but it should help me get some experience working with OpenGL. Spheres are an interesting challenge to work on as well, since they are not exactly easy to do.

The reason why I want to draw spheres, is because planets and stars should be prominent in a science fiction game. Now, we know the basic description of a sphere, the surface described by all the points a certain distance from the center. We can't draw a perfect sphere because that would require drawing infinite points. But we can draw a good approximation with a body of sufficient faces. The more faces the body has, the closer the resemblance.

If we look at a map of the earth, we see that it is crisscrossed by parallels (crossing east to west) and meridians (north to south). The regions trapped within them are square (on mercator projections, anyway), since the lines cross each other at square angles. So we can draw the sphere by drawing these lines. OpenGL has a primitive type that is a strip of squares. Using it, we can place these strips side by side, each border describing a meridian, and go around filling the whole sphere. This has the added benefit that we can increase the count of strips for better detail, or lower it when we don't need to (and save some performance).

The only remaining problem I have to figure out is the caps that the north and south. Since a sphere is wider at the equator than at the poles, the square strips need to widen as they reach the middle, and thin towards the poles. The problem arises in that at the poles themselves, all the top 'squares' meet in a point, making them in fact triangles.

I'll have to make some drawings in the future to illustrate my points. I'll add that as a weekend project. Together with customizing the look.

Two possibilities occur to me. One is to use a different kind of primitive near the caps, a triangle flower thing that would serve my needs well enough. The other is to see if I can convince OpenGL to use a single point to describe two vertexes of a square. And the third option is to use the triangle strip primitive. It works mostly the same way as the square one, but should fit my purposes perfectly.

I don't want to have to switch primitives while drawing, and I don't want to force squares into triangles since that is a sure recipe for bugs. Plus, the triangle strip has an added benefit in that squares have to have all their points within a plane (not sure how the rendering behaves if this is not true. The points of a triangle, however, are guaranteed to always lie on the same plane since three points define a plane. This might come in handy in the future if I'm still using them when I start adding detail to the worlds.

 So, I have an idea of what I want to do, an idea of how to get about doing it, and a place where to do it. However, I'm all out of time for today. I should be able to get working on it tomorrow, have some results by Saturday. Well within schedule.

As a sidenote, now that I have a place to vent my ideas, I find I have a lot I want to talk about and not enough time to say it. I'll try to make some posts regarding my plans for the game in the future.

No comments:

Post a Comment