More Model management functions, BoneInfo and Transform.

This commit is contained in:
jussi
2023-12-07 22:38:13 +02:00
parent 6ecbbcc282
commit eb7873be2b
9 changed files with 590 additions and 30 deletions

View File

@@ -845,7 +845,7 @@ static void defineGlobals() {
assignGlobalInt( RL_FRAGMENT_SHADER, "RL_FRAGMENT_SHADER" ); // GL_FRAGMENT_SHADER
assignGlobalInt( RL_VERTEX_SHADER, "RL_VERTEX_SHADER" ); // GL_VERTEX_SHADER
assignGlobalInt( RL_COMPUTE_SHADER, "RL_COMPUTE_SHADER" ); // GL_COMPUTE_SHADER
/* RLGL GlVersion */
/* RLGL GL blending factors */
assignGlobalInt( RL_ZERO, "RL_ZERO" ); // GL_ZERO
assignGlobalInt( RL_ONE, "RL_ONE" ); // GL_ONE
assignGlobalInt( RL_SRC_COLOR, "RL_SRC_COLOR" ); // GL_SRC_COLOR
@@ -1586,10 +1586,20 @@ void luaRegister() {
assingGlobalFunction( "IsModelReady", lmodelsIsModelReady );
assingGlobalFunction( "UnloadModel", lmodelsUnloadModel );
assingGlobalFunction( "GetModelBoundingBox", lmodelsGetModelBoundingBox );
assingGlobalFunction( "SetModelTransform", lmodelsSetModelTransform );
assingGlobalFunction( "SetModelMesh", lmodelsSetModelMesh );
assingGlobalFunction( "SetModelMaterial", lmodelsSetModelMaterial );
assingGlobalFunction( "SetModelMeshMaterial", lmodelsSetModelMeshMaterial );
assingGlobalFunction( "SetModelTransform", lmodelsSetModelTransform );
assingGlobalFunction( "SetModelBone", lmodelsSetModelBone );
assingGlobalFunction( "SetModelBindPose", lmodelsSetModelBindPose );
assingGlobalFunction( "GetModelTransform", lmodelsGetModelTransform );
assingGlobalFunction( "GetModelMeshCount", lmodelsGetModelMeshCount );
assingGlobalFunction( "GetModelMaterialCount", lmodelsGetModelMaterialCount );
assingGlobalFunction( "GetModelMesh", lmodelsGetModelMesh );
assingGlobalFunction( "GetModelMaterial", lmodelsGetModelMaterial );
assingGlobalFunction( "GetModelBoneCount", lmodelsGetModelBoneCount );
assingGlobalFunction( "GetModelBone", lmodelsGetModelBone );
assingGlobalFunction( "GetModelBindPose", lmodelsGetModelBindPose );
/* Model drawing functions. */
assingGlobalFunction( "DrawModel", lmodelsDrawModel );
assingGlobalFunction( "DrawModelEx", lmodelsDrawModelEx );
@@ -2481,7 +2491,6 @@ BoundingBox uluaGetBoundingBox( lua_State *L, int index ) {
lua_pop( L, 1 );
}
}
return box;
}
@@ -2518,7 +2527,6 @@ Ray uluaGetRay( lua_State *L, int index ) {
lua_pop( L, 1 );
}
}
return ray;
}
@@ -2581,6 +2589,139 @@ NPatchInfo uluaGetNPatchInfo( lua_State *L, int index ) {
return npatch;
}
GlyphInfo uluaGetGlyphInfo( lua_State *L, int index ) {
luaL_checktype( L, index, LUA_TTABLE );
GlyphInfo glyph = { 0 };
int t = index, i = 0;
lua_pushnil( L );
while ( lua_next( L, t ) != 0 ) {
/* Do not check type since there should be table and ints. */
if ( lua_isnumber( L, -2 ) ) {
switch ( i ) {
case 0:
glyph.value = lua_tointeger( L, -1 );
break;
case 1:
glyph.offsetX = lua_tointeger( L, -1 );
break;
case 2:
glyph.offsetY = lua_tointeger( L, -1 );
break;
case 3:
glyph.advanceX = lua_tointeger( L, -1 );
break;
case 4:
glyph.image = *uluaGetImage( L, lua_gettop( L ) );
break;
default:
break;
}
}
else if ( lua_isstring( L, -2 ) ) {
if ( strcmp( "value", (char*)lua_tostring( L, -2 ) ) == 0 ) {
glyph.value = lua_tointeger( L, -1 );
}
else if ( strcmp( "offsetX", (char*)lua_tostring( L, -2 ) ) == 0 ) {
glyph.offsetX = lua_tointeger( L, -1 );
}
else if ( strcmp( "offsetY", (char*)lua_tostring( L, -2 ) ) == 0 ) {
glyph.offsetY = lua_tointeger( L, -1 );
}
else if ( strcmp( "advanceX", (char*)lua_tostring( L, -2 ) ) == 0 ) {
glyph.advanceX = lua_tointeger( L, -1 );
}
else if ( strcmp( "image", (char*)lua_tostring( L, -2 ) ) == 0 ) {
glyph.image = *uluaGetImage( L, lua_gettop( L ) );
}
}
i++;
lua_pop( L, 1 );
}
return glyph;
}
BoneInfo uluaGetBoneInfo( lua_State *L, int index ) {
luaL_checktype( L, index, LUA_TTABLE );
BoneInfo bone = { 0 };
int t = index, i = 0;
lua_pushnil( L );
while ( lua_next( L, t ) != 0 ) {
/* Do not check type since there should be table and ints. */
if ( lua_isnumber( L, -2 ) ) {
switch ( i ) {
case 0:
strncpy( bone.name, lua_tostring( L, lua_gettop( L ) ), 32 );
break;
case 1:
bone.parent = lua_tointeger( L, -1 );
break;
default:
break;
}
}
else if ( lua_istable( L, -1 ) ) {
if ( lua_isstring( L, -2 ) ) {
if ( strcmp( "name", (char*)lua_tostring( L, -2 ) ) == 0 ) {
strncpy( bone.name, lua_tostring( L, lua_gettop( L ) ), 32 );
}
else if ( strcmp( "parent", (char*)lua_tostring( L, -2 ) ) == 0 ) {
bone.parent = lua_tointeger( L, -1 );
}
}
i++;
lua_pop( L, 1 );
}
}
return bone;
}
Transform uluaGetTransform( lua_State *L, int index ) {
luaL_checktype( L, index, LUA_TTABLE );
Transform transform = { 0 };
int t = index, i = 0;
lua_pushnil( L );
while ( lua_next( L, t ) != 0 ) {
/* Do not check type since there should be table and ints. */
if ( lua_isnumber( L, -2 ) ) {
switch ( i ) {
case 0:
transform.translation = uluaGetVector3( L, lua_gettop( L ) );
break;
case 1:
transform.rotation = uluaGetQuaternion( L, lua_gettop( L ) );
break;
case 2:
transform.scale = uluaGetVector3( L, lua_gettop( L ) );
break;
default:
break;
}
}
else if ( lua_istable( L, -1 ) ) {
if ( lua_isstring( L, -2 ) ) {
if ( strcmp( "translation", (char*)lua_tostring( L, -2 ) ) == 0 ) {
transform.translation = uluaGetVector3( L, lua_gettop( L ) );
}
else if ( strcmp( "rotation", (char*)lua_tostring( L, -2 ) ) == 0 ) {
transform.rotation = uluaGetQuaternion( L, lua_gettop( L ) );
}
else if ( strcmp( "scale", (char*)lua_tostring( L, -2 ) ) == 0 ) {
transform.scale = uluaGetVector3( L, lua_gettop( L ) );
}
}
i++;
lua_pop( L, 1 );
}
}
return transform;
}
Buffer* uluaGetBuffer( lua_State *L, int index ) {
if ( lua_islightuserdata( L, index ) ) {
return (Buffer*)lua_touserdata( L, index );
@@ -2866,7 +3007,7 @@ void uluaPushBoundingBox( lua_State *L, BoundingBox box ) {
}
void uluaPushGlyphInfo( lua_State *L, GlyphInfo glyphInfo, Image *image ) {
lua_createtable( L, 4, 0 );
lua_createtable( L, 5, 0 );
lua_pushinteger( L, glyphInfo.value );
lua_setfield( L, -2, "value" );
lua_pushinteger( L, glyphInfo.offsetX );
@@ -2879,6 +3020,24 @@ void uluaPushGlyphInfo( lua_State *L, GlyphInfo glyphInfo, Image *image ) {
lua_setfield( L, -2, "image" );
}
void uluaPushBoneInfo( lua_State *L, BoneInfo boneInfo ) {
lua_createtable( L, 2, 0 );
lua_pushstring( L, boneInfo.name );
lua_setfield( L, -2, "name" );
lua_pushinteger( L, boneInfo.parent );
lua_setfield( L, -2, "parent" );
}
void uluaPushTransform( lua_State *L, Transform transform ) {
lua_createtable( L, 3, 0 );
uluaPushVector3( L, transform.translation );
lua_setfield( L, -2, "name" );
uluaPushQuaternion( L, transform.rotation );
lua_setfield( L, -2, "rotation" );
uluaPushVector3( L, transform.scale );
lua_setfield( L, -2, "scale" );
}
void uluaPushBuffer( lua_State *L, Buffer buffer ) {
if ( buffer.size == 0 ) {
buffer.data = NULL;

View File

@@ -572,10 +572,49 @@ int lmodelsGetModelBoundingBox( lua_State *L ) {
return 1;
}
/*
> RL.SetModelTransform( Model model, Matrix transform )
Set model transform matrix
*/
int lmodelsSetModelTransform( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
Matrix transform = uluaGetMatrix( L, 2 );
model->transform = transform;
return 0;
}
/*
> success = RL.SetModelMesh( Model model, int meshId, Mesh mesh )
Get model mesh. Return as lightuserdata
- Failure return false
- Success return true
*/
int lmodelsSetModelMesh( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
int meshId = luaL_checkinteger( L, 2 );
Mesh *mesh = uluaGetMesh( L, 3 );
if ( 0 <= meshId && meshId < model->meshCount ) {
// TODO Test if mesh should be copied instead. Is there issues with unloading.
model->meshes[ meshId ] = *mesh;
lua_pushboolean( L, true );
}
else {
TraceLog( LOG_WARNING, "MeshId %d out of bounds", meshId );
lua_pushboolean( L, false );
}
return 1;
}
/*
> success = RL.SetModelMaterial( Model model, int modelMaterialId, Material material )
Copies material to model material. (Model material is the material id in models.)
Copies material to model material
- Failure return false
- Success return true
@@ -585,8 +624,6 @@ int lmodelsSetModelMaterial( lua_State *L ) {
int modelMaterialId = luaL_checkinteger( L, 2 );
Material *material = uluaGetMaterial( L, 3 );
//TODO Could maybe return old shader and textures for storage or get garbage collected?
if ( 0 <= modelMaterialId && modelMaterialId < model->materialCount ) {
/* Copy material data instead of using pointer. Pointer would result in double free error. */
model->materials[ modelMaterialId ].shader = material->shader;
@@ -630,17 +667,51 @@ int lmodelsSetModelMeshMaterial( lua_State *L ) {
}
/*
> RL.SetModelTransform( Model model, Matrix transform )
> success = RL.SetModelBone( Model model, int boneId, BoneInfo bone )
Set model transform matrix
Set model bone information (skeleton)
- Failure return false
- Success return true
*/
int lmodelsSetModelTransform( lua_State *L ) {
int lmodelsSetModelBone( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
Matrix transform = uluaGetMatrix( L, 2 );
int boneId = luaL_checkinteger( L, 2 );
BoneInfo bone = uluaGetBoneInfo( L, 3 );
model->transform = transform;
if ( 0 <= boneId && boneId < model->boneCount ) {
model->bones[ boneId ] = bone;
lua_pushboolean( L, true );
}
else {
TraceLog( LOG_WARNING, "boneId %d out of bounds", boneId );
lua_pushboolean( L, false );
}
return 1;
}
return 0;
/*
> success = RL.SetModelBindPose( Model model, int boneId, Transform pose )
Set model bones base transformation (pose)
- Failure return false
- Success return true
*/
int lmodelsSetModelBindPose( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
int boneId = luaL_checkinteger( L, 2 );
Transform pose = uluaGetTransform( L, 3 );
if ( 0 <= boneId && boneId < model->boneCount ) {
model->bindPose[ boneId ] = pose;
lua_pushboolean( L, true );
}
else {
TraceLog( LOG_WARNING, "boneId %d out of bounds", boneId );
lua_pushboolean( L, false );
}
return 1;
}
/*
@@ -658,6 +729,139 @@ int lmodelsGetModelTransform( lua_State *L ) {
return 1;
}
/*
> meshCount = RL.GetModelMeshCount( Model model )
Get model number of meshes
- Success return int
*/
int lmodelsGetModelMeshCount( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
lua_pushinteger( L, model->meshCount );
return 1;
}
/*
> meshCount = RL.GetModelMaterialCount( Model model )
Get model number of materials
- Success return int
*/
int lmodelsGetModelMaterialCount( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
lua_pushinteger( L, model->materialCount );
return 1;
}
/*
> mesh = RL.GetModelMesh( Model model, int meshId )
Get model mesh. Return as lightuserdata
- Failure return nil
- Success return Mesh
*/
int lmodelsGetModelMesh( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
int meshId = luaL_checkinteger( L, 2 );
if ( 0 <= meshId && meshId < model->meshCount ) {
lua_pushlightuserdata( L, &model->meshes[ meshId ] );
}
else {
TraceLog( LOG_WARNING, "MeshId %d out of bounds", meshId );
lua_pushnil( L );
}
return 1;
}
/*
> material = RL.GetModelMaterial( Model model, int materialId )
Get model material. Return as lightuserdata
- Failure return nil
- Success return Material
*/
int lmodelsGetModelMaterial( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
int materialId = luaL_checkinteger( L, 2 );
if ( 0 <= materialId && materialId < model->materialCount ) {
lua_pushlightuserdata( L, &model->materials[ materialId ] );
}
else {
TraceLog( LOG_WARNING, "MaterialId %d out of bounds", materialId );
lua_pushnil( L );
}
return 1;
}
/*
> boneCount = RL.GetModelBoneCount( Model model )
Get model number of bones
- Success return int
*/
int lmodelsGetModelBoneCount( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
lua_pushinteger( L, model->boneCount );
return 1;
}
/*
> bone = RL.GetModelBone( Model model, int boneId )
Get model bones information (skeleton)
- Failure return nil
- Success return BoneInfo
*/
int lmodelsGetModelBone( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
int boneId = luaL_checkinteger( L, 2 );
if ( 0 <= boneId && boneId < model->boneCount ) {
uluaPushBoneInfo( L, model->bones[ boneId ] );
}
else {
TraceLog( LOG_WARNING, "BoneId %d out of bounds", boneId );
lua_pushnil( L );
}
return 1;
}
/*
> pose = RL.GetModelBindPose( Model model, int boneId )
Get models bones base transformation (pose)
- Failure return nil
- Success return Transform
*/
int lmodelsGetModelBindPose( lua_State *L ) {
Model *model = uluaGetModel( L, 1 );
int boneId = luaL_checkinteger( L, 2 );
if ( 0 <= boneId && boneId < model->boneCount ) {
uluaPushTransform( L, model->bindPose[ boneId ] );
}
else {
TraceLog( LOG_WARNING, "BoneId %d out of bounds", boneId );
lua_pushnil( L );
}
return 1;
}
/*
## Models - Model drawing functions
*/