How to draw a sphere in OpenGL ES 2 correctly

opengl-es-2.0

I was wondering what the difference between gldrawarrays and gldrawelements is? Also which would you use and when? Another question i have is how to draw a sphere for the android. Say i have 360 points on the perimeter for a circle, does that mean i will need 360 * 360 coordinates for a sphere? That seems to be very expensive, there has to be a better way. The only way i can think of so far is doing nested for loops. But that's going to be so much processor time. Another way i can think of is drawing it and rotating it 360 degrees in a direction. But then it's not really a true sphere, just a rotated circle.

Best Answer

"I was wondering what the difference between gldrawarrays and gldrawelements is?"

glDrawArrays is used when you deal directly with a stream of vertices, and glDrawElements is used when you use an index buffer, which adds an extra layer of indirection and lets you reference vertices by an index number. You can check out this article for more info on glDrawElements (disclaimer: I wrote the article): http://www.learnopengles.com/android-lesson-eight-an-introduction-to-index-buffer-objects-ibos/

The OpenGL ES manual also has info about these two functions:

http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawArrays.xml http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawElements.xml

"Another question i have is how to draw a sphere for the android."

You essentially just have to break it down into triangles. One simple way of doing this is tesselating the sphere using latitude and longitude, like the lines on a globe. You can use a loop with sin and cos to generate the points.

These two questions on Stack Overflow have some example code that should be straightforward to adapt to Android:

Related Topic