Viewport not aware of scaling/translation?

Hi, I have an object that plots graphs like an oscilloscope and it uses viewports to make sure only the data between upper and lower bounds is seen on the screen:

  
	float xPlotScale = (_max.x - _min.x) / (_pointsPerWin - 1);  
	float yPlotOffset = ((_max.y - _min.y) / 2.);  
  
	// Use a viewport to manage plot area and ensure only data in the area is shown.  
	ofRectangle scopePlotViewport(_min, _max);  
  
	// keep a copy of your viewport and transform matrices for later  
	ofPushView();  
		ofViewport(scopePlotViewport);  
		// setup transform matrices for normal oF-style usage, i.e.  
		//  0,0=left,top  
		//  ofGetViewportWidth(),ofGetViewportHeight()=right,bottom  
		ofSetupScreen();  
		ofPushStyle();  
			ofEnableAlphaBlending();  
			ofSetColor(_backgroundColor);  
			ofRect(ofRectangle(0., 0., _max.x - _min.x, _max.y - _min.y));  
  
			// Scope zero line  
			ofSetColor(_zeroLineColor);  
			ofLine(0., (_max.y - _min.y)/2, 0., (_max.y - _min.y)/2);  
			ofDisableAlphaBlending();   
			ofPushMatrix();		  
				ofTranslate(_max.x - _min.x, yPlotOffset);  
				ofScale(-xPlotScale, -_yScale);  
				ofTranslate(0, _yOffset);  
				for (int i=0; i<getNumVariables(); i++) {  
					ofSetColor(_variableColors.at(i));  
					ofSetLineWidth(_plotLineWidth);  
					for (int j=1; j<_pointsPerWin; j++) {   
						ofPoint p1 = ofPoint( ((float) j-1), _buffer.at(i).at(j-1));  
						ofPoint p2 = ofPoint( ((float) j),_buffer.at(i).at(j));  
						ofLine(p1, p2);  
					}  
				}	  
			ofPopMatrix();  
		ofPopStyle();  
	ofPopView();  

Now, this works great until I scale and translate before using the plotting code above, because the plots instead of appearing in the translated position in the window and scaled, appear in the original untranslated position and unscaled.
In order to understand this better I inserted the following debugging code

  
	  
ofSetColor(0,255,0);  
ofRect(scopePlotViewport);  
  

first after ofPushView() and saw that the green rectangle is drawn in the right place - that is, in the translated position and scaled. But when I insert the same debugging code after the line ofViewport(scopePlotViewport) it is already drawn in the wrong place and unscaled.
I obviously am doing this wrong and lack the understanding of how I should be working with viewports.
Any help would be greatly appreciated!
Thanks a lot in advance

1 Like