Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [133]

By Root 816 0
drawing context must be active for any OpenGL drawing commands to have an effect.

Here are some important methods in NSOpenGLView:

- (id)initWithFrame:(NSRect)frameRect

pixelFormat:(NSOpenGLPixelFormat *)format

The designated initializer.

- (NSOpenGLContext*)openGLContext

Returns the views in the OpenGL context.

- (void)reshape

Called when the view is resized. The OpenGL context is active when this method is called.

- (void)drawRect:(NSRect)r

Called when the view needs to be redrawn. The OpenGL context is active when this method is called.

A Simple Cocoa/OpenGL Application


Figure 35.1 shows the application that you will create.

Figure 35.1. Completed Application

Create a new Cocoa Application project and call it Gliss (short for “GL Bliss”). Set the Class Prefix to Gliss. This will not be a document-based application. Open the project editor by clicking on the project in the project navigator. Use the + button under Linked Frameworks and Libraries to add the frameworks OpenGL.framework and GLUT.framework to the project. You will not be using the GLUT event model—just a couple of convenience functions.

Create a new Objective-C class subclassing NSOpenGLView, and name it GlissView. In GlissView.h, declare an outlet and an action:

#import

@interface GlissView : NSOpenGLView {

IBOutlet NSMatrix *sliderMatrix;

}

- (IBAction)changeParameter:(id)sender;

@end

Lay Out the Interface


Open MainMenu.xib.

Drag an NSOpenGLView onto the window as shown in Figure 35.2.

Figure 35.2. Drop an NSOpenGLView onto the Window

In the Identity Inspector, set the class of the view to be GlissView (Figure 35.3).

Figure 35.3. Set the Class

Select the Gliss window. In the Attributes Inspector, under the Memory section, uncheck One Shot.

Drop an NSSlider onto the window. Configure the slider to be continuous. In the Editor menu, choose Embed -> Matrix. In the Attributes Inspector, set the matrix mode to Tracking and give it three columns (Figure 35.4).

Figure 35.4. Matrix of Sliders

Set the target of the matrix to be the GlissView, and set the action to be changeParameter:. Set the sliderMatrix outlet of the GlissView to point to the matrix. (Be sure to create connections in both directions.)

The first slider will control the X-coordinate of the light. Set its range from –4 to 4, and give it an initial value of 1. It should have a tag of 0. The Inspector should look like Figure 35.5.

Figure 35.5. Set Limit, Initial Value, and Tag for First Slider Cell

The second slider will control the angle from which the scene is viewed. Set its range from –4 to 4, and give it an initial value of 0. It should have a tag of 1.

The third slider will control from how far the scene is viewed. Set its range from 0.3 to 5, and give it an initial value of 4. It should have a tag of 2.

Select the GlissView. In the Attributes Inspector, set the view to have a 16-bit depth buffer, as shown in Figure 35.6.

Figure 35.6. Create a 16-Bit Depth Buffer

Also, in the Size Inspector, make the GlissView resize with the window.

Inspect the NSMatrix. Set it to autosize its cells. In the Size Inspector, make the matrix cling to the right edge of the window, as shown in Figure 35.7.

Figure 35.7. Matrix Size Inspector

Write Code


Edit GlissView.h as follows:

#import

@interface GlissView : NSOpenGLView {

IBOutlet NSMatrix *sliderMatrix;

float lightX, theta, radius;

int displayList;

}

- (IBAction)changeParameter:(id)sender;

@end

Next, edit GlissView.m:

#import "GlissView.h"

#import

#define LIGHT_X_TAG 0

#define THETA_TAG 1

#define RADIUS_TAG 2

@implementation GlissView

- (void)prepare

{

NSLog(@"prepare");

// The GL context must be active for these functions to have an effect

NSOpenGLContext *glcontext = [self openGLContext];

[glcontext makeCurrentContext];

// Configure the view

glShadeModel(GL_SMOOTH);

glEnable(GL_LIGHTING);

glEnable(GL_DEPTH_TEST);

//

Return Main Page Previous Page Next Page

®Online Book Reader