Search code examples
luagideros

How to create 3d mesh vertices in Gideros


I'm using Lua for the first time, and of course need to check around to learn how to implement certain code.

To create a vertex in Gideros, there's this code:

 mesh:setVertex(index, x, y)

However, I would also like to use the z coordinate. I've been checking around, but haven't found any help. Does anyone know if Gideros has a method for this, or are there any tips and tricks on setting the z coordinates?


Solution

  • First of all these functions are not provided by Lua, but by the Gideros Lua API. There are no meshes or things like that in native Lua.

    Referring to the reference Gideros Lua API reference manual would give you some valuable hints:

    http://docs.giderosmobile.com/reference/gideros/Mesh#Mesh

    Mesh can be 2D or 3D, the latter expects an additionnal Z coordinate in its vertices.

    http://docs.giderosmobile.com/reference/gideros/Mesh/new

    Mesh.new([is3d])

    Parameters:

    is3d: (boolean) Specifies that this mesh expect Z coordinate in its vertex array and is thus a 3D mesh

    So in order to create a 3d mesh you have to do something like:

    local myMesh = Mesh.new(true)
    

    Although the manual does not say that you can use a z coordinate in setVertex

    http://docs.giderosmobile.com/reference/gideros/Mesh/setVertex

    It is very likely that you can do that.

    So let's have a look at Gideros source code:

    https://github.com/gideros/gideros/blob/1d4894fb5d39ef6c2375e7e3819cfc836da7672b/luabinding/meshbinder.cpp#L96-L109

    int MeshBinder::setVertex(lua_State *L)
    {
        Binder binder(L);
        GMesh *mesh = static_cast<GMesh*>(binder.getInstance("Mesh", 1));
    
        int i = luaL_checkinteger(L, 2) - 1;
        float x = luaL_checknumber(L, 3);
        float y = luaL_checknumber(L, 4);
        float z = luaL_optnumber(L, 5, 0.0);
    
        mesh->setVertex(i, x, y, z);
    
        return 0;
    }
    

    Here you can see that you can indeed provide a z coordinate and that it will be used.

    So

    local myMesh = Mesh.new(true)
    myMesh:SetVertex(1, 100, 20, 40)
    

    should work just fine.

    You could have simply tried that btw. It's for free, it doesn't hurt and it's the best way to learn!