Pro OpenGL ES for iOS

(singke) #1

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


glEnable(GL_DEPTH_TEST);

m_EyePosition=GLKVector3Make(0.0,0.0,65.0);

m_WorldModelViewMatrix=GLKMatrix4MakeTranslation(-m_EyePosition.x,-m_EyePosition.y,-
m_EyePosition.z);

m_Sphere=[[Planet alloc] init:planetSize slices:planetSize radius:10.0f squash:1.0f
textureFile:NULL];
[m_Sphere setPositionX:0.0 Y:0.0 Z:0.0];

m_EarthDayTexture=[self loadTexture:@"earth_light.png"];
m_EarthNightTexture=[self loadTexture:@"earth_night.png"];

m_LightPosition=GLKVector3Make(100.0, 10,100.0); //behind the earth

}
In loadShaders() I merely added one more attribute, namely, texCoord, or the texture
coordinates. These are recovered in the fragment shader:
glBindAttribLocation(*program, ATTRIB_VERTEX, "position");
glBindAttribLocation(*program, ATTRIB_NORMAL, "normal");
glBindAttribLocation(*program, GLKVertexAttribTexCoord0, "texCoord");
I also pass the light’s position as a uniform, instead of hard-coding it in the vertex
shader. This is set up in a couple of steps:
„ First, add it to the shader: uniform vec3 lightPosition;.
„ Then in loadShaders(), you fetch its ‘‘location’’ using
glGetUniformLocation(). That merely returns a unique ID for this
session that is then used when setting or getting data from the shader.
„ The light’s position can then be set by using this:
glUniform3fv(uniforms[UNIFORM_LIGHT_POSITION],1,m_LightPosition.v);
Then change the call to add two parameters so that it can be called with different shader
names, and add a pointer to a progam handle. And remember to change the code to
support the parameters instead of the temp variables:


  • (BOOL)loadShaders:(GLuint )program shaderName:(NSString )shaderName
    Now in Listing 10-9, both sides of the earth are drawn, with the night side going first,
    while the daylight side goes second. The programs are swapped as needed.
    Listing 10-9. The drawInRect() Method to Handle This Situation

  • (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
    {
    GLfloat gray=0.0;
    static int frame=0;

Free download pdf