OpenGL ES 2.0 Sphere
What is the easiest way to draw a textured Sphere in OpenGL ES 2.0 with GL_TRIANGLES?
I’m especially wondering how to calculate the vertices.
Solutions Collect From Internet About “OpenGL ES 2.0 Sphere”
There are various ways of triangulating spheres. Popular ones, less popular ones, good ones, and not so good ones. Unfortunately the most widely used approach isn’t very good.
This might be the most widely used approach. You iterate through the two angles in a spherical coordinate system in two nested loops, and generate points for each pair of angles. With angle
theta iterating from
pi/2 and angle
phi iterating from
2*pi, and sphere radius
r, each point is calculated as:
x = r * cos(theta) * cos(phi) y = r * cos(theta) * sin(phi) z = r * sin(theta)
The calculation can be made more efficient if necessary, but I’ll skip that aspect for this answer. The level (precision) of the tessellation is determined by the number of subdivisions of the angles.
The main advantage of this approach is that it’s simple to implement, and easy to understand. You can picture the subdivision as the lines of latitude and longitude on a globe.
It does not result in a very good triangulation, though. The triangles around the equator have similar dimensions in all directions, but the triangles closer to the north/south pole get increasingly narrow. At the north/south pole you have a large number of very narrow triangles meeting in a single point. Good triangulations have all very similar sized triangles, and this one does not.
Recursive Subdivision of Octahedron
With this approach, you start with a regular octahedron, giving you 8 triangles. You then recursively subdivide each triangle into 4 sub-triangles, as illustrated here:
/\ / \ /____\ /\ /\ / \ / \ /____\/____\
So each triangle is subdivided by calculating 3 additional vertices that are midway between two of the existing vertices, and 4 triangles are formed from these 6 vertices. For calculating the midway point between two input points, you calculate the sum of the two vectors, and normalize the result to get the point back on the sphere.
The level (precision) of the tessellation is determined by the number of levels in the recursive subdivision. It starts with the 8 original triangles of the octahedron at level 0, results in 32 triangles at level 1, 128 at level 2, 512 at level 3, etc. You normally get a reasonably good looking sphere around level 3.
This approach results in a much more regular triangulation, and is therefore superior to the spherical coordinate approach.
The main disadvantage is that it might seem more complex. The calculation of the points is in fact very simple. It gets slightly more tricky if you want to use indexed vertices, instead of repeating common vertices. And even more painful if you want to build nice triangle strips. Not terribly difficult, but it takes some work.
This is my favorite approach of drawing spheres.
You can do the same thing I described for the octahedron starting with other polyhedra. Regular polyhedra that consist of triangles are particularly suitable, which makes the tethrahedron and the icosahedron natural candidates. The octahedron is the most attractive IMHO because the initial coordinates are so easy to enumerate. Using an icosahedron would probably result in an even more regular triangulation, and the vertex coordinates can be looked up.
I’m not sure if anybody is actually using this. But I tried it recently, and it was kind of fun. 🙂 The idea is that you take a cube centered at the origin, and subdivide each of the six sides into smaller sub-squares. You can then turn the cube into a sphere by simply normalizing each of the vectors that describe a vertex.
The advantage of this approach is that it’s very simple, including building triangle strips. The quality of the triangulation seems reasonably good. I don’t think it’s as regular as the recursively subdivided octahedron, but definitely better than the (much too) widely used spherical coordinate approach.
- ActionScript's File.upload does not work on Air SDK for iOS devices
- The best way to implement lives and score count in Sprite Kit (static, IoC, other?)
- How to Install Older iOS Simulators in XCode 4.2.1 (SDK5.0)
- YouTube video playback broken on iOS6 (works fine on iOS5)
- StoreKit on iOS – autorenewable subscription – How to know whether it is a free trial or not?
- UITableViewCell show white background and cannot be modified on iOS7
- Regular expressions in an Objective-C Cocoa application
- Enumerate NSString characters via pointer
- Set Button selector (on click event) at runtime ? (Cocoa : Mac Project)
- NSMutableArray indexOfObject returning an extremely large number
- Toggle Airport Cocoa
- How do I use getData() to plot a LineChart with ObjC PNChart library in Swift?
- iPad keyboard dimensions
- Core Text and copy 'n paste on iOS
- NSLayoutConstraint SIGABRT on iPad