Code Monkey home page Code Monkey logo

zlm's Introduction

zlm

Zig linear mathemathics library.

Current provides the following types:

  • Vec2
  • Vec3
  • Vec4
  • Mat2
  • Mat3
  • Mat4

The library is currently built around the OpenGL coordinate system and is fully generic on the basic data type.

Example

const math = @import("zlm");

// Use this namespace to get access to a Vec3 with f16 fields instead of f32
const math_f16 = math.SpecializeOn(f16);

/// Accelerate the given velocity `v` by `a` over `t`.
fn accelerate(v: math.Vec3, a: math.Vec3, t: f32) math.Vec3 {
  return v.add(a.scale(t));
}

zlm's People

Contributors

antlilja avatar avokadoen avatar eriksik2 avatar fabioarnold avatar flecart avatar guigui220d avatar joachimschmidt557 avatar mahbestbro avatar masterq32 avatar mondegreengames avatar nektro avatar sirius902 avatar whatupdave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

zlm's Issues

matrix's mul method doesn't respect column-major rule

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?

Add matrix inverse function

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

3-dimension scaling

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.