Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pixel perfect DrawRectangleLines under OpenGL 1.1 #7

Closed
debiatan opened this issue Nov 17, 2014 · 2 comments
Closed

Pixel perfect DrawRectangleLines under OpenGL 1.1 #7

debiatan opened this issue Nov 17, 2014 · 2 comments

Comments

@debiatan
Copy link
Contributor

Currently, several pixel-based routines in RayLib are not pixel-perfect. I have picked DrawRectangleLines because I am using it in a program of mine.

DrawRectangleLines takes four arguments that define the shape of the rectangle to draw (int posX, int posY, int width, int height) and generates eight rlVertex2i calls to draw lines that define a square with corners in(posX+1, posY+1), (posX+width, posY+1), (posX+width, posY+height) and (posX+1, posY+height). Those corners are offset by one pixel, in both the horizontal and the vertical axis, from the intended behavior of the call.

Leaving aside that superficial mistake, in order to get pixel-perfect results, care must be taken to recompile the library after uncommenting the following line from core.c:BeginDrawing:

//  rlTranslatef(0.375, 0.375, 0);  // HACK to have 2D pixel-perfect drawing on OpenGL 1.1

Since that solution is less than ideal, and given that the initial orthographic projection is set as

rlOrtho(0, width - offsetX, height - offsetY, 0, 0, 1);   // top-left corner --> (0,0)

inside rlgl.c:rlglInitGraphics, I would suggest offsetting the corners of the rectangle by half a pixel so that they fall on their intended position. DrawRectangleLines would then read:

void DrawRectangleLines(int posX, int posY, int width, int height, Color color)
{
    rlBegin(RL_LINES);
        rlColor4ub(color.r, color.g, color.b, color.a);
        rlVertex2f(posX + .5, posY + .5);
        rlVertex2f(posX + width - .5, posY + .5);

        rlVertex2f(posX + width - .5, posY + .5);
        rlVertex2f(posX + width, posY + height - .5);

        rlVertex2f(posX + width - .5, posY + height - .5);
        rlVertex2f(posX + .5, posY + height - .5);

        rlVertex2f(posX + .5, posY + height - .5);
        rlVertex2f(posX + .5, posY + - .5);
    rlEnd();
}

I understand that working with non-integer coordinates can get cumbersome quickly, but only the rest of the 2D primitives of shapes.c would need to be adapted and users of RayLib would be blissfully shielded from having to think in terms of floating-point coordinates or from recompiling the library.

What do you think?

Cheers!

@raysan5
Copy link
Owner

raysan5 commented Nov 20, 2014

Hi debiatan! Excuse me for the late response!

Getting pixel-perfect rendering in OpenGL is quite difficult, actually, it highly depends on the graphic card and the driver implementation.

I tested same raylib program on different hardware (AMD/NVIDIA) and I get different results on pixel-alignment.

You can read more about this issue here and here.

Additionally, if you activate some kind of MSAA, disabled by default on raylib (core.c:InitDisplay():914), result are even worse.

I'm afraid there is not a perfect solution...

@debiatan
Copy link
Contributor Author

I see. That is a sad state of affairs. I'll try drawing degenerate
triangles, instead. I hope vendors will get those right.

Thanks for the discussion and the links.

2014-11-20 19:42 GMT+01:00 Ray [email protected]:

Hi debiatan! Excuse me for the late response!

Getting pixel-perfect rendering in OpenGL is quite difficult, actually, it
highly depends on the graphic card and the driver implementation.

I tested same raylib program on different hardware (AMD/NVIDIA) and I get
different results on pixel-alignment.

You can read more about this issue here
http://stackoverflow.com/questions/10040961/opengl-pixel-perfect-2d-drawing
and here
https://www.opengl.org/discussion_boards/showthread.php/148444-Pixel-perfect-drawing
.

Additionally, if you activate some kind of MSAA, disabled by default on
raylib (core.c:InitDisplay():914), result are even worse.

I'm afraid there is not a perfect solution...


Reply to this email directly or view it on GitHub
#7 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants