gpt4 book ai didi

macos - 如何让OpenGlView的init函数运行?

转载 作者:行者123 更新时间:2023-12-03 17:34:59 25 4
gpt4 key购买 nike

我正在尝试让 init 函数在我创建的 NSOpenGlView 子类版本中运行。

例如下面的代码。

- (id)initWithFrame:(NSRect)frameRect{
NSLog(@"Init function ran");
return self;
}

它应该将 init 函数运行的信​​息发送到控制台,但它没有!我尝试创建一个 init 函数和 initWithFrame:PixelFormat: 函数,看看它们是否触发了任何控制台消息,但什么也没发生。

我通过将 NSOpenGlView 拖到界面生成器中的窗口并将其类设置为我的子类版本来创建它。我的drawrect函数工作正常并绘制了一个opengl三角形,所以我知道它在那个级别上工作。

如果你好奇的话,这是我的整个类(class)

#import <Cocoa/Cocoa.h>
@interface openGLView : NSOpenGLView
- (void) drawRect:(NSRect) bounds;
@end

#import "openGLView.h"
#include <OpenGL/gl.h>

@implementation openGLView
- (id)initWithFrame:(NSRect)frameRect{
NSLog(@"Init function ran");
return self;
}
- (void) drawRect:(NSRect) bounds{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
drawAnObject();
glFlush();
NSLog(@"drawRect");
}

static void drawAnObject ()
{
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();
}

@end

我需要这样做的原因是这样我可以在初始化时返回一个新的 32 位像素格式,以便 DEPTH_TEST 真正起作用。据说你可以在界面生成器中设置它,但对我来说,我没有看到任何选项。这可能是因为它位于 ScrollView 内,但还不确定。

最佳答案

如果您在 Interface Builder 中创建了 View ,那么它不会调用 - (id)initWithFrame:(NSRect)frame,而是调用 - (id)initFromCoder:(NSCoder * )编码器

关于macos - 如何让OpenGlView的init函数运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15037929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com