异常 NSOpenGLView 设置的问题

发布于 2024-09-24 15:17:46 字数 1574 浏览 0 评论 0原文

我试图以一种不寻常的方式设置子类 NSOpenGLView ,但我遇到了一些问题。基本上,我正在编写一个程序来为我的博士学位执行生物工程模拟,我需要能够在 MacOSX 和 Unix 下编译它(我的机器是 Mac,但 sim 最终将在更强大的 Unix 机器上运行)。由于代码在接下来的一年半中会变得越来越长,我宁愿不必跟踪该程序的两个完全不同的版本。因此,我希望能够通过避免 ObjectiveC-2.0 并保持接口可选来在 Unix 下编译 ObjectiveC 代码(它主要用于在长时间模拟之前执行设置并在开发过程中监视短期模拟)。

当前版本在没有界面的情况下运行良好 - 模拟执行正确,并且该程序能够渲染 OpenGL 帧并将其导出到图像和视频文件中,没有任何问题。因为我现在在其上添加界面(现在只是一个带有 NSOpenGLView 子类和“开始”按钮的简单窗口”)(这样我就可以使用 main() 的替代版本运行代码,而无需它)我必须以一种奇怪的方式将 OpenGL“连接”在一起,因为绘图代码不在 drawRect 函数中,甚至不在子类视图中的任何位置,而是在“基本”程序中,

到目前为止我所做的是:

  1. 主程序(使用名为“Lattice”的对象)执行所有模拟和渲染,将图像和视频正确输出到文件。这还包含 NSOpenGLContext 并调用 [renderContext flashBuffer。 ];

  2. 名为 PottsView 的 NSOpenGLView 子类包含一个网格实例,它与视图一起初始化,如下所示:

    - (id)initWithFrame:(NSRect)frame {
        if(![超级 initWithFrame:frame])
            返回零;
    
        // 代码
        框架大小.宽度= WIN_WIDTH;
        框架大小.高度 = WIN_HEIGHT;
        [自行设置FrameSize:frameSize];
    
        init_genrand64(时间(0));
    
        lat = [格子分配];
    
        如果(SEED_TYPE){
            [latt initWithRandomSites];
        } 别的 {
            [latt initWithEllipse];
        }
    
        [[latt 上下文] makeCurrentContext];
    
        返回自我;
    }
    

drawRect() 为空。

  1. PottsController 是 InterfaceBuilder 中实例化的对象,它将开始按钮连接到视图。开始按钮只是告诉晶格运行多个步骤。

现在,按开始会导致模拟正确运行(即输​​出到文件和终端),但 PottsView 无法正常工作。它保持白色,但如果我 cmd+tab 部分,如果它更改为渲染帧的部分。如果我按“曝光”(F3),则相同。

我已经尝试了刷新、setNeedsDisplay 等的几种组合,但坦率地说我迷失了。今年四月之前我还没有做过任何编程,而且(据我所知)这是一种完全倒退的使用 NSOpenGLView 的方式,我已经没有想法了。我希望有人可以建议我如何使当前的设置工作或如何完全重新连接程序(同时仍然保持界面可选)。

I'm trying to set a subclassed NSOpenGLView in an unusual way and I am running into some problems. Basically, I am writing a program to perform a bioengineering simulation for my PhD and I need to be able to compile it under both MacOSX and Unix (my machine is a Mac, but the sim will eventually run on a more powerful Unix machine). Since the code will get longer and longer over the next year and a half I'd rather not have to keep track of two completely different versions of the program. So, I'm hoping to be able to compile the ObjectiveC code under Unix by avoiding ObjectiveC-2.0 and keeping the interface optional (it will mostly be there to perform setup before the long simulations and monitor things for the short ones during development).

The current version works well without the interface - the simulation is performed correctly and the program is capable of rendering OpenGL frames and exporting them into image and video files without any problems. Since I am now adding the interface (right now just a simple window with an NSOpenGLView subclass and a "start" button") on top of that (so that I can run the code with an alternate version of main() without it) I have to "wire" OpenGL together in a weird way, since the drawing code is not in the drawRect function, or even anywhere in the subclassed view, but instead in the "basic" program.

What I've done so far is this:

  1. The main program (using an object called "Lattice") performs all the simulations and rendering, correctly outputing images and video to files. This also contains the NSOpenGLContext and calls [renderContext flushBuffer];

  2. A subclass of NSOpenGLView called PottsView contains an instance of a lattice, which is initialized together with the view like this:

    - (id)initWithFrame:(NSRect)frame {
        if(![super initWithFrame:frame])
            return nil;
    
        // code
        frameSize.width = WIN_WIDTH;
        frameSize.height = WIN_HEIGHT;
        [self setFrameSize:frameSize];
    
        init_genrand64(time(0));
    
        latt = [Lattice alloc];
    
        if (SEED_TYPE) {
            [latt initWithRandomSites];
        } else {
            [latt initWithEllipse];
        }
    
        [[latt context] makeCurrentContext];
    
        return self;
    }
    

drawRect() is empty.

  1. PottsController is the object instanced in the InterfaceBuilder which connects the start button to the view. The start button simply tells the lattice to run for a number of steps.

Now, pressing start results in the simulation running correctly (i.e. output to files and terminal), but the PottsView is not working correctly. It remains white, but if I cmd+tab parts if it change to sections of a rendered frame. Same if I press Expose (F3).

I've tried several combinations of flushing, setNeedsDisplay, etc, but frankly speaking I'm lost. I haven't done any programming before this April and with this being (as far as I can tell) a completely backwards way of using NSOpenGLView I'm out of ideas. I'm hoping someone can suggest how I can make the current setup work or how to completely rewire the program (while still keeping the interface optional).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2024-10-01 15:17:46

目前尚不清楚您如何将上下文和视图“连接”在一起。你可以拥有任意数量的 openglContexts - 仅通过绘制一个不会使其内容显示在随机 NSOpenGLView 中。如果我错过了什么,请道歉。

NSOpenGLView 是 NSView 的一个相当简单的子类,用于创建上下文和像素格式。由于您已经有了这些,您可以取消 NSOpenGLView 并使用自定义 NSView 子类。

您应该查看此说明。 http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_drawing/opengl_drawing.html

要绘制到屏幕,您必须从 -drawRect 刷新图形上下文

:当 GPU 处理你的指令时会阻塞主线程,如果你有很多指令,这可能是一个问题。它也不能超过 50fps。

如果您已经将帧渲染到文件中,那么您是否可以更好地观察输出目录并在每次添加新帧时绘制图像,而不需要 opengl?

It's not clear how you think that you have 'wired' the context and the view together. You can have as many openglContexts as you like - just by drawing into one won't make it's contents show up in a random NSOpenGLView. Apologies if i have missed something.

NSOpenGLView is a fairly simple subclass of NSView that creates the context and pixel format. As you already have those you can do away with NSOpenGLView and use a custom NSView subclass.

You should look at this instruction.. http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_drawing/opengl_drawing.html

To draw to the screen you must flush the graphics context from -drawRect:

This will block the main thread while the gpu processes your instructions, this could be a problem if you have many instructions. It also can not happen more than 50fps.

If you are already rendering your frames to files woudn't you be better observing the output directory and drawing the image each time a new one is added, no opengl required?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文