简体   繁体   English

如何从Qt4传递OpenGL上下文?

[英]How do I pass an OpenGL context from Qt4?

I'm currently developing a game in the LeadWerks 2 engine and I've decided to use Qt4 as the GUI and window manager. 我目前正在使用LeadWerks 2引擎开发游戏,并且我决定使用Qt4作为GUI和窗口管理器。 What I want to do is create a Qt QGLWidget window and have the LeadWerks engine run inside of that. 我要做的是创建一个Qt QGLWidget窗口,并在其中运行LeadWerks引擎。 There is some information of building a basic QGLWidget application here . 这里有一些有关构建基本QGLWidget应用程序的信息

I'm really struggling to understand how to do this exactly. 我真的很难理解如何准确地做到这一点。 I can create a custom GL buffer within the LeadWerks engine and I believe what I need to do is give that buffer the GL context Qt created. 我可以在LeadWerks引擎中创建一个自定义的GL缓冲区,我相信我需要做的就是为该缓冲区提供Qt创建的GL上下文。 As for the LeadWerks side of things, the main thing I need to do is call a method called CreateCustomBuffer and pass it a few things: 至于LeadWerks方面,我需要做的主要事情是调用一个名为CreateCustomBuffer的方法并将其传递给一些方法:

virtual void Buffer::CreateCustom( byte* getsize, byte* makecurrent) 虚拟void Buffer :: CreateCustom(byte * getsize,byte * makecurrent)

Creates and returns a new custom buffer. 创建并返回一个新的自定义缓冲区。 GetSize (defined as void _stdcall GetSize(int* width, int* height)) and MakeCurrent (defined as void _stdcall MakeCurrent(void)) are callback functions for the buffer. GetSize(定义为void _stdcall GetSize(int * width,int * height))和MakeCurrent(定义为void _stdcall MakeCurrent(void))是缓冲区的回调函数。 GetSize should return (by changing the value provided with a pointer) the size of the custum OpenGL buffer/context used. GetSize应该返回(通过更改指针提供的值)所使用的custum OpenGL缓冲区/上下文的大小。 MakeCurrent should set the custom buffer as the current OpenGL context. MakeCurrent应将自定义缓冲区设置为当前OpenGL上下文。

and MakeCurrent (defined as void _stdcall MakeCurrent(void)) are callback functions for the buffer 和MakeCurrent(定义为void _stdcall MakeCurrent(void))是缓冲区的回调函数

If I understand it correct, this callback will be called whenever LeadWerks wants the context to become active (it doesn't manage it itself then), similar the getsize callback is to obtain the size of the available window. 如果我理解正确,那么只要LeadWerks希望上下文变为活动状态(那么它本身就不会对其进行管理), 就会调用此回调,类似于getsize回调将获取可用窗口的大小。 So normally you'd use this to activate the context from another interface you've access for. 因此,通常您会使用它从您要访问的另一个界面激活上下文。

Unfortunately those callbacks don't allow for passing a pointer, which means, you can't pass the QGLWidget class instance pointer, so that you could delegate the call to a class member function. 不幸的是,这些回调不允许传递指针,这意味着您不能传递QGLWidget类实例指针,因此可以将调用委派给类成员函数。 Not being able to pass user data to callbacks is a sign of bad API design, because it makes things hard, which would be easy otherwise. 无法将用户数据传递给回调是API设计不良的标志,因为这会使事情变得很难,否则会很容易。

There is a library called ffcall which provides a mechanism to get around this http://www.gnu.org/s/libffcall/callback.html 有一个名为ffcall的库,它提供了一种绕过此http://www.gnu.org/s/libffcall/callback.html的机制

So you'd write a delegator function 所以你要写一个委托函数

void qglwidget_makecurrent(void *data, va_list alist)
{
    GQLWidget *widget = (QGLWidget*) data;
    widget->makeCurrent();
}

void qglwidget_getsize(void *data, va_list alist)
{
    int *widthptr, *heightptr;
    GQLWidget *widget = (QGLWidget*) data;
    va_start_ptr(alist, void);
    widthptr = va_arg_ptr(alist, int*);
    heightptr = va_arg_ptr(alist, int*);
    va_return_void(alist);
    *widthptr = widget->width();
    *heightptr = widget->height();
}

create callback wrappers (as in your QGLWidget derives class' constructor) as class member variables: 创建回调包装器(如您的QGLWidget派生类的构造函数)作为类成员变量:

class MyGLWidget : QGLWidget {
/* ... */
    __TR_function qglwidget_makecurrent_callback;
    __TR_function qglwidget_getsize_callback;
}

MyGLWidget::MyGLWidget() {
    qglwidget_makecurrent_callback = alloc_callback(qglwidget_makecurrent, (void)this);
    qglwidget_getsize_callback = alloc_callback(qglwidget_makecurrent, (void*)this);
}

And those you can pass to LeadEngine: 以及您可以传递给LeadEngine的那些:

buffer->CreateCustom((void(*)(int, int))qglwidget_getsize_callback, (void(*)(void))qglwidget_makecurrent_callback);

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

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