在lua中为luaglut glReadPixels创建lightuserdata [英] create lightuserdata in lua for luaglut glReadPixels

查看:106
本文介绍了在lua中为luaglut glReadPixels创建lightuserdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用luaglut,当我尝试使用glReadPixels捕获帧时,我无法为其成功准备最后一个输入参数.

I'm using luaglut and when I try to use the glReadPixels to capture a frame, I cannot prepare the last input argument for it successfully.

这就是我调用该函数的方式:

This is how I call the function:

glReadPixels(0, 0, 250, 250, GL_RGB, GL_UNSIGNED_BYTE, img)

img是我要存储框架的位置,但是无论我定义img的类型如何,总是会出错.编译器始终需要 lightuserdata ,但是我已经搜索了好几天,而且似乎无法在lua中创建此类数据.

The img is where I want to store the frame, but no matter in which type I define img, there's always error. The compiler always wants lightuserdata, but I've searching for several days and seems there is no way to create this type of data within lua.

我应该如何处理这个问题?预先谢谢你!

How should I deal with this problem? Thank you in advance!

推荐答案

第一次更新: 所以我设法使lightuserdata进入lua,但是现在我遇到了Segmentation fault (core dumped)问题.下面是我的代码现在的样子:

1st Update: So I managed to get lightuserdata into lua, but now I get Segmentation fault (core dumped) problem. Below is how my code looks right now:

//"cpp_test.cpp"

extern "C" {
    #include <lua5.1/lua.h>
    #include <lua5.1/lualib.h>
    #include <lua5.1/lauxlib.h>
}

int getLightUserData_func_cpp(lua_State* L){
    int nargc = lua_gettop(L);
    if (0 != nargc){
        luaL_error(L, "Wrong number of arguments.");
        return 0;
    }
    static char var_name = 'a';
    lua_pushlightuserdata(L, (void*)&var_name);
    return 1;
}

static const struct luaL_reg lib_cpp[] = {
    {"getLightUserData_func_lua", getLightUserData_func_cpp},
    {NULL, NULL}
};

extern "C"
int luaopen_test(lua_State *L){
    luaL_register(L, "lib_lua", lib_cpp);
    return 1;
}

Makefile:

g++ -o test.so ./cpp_test.cpp  -I/usr/include/lua5.1 -llua5.1 -shared -fpic

然后在lua中使用test.so

require "luagl"
require "luaglut"
require "memarray"

require "test"

print(lib_lua)
a = lib_lua.getLightUserData_func_lua()
glReadPixels(0, 0, 250, 250, GL_RGB, GL_UNSIGNED_BYTE, a)
print(type(a))
print(a)

这是我得到的输出:

{
  getLightUserData_func_lua : function: 0x41ecbf10
}
userdata    
userdata: 0x7fe73297f050

所以它似乎可以工作.但是当我在其他代码中require("test")时(例如,我只在此处放置了相关部分):

So it seems to work. But when I require("test") in my other code, like (I only put the relevant part here):

-- "main.lua"
require "luagl"
require "luaglut"
require "memarray"

require "test"

HEIGHT = 250
WIDTH = 250

function captureFrame()
    color = lib_lua.getLightUserData_func_lua()
    glReadPixels(0, 0, HEIGHT, WIDTH, GL_RGB, GL_UNSIGNED_BYTE, color)
end

-- main loop
glutInit(arg)
glutInitDisplayMode(GLUT_RGB + GLUT_DOUBLE + GLUT_DEPTH)
if arg then title = arg[0] else title = "glut" end
glutInitWindowPosition(710, 0)
glutInitWindowSize(HEIGHT, WIDTH)
window = glutCreateWindow(title)
glutDisplayFunc(display_func)
glutReshapeFunc(reshape_func)

local terminal = false
while not terminal do
    io.read()
    glutMainLoopEvent()
    captureFrame()
end

调用captureFrame()时得到Segmentation fault (core dumped).有人可以帮我吗?非常感谢!

I get Segmentation fault (core dumped) when captureFrame() is called. Can anyone help me with it? Thanks a lot!

第二次更新: 好的,我发现了该错误,唯一想更改的地方是cpp_test.cpp.注意,旧行已被注释掉:

2nd Update: OK, I found the bug, the only think to change is in cpp_test.cpp. NOTE that the old line is commented off:

int getLightUserData_func_cpp(lua_State* L){
    int nargc = lua_gettop(L);
    if (0 != nargc){
        luaL_error(L, "Wrong number of arguments.");
        return 0;
    }
    // static char var_name = 'a';
    static unsigned char var_name[3][255][255] = {{{0}}};
    lua_pushlightuserdata(L, (void*)&var_name);
    return 1;
}

通过这种方式,指针类型与它将指向的图像数据相匹配. 非常感谢大家的所有建议:D

This way the pointer type matches the image data that it will point to. Thanks a lot guys for all the suggestions :D

这篇关于在lua中为luaglut glReadPixels创建lightuserdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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