order independent transformations for 2D framework

I’m working on a 2D framework based on OpenGL ES 2 and I was wondering what’s the best way to manage transformations on a quad.

Every quad is using a 4x4 modelview matrix to control the transformations, so the order of the operations is important, but it shouldn’t be from the (framework) user point of view.

So basically what I need is an interface like

class Quad
{
    ...
    SetPosition(x, y);
    Move(dx, dy);

    SetRotation(and);
    Rotate(ang);

    SetScale(s);
    Scale(s);
    ...
};

and an user should be able to call these functions in any order.

As first idea, I was thinking to reset the modelview matrix and then re-apply the 3 transformations in a precise order in every function, so basically something like:

SetPosition(x, y)
{
    ...
    _mvMatrix.LoadIdentity();
    _mvMatrix.Scale(...);
    _mvMatrix.Translate(...);
    _mvMatrix.Rotate(...);
    ...
}

or maybe having some kind of “lazy transformation” with an high-level begin/end construct like:

    
    // reset the modelview matrix
    BeginTransformations();
        // perform the transformations on scalar data members
        Rotate(ang);
        Translate(x,y)
        ...
        Scale(s);
    // perform the transformations on the modelview matrix
    EndTransformations();

where all the operations on the modelview matrix are executed when calling EndTransformation() (to save repeated operations).

Should I go for one of these 2 solutions or I’m totally wrong and totally missing something here?

Thinking about it, I realized there’s no need for a begin/end construct because the update of the modelview matrix could happen just before the rendering, if needed.

Now my Render() function looks like this:

Render()
{
    // update the modelview matrix if needed
    if(_updateModelviewMat)
        UpdateModelviewMat();

    // rendering code
    ...
}

and _updateModelviewMat is set to true every time a Translate, Rotate or Scale function is called.

Still wondering if this is the best solution for the problem though.

I’m working on a 2d project, and currently I’m doing something like this:

 
        txmap->bind();

        glTranslatef(position.x,position.y,0.0);
        glRotatef(theta,0,0,-1);
        glScalef(scale.x,scale.y,1);
        glTranslatef(-centerOffset.x,-centerOffset.y,0);

        mesh->draw();

and so all the getters/setters would just set the variables “position”, “theta”, “centerOffset”, and “scale”.
So, if you wanted to take a point in the mesh and position it in the world, FIRST you offset it to the desired center of rotation/scaling (so if you had a quad width 2 centered around the origin, and said centerOffset={-1,1}, then the quad would rotate and scale around its upper lefthand corner), then you scale it, then you rotate it, and finally, you offset it to the desired position in the world.

I think that’s the most useful and meaningful data. You can get the objects center, angle, scaling and what not easily. If your setters rebuilt the matrix and then discarded the variable, your getters would have to find the transformation info in that matrix, which would just waste time.

hey NeuroFuzzy, basically your code is doing the same as the code in my last post, good to know that :slight_smile: