Graphics Reference
In-Depth Information
GLKMatrix4GetMatrix3 function . The rotation matrix is the part of the transform that
specifies the layer's orientation, and we can use it to calculate the normal vector.
Figure 5.22 shows the result. Try tweaking the LIGHT_DIRECTION vector and
AMBIENT_LIGHT value to alter the lighting effect.
Listing 5.10 Applying Dynamic Lighting Effects to the Cube Faces
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <GLKit/GLKit.h>
#define LIGHT_DIRECTION 0 , 1 , - 0.5
#define AMBIENT_LIGHT 0.5
@interface ViewController ()
@property ( nonatomic , weak ) IBOutlet UIView *containerView;
@property ( nonatomic , strong ) IBOutletCollection ( UIView ) NSArray *faces;
@end
@implementation ViewController
- ( void )applyLightingToFace:( CALayer *)face
{
//add lighting layer
CALayer *layer = [ CALayer layer ];
layer. frame = face. bounds ;
[face addSublayer :layer];
//convert the face transform to matrix
//(GLKMatrix4 has the same structure as CATransform3D)
CATransform3D transform = face. transform ;
GLKMatrix4 matrix4 = *( GLKMatrix4 *)&transform;
GLKMatrix3 matrix3 = GLKMatrix4GetMatrix3 (matrix4);
//get face normal
GLKVector3 normal = GLKVector3Make ( 0 , 0 , 1 );
normal = GLKMatrix3MultiplyVector3 (matrix3, normal);
normal = GLKVector3Normalize (normal);
//get dot product with light direction
GLKVector3 light = GLKVector3Normalize ( GLKVector3Make (LIGHT_DIRECTION) );
float dotProduct = GLKVector3DotProduct (light, normal);
Search WWH ::




Custom Search