力为的技术博客

联系 聚合 管理
  154 Posts :: 1 Stories :: 561 Comments :: 0 Trackbacks
在Mac上使用OpenGL那是非常的简单,简单几步就也可以完成窗口初始化,这都得益于Cocoa的NSOpenGLView类。

首先,创建一个Cocoa窗口程序,打开xib设计环境,点Window进入窗口设计界面
然后,在Object Library中选择OpenGL View拖到窗口上:


接下来要实现我们自己的View。
新建一个ObjectC class,名字为MyOpenGLView,修改代码:
MyOpenGLView.h文件:
#import <Cocoa/Cocoa.h>
@interface MyOpenGLView : NSOpenGLView
{

}

-(void) drawRect:(NSRect)dirtyRect;
@end


MyOpenGLView.m
#import "MyOpenGLView.h"
#import <OpenGL/gl.h>

static void drawObjects()
{
    glColor3f(1.0f, 0.85f, 0.35f);
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(  0.0,  0.6, 0.0);
        glVertex3f( -0.2, -0.3, 0.0);
        glVertex3f(  0.2, -0.3 ,0.0);
    }
    glEnd();
}

@implementation MyOpenGLView

-(void)drawRect:(NSRect)dirtyRect
{
    glClearColor(1, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    
    drawObjects();
    glFlush();
}

@end

自定义的控件类有了,在选中上面拖到窗口上的OpenGL Object,在Show the Identity Inspector设置面板中设置其Custom Class为MyOpenGLView。大功告成:


跟在Windows上使用OpenGL相比,Cocoa方便很多。
posted on 2013-10-02 18:25 力为 阅读(2801) 评论(0)  编辑 收藏 引用 所属分类: Mac&iOS

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理