Pro OpenGL ES for iOS

(singke) #1

78 CHAPTER 3: Building a 3D World^


The first thing to do is create a new project derived from the bouncy square example.
But instead of building up all the geometry in drawInRect(), you can create a new object
called Planet and initialize the data, as in Listing 3-6a for the header and Listing 3-6b for
the init method.
Listing 3-6a. Building our 3D Planet

#import <Foundation/Foundation.h>
#import <OpenGLES/ES1/gl.h>

@interface Planet : NSObject
{

@private
GLfloat *m_VertexData;
GLubyte *m_ColorData;

GLint m_Stacks, m_Slices;
GLfloat m_Scale;
GLfloat m_Squash;
}


  • (bool)execute;

  • (id) init:(GLint)stacks slices:(GLint)slices radius:(GLfloat)radius squash:(GLfloat)
    squash;


@end
Listing 3-6b. 3D Sphere generator


  • (id) init:(GLint)stacks slices:(GLint)slices radius:(GLfloat)radius squash:(GLfloat)
    squash
    {
    unsigned int colorIncrment=0; //1
    unsigned int blue=0;
    unsigned int red=255;


m_Scale=radius;
m_Squash=squash;

colorIncrment=255/stacks; //2

if ((self = [super init]))
{
m_Stacks = stacks;
m_Slices = slices;
m_VertexData = nil;
Free download pdf