raylib: [rshapes] `DrawRectangleLines()` missing pixel in lower right corner

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • I checked the documentation on the wiki
  • My code has no errors or misuse of raylib

Issue description

If you draw a rectangle outline using DrawRectangleLines(0, 0, w, h, WHITE) the lower right corner is missing.

Environment

N/A

Issue Screenshot

/

Code Example

#include "raylib.h"
int main(void) {
    InitWindow(150, 150, "DrawRectangleLines bug");
    while (!WindowShouldClose()) {
        BeginDrawing();
            ClearBackground(BLACK);
            DrawRectangleLines(0, 0, 100, 100, WHITE); // FIXME: MISSING LOWER RIGHT CORNER
        EndDrawing();
    }
   CloseWindow();        // Close window and OpenGL context
   return 0;
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (14 by maintainers)

Most upvoted comments

I believe this is one of those super annoyances with OpenGL…

It seems that DrawRectangleLines depends on unspecified/undefined behavior which does not guarantee that lines would join in all cases.

[EDIT] Current implementation of DrawRectangleLines tries to mitigate the problem by using glVertex2f with 0.5f offsets, but this still seems not enough. Also related to https://github.com/raysan5/raylib/issues/3884 and https://github.com/raysan5/raylib/issues/4670

CC @raysan5

@sleeptightAnsiC

0.51 does not have an exact float (and double) value on most C Language implementations.

… Hitting perfect pixels with GL_LINES is not fully deterministic, see: https://stackoverflow.com/questions/4532342/opengl-gl-lines-endpoints-not-joining.

I think “deterministic” is the wrong term. They key thing is to know where in a certain “diamond” you must be hitting to light a particular pixel. Then there is the matter of stepping through pixels (as when drawing a line) to light the starting pixel, the ending pixel, and all between. That can definitely depend on how the arithmetic is done and with what precision of operations. It will be a great lesson to try the same thing manually though.

Second-guessing OpenGL implementations is certainly challenging. You could draw a line yourself pixel-by-pixel to confirm what the challenges are, but that will still leave you stymied to figure out how to predict what OpenGL would do in drawing the same line as an internal operation.

PS: To see what struggle is involved, you can investigate what PostScript and TeX go through to obtain precision and predictable graphics at very high DPI.

@Roucou despite that issue is closed, make sure to answer my questions when you’ll have some opportunity, please. I may take a look at fixing/forwarding this anyway.

Am doing some business travel the following days, will do some tests over the weekend. I did play around with switching to float instead of int earlier on, but that resulted in the same missing pixel. Also tried drawing the four lines individually using DrawLine(…) but that gave even weirder results.

@sleeptightAnsiC is probably right, it might very well be basic OpenGL incompatibility issues. So, for my game I’ll probably have to switch to textures as a workaround. But many thanks for the investigation!

I have been wondering if the FP rounding settings on float to int have an impact on these cases

I played a bit with DrawRectangleLines() inside of debugger and don’t really see where it could loose precision or cause rounding error. It does int-to-float cast (not float-to-int) and values from DrawRectangleLines(0, 0, 100, 100, WHITE) are too small to cause this issue (I think).

Maybe it happens somewhere deeper in rlVertex3f()

Image