Cocoa+OpenGL编程(一):一个简单的例子

来源:互联网 发布:儿童编程入门 编辑:程序博客网 时间:2024/06/07 07:57

 

工具:Xcode 4.2

开发环境:Cocoa,OpenGL,Objective-C

一、创建工程MyOpenGL

打开Xcode,选择Create a new Xcode project, 然后在弹出地窗口中选择 Cocoa Application, 下一步以后输入工程的名字:MyOpenGL。

成功以后,Xcode窗口如下:

 

二、加入OpenGL,GLUT库

点击Xcode窗口左边Project Navigator中的MyOpenGL,窗口中间会出现Project的信息,选择Targets中的MyOpenGL,选择右边的Build Phase, 选择Link Binary With Libraries,添加OpenGL和GLUT库。

三、设计程序界面

点击Xcode窗口左边Project Navigator中的MyOpenGL上的下拉菜单,进入MyOpenGL目录,点击MainMenu.xib。点击中间Objects栏的Window-MyOpenGL,会出现程序的主界面。

在Object library中选择OpenGL View对象,双击或者将其拖到主界面中,调整成自己想要的大小。

同样,选择Horizantal Slider对象,放到界面中。选择Text Field对象,放到界面中。

至此,可以编译运行程序了。Product-》Build / Run。

 

四、设置对象属性

点击界面中的OpenGL View对象,在右边的Inspector栏,选择Attributes Inspector,设置Depth为24Bit,选择Double Buffer。在Size Inspector栏,在Autosizing中选择自己想要的对象大小调整方式。

同样对Slider和Text Field设置Autosizing属性。

这时重新编译运行程序,就可以看到,当界面改变大小时,对象如何改变了。

 

五、添加MyOpenGLView类

File-》New File-》Objective-C Class-》输入文件名:MyOpenGLView,类选择NSObject。这时在Project Navigator中会出现两个文件,分别为头文件MyOpenGLView.h和主文件MyOpenGLView.m。编辑其内容如下:

其中size变量用来控制Teaport的大小。

复制代码
////  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


六、添加MyController类

同样,添加文件MyController,编辑其内容如下:

复制代码
////  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
复制代码

其中插座变量 *theView用于建立OpenGL View实例,textField用于设置Text Field对象显示的内容。动作doUpdate用于响应Slider的滑动。

////  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


按 Ctrl+C 复制代码

 

七、建立连接

将应用程度中的各个对象之间建立连接,使它们能够相互通信。在Xcode中,建立连接的方法很简单。

首先需要将类实例加入到Interface Builder中。点击MainMenu.xib,在Objects Library中选择Objects对象,双击。在Xcode中间的Objects栏中会出现Objects图标,在Insecptor栏中修改其Class为MyOpenGLView类。

同样,添加MyController类实例。

按住Control键,点击Slider对象,移动鼠标到My Controller类实例,在弹出的对话框中选择doUpdate动作。这样,一旦Slider被滑动,doUpdate动作就会别激活。

同样,建立从My Controller类实例到OpenGL View对象的连接,选择插座变量 theView; 建立从My Controller类实例到TextField对象的连接,选择插座变量 textField。

 

八、编译运行,完成。

当滑动Slider时,OpenGL View和Text Field会同时更新。


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

原创粉丝点击