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

Linear sampling and texture wrapping #82

Open
Leadwerks opened this issue Sep 30, 2021 · 1 comment
Open

Linear sampling and texture wrapping #82

Leadwerks opened this issue Sep 30, 2021 · 1 comment

Comments

@Leadwerks
Copy link

Leadwerks commented Sep 30, 2021

Here is some code that adds linear sampling and texture wrapping:

    TGAColor TGAImage::sample(const float x, const float y) const {
        float u = fmodf(x,1.0f);
        float v = 1.0f- fmodf(y,1.0f);

        int x0 = floor(u * float(width));
        int x1 = ceil(u * float(width));
        int y0 = floor(v * float(height));
        int y1 = ceil(v * float(height));

        TGAColor sample00 = get(x0, y0);
        TGAColor sample01 = get(x0, y1);
        TGAColor sample11 = get(x1, y1);
        TGAColor sample10 = get(x1, y0);

        float mx = u * float(width) - float(x0);
        float my = v * float(height) - float(y0);

        float sample0[4], sample1[4];

        TGAColor color;

        for (int n = 0; n < bytespp; ++n)
        {
            sample0[n] = float(sample10[n]) * mx + float(sample00[n]) * (1.0f - mx);
            sample1[n] = float(sample11[n]) * mx + float(sample01[n]) * (1.0f - mx);
            color[n] = sample1[n] * my + sample0[n] * (1.0f - my);
        }

        return color;
    }

    TGAColor TGAImage::get(const int x, const int y) const {
        //if (!data.size() || x < 0 || y < 0 || x >= width || y >= height)
        //    return {};
        return TGAColor(data.data() + ((x % width) + (y % height) * width) * bytespp, bytespp);
    }
@mikkorantalainen
Copy link

This kind of sampling is usually called bilinear texture mapping, right?

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