如何创建一个类来包装 GLUT?

发布于 2024-08-19 05:16:13 字数 731 浏览 2 评论 0原文

我正在用 OpenGL 为学校编写一个游戏。由于还会有更多类似的作业,我想制作一个小型框架来在 OpenGL 中完成常见的事情。我之前制作过一些简单的游戏,通常将其分解为 IO 类来处理输入和绘制到屏幕,Game 类用于主游戏循环/逻辑,以及游戏中任何对象的类。

在我使用 SDL 之前,我的问题是,这是在 OpenGL 中处理此问题的正确方法吗?我已经遇到了一些麻烦。我希望我的 IO 类能够处理初始化窗口、绘制场景和鼠标单击。所以构造函数看起来像这样:

IO::IO()
{
    currWindowSize[0] = DEF_WIDTH;
    currWindowSize[1] = DEF_HEIGHT;

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowPosition( INIT_WINDOW_POSITION[0], INIT_WINDOW_POSITION[1] );
    glutInitWindowSize( currWindowSize[0], currWindowSize[1] );
    glutCreateWindow( "TEST" );

    setUp();

    glutDisplayFunc(drawScene);
    glutMainLoop();
}

但是,drawScene 是一个类方法。有没有办法将类方法传递给 glutDisplayFunc() 而不将其设为静态?

I'm writing a game for school in OpenGL. Since there will be several more similar assignments I want to make a small framework for doing common things in OpenGL. I have made a few simple games before and I usually break it down into an IO class to handle input and drawing to the screen, Game class for the main game loop/logic, and classes for whatever objects there are in the game.

Before I was using SDL, so my question is, is this the right way of going about this in OpenGL? I've already ran into some trouble. I want my IO class to handle initializing the window, drawing the scene, and mouse clicks. So the constructor looked like this:

IO::IO()
{
    currWindowSize[0] = DEF_WIDTH;
    currWindowSize[1] = DEF_HEIGHT;

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowPosition( INIT_WINDOW_POSITION[0], INIT_WINDOW_POSITION[1] );
    glutInitWindowSize( currWindowSize[0], currWindowSize[1] );
    glutCreateWindow( "TEST" );

    setUp();

    glutDisplayFunc(drawScene);
    glutMainLoop();
}

However, drawScene is a class method. Is there a way to pass a class method to glutDisplayFunc() without making it static?

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

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

发布评论

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

评论(3

忘你却要生生世世 2024-08-26 05:16:13

不幸的是,glutDisplayFunc() 不接受 void* 指针,因此您可以伪造一个对象上下文。您必须创建一个静态函数,该函数可以使用静态变量调用正确的 IO 实例。

不过,我也发现你的模式有一些小问题。据我所知,在终止 GLUT 上下文之前, glutMainLoop() 永远不会返回,因此您有一个几乎永远不会返回的构造函数,因此您的程序流程是不合理的。您应该将该调用移至类中单独的 run() 方法中。

(我个人会使用 GLFW,它可以避免 GLUT 造成的整个回调混乱,尽管你必须编写主循环。)

Unfortunately the glutDisplayFunc() doesn't take a void* pointer so you could've fake an object context. You will have to make a static function that can call into the correct IO instance using a static variable.

I see some slight trouble with your pattern also, though. As far as I know, glutMainLoop() never returns until you terminate the GLUT context, therefore you have a constructor that practically never returns, so your program flow is unreasonable. You should move that call into a separate run() method in your class.

(Personally I would use GLFW, which avoids the entire callback mess with GLUT, although you have to write your mainloop.)

荒芜了季节 2024-08-26 05:16:13

两点,

第一, glutMainLoop 永远不会返回,并开始发送消息。因为您从构造函数中调用了它,所以您的类可能尚未正确构造。另外 glut 确实支持多个窗口,并且 glutModalLoop 只需要调用一次。

其次,正如您所注意到的,传递给 glutDisplayFunc 的方法必须是静态的。您将需要一个静态数组,将过剩窗口 ID 与 IO 指针关联起来。当 glutCreateWindow 被 IO 实例调用时,将返回的 id 和 IO 实例存储在数组中。每当调用静态drawScene时,您都会调用 glutGetWindow 来获取当前窗口的id,并查找关联的IO指针并调用非静态drawScene实现。

Two points,

First, glutMainLoop never returns, and starts dispatching messages. Because you called it from your constructor your class is potentially not properly constructed yet. Additionally glut does support more than one window, and glutModalLoop only needs to be called once.

Second, as you have noted, the method passed to glutDisplayFunc must be static. You will need a static array associating glut window id's with IO pointers. When glutCreateWindow is called by an IO instance, store the returned id and IO instance in the array. Whenever the static drawScene is called you would call glutGetWindow to get the id of the current window, and look up the associated IO pointer and call the non static drawScene implementation.

紅太極 2024-08-26 05:16:13

在我使用 SDL 之前,所以我的问题
是,这是正确的方法吗
关于 OpenGL 中的这个吗?

我宁愿完全绕过 GLUT(如果您想了解 OpenGL 的内部结构和您的操作系统窗口系统界面)。另外,iPhone 不支持 GLUT(如果您将来想瞄准此平台)。

因此,我们的想法是在 Windows 上使用 WGL,在 Linux 上使用 GLX,在 OS X 上使用 CGL

Before I was using SDL, so my question
is, is this the right way of going
about this in OpenGL?

I'd rather bypass GLUT completely (if you want to learn the inner guts of OpenGL and your OS windowing system interface). Plus GLUT is not supported on the iPhone (if you want to target this platform in the future).

So the idea would be to use WGL on Windows, GLX on Linux and CGL on OS X.

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