Sign in to follow this  
Zotoaster

Getting absolute position/rotation/scale in a 2D scene graph

Recommended Posts

Zotoaster    148

I have a simple scene graph system. Entities can have child entities, and every update on an entity pushes a matrix, applies the transforms, draws that entity and then updates the child entities, before popping the matrix again. Here's the code using OpenGL:

        glPushMatrix();
        glTranslatef(_position.X, _position.Y, 0);
        glRotatef(_angle, 0.0f, 0.0f, 1.0f);
        glScalef(_scale.X, _scale.Y, 0);

        Draw();
        
        for (size_t e = 0; e < _entities.size(); e++)
            _entities[e]->Update();
        
        glPopMatrix();

Now I want to be able to access an entity's absolute transformations. The caveat here is that I need to be able to do it outside the push/pop statements, so the matrices will no longer be correct. Entities have access to their parent so you can access those, but that means that every time I want to check an absolute value, I'd have to recursively go up the parents.

 

Does anyone have any solutions to this?

Share this post


Link to post
Share on other sites

1) Store the absolute transform with the entity

2) Use the fact that matrix multiplication of transforms e.g. (ABCD)-1 = D-1C-1B-1A-1 where X-1 is the inverse transform for X (which is trivial for scale, rotations and translations). This works for any number of composite matrix multiplications (EDIT: accuracy may be a problem though), I just used 4 as an example. Option 1 is probably best.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
Aliii    1456

In case you need it:

 

float matrix[16]

 

glMultMatrixf(   matrix) - apply your own transformation to the current matrix

glLoadMatrixf(  matrix) - set the current matrix for OpenGL

glGetFloatv(     GL_MODELVIEW_MATRIX, matrix) - get the current matrix from OpenGL

 

So you dont have to go through the push-pops if you have your matrices saved.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this