ziglibs / zlm Goto Github PK
View Code? Open in Web Editor NEWZig linear mathemathics
License: MIT License
Zig linear mathemathics
License: MIT License
As an example here's one for a 4x4 matrix
fn inverseMat4(m: Mat4) Mat4 {
const A2323 = m.fields[2][2] * m.fields[3][3] - m.fields[2][3] * m.fields[3][2];
const A1323 = m.fields[2][1] * m.fields[3][3] - m.fields[2][3] * m.fields[3][1];
const A1223 = m.fields[2][1] * m.fields[3][2] - m.fields[2][2] * m.fields[3][1];
const A0323 = m.fields[2][0] * m.fields[3][3] - m.fields[2][3] * m.fields[3][0];
const A0223 = m.fields[2][0] * m.fields[3][2] - m.fields[2][2] * m.fields[3][0];
const A0123 = m.fields[2][0] * m.fields[3][1] - m.fields[2][1] * m.fields[3][0];
const A2313 = m.fields[1][2] * m.fields[3][3] - m.fields[1][3] * m.fields[3][2];
const A1313 = m.fields[1][1] * m.fields[3][3] - m.fields[1][3] * m.fields[3][1];
const A1213 = m.fields[1][1] * m.fields[3][2] - m.fields[1][2] * m.fields[3][1];
const A2312 = m.fields[1][2] * m.fields[2][3] - m.fields[1][3] * m.fields[2][2];
const A1312 = m.fields[1][1] * m.fields[2][3] - m.fields[1][3] * m.fields[2][1];
const A1212 = m.fields[1][1] * m.fields[2][2] - m.fields[1][2] * m.fields[2][1];
const A0313 = m.fields[1][0] * m.fields[3][3] - m.fields[1][3] * m.fields[3][0];
const A0213 = m.fields[1][0] * m.fields[3][2] - m.fields[1][2] * m.fields[3][0];
const A0312 = m.fields[1][0] * m.fields[2][3] - m.fields[1][3] * m.fields[2][0];
const A0212 = m.fields[1][0] * m.fields[2][2] - m.fields[1][2] * m.fields[2][0];
const A0113 = m.fields[1][0] * m.fields[3][1] - m.fields[1][1] * m.fields[3][0];
const A0112 = m.fields[1][0] * m.fields[2][1] - m.fields[1][1] * m.fields[2][0];
const det = 1 / (m.fields[0][0] * (m.fields[1][1] * A2323 - m.fields[1][2] * A1323 + m.fields[1][3] * A1223) - m.fields[0][1] * (m.fields[1][0] * A2323 - m.fields[1][2] * A0323 + m.fields[1][3] * A0223) + m.fields[0][2] * (m.fields[1][0] * A1323 - m.fields[1][1] * A0323 + m.fields[1][3] * A0123) - m.fields[0][3] * (m.fields[1][0] * A1223 - m.fields[1][1] * A0223 + m.fields[1][2] * A0123));
return Mat4{
.fields = [4][4]f32{
[4]f32{
det * (m.fields[1][1] * A2323 - m.fields[1][2] * A1323 + m.fields[1][3] * A1223),
det * -(m.fields[0][1] * A2323 - m.fields[0][2] * A1323 + m.fields[0][3] * A1223),
det * (m.fields[0][1] * A2313 - m.fields[0][2] * A1313 + m.fields[0][3] * A1213),
det * -(m.fields[0][1] * A2312 - m.fields[0][2] * A1312 + m.fields[0][3] * A1212),
},
[4]f32{
det * -(m.fields[1][0] * A2323 - m.fields[1][2] * A0323 + m.fields[1][3] * A0223),
det * (m.fields[0][0] * A2323 - m.fields[0][2] * A0323 + m.fields[0][3] * A0223),
det * -(m.fields[0][0] * A2313 - m.fields[0][2] * A0313 + m.fields[0][3] * A0213),
det * (m.fields[0][0] * A2312 - m.fields[0][2] * A0312 + m.fields[0][3] * A0212),
},
[4]f32{
det * (m.fields[1][0] * A1323 - m.fields[1][1] * A0323 + m.fields[1][3] * A0123),
det * -(m.fields[0][0] * A1323 - m.fields[0][1] * A0323 + m.fields[0][3] * A0123),
det * (m.fields[0][0] * A1313 - m.fields[0][1] * A0313 + m.fields[0][3] * A0113),
det * -(m.fields[0][0] * A1312 - m.fields[0][1] * A0312 + m.fields[0][3] * A0112),
},
[4]f32{
det * -(m.fields[1][0] * A1223 - m.fields[1][1] * A0223 + m.fields[1][2] * A0123),
det * (m.fields[0][0] * A1223 - m.fields[0][1] * A0223 + m.fields[0][2] * A0123),
det * -(m.fields[0][0] * A1213 - m.fields[0][1] * A0213 + m.fields[0][2] * A0113),
det * (m.fields[0][0] * A1212 - m.fields[0][1] * A0212 + m.fields[0][2] * A0112),
},
},
};
}
test "inverse" {
const input = Mat4{
.fields = [4][4]f32{
[4]f32{ 4, 0, 0, 0 },
[4]f32{ 0, 0, 2, 0 },
[4]f32{ 0, 1, 2, 0 },
[4]f32{ 1, 0, 0, 1 },
},
};
const expected = Mat4{
.fields = [4][4]f32{
[4]f32{ 0.25, 0, 0, 0 },
[4]f32{ 0, -1, 1, 0 },
[4]f32{ 0, 0.5, 0, 0 },
[4]f32{ -0.25, 0, 0, 1 },
},
};
assert(std.meta.eql(inverseMat4(input), expected));
}
Which is ported from the output of this program: https://github.com/willnode/N-Matrix-Programmer
Hi guys, I'm experimenting opengl using zlm, and the matrix part confuses me a little.
If my understanding is right, zlm's matrix is using column-major style like glm/opengl,
as utility functions like createLookAt/createPerspective are token from glm. However,
the mul method of matrix is using row-major style instead, which is bit confusing and
caused different mul order when composing mvp matrix:
in glm:
mvp = project * view * model
in zlm:
mvp = model.mul(view).mul(project)
Is it an accident or by design?
Currently scaling (on Mat4) is restricted to having one value for all axes, so there is no way to scale an object in different directions, like only scaling it on X, Y or Z axis.
To fix that i would suggest replacing
pub fn createScale(scale: Real) Self {
return Self{
.fields = [4][4]Real{
[4]Real{ scale, 0, 0, 0 },
[4]Real{ 0, scale, 0, 0 },
[4]Real{ 0, 0, scale, 0 },
[4]Real{ 0, 0, 0, 1 },
},
};
}
by:
pub fn createScale(v: Vec3) Self {
return Self{
.fields = [4][4]Real{
[4]Real{ v.x, 0, 0, 0 },
[4]Real{ 0, v.y, 0, 0 },
[4]Real{ 0, 0, v.z, 0 },
[4]Real{ 0, 0, 0, 1 },
},
};
}
and also adding a createScaleXYZ
variant.
Also, a completely unrelated question: why are the matrixes in this library row-ordered instead of column-ordered like OpenGL? This forces to transpose the matrix before feeding it to OpenGL which is strange for a library built around OpenGL coordinate system.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.