具有 OpenGL 3.2 核心的 NSOpenGLView
如果这个问题是微不足道的或无意义的,我提前道歉......这是我正在开发的第一个应用程序之一。 (不幸的是)我不是开发人员。
我有一个 NSWindow,其中包含一个自定义视图,它是 NSOpenGLView 的子类。
由于 NSWindow 和 NSOpenGLView 是通过 Interface Builder 创建的,因此我不需要初始化 NSOpenGLContext 也不需要初始化NSOpenGLPixelFormat
。
我最近切换到 OS X Lion,现在想利用新的 OpenGL 3.2。
从 Apple 文档中我发现要启用 OpenGL 3.2,我只需要编写如下内容:
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
在初始化 NSOpenGLContext
之前。
这非常简单(即使对我来说也是如此),但是由于我从未 init
NSOpenGLContext
,我如何在我的应用程序中实现这一点?
由于 NSOpenGLView
是从 Interface Builder 创建的,因此不会调用 -initWithFormat:shareContext:
方法。
因此,我尝试使用如下内容覆盖 -initWithCoder:
方法:
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
[super initWithCoder:aDecoder];
[self setOpenGLContext:openGLContext];
[openGLContext makeCurrentContext];
}
并且 OpenGL 3.2 已正确加载,如 glGetString(GL_VERSION)
所报告,但创建的上下文不再交互使用该应用程序...视图中没有绘制任何内容...
我尝试了我所知道的一切...但我无法解决这个问题。
我该怎么办呢?
I apologies in advance if this question will be trivial or a nonsense...this is one of the first app that I'm developing. (Unfortunatley) I'm not a developer.
I have a NSWindow which contains a custom View which is a subclass of NSOpenGLView
.
Since the NSWindow
and the NSOpenGLView
are created through Interface Builder, I don't need to init
neither NSOpenGLContext
nor NSOpenGLPixelFormat
.
I've recently switched to OS X Lion and I now want to leverage new OpenGL 3.2.
From Apple documentation I found out that to enable OpenGL 3.2 I simply need to write something like:
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
before initializing the NSOpenGLContext
.
This is quite easy (even for me), but how can I implement this in my app since I never init
NSOpenGLContext
?
Since the NSOpenGLView
is created from Interface Builder, the method -initWithFormat:shareContext:
doesn't get called.
Therefore I tried to override the -initWithCoder:
method with something like this:
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
[super initWithCoder:aDecoder];
[self setOpenGLContext:openGLContext];
[openGLContext makeCurrentContext];
}
and OpenGL 3.2 is correctly loaded as reported by glGetString(GL_VERSION)
but the created Context is no longer interacting with the app...nothing is drawn in the view..
I tried everything I know of...but I'm not able to solve this.
How should I do instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSOpenGLView 有一个 setOpenGLContext 方法。以这种方式将 3.2 上下文传递给视图,然后在 NSOpenGLContext 上调用 setView 。
我还没有测试过这个,但它应该可以工作,尽管我刚刚制作了自己的 OpenGL 视图。
NSOpenGLView has a setOpenGLContext method. Pass your 3.2 context to the view that way, and call setView on the NSOpenGLContext afterwards.
I haven't tested this, but it should work, although I just made my own OpenGL View.
听起来你的代码是基于 OpenGL 2.1 的。 Lion中的OpenGL 3.2仅支持核心配置文件(没有兼容性配置文件),这意味着所有固定功能管道的东西(例如glBegin/End、glVertex、glPushMatrix、glMultMatrix)都消失了。
你必须经常更新你的代码。如果您已经使用 VBO,工作量将会减少。
Sounds your code is OpenGL 2.1 based. The OpenGL 3.2 in Lion only support the core profile (without compatibility profile), which means all fixed-function pipeline stuff (e.g. glBegin/End, glVertex, glPushMatrix, glMultMatrix) all have gone.
You have to update your code very much. The work will be less if you already use VBO.
在我的子类的 awakeFromNib 中使用 [setPixelFormat …] 对我来说很有效,尽管这让我感到不舒服,因为文档中没有任何内容承诺它应该这样做。
此覆盖来自 Interface Builder 的任何设置。我看不到一种未弃用的方法来检查像素格式以将我需要的像素格式添加到 Interface Builder 中选择的像素格式中。
希望 IB 人员将其添加到那里,或者更好的是 NSOpenGLView 类将萌生一种子类机制
Using [setPixelFormat …] in my subclass's awakeFromNib is working for me, though it makes me uncomfortable because nothing in the docs promises it should.
This override's any settings from Interface Builder. I can't see a non-deprecated way to inspect a pixel format to add the ones I need to the ones selected in Interface Builder.
Hopefully the IB folks will add it there, or better yet the NSOpenGLView class will sprout a mechanism for subclasses to f