Quaternion library.

This commit is contained in:
jussi
2023-11-15 22:04:45 +02:00
parent 841aa897f1
commit 118a1f3a8b
10 changed files with 210 additions and 34 deletions

11
API.md
View File

@@ -41,6 +41,10 @@ This function will be called on program close. Cleanup could be done here.
--- ---
## Object unloading
Some objects allocate memory that needs to be freed when object is no longer needed. By default objects like Textures are unloaded by the Lua garbage collector. It is generatty however recommended to handle this manually in more complex projects. You can change the behavior with SetGCUnload.
## Arguments ## Arguments
Arguments are stored in 'RL.arg' array. Arguments are stored in 'RL.arg' array.
@@ -69,7 +73,7 @@ Vector4, 4 components
--- ---
> Quaternion = { 1.0, 1.0, 1.0, 1.0 } or { x = 1.0, y = 1.0, z = 1.0, w = 1.0 } > Quaternion = { 0.0, 0.0, 0.0, 1.0 } or { x = 0.0, y = 0.0, z = 0.0, w = 1.0 }
Quaternion, 4 components (Vector4 alias) Quaternion, 4 components (Vector4 alias)
@@ -6310,10 +6314,13 @@ Compute model bounding box limits (considers all meshes)
--- ---
> RL.SetModelMaterial( Model model, Material modelMaterial, Material material ) > 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. (Model material is the material id in models.)
- Failure return false
- Success return true
--- ---
> RL.SetModelMeshMaterial( Model model, int meshId, int materialId ) > RL.SetModelMeshMaterial( Model model, int meshId, int materialId )

View File

@@ -16,7 +16,7 @@ Included submodules.
* Raymath * Raymath
* Lights * Lights
* Easings * Easings
* RLGL WIP * RLGL WIP (Mostly done)
List of some MISSING features that are planned to be included. For specific function, check API. List of some MISSING features that are planned to be included. For specific function, check API.
@@ -28,7 +28,7 @@ List of some MISSING features that are planned to be included. For specific func
## Roadmap ## Roadmap
* v0.7 * v0.7
* Switch to Raylib v5.0? * Switch to Raylib v5.0
## Usage ## Usage

View File

@@ -3712,11 +3712,13 @@ function RL.UnloadModel( model ) end
function RL.GetModelBoundingBox( model ) end function RL.GetModelBoundingBox( model ) end
---Copies material to model material. (Model material is the material id in models.) ---Copies material to model material. (Model material is the material id in models.)
---- Failure return false
---- Success return true
---@param model any ---@param model any
---@param modelMaterial any ---@param modelMaterialId integer
---@param material any ---@param material any
---@return any RL.SetModelMaterial ---@return any success
function RL.SetModelMaterial( model, modelMaterial, material ) end function RL.SetModelMaterial( model, modelMaterialId, material ) end
---Set material for a mesh (Mesh and material on this model) ---Set material for a mesh (Mesh and material on this model)
---@param model any ---@param model any

View File

@@ -19,6 +19,7 @@ KEY CHANGES:
- CHANGED: Organized functions by putting them in the same order as in Raylib. - CHANGED: Organized functions by putting them in the same order as in Raylib.
- ADDED: Matrix library. - ADDED: Matrix library.
- Removed: GC_UNLOAD build time define and replaced with flag to change it at runtime. - Removed: GC_UNLOAD build time define and replaced with flag to change it at runtime.
- ADDED: Quaternion library.
DETAILED CHANGES: DETAILED CHANGES:
- CHANGED: GenImageColor now takes Vector2 as size. - CHANGED: GenImageColor now takes Vector2 as size.

View File

@@ -8,16 +8,13 @@ Backlog {
* Audio * Audio
* AudioStream. * AudioStream.
* Models * Models
* More Model management functions. Get mesh and material etc.
* BoneInfo. * BoneInfo.
* LoadMaterials (Load materials from model file).
* LoadMaterialsFromModel (Could then for example edit and set back to model). * LoadMaterialsFromModel (Could then for example edit and set back to model).
* rlgl * rlgl
* More Textures management functions. * More Textures management functions.
* Shader buffer storage object management (ssbo). * Shader buffer storage object management (ssbo).
* Matrix class.
* Quaternion class.
* Examples * Examples
* Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads. * Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads.
* Platformer example physics process for true framerate independence. * Platformer example physics process for true framerate independence.

View File

@@ -122,6 +122,11 @@ luaApiFile:write(
luaApiFile:write( luaApiFile:write(
"---"..FUNC_DESC.exit.."\nfunction RL.exit() end\n" ) "---"..FUNC_DESC.exit.."\nfunction RL.exit() end\n" )
-- Object unloading.
apiFile:write( "\n## Object unloading\n" )
apiFile:write( "\nSome objects allocate memory that needs to be freed when object is no longer needed. By default objects like Textures are unloaded by the Lua garbage collector. It is generatty however recommended to handle this manually in more complex projects. You can change the behavior with SetGCUnload.\n" )
-- Arguments. -- Arguments.
apiFile:write( "\n## Arguments\n" ) apiFile:write( "\n## Arguments\n" )
@@ -138,7 +143,7 @@ apiFile:write( "\n> Vector3 = { 1.0, 1.0, 1.0 } or { x = 1.0, y = 1.0, z = 1.0 }
Vector3, 3 components\n\n---\n" ) Vector3, 3 components\n\n---\n" )
apiFile:write( "\n> Vector4 = { 1.0, 1.0, 1.0, 1.0 } or { x = 1.0, y = 1.0, z = 1.0, w = 1.0 }\n\ apiFile:write( "\n> Vector4 = { 1.0, 1.0, 1.0, 1.0 } or { x = 1.0, y = 1.0, z = 1.0, w = 1.0 }\n\
Vector4, 4 components\n\n---\n" ) Vector4, 4 components\n\n---\n" )
apiFile:write( "\n> Quaternion = { 1.0, 1.0, 1.0, 1.0 } or { x = 1.0, y = 1.0, z = 1.0, w = 1.0 }\n\ apiFile:write( "\n> Quaternion = { 0.0, 0.0, 0.0, 1.0 } or { x = 0.0, y = 0.0, z = 0.0, w = 1.0 }\n\
Quaternion, 4 components (Vector4 alias)\n\n---\n" ) Quaternion, 4 components (Vector4 alias)\n\n---\n" )
apiFile:write( "\n> Matrix = { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 } }\n\ apiFile:write( "\n> Matrix = { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 } }\n\
Matrix, 4x4 components, column major, OpenGL style, right-handed. Identity matrix example\n\n---\n" ) Matrix, 4x4 components, column major, OpenGL style, right-handed. Identity matrix example\n\n---\n" )

View File

@@ -3,6 +3,8 @@ if table.unpack == nil then
table.unpack = unpack table.unpack = unpack
end end
local Vector3 = require( "vector3" )
Color = {} Color = {}
Color.meta = { Color.meta = {
__index = Color, __index = Color,

View File

@@ -27,10 +27,10 @@ Matrix.meta = {
__index = Matrix, __index = Matrix,
__tostring = function( m ) __tostring = function( m )
return "{\n" return "{\n"
.." { "..m.m[1][1]..", "..m.m[1][2]..", "..m.m[1][3]..", "..m.m[1][4].." },\n" .." {"..m.m[1][1]..", "..m.m[1][2]..", "..m.m[1][3]..", "..m.m[1][4].."},\n"
.." { "..m.m[2][1]..", "..m.m[2][2]..", "..m.m[2][3]..", "..m.m[2][4].." },\n" .." {"..m.m[2][1]..", "..m.m[2][2]..", "..m.m[2][3]..", "..m.m[2][4].."},\n"
.." { "..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].." },\n" .." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n"
.." { "..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].." },\n" .." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n"
.."}" .."}"
end, end,
__add = function( m1, m2 ) __add = function( m1, m2 )

View File

@@ -0,0 +1,153 @@
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local Vector3 = require( "vector3" )
local Matrix = require( "matrix" )
Quaternion = {}
Quaternion.meta = {
__index = Quaternion,
__tostring = function( q )
return "{"..tostring( q.x )..", "..tostring( q.y )..", "..tostring( q.z )..", "..tostring( q.w ).."}"
end,
__add = function( q1, q2 )
return Quaternion:new( RL.QuaternionAdd( q1, q2 ) )
end,
__sub = function( q1, q2 )
return Quaternion:new( RL.QuaternionSubtract( q1, q2 ) )
end,
__mul = function( q1, q2 )
return Quaternion:new( RL.QuaternionMultiply( q1, q2 ) )
end,
__div = function( q1, q2 )
return Quaternion:new( RL.QuaternionDivide( q1, q2 ) )
end,
__unm = function( q )
return Quaternion:new( RL.QuaternionInvert( q ) )
end,
__len = function( _ )
return 4
end,
__eq = function( q1, q2 )
return RL.QuaternionEquals( q1, q2 ) == 1
end,
}
function Quaternion:new( x, y, z, w )
if type( x ) == "table" then
x, y, z, w = table.unpack( x )
elseif type( x ) == "nil" then
x, y, z, w = 0, 0, 0, 1 -- QuaternionIdentity.
end
local object = setmetatable( {}, Quaternion.meta )
object.x = x
object.y = y
object.z = z
object.w = w
return object
end
function Quaternion:set( x, y, z, w )
if type( x ) == "table" then
x, y, z, w = table.unpack( x )
elseif type( x ) == "nil" then
x, y, z, w = 0, 0, 0, 1 -- QuaternionIdentity.
end
self.x = x
self.y = y
self.z = z
self.w = w
end
function Quaternion:arr()
return { self.x, self.y, self.z, self.w }
end
function Quaternion:unpack()
return self.x, self.y, self.z, self.w
end
function Quaternion:clone()
return Quaternion:new( self.x, self.y, self.z, self.w )
end
function Quaternion:addValue( value )
return Quaternion:new( RL.QuaternionAddValue( self, value ) )
end
function Quaternion:subValue( value )
return Quaternion:new( RL.QuaternionSubtractValue( self, value ) )
end
function Quaternion:identity()
return Quaternion:new( RL.QuaternionIdentity() )
end
function Quaternion:length()
return RL.QuaternionLength( self )
end
function Quaternion:normalize()
return Quaternion:new( RL.QuaternionNormalize( self ) )
end
function Quaternion:invert()
return Quaternion:new( RL.QuaternionInvert( self ) )
end
function Quaternion:scale( scalar )
return Quaternion:new( RL.QuaternionScale( self, scalar ) )
end
function Quaternion:lerp( q2, value )
return Quaternion:new( RL.QuaternionLerp( self, q2, value ) )
end
function Quaternion:nLerp( q2, value )
return Quaternion:new( RL.QuaternionNLerp( self, q2, value ) )
end
function Quaternion:sLerp( q2, value )
return Quaternion:new( RL.QuaternionSLerp( self, q2, value ) )
end
function Quaternion:fromVector3ToVector3( from, to )
return Vector3:new( RL.QuaternionFromVector3ToVector3( from, to ) )
end
function Quaternion:fromMatrix( mat )
return Quaternion:new( RL.QuaternionFromMatrix( mat ) )
end
function Quaternion:toMatrix()
return Matrix:new( RL.QuaternionToMatrix( self ) )
end
function Quaternion:fromAxisAngle( axis, angle )
return Quaternion:new( RL.QuaternionFromAxisAngle( axis, angle ) )
end
function Quaternion:toAxisAngle()
local axis, angle = Quaternion:new( RL.QuaternionToAxisAngle( self ) )
return Vector3:new( axis ), angle
end
function Quaternion:fromEuler( pitch, yaw, roll )
return Quaternion:new( RL.QuaternionFromEuler( pitch, yaw, roll ) )
end
function Quaternion:toEuler()
return Vector3:new( RL.QuaternionToEuler( self ) )
end
function Quaternion:transform( mat )
return Quaternion:new( RL.QuaternionTransform( self, mat ) )
end
return Quaternion

View File

@@ -573,9 +573,12 @@ int lmodelsGetModelBoundingBox( lua_State *L ) {
} }
/* /*
> RL.SetModelMaterial( Model model, Material modelMaterial, Material material ) > 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. (Model material is the material id in models.)
- Failure return false
- Success return true
*/ */
int lmodelsSetModelMaterial( lua_State *L ) { int lmodelsSetModelMaterial( lua_State *L ) {
Model *model = uluaGetModel( L, 1 ); Model *model = uluaGetModel( L, 1 );
@@ -584,25 +587,31 @@ int lmodelsSetModelMaterial( lua_State *L ) {
//TODO Could maybe return old shader and textures for storage or get garbage collected? //TODO Could maybe return old shader and textures for storage or get garbage collected?
/* Copy material data instead of using pointer. Pointer would result in double free error. */ if ( 0 <= modelMaterialId && modelMaterialId < model->materialCount ) {
model->materials[ modelMaterialId ].shader = material->shader; /* Copy material data instead of using pointer. Pointer would result in double free error. */
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_ALBEDO ] = material->maps[ MATERIAL_MAP_ALBEDO ]; model->materials[ modelMaterialId ].shader = material->shader;
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_METALNESS ] = material->maps[ MATERIAL_MAP_METALNESS ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_ALBEDO ] = material->maps[ MATERIAL_MAP_ALBEDO ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_NORMAL ] = material->maps[ MATERIAL_MAP_NORMAL ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_METALNESS ] = material->maps[ MATERIAL_MAP_METALNESS ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_ROUGHNESS ] = material->maps[ MATERIAL_MAP_ROUGHNESS ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_NORMAL ] = material->maps[ MATERIAL_MAP_NORMAL ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_OCCLUSION ] = material->maps[ MATERIAL_MAP_OCCLUSION ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_ROUGHNESS ] = material->maps[ MATERIAL_MAP_ROUGHNESS ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_EMISSION ] = material->maps[ MATERIAL_MAP_EMISSION ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_OCCLUSION ] = material->maps[ MATERIAL_MAP_OCCLUSION ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_HEIGHT ] = material->maps[ MATERIAL_MAP_HEIGHT ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_EMISSION ] = material->maps[ MATERIAL_MAP_EMISSION ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_CUBEMAP ] = material->maps[ MATERIAL_MAP_CUBEMAP ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_HEIGHT ] = material->maps[ MATERIAL_MAP_HEIGHT ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_IRRADIANCE ] = material->maps[ MATERIAL_MAP_IRRADIANCE ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_CUBEMAP ] = material->maps[ MATERIAL_MAP_CUBEMAP ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_PREFILTER ] = material->maps[ MATERIAL_MAP_PREFILTER ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_IRRADIANCE ] = material->maps[ MATERIAL_MAP_IRRADIANCE ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_BRDF ] = material->maps[ MATERIAL_MAP_BRDF ]; model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_PREFILTER ] = material->maps[ MATERIAL_MAP_PREFILTER ];
model->materials[ modelMaterialId ].maps[ MATERIAL_MAP_BRDF ] = material->maps[ MATERIAL_MAP_BRDF ];
for ( int i = 0; i < 4; i++ ) { for ( int i = 0; i < 4; i++ ) {
model->materials[ modelMaterialId ].params[i] = material->params[i]; model->materials[ modelMaterialId ].params[i] = material->params[i];
}
lua_pushboolean( L, true );
} }
else {
return 0; TraceLog( LOG_WARNING, "SetModelMaterial modelMaterialId %d out of bounds", modelMaterialId );
lua_pushboolean( L, false );
}
return 1;
} }
/* /*