r/libgdx•
Versec

Shaperenderer fails to draw top and right edges properly

Hi people. I'm working on a 2d game, and for some reason when trying to draw an empty box the size of the viewport (480,360), the top and right edges don't appear. I have turned off the debug option for other things I had on the screen because I thought something might be on top of it, but the edges don't appear. I'm not sure if there is a discrepancy between how the program is understanding where the viewport ends or if this is normal.

public Renderer(){
	Srenderer= new ShapeRenderer();
	
	camera = new OrthographicCamera();
	camera.setToOrtho(false);
	viewP = new FitViewport(480f, 360f, camera);
}

public void render (Simulation simulation, float delta) {
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);	
	Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
	camera.update();
	
	Srenderer.setProjectionMatrix(camera.combined);
	Srenderer.begin(ShapeType.Line);
	Srenderer.setColor(1, 1, 1, 1);
	Srenderer.box(0f, 0f, 0f, 480f, 360f, 0f);
	
	Srenderer.end();
}

If i reduce the size of the Box by one:

Srenderer.box(0f, 0f, 0f, 479f, 359f, 0f);

You can see the missing edges, but you can also see that is not drawing it "on the border" as the other edges, specially if you go full screen.

Can someone explain why does this happen? Thanks in advance.

Posted May 21, 2015, 8:41 PM UTC

33
Xoppa•

Looks like you're using imaginary pixels. You probably want to reconsider that (see blog.xoppa.com/pixels). If you want to render pixel perfect, then the first thing you should make sure is that your projection is pixel perfect (using ScreenViewport e.g.).

If it would be pixel perfect then if you have a viewport of 480x360, it would be 480 pixels width and 360 pixels in height. Pixels are integers. Your projection and GPU implementation decide how fractional coordinates are projected to these integer pixels. Usually the center of each pixel is used. So, the lower left pixel is located at 0.5, 0.5.

If your code would be pixel perfect then you would be rendering a box at 0, 0 with the size of 480, 360. The lower left coordinates would be 0,0, which would be rounded up to 0.5,0.5. Add 480x360 to add and you'll end up with 480.5, 360.5. Keep in mind that the values you specify aren't pixels.

3
VersecOP•

Hi Xoppa, thanks for answering! I have read you blog post and looked at similar stuff, and yes, I understand using virtual units is better than pixels in most cases.

However, I have modified my program to use virtual units and try to draw the same thing (white box on the border); and the same result happens: top and right borders are not being drawing, so with what you say here...

Pixels are integers. Your projection and GPU implementation decide how fractional coordinates are projected to these integer pixels. Usually the center of each pixel is used. So, the lower left pixel is located at 0.5, 0.5.

If your code would be pixel perfect then you would be rendering a box at 0, 0 with the size of 480, 360. The lower left coordinates would be 0,0, which would be rounded up to 0.5,0.5. Add 480x360 to add and you'll end up with 480.5, 360.5. Keep in mind that the values you specify aren't pixels.

There is no way to make them both agree?

Thanks again.

1
Xoppa•

If you want it to be pixel perfect then you should use a pixel perfect projection (and not virtual units). E.g. ScreenViewport. You could slightly move your camera (e.g. to x:0.47, y:0.47) to make up for the pixel center without having some (rare) gpu's rounding up 0.5 to 1 (although thats probably only relevant for texture filtering).

What do you mean with "both agree"?

Perhaps think about it another way. Pixels don't have a relevant size (for you they are integers, which you can use to index them, but that has nothing to do with its actual size). So a vertical line has zero width, it just has an x coordinate. Two vertical lines next to each other (perhaps the smallest rectangle in your case) has a width of one: it has an x coordinate, and a width of one indicating that the next line should be on the next pixel (note the difference when you would be using actual pixels, in which case the width would be 2 pixels). Two vertical lines one pixel apart from eachother (which is what you would call a rectangle with a width of three I guess) has a width of two: it has an x coordinate and a width of two indicating that the next line should on the second next pixel. In other words: the width is not in pixels, because pixels dont have a relevant width.

The size you specifiy (which is unrelated to pixels) is just added to the origin. When rendering that location is ultimately mapped to a pixel on your screen. You expect the value x:480 to be mapped to the horizontal pixel 479. Luckily (for various reasons) that isnt the case. When you have a pixel perfect projection then the easiest way to fix your expectations is to substract one for the dimensions.

2