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

[rshapes] Line drawing bug when using Camera2D #4075

Closed
mfbulut opened this issue Jun 18, 2024 · 2 comments
Closed

[rshapes] Line drawing bug when using Camera2D #4075

mfbulut opened this issue Jun 18, 2024 · 2 comments

Comments

@mfbulut
Copy link

mfbulut commented Jun 18, 2024

I tested this on latest commit (commit/52f2a10db610d0e9f619fd7c521db08a876547d0)

Issue description

When using Camera2D Line drawing i want to draw lines from center of the screen to somewhere else here is code i used

Environment

I use Windows 11 with Nvdia graphics card more information below

INFO: GLFW platform: Win32
INFO: PLATFORM: DESKTOP (GLFW): Initialized successfully
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 403
INFO: GL: OpenGL device information:
INFO: > Vendor: NVIDIA Corporation
INFO: > Renderer: NVIDIA GeForce RTX 3060 Laptop GPU/PCIe/SSE2
INFO: > Version: 3.3.0 NVIDIA 555.99
INFO: > GLSL: 3.30 NVIDIA via Cg compiler

Issue Screenshot

Ekran görüntüsü 2024-06-18 165912

Code Example

#include "raylib.h"

const char* title = "Title";
const int screenWidth = 1280;
const int screenHeight = 720;
Camera2D camera;

int main()
{
    camera.offset = (Vector2){ screenWidth / 2, screenHeight / 2 };
    camera.target = (Vector2){ 0, 0 };
    camera.rotation = 0.0f;
    camera.zoom = screenHeight / 2.0f;

    SetConfigFlags(FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT);
    InitWindow(screenWidth, screenHeight, title);

    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(BLACK);
        BeginMode2D(camera);

        DrawCircleV((Vector2){0, 0}, 0.1f, WHITE);  // Centered
        DrawLine(0, 0, 0, 1, WHITE);  // Not centered, why?

        EndMode2D();
        EndDrawing();
    }
    CloseWindow();
}
@mfbulut
Copy link
Author

mfbulut commented Jun 19, 2024

Found the source of the issue. Adding 0.5f would center the pixel if using default camera but when using a diffrent camera it causes some issues.

void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color)
{
    rlBegin(RL_LINES);
        rlColor4ub(color.r, color.g, color.b, color.a);
        // WARNING: Adding 0.5f offset to "center" point on selected pixel
        rlVertex2f((float)startPosX + 0.5f, (float)startPosY + 0.5f);
        rlVertex2f((float)endPosX + 0.5f, (float)endPosY + 0.5f);
    rlEnd();
}

Potential fixes:

  1. Remove 0.5f offset return to old version (does it really matter);
  2. Divide 0.5f with camera's zoom level

@raysan5
Copy link
Owner

raysan5 commented Jun 23, 2024

@mfbulut Oh! Good catch! Reviewing it!

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