glRotatef and transformations

Hi, I am doing a data-viz + animation project and using glRotatef() to animate any number of multi-frame objects. This seems to work well with the code:

// Rotation code
glPushMatrix();
glTranslatef( -imageWidth/2 + x,-imageHeight/2 + y, 0);
glRotatef(angle,0,0,1);
glTranslatef(imageWidth/2 - x,imageHeight/2 - y,0);
(pImage+frameNum)->draw(x,y);
glPopMatrix();

…where pImage is an array of pointers ofImage objects (4-8 frames, current frame is frameNum); x and y are the (x,y) drawing position of the object; imageWidth and imageHeight are instance variables of the width + height of current frame (right now, each frame is the same width & height)

I’m not an experienced graphics programmer and am getting stuck on tracking the location of the true bounding box of the image.

Using the code here to see where the bounding box should be (and realizing that I will have to adjust the bounding box to the translated angle, but that’s a different story), the location of the bounding box is very different than the rotated image (see attached)

ofSetColor(255,0,0);
ofNoFill();
ofRect( x,y,imageWidth,imageHeight);

I’ve been monkeying around with the glTranslatef() calls, since this seems like the culprit, but each time I try to modify or alter these functions, I don’t see the animation at all. This is where I’m butting up against the limits of my graphics programming skills.

Also, if I leave the angle as zero, the bounding box draws fine.

I tried using ofImage::setAnchorPoint and ofImage::setAnchorPercent, but had the same problem where I no longer see the image drawn on the screen

Any help is appreciated and thanks,
Scott

When you’re calling translate, rotate, etc, you’re actually creating a bunch of data stored in a matrix that will be used to draw your images and then losing it when you call popMatrix. To get a rectangle that accurately represents the location of your sprite you need to store that data translation and rotation data and then use it to position the upper left and upper right of your rectangle. The way I’d probably do something like this is to store that translate/rotate data in an of4x4Matrix and then multiple my points by it later:

  
ofVec3f updated = mat * original;  

Check out the ofMatrix4x4 class, you’ll see a bunch of methods in there for storing all kinds of rot/trans data.

Thanks Joshua, this is helpful…though I still have to dive into matrices which is a topic I was hoping to avoid :slight_smile:

Something I found and played around with this morning was the ofxAnimationKit add-on, which looks is fairly powerful and can do a lot of the animation tasks I was trying to accomplish on my own. Though, it still has the translation issues that I’ll have to sort out at some point.

Scott