Pro OpenGL ES for iOS

(singke) #1

336 CHAPTER 10: OpenGL ES 2, Shaders, and...^


With that in mind, let’s add cube map to the earth using GLKReflectionMapEffect.

You can start again with the standard template project and then add Planet.mm to it.
You will need to modify both the view controller and the planet code. First, let’s handle
your setupGL() method in the view controller by substituting Listing 10-17 for the
template code.
Listing 10-17. Setting Up Your Reflection Map

-(NSString *)imagePath:(NSString *)image
{
return [[NSBundle mainBundle] pathForResource:image ofType:NULL];
}


  • (void)setupGL
    {
    int planetSize=50;


NSArray *images = [[NSMutableArray alloc] initWithObjects: //1
[self imagePath:@"hedly1.png"],
[self imagePath:@"hedly2.png"],
[self imagePath:@"hedly3.png"],
[self imagePath:@"hedly4.png"],
[self imagePath:@"hedly5.png"],
[self imagePath:@"hedly6.png"],
nil];

[EAGLContext setCurrentContext:self.context];

NSDictionary *options=
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],GLKTextureLoaderOriginBottomLeft,nil];

GLKTextureInfo *info= //2
[GLKTextureLoader cubeMapWithContentsOfFiles:images options:options error:nil];

self.effect = [[GLKReflectionMapEffect alloc] init]; //3
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
self.effect.light0.specularColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
self.effect.material.shininess = 15.0f;
self.effect.lightingType = GLKLightingTypePerPixel;

self.effect.textureCubeMap.name =info.name;

self.effect.light0.position=GLKVector4Make(-5.0f, 5.0f, 10.0f, 1.0);

glEnable(GL_DEPTH_TEST);
Free download pdf