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
Then when I use test.so
in lua:
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)
This is the output I get:
{
getLightUserData_func_lua : function: 0x41ecbf10
}
userdata
userdata: 0x7fe73297f050
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
I get Segmentation fault (core dumped)
when captureFrame()
is called. Can anyone help me with it? Thanks a lot!
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;
}
This way the pointer type matches the image data that it will point to.
Thanks a lot guys for all the suggestions :D