- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我正在尝试创建一个 openGL View (在我的窗口中)。我正在制作一个 Cocoa 应用程序。我设法通过 Interface Builder 创建了一个,但出于教育目的,我想继续并在没有它的情况下创建一个。就在纸上。
这就是我告诉你我正在努力解决的问题。所以到目前为止我基本上做过的尝试是这样的:我创建了一个继承自 NSOpenGLView 的新类“MyOpenGLView.h/m”。我没有向它添加任何私有(private)变量或方法,只是类名。我唯一做的就是重写 initWithFrame:(在里面添加一个 self = [super initWithFrame:pixelFormat:]。)我在网上读到过,你必须先用这样的东西实例化它,然后才能使用它)。这是代码:
- (id) initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
initWithAttributes:(NSOpenGLPixelFormatAttribute[])
{
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 32,
nil
}];
self = [super initWithFrame:frameRect pixelFormat:pixelFormat];
[[self openGLContext] makeCurrentContext];
}
所以我有另一个名为“MyViewController.h/m”的类来处理我的 View ?那里有我的 MyOpenGLView *myView。在 .m 文件中,我使用类似这样的内容:
myView = [[MyOpenGLView alloc] initWithFrame:CGRectMake(0,0,100.0,100.0)];
if (!myView) { NSLog(@"ERROR"); }
当然,我会出错。
我的窗口应用程序中没有移植 openGL View 。我猜它与被调用方法的层次结构有关,但话又说回来..我不确定。你能帮我吗?
最佳答案
他们让这个工作的方式是我没有在我看来实现 init
方法。然后在我的 Controller 或应用程序委托(delegate)中。
@implementation AppDelegate
@synthesize window = _window;
@synthesize view = _view;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSRect mainDisplayRect = [[NSScreen mainScreen] frame]; // I'm going to make a full screen view.
NSOpenGLPixelFormatAttribute attr[] = {
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core, // Needed if using opengl 3.2 you can comment this line out to use the old version.
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer,
0
};
NSOpenGLPixelFormat *pix = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
self.view = [[OpenGLViewCoreProfile alloc] initWithFrame:mainDisplayRect pixelFormat:pix];
// Below shows how to make the view fullscreen. But you could just add to the contact view of any window.
self.window = [[NSWindow alloc] initWithContentRect:mainDisplayRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES];
self.window.opaque = YES;
self.window.hidesOnDeactivate = YES;
self.window.level = NSMainMenuWindowLevel + 1; // Show window above main menu.
self.window.contentView = self.view;
[self.window makeKeyAndOrderFront:self]; // Display window.
}
@end
可以在-prepareOpenGl
方法中调用-makeCurrentContext
。我在下面写的所有内容都不是必需的,但出于性能原因很好。我已经开始使用 CVDisplayLink
将帧绘制与屏幕刷新率同步,所以我的 openGLview 看起来像
// This is the callback function for the display link.
static CVReturn OpenGLViewCoreProfileCallBack(CVDisplayLinkRef displayLink,
const CVTimeStamp* now,
const CVTimeStamp* outputTime,
CVOptionFlags flagsIn,
CVOptionFlags *flagsOut,
void *displayLinkContext) {
@autoreleasepool {
OpenGLViewCoreProfile *view = (__bridge OpenGLViewCoreProfile*)displayLinkContext;
[view.openGLContext makeCurrentContext];
CGLLockContext(view.openGLContext.CGLContextObj); // This is needed because this isn't running on the main thread.
[view drawRect:view.bounds]; // Draw the scene. This doesn't need to be in the drawRect method.
CGLUnlockContext(view.openGLContext.CGLContextObj);
CGLFlushDrawable(view.openGLContext.CGLContextObj); // This does glFlush() for you.
return kCVReturnSuccess;
}
}
- (void)reshape {
[super reshape];
CGLLockContext(self.openGLContext.CGLContextObj);
... // standard opengl reshape stuff goes here.
CGLUnlockContext(self.openGLContext.CGLContextObj);
}
- (void)prepareOpenGL {
[super prepareOpenGL];
[self.openGLContext makeCurrentContext];
GLint swapInt = 1;
[self.openGLContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
CGLLockContext(self.openGLContext.CGLContextObj);
... // all opengl prep goes here
CGLUnlockContext(self.openGLContext.CGLContextObj);
// Below creates the display link and tell it what function to call when it needs to draw a frame.
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
CVDisplayLinkSetOutputCallback(self.displayLink, &OpenGLViewCoreProfileCallBack, (__bridge void *)self);
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(self.displayLink,
self.openGLContext.CGLContextObj,
self.pixelFormat.CGLPixelFormatObj);
CVDisplayLinkStart(self.displayLink);
}
关于objective-c - 不使用 Interface Builder 创建 OpenGL View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8559493/
我正在开发一个应用程序,我成功地消除了除一个错误之外的所有错误: no suitable constructor found for builder 更改为“意图” https://file.io/O
我可以在 C++Builder 6 中成功编译以下代码片段,但我无法在 RAD Studio Seattle 中编译它: unsigned long x = 50; String s = In
我有一个项目(新开始),其中 C++ Builder 没有在任何断点处停止。我已确保我处于 Debug模式(未发布),链接器->完整调试信息 = True,C++ 编译器->调试配置,C++ 编译器-
我们想在正在开发的 C++ builder XE 应用程序中绘制大型控制流程图。 这些图表将以编程方式生成并以交互方式显示给用户(用户可以滚动大流程图、选择节点等)。节点必须能够显示自定义组件(如 T
我有以下问题 午餐时 FlashBuilder.exe (BURRITO):它崩溃并创建一个错误日志文件,例如: hs_err_pid7084.log 及以下 但是当我咀嚼 FlashBuilderC
我有一个大型 Flash Builder 项目,它是更大 (.net) 解决方案的一部分。对于整个项目,我通常有一个前进的开发分支,以及一个或多个错误修复分支。考虑到 Flash Builder 不想
乘数(自动布局中约束的属性)有什么作用? 最佳答案 约束中两个值之间的关系由以下公式确定: b = am + c 其中 a 和 b 是要关联的两个值,m 是乘数,c code> 是常量。 例如,如果
我们的开发团队使用 Borland C++ Builder 6 和 CodeGear C++ Builder 2007(以及 Visual Studio)。我听到很多评论说 Builder 2007
我想阐明我对构建器模式的使用,特别是构建器类型是如何创建的。在示例中,它只是假定构建器的类型并创建它。但是,我在“ChartBuilderFactory”类中创建了一个 CreateBuilder 方
首先,我对 Java 比较陌生,所以我问的可能是微不足道的,但我在这里或其他地方找不到答案。 为简单起见,假设我有以下类层次结构: class Shape { protected Shape(
我试图在另一个 AlertDialog 中打开一个 AlertDialog,但它不起作用,知道为什么它不起作用吗? String items[] = {"Details","Edit","Delete
我有一个包含 Form1 和 Form2 的程序。如何单击按钮从 form1 打开 form2? 最佳答案 更多信息 在你的 Project.cpp 中有这个:Application->CreateF
每当我使用 C++ Builder(XE4 版,但以前的版本也这样做)在 Release模式下构建 Win32 EXE 时,它总是创建一个导出目录并为我的项目中的每个单元导出一个 Initialize
我正在尝试在我的试用版 flashbuilder 上启用设计模式,但找不到任何选项, 我已经查看了 Windows 菜单,但没有启用设计模式, 和首选项,但首选项对话框中没有 Flex 来启用其设计模
我目前正在将一个大型 RAD Studio 2010 项目迁移到 XE4。作为其中的一部分,我正在重新创建许多项目文件。我想借此机会确保我们对预编译头使用最好的机制,因为似乎有几种方法可以做到这一点。
我观看了“Interface Builder 中的新增功能” session 视频并尝试复制显示的代码,但不幸的是,当我将 View 分配给具有 @IBDesignable 的自定义类时,出现 2 个
这个问题在这里已经有了答案: Why is NotificationCompat needed? (3 个答案) 关闭 5 年前。 我看到的几乎所有 Android 通知示例代码似乎都使用了 Not
我正在使用 fcm 从我的 Android 应用程序发送通知,并且我已经实现了它要求我提供的所有库。 val topic = "highScores" // See docum
我正在尝试在Flash Builder 4.6中进行项目范围内的查找和替换,但是对我而言,如何实现这一点并不明显。 我试过Edit-> Find/Replace然后全部替换,但它仅替换当前打开的文件中
帮助我在 XCode4 中取消 fubar 界面构建器。 我在 interface-builder 中创建了一个按钮,并在 View 的代码中为它定义了一个 IBAction 方法。它运行良好。然后我
我是一名优秀的程序员,十分优秀!