Pro OpenGL ES for iOS

(singke) #1

CHAPTER 4: Turning On the Lights (^123)
Next, a second object, in this case, our sun, needs to be generated, sized, and placed.
And while we’re at it, change the size of the earth to make it smaller than the sun. So,
replace the initGeometry() method in OpenGLSolarSystemController with Listing 4-7.
Listing 4-7. Add a second object and initialize the viewer’s position
-(void)initGeometry
{
m_Eyeposition[X_VALUE]=0.0; //1
m_Eyeposition[Y_VALUE]=0.0;
m_Eyeposition[Z_VALUE]=5.0;
m_Earth=[[Planet alloc] init:50 slices:50 radius:.3 squash:1.0]; //2
[m_Earth setPositionX:0.0 Y:0.0 Z:-2.0]; //3
m_Sun=[[Planet alloc] init:50 slices:50 radius:1.0 squash:1.0]; //4
[m_Sun setPositionX:0.0 Y:0.0 Z:0.0];
}
Here’s what’s going on:
„ Our eyepoint now has a well-defined location of +5 on the Z-axis as
defined in line 1ff.
„ In line 2, the earth’s diameter is reduced to .3.
„ And in line 3 we initialize the earth’s location to be behind the sun from
our standpoint, at z=-2.
„ Now we can create the sun and place it at the exact center of our
relatively fake solar system.
InitLighting() needs to look like Listing 4-8, cleaned up from all of the mucking around
in the previous examples.
Listing 4-8. Expanded lighting for the Solar-System model
-(void)initLighting
{
GLfloat sunPos[]={0.0,0.0,0.0,1.0};
GLfloat posFill1[]={-15.0,15.0,0.0,1.0};
GLfloat posFill2[]={-10.0,-4.0,1.0,1.0};
GLfloat white[]={1.0,1.0,1.0,1.0};
GLfloat dimblue[]={0.0,0.0,.2,1.0};
GLfloat cyan[]={0.0,1.0,1.0,1.0};
GLfloat yellow[]={1.0,1.0,0.0,1.0};
GLfloat magenta[]={1.0,0.0,1.0,1.0};
GLfloat dimmagenta[]={.75,0.0,.25,1.0};

Free download pdf