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

[rtextures] Adding triangle drawing function for images #4094

Merged
merged 3 commits into from
Jun 23, 2024

Conversation

Bigfoot71
Copy link
Contributor

New functions added

void ImageDrawTriangle(Image *dst, Vector2 v1, Vector2 v2, Vector2 v3, Color color);
void ImageDrawTriangleEx(Image *dst, Vector2 v1, Vector2 v2, Vector2 v3, Color c1, Color c2, Color c3);
void ImageDrawTriangleLines(Image *dst, Vector2 v1, Vector2 v2, Vector2 v3, Color color);
void ImageDrawTriangleFan(Image *dst, Vector2 *points, int pointCount, Color color);
void ImageDrawTriangleStrip(Image *dst, Vector2 *points, int pointCount, Color color);

Why and how

  1. These functions enable the rendering of triangles on images, supporting any vertex order (CW/CCW).

  2. The ImageDrawTriangleEx function allows the rendering of a triangle with vertex colors specified for interpolation. While not strictly necessary, it serves as a reference for those seeking a more comprehensive function and understanding of its operation.

  3. Additional functions ImageDrawTriangleLines, ImageDrawTriangleFan, and ImageDrawTriangleStrip were introduced to align with rshapes.c.

  4. The chosen rendering method is the barycentric test method. This method was preferred over the scanline method due to its simplicity and the ease with which pixel processing can be parallelized, potentially resulting in significant efficiency gains when implemented correctly.

Basic example

#include <raylib.h>

typedef struct {
    Vector2 p1, p2, p3;
    Color c1, c2, c3;
} Triangle;

int main(void)
{
    InitWindow(800, 600, "Raysterized");

    // CW order
    Triangle t1 = {
        (Vector2) { 0, 0 },
        (Vector2) { 800, 0 },
        (Vector2) { 0, 600 },
        (Color) { 255, 0, 0, 255 },
        (Color) { 0, 255, 0, 255 },
        (Color) { 0, 0, 255, 255 },
    };

    // CCW order
    Triangle t2 = {
        (Vector2) { 800, 0 },
        (Vector2) { 0, 600 },
        (Vector2) { 800, 600 },
        RED
    };

    Image image = GenImageColor(800, 600, BLACK);

    ImageDrawTriangleEx(&image, t1.p1, t1.p2, t1.p3, t1.c1, t1.c2, t1.c3);
    ImageDrawTriangle(&image, t2.p1, t2.p2, t2.p3, t2.c1);

    Texture texture = LoadTextureFromImage(image);

    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(RAYWHITE);
        DrawTexture(texture, 0, 0, WHITE);
        EndDrawing();
    }

    CloseWindow();

    return 0;
}

image

@raysan5
Copy link
Owner

raysan5 commented Jun 23, 2024

@Bigfoot71 Hey! This is a nice addition! Is it ready to merge?

@Bigfoot71
Copy link
Contributor Author

@raysan5 I just fixed two small mistakes in ImageDrawTriangleFan and ImageDrawTriangleStrip.
Everything is tested and working, you can merge if you want! :)

image

@raysan5 raysan5 merged commit 7e50270 into raysan5:master Jun 23, 2024
14 checks passed
@raysan5
Copy link
Owner

raysan5 commented Jun 23, 2024

@Bigfoot71 Nice! Thanks for this addition! 👍😄

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

Successfully merging this pull request may close these issues.

2 participants