# Rotate and translate images in Z?

hi,
again me…

Is it possible to rotate or translate images in the z axis?

Daniel

sure, any opengl calls can be made, so you can do the following:

``````

void draw(){

glPushMatrix();
glTranslatef(0,0,-200);  // translate back in z by 200
// draw something
glPopMatrix();

}

``````

You may discover the ofSetRectMode (in 0.02) is helpful, because it will draw rects (and rectangular textures, ie images) with the 0,0 from the center.

``````

void draw(){
ofSetRectMode(OF_RECTMODE_CENTER);
glPushMatrix();
glTranslatef(0,0,-200);  // translate back in z by 200
glRotatef(ofRandomuf()*90, 0,1,0); // gl rotate takes 0-360, not 0-TWO_PI
myImage.draw(0,0);  // center at 0,0, so rotation is around center
glPopMatrix();
}

``````

hope that helps!

there’s some info about opengl calls: here and here

next release we will add some opengl try coding calls with some examples of push/pop glTranslate, glRotate, etc.

take care –
zach

You can do rotation and translation in all axis with the openGL commands:

glTranslatef(x, y, z);
glRoatef(angle, x, y, z);

The way that openGL works is that calling either of these commands performs the transformation on the whole openGL ‘world’. So you need to use glPushMatrix() and glPopMatrix() to isolate the transformation to the object you wish to tansform.

A typical example of a z-depth translation would be:

glPushMatrix();
glTranslatef(0,0,-500);
ofRect(100, 100, 50, 50);
glPopMatrix();

Where the -500 would mean to draw the rectangle 500 units back in z space.

Another important thing to know is that all translations and rotations are done around the (0,0,0) position. So in our case this is the top left corner of the window. If you do a rotation of 90 degrees around the z axis and you are drawing the object at 200,200 you will not see the object rotate around its center but instead the entire object will rotate around the top left corner of the screen (like it was orbiting it). The trick then for rotation is to translate the (0,0,0) to the center of the object and then do the rotation.

So for a rectangle at 200,200

``````

void testApp::setup(){
angle = 90.0;
}

void testApp::draw(){

angle += 0.2; //so it rotates
ofSetRectMode(OF_RECTMODE_CENTER);
ofSetColor(100,20,20);
glPushMatrix();
glTranslatef(200,200,0);
glRotatef(angle, 0, 0, 1); //only rotate around the z axis
ofRect(0, 0, 50, 50); //draw at 0,0 as we have translated to 200, 200
glPopMatrix();

}

``````

Also check the advanced graphics example of the new release. There is some rotation and translation that goes on there.

Theo

wow, thank you for your response.
i can work on now with this information.

greets,
dan