CHAPTER 4: Turning On the Lights (^127)
The emissive material attribute is switched off, here in line 15, followed
by another glPopMatrix(). Note that every time you do use a push
matrix, it must be paired with a pop. OpenGL ES can handle stacks up
to 16 deep. Also, since there are three kinds of matrices in use in
OpenGL (the modelview, projection, and texture), make sure that you
are pushing/popping the proper stack. You can ensure this by
remembering to use glMatrixMode().
Now in executePlanet(), line 17 gets the planet’s current position so
line 18 can translate the planet to the proper position. In this case, it
never actually changes, because we’re letting glRotatef() handle the
orbital duties. Otherwise, the xyz would constantly change as a factor
of time.
Finally, call the planet’s own execute routine in line 19.
We’re almost done. Planet.h (Listing 4-10) and Planet.m (Listing 4-11) need to be
modified to hold some state information. Note that I am very old-school and prefer to
write my own setter/getters.
Listing 4-10. Modifications to Planet.h to support the Solar-System model
#import <Foundation/Foundation.h>
#import <OpenGLES/ES1/gl.h>
@interface Planet : NSObject
{
@private
GLfloat m_VertexData;
GLubyte m_ColorData;
GLfloat m_NormalData;
GLint m_Stacks, m_Slices;
GLfloat m_Scale;
GLfloat m_Squash;
GLfloat m_Angle;
GLfloat m_Pos[3];
GLfloat m_RotationalIncrement;
}
-(bool)execute;
-(id) init:(GLint)stacks slices:(GLint)slices radius:(GLfloat)radius squash:(GLfloat)
squash;
-(void)getPositionX:(GLfloat )x Y:(GLfloat )y Z:(GLfloat )z;
-(void)setPositionX:(GLfloat)x Y:(GLfloat)y Z:(GLfloat)z;
-(GLfloat)getRotation;
-(void)setRotation:(GLfloat)angle;
-(GLfloat)getRotationalIncrement;
-(void)setRotationalIncrement:(GLfloat)inc;
-(void)incrementRotation;
@end
singke
(singke)
#1