package renderer;

import renderer.canvas.ContributionBuffer;
import java.util.concurrent.Callable;

public class TileProcessor implements Callable<Void> {

    private final ContributionBuffer buffer;

    private final FragmentProgram program;

    private final int width;
    private final int height;
    private final int x0;
    private final int y0;
    private final int x1;
    private final int y1;

    public TileProcessor(ContributionBuffer buffer, int x0, int y0, int x1, int y1, int width, int height, FragmentProgram program) {
        this.buffer = buffer;
        this.width = width;
        this.height = height;
        this.x0 = x0;
        this.y0 = y0;
        this.x1 = x1;
        this.y1 = y1;
        this.program = program;
    }

    @Override
    public Void call() {
        double widthTransform = 1.0 / (double) this.width * 2.0;
        double heightTransform = 1.0 / (double) this.height * 2.0;

        for (int x = this.x0; x < x1; x++) {
            double u = (x + Math.random()) * widthTransform - 1.0;

            for (int y = this.y0; y < y1; y++) {
                double v = (y + Math.random()) * heightTransform - 1.0;

                this.buffer.contribute(x, y, this.program.fragment(u, v));
            }
        }

        return null;
    }
}
