1

当我使用 NSOpenGLView 创建基于文档的 Xcode 项目时,我遇到了一个奇怪的问题。该应用程序适用于单个文档,响应鼠标点击。当您选择文件/新建打开第二个文档时,顶部会出现一个新的前窗口,但鼠标单击会发送到前一个(背景)对象。您可以移动新窗口,点击仍会发送到前一个对象。但是,如果您调整新窗口的大小或在前一个窗口中来回单击,则一切正常。所以问题似乎是新的(前)窗口没有成为活动上下文。你能提出一个解决方案吗?我已将 Xcode 4.4 代码 (44kb) 放在http://www.mccauslandcenter.sc.edu/CRNL/sw/GLdoc.zip

  1. 创建一个新的基于文档的应用程序
  2. 将 MyOpenGLView.m 和 MyOpenGLView.h 文件添加到项目中
  3. 对于 Targets/BuildPhases,将 OpenGL.framework 添加到“Link Binary With Libraries”
  4. 对于 Targets/BuildPhases,将 MyOpenGLView.m 添加到“Compile Sources”
  5. 在界面生成器中 a.) 选择 Document's Window 并在属性检查器中关闭“One shot” b.) 从对象库中添加一个 OpenGL 视图 c.) 使用身份检查器,将新创建的 NSOpenGLView 的类设置为 MyOpenGLView
  6. 运行项目

----开始我的OpenGLView.h

#import <Cocoa/Cocoa.h>
#import <GLUT/GLUT.h>

@interface MyOpenGLView : NSOpenGLView
{
    NSTimer* timer; //animation timer
    NSPoint mouseloc;
    NSPoint screenSize;
    int hourglassSize;
    BOOL updateGL;
}

@end

----START MyOpenGLView.m

// document based OpenGL application inspired by http://www.alecjacobson.com/weblog/?p=2110
#import "MyOpenGLView.h"

@implementation MyOpenGLView

-(void)prepareOpenGL
{
    //NSLog(@"preparing");
}

- (void)reshape
{
    //NSLog(@"reshaping");
    NSRect rectView = [self bounds];
    screenSize.x = rectView.size.width;
    screenSize.y = rectView.size.height;
}

void enter2D (int width, int height) //Enter2D = reshapeGL
{
    glDisable(GL_DEPTH_TEST);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height,-1,1); //map view to match pixel size, e.g. gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable (GL_BLEND); //allow transparent objects
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void drawTriangle (int sz, int left, int bottom)
{
    glLoadIdentity ();
    glTranslatef(left,bottom,0.0f);
    glBegin(GL_TRIANGLES);
    glVertex3f( sz/2, sz, 0.0f);// Top
    glVertex3f(0.0,0.0, 0.0); // Bottom Left
    glVertex3f( sz,0.0f, 0.0f);  // Bottom Right
    glEnd(); // Finished Drawing The Triangle
}

void drawHourGlass (int sz, NSPoint center)
{
    glLoadIdentity ();
    glTranslatef(center.x,center.y,0.0f);
    glBegin(GL_TRIANGLES); //lower triangle
    glVertex3f( 0.0, 0.0, 0.0f); // Top
    glVertex3f(-sz,-sz, 0.0);// Bottom Left
    glVertex3f( sz,-sz, 0.0f);// Bottom Right
    glEnd(); 
    glBegin(GL_TRIANGLES); //upper triangle
    glVertex3f( 0.0, 0.0, 0.0f); // bottom
    glVertex3f(-sz,sz, 0.0);// Top Left
    glVertex3f( sz,sz, 0.0f);// Top Right
    glEnd();
}

-(void)drawRect:(NSRect)rect
{
    updateGL = FALSE;
    //NSLog(@" %f %f",rect.size.width,rect.size.height);
    enter2D(screenSize.x,screenSize.y);
    glClearColor(0.8,0.9,1,1); //background - blue sky
    glClear(GL_COLOR_BUFFER_BIT);
    glColor4f(0.6,0.3,0.1,1.0); //mountains - brown opaque
    drawTriangle(100,10,0);
    drawTriangle(70,80,0);
    glColor4f(0.1,0.1,0.1,0.7); //crosshair - gray, slightly translucent
    drawHourGlass (hourglassSize, mouseloc);
    glFlush();     // Flush all OpenGL calls
}

- (void)mouseDown:(NSEvent *)event
{
    mouseloc = [self convertPoint:[event locationInWindow] fromView:nil];
    updateGL = TRUE;
}

int constrain (int size)
{
    if (size < 5)
        return 5;
    else if (size > 50)
        return 50;
    else
        return size;
}

- (void) magnifyWithEvent:(NSEvent *)event;
{
    if ([event magnification] > 0)
        hourglassSize = hourglassSize + 5;
    else
        hourglassSize = hourglassSize - 5;
    hourglassSize = constrain(hourglassSize);
    updateGL = TRUE;
}

- (void)scrollWheel:(NSEvent *)event
{
    if (event.deltaY < 0)
       hourglassSize = hourglassSize + 5;
    if (event.deltaY > 0)
        hourglassSize = hourglassSize - 5;
    hourglassSize = constrain(hourglassSize);
    updateGL = TRUE;
}

- (void)animationTimer:(NSTimer *)timer
{
    if (updateGL == TRUE)
        [self drawRect:[self bounds]];
}

-(BOOL)acceptsFirstResponder { return YES; }
-(BOOL)becomeFirstResponder { return YES; }
-(BOOL)resignFirstResponder { return YES; }

- (void) awakeFromNib
{
    // [[self window] setAcceptsMouseMovedEvents:YES];
    mouseloc.x = 30;
    mouseloc.y = 40;
    hourglassSize = 30;
    timer = [NSTimer timerWithTimeInterval:(1.0f/60.0f) target:self selector:@selector(animationTimer:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    // ensure timer fires during resize
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
    updateGL = TRUE;
}

@end
4

1 回答 1

1

对于遇到此问题的其他人(在 Apple 分发的一些较旧的演示中存在),对于现代版本的 OSX(10.4 及更高版本),您使用 CVDisplayLink 而不是计时器来更新 OpenGL 视图。

http://developer.apple.com/library/mac/#qa/qa1385/_index.html

亚历克雅各布森在这里概述了这个解决方案 http://www.alecjacobson.com/weblog/?p=2185

于 2012-09-28T20:23:09.010 回答