Sphere in OpenGL es 2.0 iOS [duplicate]

▼魔方 西西 提交于 2019-12-21 03:00:31

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/13606130/sphere-in-opengl-es-2-0-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!