draw Affine Texture Scan Line - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

draw Affine Texture Scan Line

Demo Code


//package com.java2s;
import java.awt.image.BufferedImage;

public class Main {
    private static void drawAffineTextureScanLine(int y, int x0, int x1,
            int u0, int v0, int u1, int v1, BufferedImage image, int width,
            BufferedImage texture) {

        if (x0 != x1) {
            if (x1 < x0) {
                int temp = x1;
                x1 = x0;/*from  w  ww. java  2s.c  om*/
                x0 = temp;

                temp = u1;
                u1 = u0;
                u0 = temp;

                temp = v1;
                v1 = v0;
                v0 = temp;
            }

            int length = x1 - x0;

            int uadd = ((u1 - u0) * 256) / length;
            int u = u0 * 256;
            int vadd = ((v1 - v0) * 256) / length;
            int v = v0 * 256;

            for (int loop = x0; loop < x1; ++loop) {

                if (loop > 0 && loop < width) {
                    int rgb = texture
                            .getRGB((u >> 8) & 0xff, v >> 8 & 0xff);
                    image.setRGB(loop, y, rgb);
                }

                u += uadd;
                v += vadd;
            }
        }
    }
}

Related Tutorials