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.
- Drawing on top of UIImageView to make the image transparent
- CVOpenGLESTextureCacheCreateTextureFromImage on iPad2 is too slow ,it needs almost 30 ms, too crazy
- How to merge two FBOs?
- Make a line thicker in 3D?
- Where is the official documentation for CVOpenGLESTexture method types?
- GPUImage apply filter to a buffer of images
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.
- What are the new “iOS data protection APIs”?
- UINavigationBar frame height returns 44.0 but is actually 64.0
- How to load multiple storyboard files depending on iOS version? (5 and 6)
- How to Open Google Play store from Phonegap
- fbDidLogin not called – iOS
- iPhone corebluetooth central Manager send data to peripheral
- Installing iOS Apps from a local plist (null) would like to install
- how can i update other controllers' UI in swift?
- iPad: How to get IP address programmatically WIRED (not via wireless)
- OBJ-C how-to: App using BLE connection and iBeacon in same device
- App rejected: strange iPhone crash log
- How to make the union between two MKCoordinateRegion
- How to track multiple touches in Xcode
- Extension of constructed generic type in Swift
- Swift : Checking if 2 NSDates are the same