How to draw a sphere in OpenGL ES 2 correctly
"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:
- Creating a 3D sphere in Opengl using Visual C++
- calculating a sphere in opengl
Drawing a sphere in OpenGL ES 2.0
You're setting vertexCount
as lat * lon * bytes per float
, which looks very weird to me.
I think you have misnamed this variable, as the number of vertices has nothing to do with bytes per float.
You're using the same variable in glDrawArrays, which seems to me will not have the accurate number of vertices.
Android OpenGL ES 2.0: Sphere Texture Mapping
I can see at least one thing which seems to be wrong...
GLES20.glVertexAttribPointer(textureCoordHandle, 3,
GLES20.GL_FLOAT, false, 0, ufo_sphere.getTexCoords());
Texture coordinates typically have a size of 2 (u, v). So you could try this instead:
GLES20.glVertexAttribPointer(textureCoordHandle, 2,
GLES20.GL_FLOAT, false, 0, ufo_sphere.getTexCoords());
Also, in the link you provided they use the GL_TRIANGLES format instead of GL_TRIANGLE_STRIP so you should switch back to that.
OpenGL ES 2.0: Filling in a Sphere instead of it being wireframe
It looks like you are calculating vertex indices in your setup code, but not using them. Consider using glDrawElements instead of glDrawArrays.
It also looks like you are half way between using GL_TRIANGLE_STRIP (one new index per triangle after the first triangle) and GL_TRIANGLES (three indices per triangle). You will probably find it easier to use GL_TRIANGLES.
How to draw and texture a UV Sphere with OpenGL 2.0 for Android
Ok, the problem is solved now. The textures were not working properly because the generated point indices start at 1 instead of 0. By substracting 1 to all indices the problem is solved... :)
Related Topics
How to Use Multiple Segues with One Uitableviewdelegate
JSON Parsing Using Nsjsonserialization in iOS
Adding Swift Files to Test Target Not Fixing Unit Tests
Core Data Predicate:Unimplemented SQL Generation for Predicate
iOS Permission Alerts - Removing or Suppressing
Calling Instance Method During Initialization in Swift
Uiscrollview with iOS Auto Layout Constraints: Wrong Size for Subviews
Add a Text Overlay with Avmutablevideocomposition to a Specific Timerange
How to Control Avassetwriter to Write at the Correct Fps
Detect Hotspot Enabling in iOS with Private API'S
How to Determine If a User Has an iOS App Installed
Libz.Dylib Versus Libz.1.2.3.Dylib Versus Libz.1.2.5.Dylib
Asynchronous Upload with Nsurlsession Will Not Work But Synchronous Nsurlconnection Does
Firebase Queryorderedbychild() Method Not Giving Sorted Data