userdata expected, got userdata

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

userdata expected, got userdata

Issac Trotts
I'm running into some trouble with a lua interpreter linked statically with luaglut-0.4.  When I try to run a straightforward translation of the OpenGL Redbook demo select.c, it gives an error in this code

BUFSIZE=512
local selectBuf = memarray('int', BUFSIZE)
glSelectBuffer (BUFSIZE, selectBuf);

$ ./lua select.lua
./lua: select.lua:148: bad argument #2 to `glSelectBuffer' (userdata expected, got userdata)
stack traceback:
        [C]: in function `glSelectBuffer'
        select.lua:148: in function `selectObjects'
        select.lua:183: in function `display'
        [C]: in function `glutMainLoop'
        select.lua:201: in main chunk
        [C]: ?

A Google search for "userdata expected, got userdata" came up empty, so I'm hoping someone here knows what's going on.

Issac


Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

Adam D. Moss
Issac Trotts wrote:
> A Google search for "userdata expected, got userdata" came up empty, so
> I'm hoping someone here knows what's going on.

There are two types of userdata: light and heavy.  I guess
you're supplying one and the API expects the other.  Sadly,
it seems impossible to distinguish between the two types on
the Lua side.

--Adam
--
Adam D. Moss   -   [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

Luiz Henrique de Figueiredo
> There are two types of userdata: light and heavy.  I guess
> you're supplying one and the API expects the other.  Sadly,
> it seems impossible to distinguish between the two types on
> the Lua side.

This is true, but it is not an excuse for that poor error message.
The io library gives nice messages even when supplied with userdata:
        bad argument #1 to 'close' (FILE* expected, got userdata)
This is message is produced by luaL_checkudata.
--lhf
Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

Issac Trotts
In reply to this post by Adam D. Moss


On 2/8/06, Adam D. Moss <[hidden email]> wrote:
Issac Trotts wrote:
> A Google search for "userdata expected, got userdata" came up empty, so
> I'm hoping someone here knows what's going on.

There are two types of userdata: light and heavy.  I guess
you're supplying one and the API expects the other.  Sadly,
it seems impossible to distinguish between the two types on
the Lua side.


Thanks for your reply.  I worked around the issue by writing a higher-level C function that does some extra work so the caller doesn't have to allocate a buffer.  From what you're saying, this is probably a good thing.  The code is attached below.

Issac


/* do_gl_selection(selection_buffer_size, proc)
 *
 * proc is a procedure that calls glInitNames(), glPushName(), and glLoadName(), as in the
 * OpenGL demo select.c, picksquare.c and pickdepth.c.
 *
 * The returned value is an array of hit records.
 */
int do_gl_selection(lua_State* L)
{
    int bufsize, num_hits;
    GLuint* select_buf;

    bufsize = luaL_checkint(L,1);
    select_buf = calloc(bufsize,sizeof(GLuint));
    glSelectBuffer(bufsize, select_buf);
    (void)glRenderMode(GL_SELECT);

    /* Call the procedure that was passed in. */
    {
        luaL_checktype(L,2,LUA_TFUNCTION);
        if(lua_pcall(L,0,0,0) != 0) {
            fprintf(stderr,"Unable to run callback procedure in do_gl_selection: %s\n",
                    lua_tostring(L,-1));
        }
    }

    /* Change the render mode back to the default and get the number of
     * selection hits. */
    num_hits = glRenderMode(GL_RENDER);

    lua_newtable(L);  /* ret */

    /* Process the hits. */
    {
        GLint i;
        GLuint j, num_names, *ptr;

        ptr = (GLuint *) select_buf;
        for (i = 0; i < num_hits; i++) {  /* for each hit  */
            /* Push a hit record (table) onto the list of things to return. */
            /* We'll call this table `hitrecord', even though it is anonymous in
             * Lua. */
            lua_newtable(L);
            num_names = *ptr;

            /* hitrecord.num_names = *ptr */
            {
                lua_pushstring(L, "num_names");
                lua_pushnumber(L, *ptr);
                lua_settable(L,-3);
                ptr++;
            }

            /* hitrecord.z1 = *ptr / (2**32-1) */
            {
                lua_pushstring(L, "z1");
                lua_pushnumber(L, *ptr/(pow(2.0,32.0)-1.0));
                lua_settable(L,-3);
                ptr++;
            }

            /* hitrecord.z2 = *ptr / (2**32-1) */
            {
                lua_pushstring(L, "z2");
                lua_pushnumber(L, *ptr/(pow(2.0,32.0)-1.0));
                lua_settable(L,-3);
                ptr++;
            }

            for (j = 0; j < num_names; j++) {  /* for each name */
                /* hitrecord[j+1] = *ptr */
                lua_pushnumber(L, j+1);
                lua_pushnumber(L, *ptr);
                lua_settable(L,-3);
                ptr++;
            }

            /* Append hitrecord to the ret table. */
            {
                lua_pushnumber(L, i+1);
                lua_pushvalue(L,-2); /* hitrecord */
                lua_settable(L,2);  /* ret */
            }

            /* Remove hitrecord from the stack. */
            assert(lua_istable(L,-1));
            assert(lua_istable(L,-2));
            lua_pop(L,1);
            assert(lua_istable(L,-1));
            assert(lua_isnumber(L,-2));
            assert(lua_gettop(L)==2);
        }
    }

    free(select_buf);
    return 1;
}

Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

varol kaptan
In reply to this post by Issac Trotts
The last line should have been glSelectBuffer(BUFSIZE, selectBuf:ptr())

I'll see what I can do to make the error message make sense.

Varol Kaptan

On 2/8/06, Issac Trotts <[hidden email]> wrote:

> I'm running into some trouble with a lua interpreter linked statically with
> luaglut-0.4.  When I try to run a straightforward translation of the OpenGL
> Redbook demo select.c, it gives an error in this code
>
>  BUFSIZE=512
>  local selectBuf = memarray('int', BUFSIZE)
>  glSelectBuffer (BUFSIZE, selectBuf);
>
>  $ ./lua select.lua
>  ./lua: select.lua:148: bad argument #2 to `glSelectBuffer' (userdata
> expected, got userdata)
>  stack traceback:
>          [C]: in function `glSelectBuffer'
>          select.lua:148: in function `selectObjects'
>          select.lua:183: in function `display'
>          [C]: in function `glutMainLoop'
>          select.lua:201: in main chunk
>          [C]: ?
>
>  A Google search for "userdata expected, got userdata" came up empty, so I'm
> hoping someone here knows what's going on.
>
>  Issac
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

Issac Trotts
OK, that's good to know.  Btw, it seems like memarray isn't quite general enough to be used safely with OpenGL.  Here's the line from memarray.c where it lists the types.

const char * m_type_names[] = {
   "byte", "short", "int", "long", "float", "double" };

There isn't an entry for unsigned int or GLuint, which is what's required by glSelectBuffer, so I think I'm going to stick with my own wrapping function for now.  Would you be willing to include it in luaglut?

Issac


On 2/8/06, varol kaptan <[hidden email]> wrote:
The last line should have been glSelectBuffer(BUFSIZE, selectBuf:ptr())

I'll see what I can do to make the error message make sense.

Varol Kaptan

On 2/8/06, Issac Trotts <[hidden email]> wrote:

> I'm running into some trouble with a lua interpreter linked statically with
> luaglut-0.4.  When I try to run a straightforward translation of the OpenGL
> Redbook demo select.c, it gives an error in this code
>
>  BUFSIZE=512
>  local selectBuf = memarray('int', BUFSIZE)
>  glSelectBuffer (BUFSIZE, selectBuf);
>
>  $ ./lua select.lua
>  ./lua: select.lua :148: bad argument #2 to `glSelectBuffer' (userdata
> expected, got userdata)
>  stack traceback:
>          [C]: in function `glSelectBuffer'
>          select.lua:148: in function `selectObjects'
>          select.lua:183: in function `display'
>          [C]: in function `glutMainLoop'
>          select.lua:201: in main chunk
>          [C]: ?
>
>  A Google search for "userdata expected, got userdata" came up empty, so I'm
> hoping someone here knows what's going on.
>
>  Issac
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

varol kaptan
I've fixed the error message and some other things (Lua 5.1 fixes by
Anders F. Björklund, for example) and was ready to release a new
version. However, you have a very good point about the unsigned types.
I think it will be better to add them to memarray. I will release
luaglut 0.5 in a day or two with a better memarray.

Unfortunately, I have no experience with OpenGL selection and
feedback. Will this solve you selection problem?

Varol Kaptan

On 2/8/06, Issac Trotts <[hidden email]> wrote:

> OK, that's good to know.  Btw, it seems like memarray isn't quite general
> enough to be used safely with OpenGL.  Here's the line from memarray.c where
> it lists the types.
>
>  const char * m_type_names[] = {
>     "byte", "short", "int", "long", "float", "double" };
>
>  There isn't an entry for unsigned int or GLuint, which is what's required
> by glSelectBuffer, so I think I'm going to stick with my own wrapping
> function for now.  Would you be willing to include it in luaglut?
>
>  Issac
>
> On 2/8/06, varol kaptan <[hidden email]> wrote:
> > The last line should have been glSelectBuffer(BUFSIZE, selectBuf:ptr())
> >
> > I'll see what I can do to make the error message make sense.
> >
> > Varol Kaptan
> >
> > On 2/8/06, Issac Trotts < [hidden email]> wrote:
> > > I'm running into some trouble with a lua interpreter linked statically
> with
> > > luaglut-0.4.  When I try to run a straightforward translation of the
> OpenGL
> > > Redbook demo select.c, it gives an error in this code
> > >
> > >  BUFSIZE=512
> > >  local selectBuf = memarray('int', BUFSIZE)
> > >  glSelectBuffer (BUFSIZE, selectBuf);
> > >
> > >  $ ./lua select.lua
> > >  ./lua: select.lua :148: bad argument #2 to `glSelectBuffer' (userdata
> > > expected, got userdata)
> > >  stack traceback:
> > >          [C]: in function `glSelectBuffer'
> > >          select.lua:148: in function `selectObjects'
> > >          select.lua:183: in function `display'
> > >          [C]: in function `glutMainLoop'
> > >          select.lua:201: in main chunk
> > >          [C]: ?
> > >
> > >  A Google search for "userdata expected, got userdata" came up empty, so
> I'm
> > > hoping someone here knows what's going on.
> > >
> > >  Issac
Reply | Threaded
Open this post in threaded view
|

Re: userdata expected, got userdata

Issac Trotts
On 2/8/06, varol kaptan <[hidden email]> wrote:
I've fixed the error message and some other things (Lua 5.1 fixes by
Anders F. Björklund, for example) and was ready to release a new
version. However, you have a very good point about the unsigned types.
I think it will be better to add them to memarray. I will release
luaglut 0.5 in a day or two with a better memarray.

Unfortunately, I have no experience with OpenGL selection and
feedback. Will this solve you selection problem?

Yes, that should do it. 

Thanks,
Issac