4.4.2. Drawing both the teapot and the cube

This example demonstrates loading and drawing two shapes:

  1. Use the modified code from Replacing the teapot with a cube as a starting point.

  2. Modify the getSceneAsset() call to both the load the cube and teapot assets as shown in Example 4.28:

    Example 4.28. Load the assets for the cube and teapot

    ...
    		// Loading scene
    		SceneAsset* scene = proxy.getSceneAsset("teapot.mba");
    		SceneAsset* scene2 = proxy.getSceneAsset("cube.mba");
    		GeometryAsset* cube = 			static_cast<GeometryAsset*>(scene->getAsset(Asset::TYPE_GEOMETRY, 0));
    		GeometryAsset* teapot = 			static_cast<GeometryAsset*>(scene2->getAsset(Asset::TYPE_GEOMETRY, 0));
    ...
            teapot->draw();
            cube->draw();
    ...
    

  3. Rebuild the project and run it. Both shapes are displayed, but they overlap each other. The location and size of the objects must be modified to fit them in the display area as distinct objects.

  4. Modify the world-view-projection matrix and add translation and scale multiplications as shown in Example 4.29:

    Example 4.29. Moving the teapot and cube

    ...
                wvpMatrix = proj * view * world;
                // scale the teapot to 70% and move it left, down, and forward
                mat4 wvpMatrix_teapot = wvpMatrix *
                     mat4::translation(-12.0, -12.0, -12.0)* mat4::scale(0.7, 0.7, 0.7);
    
                // Clear the screen and set the uniforms
                ...
    
                teapot->draw();
    
                // scale the cube to 30% and move it right, up, and back
                mat4 wvpMatrix_cube = wvpMatrix * 
                      mat4::translation(12.0, 12.0, 12.0)* mat4::scale(0.3, 0.3, 0.30);
                context->setUniformMatrix("WORLD_VIEW_PROJECTION", wvpMatrix_cube);
                cube->draw();
            }while ( context->update() );
    

  5. Rebuild the project and run it. Figure 4.7 now shows a cube shape and a teapot:

    Figure 4.7. The lighting example with a cube and teapot

    The lighting example with a cube and teapot

  6. Use the arrow keys to rotate the objects and the camera. Because both objects are in the same world, they keep the same relationship and move together.

  7. Create separate world matrices for the objects shown in Example 4.30:

    Example 4.30. Independent worlds for the teapot and cube

    ...
            // 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_teapot = mat4::scale(0.7, 0.7, 0.7) * 
                    mat4::rotation(90.0f, vec3(0.0f, 0.0f, 1.0f));
            mat4 world_cube = mat4::scale(0.3, 0.3, 0.30) * 
                    mat4::rotation(50.0f, vec3(0.0f, 0.0f, 1.0f));
        
            mat4 wvpMatrix_teapot = proj * view * world_teapot;
            mat4 wvpMatrix_cube = proj * view * world_cube;
    ...
    

  8. Modify the code that rotates the world so that the left arrow moves the teapot and the right arrow rotates the cube as shown in Example 4.31:

    Example 4.31. Independent worlds for the teapot and cube

    ...
                // Rotate object or camera
                if (keyboard->getSpecialKeyState(Keyboard::KEY_SPACE))
                {
                   // rotate camera
                }
                }
                else 
                {
                    if (keyboard->getSpecialKeyState(Keyboard::KEY_RIGHT))
                    {
                        world_teapot *= mat4::rotation(1.0f, vec3(0.0f, 0.0f, 1.0f));
                    }
                    if (keyboard->getSpecialKeyState(Keyboard::KEY_LEFT))
                    {
                        world_cube *= mat4::rotation(-1.0f, vec3(0.0f, 0.0f, 1.0f));
                    }
                }
    ...
    

  9. Modify the code that draws the shapes to use the new world-view-projections as shown in Example 4.32:

    Example 4.32. Independent worlds for the teapot and cube

    ...
                wvpMatrix_teapot = proj * view * world_teapot 
                     * mat4::translation(0.0, 0.0,20.0);
    
                // Clear the screen and set the uniforms
                context->setUniformMatrix("WORLD_VIEW_PROJECTION", wvpMatrix_teapot);
                context->setUniformMatrix("WORLD", world_teapot.toMat3());
               ...
    
                teapot->draw();
    
                wvpMatrix_cube = proj * view * world_cube 
                     * mat4::translation(0.0, -0.0, -20.0);
    
                context->setUniformMatrix("WORLD_VIEW_PROJECTION", wvpMatrix_cube);
                context->setUniformMatrix("WORLD", world_cube.toMat3());
                cube->draw();
            }while ( context->update() );
    

  10. Rebuild the project and run it. Figure 4.8 now shows a shapes at different location on the z axis:

    Figure 4.8. The lighting example with a cube and teapot vertically offset

    The lighting example with a cube and teapot vertically offset

Copyright © 2010 ARM. All rights reserved.ARM DUI 0527A‑02a
Non-Confidential - Draft - BetaID070710