1

Possible Duplicate:
Drawing a sphere in OpenGL ES

I have been browsing around for quite some time now, and have yet to find an adequate answer. I started learning Opengl es 2.0 (Because I need to for a project in university) and have recently accomplished drawing a circle. WOOPIE!

I have looked at a great deal of similar questions, but it is either outdated or I too complex for me to grasp. Can anyone point me in the proper direction of how to draw a solid sphere with opengl ES 2.0 on iOS?

Community
  • 1
  • 1
rayred
  • 107
  • 1
  • 9

1 Answers1

6

I'm not sure how far along you are, so I'm just going to give some general notes that I think might be helpful, and point you to some resources that I've been using to climb up the learning curve.

A sphere is a complex enough object that you're probably not going to want to generate the vertices in code, as you may have drawn the circle -- you'll want to use a program like Blender or Maya or Houdini, whatever you like to use to build a 3d object, and then export it.

Your goal will be to follow a workflow like: 3D program > .obj or maybe a .collada file > array of vertices that OpenGL can use.

Your array of vertices (which should be a C array[], not NSArray) will hold a {x,y,z} position for each vertex, and you may also want to use texture coordinates and normals. You'll want to export texture coordinates from your 3D program if you plan on using textures, and you'll want to export normals if you plan on lighting the object. The texture coordinates will be in the format {s,t} which connects the vertex it is associated with to a 2d coordinate on the rectangular texture. The normals will be a vector in the format {x,y,z}. The tex coords & normals may either be in the same array you have the vertices in (interleaved) or in separate arrays. If they're interleaved, then in your code you'll have one VBO and it's generally faster, but if they're not, you'll have separate VBOs - one for the position vertices, one for the tex coords, one for the normals.

This is a good script I've come across for converting .obj to a C header for use with OpenGL. So after you export from your 3D program to .obj, you'd pass the .obj to this script and it would spit out a .h file: http://heikobehrens.net/2009/08/27/obj2opengl/

Once you have the C header file, you just #import "sphere.h" - and then when you later call glBufferData to read in the vertices, you pass the name of the array that's in sphere.h.

This book is the best I've come across for learning OpenGL on iOS. It provides good explanations of the GLKit classes, and is friendly to beginners: http://my.safaribooksonline.com/book/animation-and-3d/9780132478939

And in case it's helpful, here's some sample code I put together for a talk I gave a couple months ago. It actually puts us 'inside' an exploded sphere and lets us swipe to rotate: http://davidsweetman.com/mobilemeetup-talk-glkit-demo.html

jankins
  • 670
  • 5
  • 16
  • Hi. You mentioned that our work flow should be 3D program > .obj or maybe a .collada file > .h file. Can you suggest any script like the one you did for .obj file, which converts collada file to .h for OpenGL? Thanks :) – ScarletWitch Sep 07 '13 at 16:36
  • I believe there's a collada importer included in the sample code for the book Learning OpenGL ES for iOS: http://my.safaribooksonline.com/book/animation-and-3d/9780132478939 – jankins Sep 08 '13 at 18:21