Comments (2)
Hm, I made a prototype for generic 4x4 inverse:
bool gb_mat4_inverse(gbMat4 * out, gbMat4 * in)
{
#define _M(__i, __j) in->col[__j].e[__i]
#define _MO(__i, __j) out->col[__j].e[__i]
#define _M3(a0, a1, a2, a3, a4, a5, a6, a7, a8) \
( a0 * (a4 * a8 - a5 * a7) \
- a1 * (a3 * a8 - a5 * a6) \
+ a2 * (a3 * a7 - a4 * a6) ) * d
float ka = _M(2, 2) * _M(3, 3) - _M(2, 3) * _M(3, 2);
float kb = _M(2, 1) * _M(3, 3) - _M(2, 3) * _M(3, 1);
float kc = _M(2, 1) * _M(3, 2) - _M(2, 2) * _M(3, 1);
float kd = _M(2, 0) * _M(3, 3) - _M(2, 3) * _M(3, 0);
float ke = _M(2, 0) * _M(3, 2) - _M(2, 2) * _M(3, 0);
float kf = _M(2, 0) * _M(3, 1) - _M(2, 1) * _M(3, 0);
float d = _M(0, 0) * (ka * _M(1, 1) - kb * _M(1, 2) + kc * _M(1, 3))
- _M(0, 1) * (ka * _M(1, 0) - kd * _M(1, 2) + ke * _M(1, 3))
+ _M(0, 2) * (kb * _M(1, 0) - kd * _M(1, 1) + kf * _M(1, 3))
- _M(0, 3) * (kc * _M(1, 0) - ke * _M(1, 1) + kf * _M(1, 2));
if(fabs(d) <= 0.0000000001f) // find a good epsilon
{
gb_mat4_identity(out);
return false;
}
d = 1.0f / d;
_MO(0, 0) = _M3(_M(1, 1), _M(1, 2), _M(1, 3), _M(2, 1), _M(2, 2), _M(2, 3), _M(3, 1), _M(3, 2), _M(3, 3));
_MO(0, 1) = -_M3(_M(0, 1), _M(0, 2), _M(0, 3), _M(2, 1), _M(2, 2), _M(2, 3), _M(3, 1), _M(3, 2), _M(3, 3));
_MO(0, 2) = _M3(_M(0, 1), _M(0, 2), _M(0, 3), _M(1, 1), _M(1, 2), _M(1, 3), _M(3, 1), _M(3, 2), _M(3, 3));
_MO(0, 3) = -_M3(_M(0, 1), _M(0, 2), _M(0, 3), _M(1, 1), _M(1, 2), _M(1, 3), _M(2, 1), _M(2, 2), _M(2, 3));
_MO(1, 0) = -_M3(_M(1, 0), _M(1, 2), _M(1, 3), _M(2, 0), _M(2, 2), _M(2, 3), _M(3, 0), _M(3, 2), _M(3, 3));
_MO(1, 1) = _M3(_M(0, 0), _M(0, 2), _M(0, 3), _M(2, 0), _M(2, 2), _M(2, 3), _M(3, 0), _M(3, 2), _M(3, 3));
_MO(1, 2) = -_M3(_M(0, 0), _M(0, 2), _M(0, 3), _M(1, 0), _M(1, 2), _M(1, 3), _M(3, 0), _M(3, 2), _M(3, 3));
_MO(1, 3) = _M3(_M(0, 0), _M(0, 2), _M(0, 3), _M(1, 0), _M(1, 2), _M(1, 3), _M(2, 0), _M(2, 2), _M(2, 3));
_MO(2, 0) = _M3(_M(1, 0), _M(1, 1), _M(1, 3), _M(2, 0), _M(2, 1), _M(2, 3), _M(3, 0), _M(3, 1), _M(3, 3));
_MO(2, 1) = -_M3(_M(0, 0), _M(0, 1), _M(0, 3), _M(2, 0), _M(2, 1), _M(2, 3), _M(3, 0), _M(3, 1), _M(3, 3));
_MO(2, 2) = _M3(_M(0, 0), _M(0, 1), _M(0, 3), _M(1, 0), _M(1, 1), _M(1, 3), _M(3, 0), _M(3, 1), _M(3, 3));
_MO(2, 3) = -_M3(_M(0, 0), _M(0, 1), _M(0, 3), _M(1, 0), _M(1, 1), _M(1, 3), _M(2, 0), _M(2, 1), _M(2, 3));
_MO(3, 0) = -_M3(_M(1, 0), _M(1, 1), _M(1, 2), _M(2, 0), _M(2, 1), _M(2, 2), _M(3, 0), _M(3, 1), _M(3, 2));
_MO(3, 1) = _M3(_M(0, 0), _M(0, 1), _M(0, 2), _M(2, 0), _M(2, 1), _M(2, 2), _M(3, 0), _M(3, 1), _M(3, 2));
_MO(3, 2) = -_M3(_M(0, 0), _M(0, 1), _M(0, 2), _M(1, 0), _M(1, 1), _M(1, 2), _M(3, 0), _M(3, 1), _M(3, 2));
_MO(3, 3) = _M3(_M(0, 0), _M(0, 1), _M(0, 2), _M(1, 0), _M(1, 1), _M(1, 2), _M(2, 0), _M(2, 1), _M(2, 2));
#undef _M3
#undef _M
#undef _MO
return true;
}
Is it something we can merge to upstream? Probably would be nice to have affine and generic versions for all implemented matrix sizes as well.
from gb.
In the next update (v0.07), I will have inverse functions for all the matrix types.
from gb.
Related Issues (20)
- 4d-vectors as columns in 2 by 2 matrix HOT 1
- Clarify column-major vs row-major (docs) HOT 1
- gb_mat4_inverse still broken a bit (0.07b) HOT 1
- Line 327~329 in gb_string.h will core dump HOT 1
- gb_utf8_decode
- gb_math: gb_rectX_union
- file_last_write_time on non-windows
- Cant not use at vs2008
- the gb.h inside demo is outdate HOT 1
- gb_memcopy returns invalid pointer
- Errors when compiling HOT 1
- Quaternion slerp fairly broken HOT 3
- Improving Quality of Exponential and Logarithm Functions HOT 2
- Clang compiler detected as gcc
- GB_MATH_NO_MATH_H's gb_tan doesn't handle values over half PI
- gb_float22_mul_vec2 and gb_float33_mul_vec3 seem to expect a row-major matrix HOT 1
- `gb_mod` is broken
- Optimized version of gb_quat_euler_angles
- `GB_NO_WINDOWS_H` doesn't compile
- gb_gen_range_isze generates random values outside of its range
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gb.