glmnet icon indicating copy to clipboard operation
glmnet copied to clipboard

Fluent API for mat4?

Open observant2 opened this issue 5 years ago • 2 comments

Hi! Thank you for this library! I'm porting code from another language that had a fluent API for matrix operations and I think it would be a nice addition for this library too.

I had this monstrosity:

            var model = glm.translate(
                glm.rotate(
                    glm.translate(
                        glm.translate(mat4.identity(), new vec3(position, 0)),
                        new vec3((float) Width / 2, (float) Height / 2, 0)),
                    glm.radians(rotation),
                    new vec3(0, 0, 1)),
                new vec3((float) -Width / 2, (float) -Height / 2, 0));

Added this:

    public static class Util
    {
        public static mat4 translate(this mat4 m, vec3 v)
        {
            return glm.translate(m, v);
        }
        
        public static mat4 rotate(this mat4 m, float angle, vec3 v)
        {
            return glm.rotate(m, angle, v);
        }

        public static mat4 scale(this mat4 m, vec3 v)
        {
            return glm.scale(m, v);
        }
    }

And got this:

            var model =
                mat4.identity()
                    .translate(new vec3(position, 0))
                    .translate(new vec3((float) Width / 2, (float) Height / 2, 0))
                    .rotate(glm.radians(rotation), new vec3(0, 0, 1))
                    .translate(new vec3((float) -Width / 2, (float) -Height / 2, 0));

I don't have time right now to give it a second thought and create a pull request, but if you don't see any arguments against adding this stuff, it would be highly appreciated! 😄

PS: Extension methods rule! 😉

observant2 avatar Feb 23 '20 08:02 observant2

Hi @itmuckel sounds like a great idea! I'd happy include this in the library, will take a look at it shortly

dwmkerr avatar Mar 03 '20 05:03 dwmkerr

Did a quick look into this should be straightforward to implement by adding the extension methods to some classes (e.g. mat3fluent).

dwmkerr avatar Mar 03 '20 05:03 dwmkerr