dwmkerr / glmnet

GlmNet is a .NET version of the excellent OpenGL Mathematics library (GLM).
MIT License
53 stars 20 forks source link

Fluent API for mat4? #16

Open observant2 opened 4 years ago

observant2 commented 4 years ago

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! šŸ˜‰

dwmkerr commented 4 years ago

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

dwmkerr commented 4 years ago

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