Tutorial on Drawing 3D Shapesfloat nDotL = max(dot(normal, lightVector), 0.0);vec3 diffuse = DIFFUSE_LIGHT * nDotL;float specPow = 0.0;if (nDotL > 0.0) // are there any specular reflections?{vec3 cameraVector = normalize(vCameraVector);// calculate how much specular light will be reflectedvec3 reflectVector = reflect(-cameraVector, normal);specPow = pow(max(dot(reflectVector, lightVector), 0.0), 4.0);}// calculate the specular light based on the light value and the reflection valuevec3 specular = SPECULAR_LIGHT * specPow;}// sum all of the light sources togethergl_FragColor = vec4(AMBIENT_LIGHT, 1.0) * color +vec4(diffuse, 1.0) * color +vec4(specular, 1.0);7. Example 4-37 shows setting the light and position values and passing them to the shaders:Example 4-37 Set the camera, projection, view, and world...float cameraAngle = 0.0f;vec3 camPos = calculateCamPos(75.0f, cameraAngle);vec3 camTarget = vec3(0.0f, 0.0f, 0.0f);vec3 upVector = vec3(0.0f, 0.0f, 1.0f);// Set up the view, projection and world matrices and multiply them to get the// modelviewprojection and modelview matrices.mat4 view = mat4::lookAt(camPos, camTarget, upVector);mat4 proj = mat4::perspective(60.0f, 4.0f/3.0f, 1.0f, 500.0f);mat4 world = mat4::identity();mat4 wvpMatrix = proj * view * world;vec3 lightPosition(75.0f, 75.0f, 0.0f);// Set the program to use and initialize the uniformscontext->setProgram(perVertexLightingProgram);context->setUniform("LIGHT_POSITION", lightPosition);context->setUniform("CAMERA_POSITION", camPos);context->setUniform("AMBIENT_LIGHT", AMBIENT_LIGHT);context->setUniform("DIFFUSE_LIGHT", DIFFUSE_LIGHT);context->setUniform("SPECULAR_LIGHT", SPECULAR_LIGHT);context->setUniformSampler("diffuseTexture", diffuseTexture);context->setUniformSampler("normalMap", normalMap);...// Enable depth test and start drawing loopglEnable(GL_DEPTH_TEST);8. Example 4-38 on page 4-29 shows the part of the draw loop that reacts to keyboard inputto change the camera, view, and lighting:ARM DUI 0527A-02a Copyright © 2010 ARM. All rights reserved. 4-28ID070710Non-Confidential - Draft - Beta
Tutorial on Drawing 3D ShapesExample 4-38 Use the to keyboard input to change the view, camera, and lighting......do{// Exit the application if the escape key is pressedif (keyboard->getSpecialKeyState(Keyboard::KEY_ESCAPE)){return 0;}// Rotate object or cameraif (keyboard->getSpecialKeyState(Keyboard::KEY_SPACE)){if (keyboard->getSpecialKeyState(Keyboard::KEY_LEFT)){camPos = calculateCamPos(75.0f, --cameraAngle);view = mat4::lookAt(camPos, camTarget, upVector);}if (keyboard->getSpecialKeyState(Keyboard::KEY_RIGHT)){camPos = calculateCamPos(75.0f, ++cameraAngle);view = mat4::lookAt(camPos, camTarget, upVector);}}else{if (keyboard->getSpecialKeyState(Keyboard::KEY_RIGHT)){world *= mat4::rotation(1.0f, vec3(0.0f, 0.0f, 1.0f));}if (keyboard->getSpecialKeyState(Keyboard::KEY_LEFT)){world *= mat4::rotation(-1.0f, vec3(0.0f, 0.0f, 1.0f));}}9. Example 4-39 shows the part of the draw loop that sets the uniform values and draws theteapot:Example 4-39 Drawing the bump-mapped object...wvpMatrix = proj * view * world;context->setUniformMatrix("WORLD_VIEW_PROJECTION", wvpMatrix);context->setUniformMatrix("WORLD", world.toMat3());// Clear the screen and draw teapot with lightingglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );teapot->draw();}while ( context->update() );}catch (Exception &e){ARM DUI 0527A-02a Copyright © 2010 ARM. All rights reserved. 4-29ID070710Non-Confidential - Draft - Beta