Chapter 5, Color, Materials and Lighting: The Basics

The first program in chapter 5 is LitJet, which draws a very simple airplane constructed from a small number of triangles. So the user could rotate the jet to any arbitrary orientation, I added sliders to set the roll, pitch and yaw angles. A horizontal slider sets the yaw angle, a vertical slider sets the pitch angle, and a circular slider sets the roll angle.

The LitJetAppDelegate class took care of monitoring the sliders and passing the angles to JetView, the NSOpenGLView subclass. Here’s the app delegate header file:

//
//  LitJetAppDelegate.h
//  LitJet
//
//  Created by Norm Hecht on 9/9/10.
//  Copyright 2010 Scamp Dog Software. All rights reserved.
//

#import 
@class JetView;

@interface LitJetAppDelegate : NSObject  {
    NSWindow *window;
	JetView *jetView;
	NSSlider *pitchSlider;
	NSSlider *yawSlider;
	NSSlider *rollSlider;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet JetView *jetView;
@property (assign) IBOutlet NSSlider *pitchSlider;
@property (assign) IBOutlet NSSlider *yawSlider;
@property (assign) IBOutlet NSSlider *rollSlider;

-(IBAction)changeRotation:(id)sender;

@end

Here’s the implementation file:

//
//  LitJetAppDelegate.m
//  LitJet
//
//  Created by Norm Hecht on 9/9/10.
//  Copyright 2010 Scamp Dog Software. All rights reserved.
//

#import "LitJetAppDelegate.h"
#import "JetView.h"

@implementation LitJetAppDelegate

@synthesize window;
@synthesize jetView;
@synthesize pitchSlider;
@synthesize yawSlider;
@synthesize rollSlider;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	// Insert code here to initialize your application 
}

-(IBAction)changeRotation:(id)sender {
	GLfloat yAngle, pAngle, rAngle;
	yAngle = [yawSlider floatValue];
	pAngle = [pitchSlider floatValue];
	rAngle = [rollSlider floatValue];
	[jetView setYawRotation:yAngle];
	[jetView setPitchRotation:pAngle];
	[jetView setRollRotation:rAngle];
	NSLog(@"%s: angles are %f, %f, %f", __PRETTY_FUNCTION__, 
		  yAngle, pAngle, rAngle);

	[jetView setNeedsDisplay:YES];
}

@end

The chapter’s second program is ShinyJet, which adds specular reflection and shininess to the airplane model. Instead of creating a separate program, I just added that feature to my LitJet.

Advertisements
This entry was posted in OpenGL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s