如何从 Qt4 传递 OpenGL 上下文? [英] How do I pass an OpenGL context from Qt4?

查看:83
本文介绍了如何从 Qt4 传递 OpenGL 上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 LeadWerks 2 引擎开发游戏,我决定使用 Qt4 作为 GUI 和窗口管理器.我想要做的是创建一个 Qt QGLWidget 窗口,并在其中运行 LeadWerks 引擎.有一些关于构建基本 QGLWidget 应用程序的信息这里.

I'm currently developing a game in the LeadWerks 2 engine and I've decided to use Qt4 as the GUI and window manager. What I want to do is create a Qt QGLWidget window and have the LeadWerks engine run inside of that. There is some information of building a basic QGLWidget application here.

我真的很难理解如何准确地做到这一点.我可以在 LeadWerks 引擎中创建一个自定义的 GL 缓冲区,我相信我需要做的是为该缓冲区提供 Qt 创建的 GL 上下文.至于 LeadWerks 方面,我需要做的主要事情是调用一个名为 CreateCustomBuffer 并传递一些东西:

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. 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:

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

创建并返回一个新的自定义缓冲区.GetSize(定义为 void _stdcall GetSize(int* width, int* height))和 MakeCurrent(定义为 void _stdcall MakeCurrent(void))是缓冲区的回调函数.GetSize 应返回(通过更改指针提供的值)使用的自定义 OpenGL 缓冲区/上下文的大小.MakeCurrent 应将自定义缓冲区设置为当前 OpenGL 上下文.

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 should return (by changing the value provided with a pointer) the size of the custum OpenGL buffer/context used. MakeCurrent should set the custom buffer as the current OpenGL context.

推荐答案

和MakeCurrent(定义为void _stdcall MakeCurrent(void))是缓冲区的回调函数

and MakeCurrent (defined as void _stdcall MakeCurrent(void)) are callback functions for the buffer

如果我理解正确,每当 LeadWerks 希望上下文变为活动状态时,就会调用此回调(然后它自己不管理它),类似于 getsize 回调是获取上下文的大小可用的窗口.所以通常你会使用它来从你可以访问的另一个界面激活上下文.

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. So normally you'd use this to activate the context from another interface you've access for.

不幸的是,这些回调不允许传递指针,这意味着您不能传递 QGLWidget 类实例指针,因此您可以将调用委托给类成员函数.无法将用户数据传递给回调是 API 设计糟糕的标志,因为这会让事情变得困难,否则会很容易.

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. 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.

有一个名为 ffcall 的库,它提供了一种机制来解决这个问题 http://www.gnu.org/s/libffcall/callback.html

There is a library called ffcall which provides a mechanism to get around this 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();
}

创建回调包装器(如在您的 QGLWidget 派生类的构造函数中)作为类成员变量:

create callback wrappers (as in your QGLWidget derives class' constructor) as class member variables:

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);
}

那些你可以传递给 LeadEngine 的:

And those you can pass to LeadEngine:

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

这篇关于如何从 Qt4 传递 OpenGL 上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆