ofTranslate: Not getting something

I am trying to ofRotateY a grid of boxes the same value of rotation from their center

Each column of boxes appears to be rotated a different value which doesn’t make sense to me. As commented in the code ofRotateZ appears to work correctly.

  
#include "testApp.h"  
  
float rotation =60.0f;  
float boxSize = 40.0f;  
float spacing = 1.0f;  
vector<ofRectangle> boxes;  
//--------------------------------------------------------------  
void testApp::setup() {  
  
	float spacingWidth = ofGetWidth()/(boxSize+spacing);  
	float spacingHeight = ofGetHeight()/(boxSize+spacing);  
	  
	int rangeWidth = (int)((ofGetWidth()-spacingWidth)/boxSize);  
	int rangeHeight = (int)((ofGetHeight()-spacingHeight)/boxSize);  
	for (int i=0; i<rangeWidth; i++)   
	{  
		for (int j=0; j<rangeHeight; j++)   
		{  
			boxes.push_back(ofRectangle((boxSize*i) + (i*spacing), (boxSize*j) + (j*spacing), boxSize, boxSize));  
		}  
	}  
	  
	  
}  
  
//--------------------------------------------------------------  
void testApp::update() {  
	//rotation +=0.2;  
}  
  
//--------------------------------------------------------------  
void testApp::draw() {  
  
	ofSetRectMode(OF_RECTMODE_CENTER);  
  
	for (int i=0; i<boxes.size(); i++)   
	{  
		ofRectangle box = boxes[i];  
		  
		ofPushMatrix();  
			ofPushStyle();  
				ofSetColor(255, 0, 0);  
				  
				  
				ofTranslate(box.x+(boxSize/2), box.y+(boxSize/2));  
				//using Z all boxes rotate the same value  
				//ofRotateZ(rotation);  
				  
				//Using y it seems the x coordinates effect the amount of rotation  
				ofRotateY(rotation);  
				ofRect(0, 0, boxSize, boxSize);  
				ofSetColor(0, 0, 255);  
				ofCircle(0, 0, 2);  
			ofPopStyle();  
		ofPopMatrix();  
	}  
  
  
  
}  

Here is the ofRotateY output:

here is the ofRotateZ output

Hey,

I think you’re just seeing a different “rotation” because of the camera perspective. When you rotate around Z, all the points are still at z=0 in the same plane, so it’s a 2D rotation. When you rotate around Z, the points have different depths, so the camera perspective comes into play. The only strip of rectangles that will appear at the correct rotation of 60 degrees will be the one in the center.

You can try using glOrtho (http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml) at the top of your draw function, I think that will give you the desired effect. Something along the lines of:

  
  
void testApp::draw() {  
  glMatrixMode(GL_PROJECTION);  
  glLoadIdentity();  
  glOrtho(0, ofGetWidth(), 0, ofGetHeight(), -200, 200);  
  glMatrixMode(GL_MODELVIEW);  
  
  // ...draw your rects here  
}  
  

Hope it helps,
-Elie

1 Like

Ah, the camera… In my mind ofPushMatrix and ofTranslate were working together with the camera (resetting the camera’s perspective at each box)

In addition to your code the only thing I had to add was another glLoadIndentity inside of ofPushMatrix. (saw that usage here : [http://forum.openframeworks.cc/index.php/topic,3981.msg20912.html#msg20912]http://forum.openframeworks.cc/index.php/topic,3981.msg20912.html#msg20912[/url]Ah,](http://forum.openframeworks.cc/index.php/topic,3981.msg20912.html#msg20912]http://forum.openframeworks.cc/index.php/topic,3981.msg20912.html#msg20912[/url]Ah,) the camera… In my mind ofPushMatrix and ofTranslate were working together with the camera (resetting the camera’s perspective at each box)

In addition to your code the only thing I had to add was another glLoadIndentity inside of ofPushMatrix. (saw that usage here : http://www.openframeworks.cc/forum/viewtopic.php?p=20912)

Here is the updated draw()

  
//--------------------------------------------------------------  
void testApp::draw() {  
  
	ofSetRectMode(OF_RECTMODE_CENTER);  
	glMatrixMode(GL_PROJECTION);  
	glLoadIdentity();  
	glOrtho(0, ofGetWidth(), ofGetHeight(),0, -200, 200);  
	glMatrixMode(GL_MODELVIEW);  
	  
	for (int i=0; i<boxes.size(); i++)   
	{  
		ofRectangle box = boxes[i];  
		  
		ofPushMatrix();  
			//didn't see anything until this  
			glLoadIdentity();  
			ofPushStyle();  
				ofSetColor(255, 0, 0);  
				  
				  
				ofTranslate(box.x+(boxSize/2), box.y+(boxSize/2), 0);  
  
				ofRotateY(rotation);  
				ofRect(0, 0, boxSize, boxSize);  
								ofSetColor(0, 0, 255);  
				  
				ofCircle(0, 0, 2);  
			ofPopStyle();  
		ofPopMatrix();  
  
	}  
  
  
  
}  

Thanks Elie! Much appreciated - would have been spinning on this forever