Graphics Reference
In-Depth Information
That has the effect of rotating the camera (or rotating the entire scene relative to the camera,
depending on how you look at it) 45 degrees around the Y axis, and then a further 45
degrees around the X axis. We are now viewing the cube corner-on and can see it for what
it really is (see Figure 5.21).
Figure 5.21 The cube, displayed corner-on
Light and Shadow
It definitely looks more like a cube now, but it's difficult to make out the joins between the
faces. Core Animation can display layers in 3D, but it has no concept of lighting. If you
want to make your cube look more realistic, you'll have to apply your own shading effects.
You can do this by adjusting the different view background colors or by using images with
lighting effects pre-applied to them.
If you need to create dynamic lighting effects, you can do so by overlaying each view with
a translucent black shadow layer with varying alpha based on the view orientation. To
calculate the opacity of the shadow layer, you need to get the normal vector for each face (a
vector that points perpendicular to the surface) and then calculate the cross product
between that vector and the vector from an imaginary light source. The cross product gives
you the angle between the light source and the layer, which indicates the extent to which it
should be illuminated.
An implementation of this idea is shown in Listing 5.10. We've used the GLKit framework
to do the vector calculations (you'll need to include this framework in your project to run
the code). The CATransform3D for each face is cast to a GLKMatrix4 using some
pointer trickery, and then the 3×3 rotation matrix is extracted using the
Search WWH ::




Custom Search