CHAPTER 10: OpenGL ES 2, Shaders, and... (^319)
Figure 10-2. The daytime earth (left) vs. the nighttime earth (right)
Under OpenGL 1.1, this would be very tricky to accomplish if at all. The algorithm should
be relatively simple: render two spheres of exactly the same dimensions. One has the
night image, and the other has the day image. Vary the daylight-side alpha channel of
the texture of the day-side earth based on the illumination. When illumination reaches 0,
it is completely transparent, and the night portion shows through. However, under
OpenGL ES 2, you can code the shaders very easily to match the algorithm almost
exactly.
So, I started with the cube template from Apple and dumped the cube stuff and added
Planet.mm and Planet.h files. setupGL() was changed to Listing 10-8. Notice the loading
of the two textures and two shader programs.
Listing 10-8. Setting Up to Show Earth at Night
- (void)setupGL
{
int planetSize=20;
[EAGLContext setCurrentContext:self.context];
[self loadShaders:&m_NightsideProgram shaderName:@"nightside"];
[self loadShaders:&m_DaysideProgram shaderName:@"dayside"];
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
m_ProjectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),
aspect, 0.1f, 100.0f);