Code Monkey home page Code Monkey logo

zlm's Issues

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.

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

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.