Friday, October 22, 2010

Project 1 - Drawing a sphere, part 2

So, last night I decided on using triangle strips for drawing the circle. The reason strips are better than the primitives on their own (something I should have mentioned in the last post) is that it cuts down on redundancy. In order to draw a triangle, you need to provide three points that the computer will then group into a triangle. If you want to draw a triangle adjacent to the one you just drew, however, you have to again give three points, two of which are the same ones as the ones in the first triangle. This is wasteful.

The strip primitives, on the other hand, will take three points for the first triangle, but then each following triangle takes only a single new point. The computer knows it is adjacent to the last triangle you drew, and uses the points it already knows instead of asking you to repeat them.

While this is great for strips, however, we still end up repeating points when we need to add triangles to the free edge. Still, this is better than having to draw each of them individually.

Now on to the code.

I want to draw the triangle strips going north to south, then back up, so I start at the north pole, and all the triangles fall within two meridians. Each couple of triangles should form squares that are delimited by the parallels. I promise to post images eventually.

However, I have to provide the positions of the points in a cartesian coordinate system. So I'll need to do some trigonometry. Before that, however, I need to define some parameters. Let's call the center of the sphere (0,0,0). The radius will be 1. And I'll use 6 meridians and 6 parallels to draw the sphere. So the meridians will be 0°, 60°, 120° , 180° , -120° , -60° (positive is east, negative is west). The parallels will be 0°, +/-30°, +/-60°, +/-90° (positive is north, negative is south).

The 0° meridian will cross the x positive axis, while the 0° parallel will be at z = 0.

The reason I like the parallels and meridians description is because it maps directly to spherical coordinates, from which we can get the cartesian coordinates simply with these equations:

x = r * sin θ * cos φ
y = r * sin θ * sin φ
z = r * cos θ

θ maps to the parallels, almost. Parallels go from -90° to +90°, θ goes from 0° to 180°. φ maps to the meridians. r is the radius (1 for now). Will need a basic math library for the sine and cosine functions, but the cmath header from the STL should have everything we need. And we need to use radians instead of degrees.

So, we need a list of the points that will make up the triangles that will make up the sphere. We start with the north pole, (1,0,0) in spherical coordinates. Then advance θ one step, mark a vertex, then increase φ one step, mark a vertex, then advance θ another step and decrease φ, mark a vertex, and so on.

Well, that's all for today. I managed to do some progress, though I need to work out the commands better. However, I'm all out of time. Until next time.

No comments:

Post a Comment