简体   繁体   English

我如何一起使用GTK和过剩?

[英]How do I use GTK and glut together?

I know that in order to write a GTK application, I write a bunch of code which describes what is put in the main window, then I call: 我知道为了编写一个GTK应用程序,我写了一堆代码来描述主窗口中的内容,然后我调用:

gtk_main();

Any code statements after this do not get executed. 此后的任何代码语句都不会被执行。

Now let's suppose I'd like my GTK app to display something I wrote with glut, which itself contains a bunch of statements about what graphics need to be set etc. then ends with the statement: 现在让我们假设我喜欢我的GTK应用程序来显示我用glut编写的东西,它本身包含一堆关于需要设置图形的语句等等,然后以语句结束:

glutMainLoop();

Anything after this is not executed. 此后的任何内容都不会执行。

So my problem is that either of these two statements prevents me from calling the other. 所以我的问题是这两个语句中的任何一个都阻止我调用另一个。

  1. Is there a way to execute a glut main loop inside a GTK widget ? 有没有办法在GTK小部件中执行过剩主循环?
  2. Is there a way to write a code that could somehow simultaneously call both a GTK main loop and a glut main loop (but called from the main program and rendered in a separate X window, not within a widget)? 有没有办法编写一个代码,可以某种方式同时调用GTK主循环和glut主循环(但从主程序调用并在单独的X窗​​口中呈现,而不是在小部件内)? I've got a feeling this could be done with "threads"... 我有一种感觉,这可以通过“线程”来完成......

You don't. 你没有。 There's generally no point to it. 通常没有意义。

GLUT is a library for creating and managing OpenGL windows. GLUT是一个用于创建和管理OpenGL窗口的库。 GTK already has an OpenGL window in it. GTK已经一个OpenGL窗口。 If you're using GTK, then there's no point in using GLUT. 如果你正在使用GTK,那么使用GLUT是没有意义的。 It's like having two vector math libraries or something. 这就像有两个矢量数学库或其他东西。

You are running the main loops. 您正在运行主循环。 gtk_main() runs until gtk_quit() is called. gtk_main()运行直到调用gtk_quit()

gtk_main() at GTK.org GTK.org上的gtk_main()

Runs the main loop until gtk_main_quit() is called. 运行主循环,直到调用gtk_main_quit() You can nest calls to gtk_main(). 您可以将调用嵌套到gtk_main()。 In that case gtk_main_quit() will make the innermost invocation of the main loop return. 在这种情况下,gtk_main_quit()将使主循环的最内部调用返回。

Also, glutMainLoop() works the same way, it processes GL events forever. 此外, glutMainLoop()以相同的方式工作,它永远处理GL事件。

glutMainLoop() at OpenGL.org 在OpenGL.org上的glutMainLoop()

glutMainLoop() enters the GLUT event processing loop. glutMainLoop()进入GLUT事件处理循环。 This routine should be called at most once in a GLUT program. 在GLUT程序中,该程序最多应调用一次。 Once called, this routine will never return. 一旦被调用,该例程将永远不会返回。 It will call as necessary any callbacks that have been registered. 它将根据需要调用已注册的任何回调。

So, you you wan't both of these things to execute at the same time (I think they might interfere with each other so you might get unexpected results) then you will need to call gtk_main_iteration() from inside glut. 所以,你们要同时执行这些事情(我认为它们可能会相互干扰,因此你们可能会得到意想不到的结果)然后你需要从内部调用gtk_main_iteration()

gtk_main_iteration() at GTK.org GTK.org上的gtk_main_iteration()

Runs a single iteration of the mainloop. 运行mainloop的单次迭代。 If no events are waiting to be processed GTK+ will block until the next event is noticed. 如果没有等待处理的事件,GTK +将阻塞,直到注意到下一个事件。 If you don't want to block look at gtk_main_iteration_do() or check if any events are pending with gtk_events_pending() first. 如果您不想阻止查看gtk_main_iteration_do()或先检查gtk_events_pending()是否有任何事件待处理。

Now.. GLUT doesn't have an equivalent to gtk_main_iteration() so you are going to need to register GLUT callbacks. 现在.. GLUT没有等效的gtk_main_iteration()所以你需要注册GLUT回调。

You could register a callback with GLUT that runs gtk_main_iteration() using glutIdleFunc(void (*func)(void)) which will run a callback for every frame - glutIdleFunc() .. 您可以使用glutIdleFunc(void (*func)(void))运行gtk_main_iteration()运行glutIdleFunc(void (*func)(void)) GLUT注册一个回调,它将为每个帧运行一个回调 - glutIdleFunc() ..

Or you could give a callback to glutTimerFunc(unsigned int msecs, void (*func)(int value), value) to call and check the return value of gtk_main_iteration() every 200msec or so. 或者你可以回调glutTimerFunc(unsigned int msecs, void (*func)(int value), value)来调用并每隔200毫秒左右检查一次gtk_main_iteration()的返回值。

I'd probably experiment with both, glutIdleFunc() might not always get called regularly enough for good responsiveness. 我可能会尝试两者, glutIdleFunc()可能并不总是被定期调用以获得良好的响应能力。

It really is worth looking at driving GTK's GL support though. 尽管如此,真的值得看看GTK的GL支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM