Problems with ofRect (missing pixels)

When I draw ofRect() (no fill mode), one pixel is always missing, but not the same on Mac (top/left) and windows (top/right). Such error doesn’t occur in the corresponding rect() method in processing (correct both on Mac and Windows).

When drawing a filled rectangle with same coordinates :

  • on Mac, the top and left lines are not drawn
  • on Windows, the top and right lines are not drawn
    in processing, the bottom and right lines are not drawn

I used the following tests:

  
  
void testApp::draw(){  
	int x0, y0, xSize, ySize, xShift;  
  
	x0 = 100; y0 = 100; xSize = 100; ySize = 100;  
	xShift = 120;  
  
	ofSetColor(0x000000);  
	ofNoFill();  
  
// On Mac, Left/Top point of rectangle always missing:  
// On Windows, Right/Top point of rectangle always missing:  
	ofRect(x0, y0, xSize, ySize);  
  
	x0 += xShift;  
  
// On Mac, same result(still missing Left/Top):  
// On Windows, same result(still missing Right/Top):  
	ofLine(x0, y0, x0+xSize, y0);  
	ofLine(x0, y0, x0, y0+ySize);  
	ofLine(x0+xSize, y0, x0+xSize, y0+ySize);  
	ofLine(x0, y0+ySize, x0+xSize, y0+ySize);  
  
	x0 += xShift;  
  
// When starting x 1 point earlier:  
// On Mac, IT'S OK NOW !!!  
  
	ofLine(x0-1, y0, x0+xSize, y0);  
	ofLine(x0, y0, x0, y0+ySize);  
	ofLine(x0+xSize, y0, x0+xSize, y0+ySize);  
	ofLine(x0, y0+ySize, x0+xSize, y0+ySize);  
  
	x0 += xShift;  
  
// When adding 1 point to the top line:  
// On Windows, IT'S OK NOW !!!  
  
	ofLine(x0, y0, x0+xSize+1, y0);  
	ofLine(x0, y0, x0, y0+ySize);  
	ofLine(x0+xSize, y0, x0+xSize, y0+ySize);  
	ofLine(x0, y0+ySize, x0+xSize, y0+ySize);  
  
	x0 += xShift;  
  
// When trying to cover the rectangle with a filled rectangle  
// On Mac, the left and top lines remain still visible!  
// On Windows, the right and top lines remain still visible!  
	ofRect(x0, y0, xSize, ySize);  
	ofSetColor(0x00FF00);  
	ofFill();  
	ofRect(x0, y0, xSize, ySize);  
}  
  
  

and in processing:

  
  
int x0, y0, xSize, ySize, xShift;  
x0 = 100; y0 = 100; xSize = 100; ySize = 100;  
xShift = 120;  
  
size(600,400);  
noFill();  
  
// OK, perfect!  
rect(x0, y0, xSize, ySize);  
  
x0 += xShift;  
  
// By trying to cover the rectangle with a filled rectangle  
// On Mac and on Windows, the right and bottom lines remain still visible:  
rect(x0, y0, xSize, ySize);  
fill(0, 255, 0);  
noStroke();  
rect(x0, y0, xSize, ySize);  
  

Did you ever figure anything out with this?

I am seeing the same thing. I am running on Windows using code::blocks.

When I draw a rectangle using ofRect with ofNoFill() on the left my window the rectangle is missing the top right corner pixel. When I move to the right side of the screen I am missing the top left pixel.

With a screen width of 1024 the switch from top right to top left occurs for anything with x >= 601

With a screen width of 1200 the switch from top right to top left occurs for anything with x >= 780. For x = 777 or 778 or 779 the top pixel on both ends is missing.

Really strange.

digitalhack
Rockville, MD

I found a workaround that works on all of my different platforms:

  
  
void ofFrameRect(int x, int y, int w, int h)  
{  
    ofLine (x,     y,   x+w,  y+1); // +1: strange but works!  
    ofLine (x,     y,   x,    y+h);  
    ofLine (x+w,   y,   x+w,  y+h);  
    ofLine (x,     y+h, x+w,  y+h);  
}  
  

I don’t know why it works, but it works

Thanks for the follow up.

Working at it a bit more I came up with the following reference in the OpenGL docs. It solved the problem for me.

http://opengl.org/resources/faq/technic-…-m#tran0030

It says:

If exact pixelization is required, you might want to put a small translation in the ModelView matrix, as shown below:

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.)

I used the following code in OF.

glMatrixMode (GL_MODELVIEW);
ofPushMatrix();
ofTranslate(0.375, 0.375, 0.);

something like ofRect
ofPopMatrix();

From what I read you may need to tweak the number you use in the translate function.

Hope this helps,

digitalhack
Rockville, MD

I know.

I tried it (both 0.375 and 0.5) but it don’t work.