Cocoa + OpenGL programming (1): A simple example

Source: Internet
Author: User

 

Tool: xcode 4.2

Development Environment: cocoa, OpenGL, objective-C

1. Create a project myopengl

Open xcode, select create a new xcode project, select cocoa application in the pop-up window, and enter the project name myopengl after the next step.

The xcode window is as follows:

 

2. Add OpenGL and glut Libraries

Click myopengl in project navigator on the left of the xcode window. Project information is displayed in the middle of the window. Select myopengl in targets, select build phase on the right, select link binary with libraries, and add OpenGL and glut libraries.

3. Design the Program Interface

Click the drop-down menu on myopengl in project navigator on the left of the xcode window to enter the myopengl directory, and click mainmenu. XIB. Click window-myopengl in the middle objects column to display the main interface of the program.

Select the OpenGL view object in the object library, double-click it, or drag it to the main interface to adjust it to the desired size.

Similarly, select the horizantal slider object and place it on the interface. Select the text field object and place it in the interface.

Now, you can compile and run the program. Product-> build/run.

 

Iv. Set Object Attributes

Click the OpenGL view object in the interface. In the Inspector column on the right, select attributes inspector, Set depth to 24bit, and select double buffer. In the size Inspector column, select the desired object size adjustment method in autosizing.

Set the autosizing attribute for slider and text field.

Then re-compile and run the program. You can see how the object is changed when the interface is changed.

 

5. Add the myopenglview class

File-new file-objective-C Class-input file name: myopenglview, class select nsobject. At this time, two files will appear in the project navigator, namely the header file myopenglview. h and the main file myopenglview. M. Edit the content as follows:

The size variable is used to control the teaport size.

////  MyOpenGLView.h//  MyOpenGL////  Created by liyropt on 7/25/12.//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>#import <GLUT/GLUT.h>@interface MyOpenGLView : NSOpenGLView{    float size;                        }- (void)setSize:(float) s;@end
////  MyOpenGLView.m//  MyOpenGL////  Created by liyropt on 7/25/12.//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import "MyOpenGLView.h"@implementation MyOpenGLView- (id)initWithCoder:(NSCoder *)c{    self = [super initWithCoder:c];    NSLog(@"prepare");        [self setSize:0.5];        // The GL Context must be active before you can use OpenGL functions    NSOpenGLContext *glcontext = [self openGLContext];    [glcontext makeCurrentContext];        //Add your OpenGL init code here    GLfloat mat_specular []={0.9, 0.3, 0.2, 1.0};    GLfloat mat_shininess []={50.0};    GLfloat light_position []={2.0, 4.0, -1.0, 0.0};    float white_light []={1.0, 1.0, 1.0, 1.0};    GLfloat lmodel_ambient []={0.1, 0.5, 0.8, 1.0};    glClearColor(0.0, 0.0, 0.0, 0.0);    glShadeModel(GL_SMOOTH);    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);    glLightfv(GL_LIGHT0, GL_POSITION, light_position);    glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);    glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);        glEnable(GL_LIGHTING);    glEnable(GL_LIGHT0);    glEnable(GL_DEPTH_TEST);        glMatrixMode(GL_PROJECTION);    glLoadIdentity();    //    gluPerspective(90.0, 0.7, 0.0, 10.0);    glRotatef(30, 0.0, 1.0, 0.0);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();        return self;}- (void)reshape{    NSLog(@"reshaping %f", size);        //Get view dimensions    NSRect baseRect = [self convertRectToBase:[self bounds]];    int w, h;    w = baseRect.size.width;    h = baseRect.size.height;        //Add your OpenGL resize code here    glViewport(0, 0, (GLsizei)w, (GLsizei)h);}- (void)drawRect:(NSRect)r{    //Add your OpenGL drawing code here        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glColor3f(1.0, 0.0, 0.0);    glutSolidTeapot(size);    glutWireCube(size*2.0);        //Signal that drawing is done - causes a buffer swap    [[self openGLContext] flushBuffer];    }- (void)setSize:(float)s{    size=s;}@end

6. Add the mycontroller class

Similarly, add the file mycontroller and edit the file as follows:

////  MyController.h//  MyOpenGL////  Created by liyropt on 7/25/12.//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>#import "MyOpenGLView.h"@interface MyController : NSObject {    IBOutlet MyOpenGLView *theView;    IBOutlet id textField;}- (IBAction)doUpdate:(id)sender;@end

The socket variable * theview is used to create an OpenGL view instance, and textfield is used to set the content displayed by the text field object. Action doupdate is used to respond to slider sliding.

////  MyController.m//  MyOpenGL////  Created by liyropt on 7/25/12.//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import "MyController.h"@implementation MyController- (IBAction)doUpdate:(id)sender{    NSLog(@"%f\n", [sender floatValue]);    [theView setSize:[sender floatValue]];    [theView setNeedsDisplay:YES];        [textField setFloatValue:[sender floatValue]];}@end
Press Ctrl + C to copy the code

 

7. Establish a connection

Establish connections between objects in the application level so that they can communicate with each other. In xcode, the method for establishing a connection is simple.

First, you must add the class instance to interface builder. Click mainmenu. XIB, select the objects object in the Objects library, and double-click. The objects icon appears in the objects column in xcode. In the insecptor column, change the class to the myopenglview class.

Similarly, add a mycontroller class instance.

Press and hold the control key, click the slider object, move the mouse to the my controller class instance, and select the doupdate action in the displayed dialog box. In this way, the doupdate action will not be activated once the slider is slide.

Similarly, establish a connection from the my controller class instance to the OpenGL view object, select the socket variable theview, establish a connection from the my controller class instance to the textfield object, and select the socket variable textfield.

 

8. Compile and run.

When slider is swiped, OpenGL view and text field are updated at the same time.

From: http://www.cnblogs.com/liyropt/archive/2012/07/25/2608794.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.