Search in sources :

Example 1 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MapCanvas method drawQuad.

private final MapCanvas drawQuad(MapCanvas canvas, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, int half) {
    // This matrix can be cached, saving a precious matrix inversion
    if (canvas.projMatrix == null) {
        Vector3 ip0 = new Vector3(0, 0, 0);
        Vector3 ip1 = new Vector3(0, 0, canvas.getHeight());
        Vector3 ip2 = new Vector3(canvas.getWidth(), 0, canvas.getHeight());
        Vector3 ip3 = new Vector3(canvas.getWidth(), 0, 0);
        canvas.projMatrix = Matrix4x4.computeProjectionMatrix(new Vector3[] { ip0, ip1, ip2, ip3 });
        if (canvas.projMatrix == null) {
            // texture resolution is invalid (0x0?)
            return this;
        }
        canvas.projMatrix.invert();
    }
    Matrix4x4 m0 = Matrix4x4.computeProjectionMatrix(new Vector3[] { p0, p1, p2, p3 });
    if (m0 == null) {
        return this;
    }
    m0.multiply(canvas.projMatrix);
    return drawQuad(canvas, m0, half);
}
Also used : Vector3(com.bergerkiller.bukkit.common.math.Vector3) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 2 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MapCanvas method drawModel.

/**
 * Draws a 3D model onto this canvas, using the position, rotation and scale parameters
 *
 * @param model to draw
 * @param scale to draw the model at
 * @param x - position of the model origin on the canvas
 * @param y - position of the model origin on the canvas
 * @param yaw rotation
 * @param pitch rotation
 * @return this canvas
 */
public final MapCanvas drawModel(Model model, float scale, int x, int y, float yaw, float pitch) {
    Matrix4x4 transform = new Matrix4x4();
    transform.translate(x, 0.0f, y);
    transform.scale(scale);
    transform.rotateX(pitch);
    transform.rotateY(yaw);
    return drawModel(model, transform);
}
Also used : Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 3 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MapCanvas method drawQuad.

private final MapCanvas drawQuad(MapCanvas canvas, Matrix4x4 projectionMatrix, int half) {
    Matrix4x4 mInv = new Matrix4x4(projectionMatrix);
    mInv.invert();
    // Calculate the pixel coordinates of the 3 corners of the projected quad
    Vector3[] corners = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0, canvas.getHeight()), new Vector3(canvas.getWidth(), 0, canvas.getHeight()), new Vector3(canvas.getWidth(), 0, 0) };
    for (Vector3 corner : corners) {
        projectionMatrix.transformPoint(corner);
    }
    // Check whether the face is back-facing or front-facing, and correct light that way
    Vector3 v1 = Vector3.subtract(corners[0], corners[1]);
    Vector3 v2 = Vector3.subtract(corners[2], corners[1]);
    Vector3 cross = Vector3.cross(v1, v2).normalize();
    if (cross.y < 0.0f) {
        cross = cross.negate();
    }
    // Get the bounds on the screen area that will be drawn when drawing this quad
    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int maxX = Integer.MIN_VALUE;
    int maxY = Integer.MIN_VALUE;
    for (Vector3 corner : corners) {
        int cx = (int) corner.x;
        int cy = (int) corner.z;
        if (cx > maxX)
            maxX = cx;
        if (cx < minX)
            minX = cx;
        if (cy > maxY)
            maxY = cy;
        if (cy < minY)
            minY = cy;
    }
    MapTexture temp = MapTexture.createEmpty(maxX - minX + 1, maxY - minY + 1);
    float light = this.ambientLightFact;
    if (this.directionalLightVec != null) {
        double dot = Vector3.dot(cross.normalize(), this.directionalLightVec);
        // Change -1.0...1.0 to 0...1
        dot = (dot + 1.0) / 2.0;
        light += this.directionalLightFact * dot;
    }
    Vector3 p = new Vector3();
    for (int y = minY; y <= maxY; y++) {
        for (int x = minX; x <= maxX; x++) {
            p.x = x;
            p.z = y;
            p.y = 1.0;
            mInv.transformPoint(p);
            double ax = p.x;
            double ay = p.z;
            // Half parameter makes it draw only one half (triangle)
            if ((half > 0 && ax > ay) || (half < 0 && ax <= ay)) {
                continue;
            }
            if (ax >= 0.0f && ay >= 0.0f && ax <= (canvas.getWidth()) && ay <= (canvas.getHeight())) {
                byte color = canvas.readPixel((int) ax, (int) ay);
                if (color != MapColorPalette.COLOR_TRANSPARENT) {
                    // Shows specular brightness based on distance from camera (debug)
                    color = MapColorPalette.getSpecular(color, light);
                    temp.writePixel(x - minX, y - minY, color);
                }
            }
        }
    }
    return this.draw(temp, minX, minY);
}
Also used : Vector3(com.bergerkiller.bukkit.common.math.Vector3) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4) Point(java.awt.Point)

Example 4 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MapIsometricTest method renderSprite.

private MapTexture renderSprite(Model model) {
    MapTexture map = null;
    map = MapTexture.createEmpty(32, 43);
    // map.setBrushMask(mask);
    // map.fill(MapColorPalette.COLOR_RED);
    // map.setBrushMask(null);
    Matrix4x4 transform = new Matrix4x4();
    // transform.rotateOrigin(new Vector3f(8,8,8), new Vector3f(180, 0, 0));
    transform.translate(map.getWidth(), 0.0f, map.getWidth() - 1);
    transform.scale(1.45f, 1.0f, 1.71f);
    // pitch
    transform.rotateX(-45.0f);
    // yaw
    transform.rotateY(225.0f);
    // transform.rotateOrigin(new Vector3f(8,8,8), new Vector3f(0, 0, 0));
    // map.fill(MapColorPalette.COLOR_RED);
    map.setLightOptions(0.2f, 0.8f, new Vector3(-1.0, 1.0, -1.0));
    map.drawModel(model, transform);
    return map;
}
Also used : MapTexture(com.bergerkiller.bukkit.common.map.MapTexture) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 5 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class Pseudo3DImagePanel method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    MapTexture image = MapTexture.createEmpty(MapModelRenderTest.RES_WIDTH, MapModelRenderTest.RES_HEIGHT);
    image.draw(background, 0, 0);
    image.setBlendMode(MapBlendMode.OVERLAY);
    // new Vector3(-1.0, 1.0, -1.0));
    image.setLightOptions(0.0f, 1.0f, new Vector3(-1, 1, -1));
    System.out.println("{" + p3.x + ", " + p2.x + ", " + p3.y + "}");
    // Draws a 3D quad
    float scale = 16.0f;
    float yaw = (float) (p1.x - 128);
    float pitch = (float) (p1.y - 128);
    System.out.println("Yaw=" + yaw + " Pitch=" + pitch);
    BlockRenderOptions opt = BlockData.fromMaterialData(Material.RAILS, 5).getDefaultRenderOptions();
    opt.put("west", "side");
    System.out.println(opt);
    ItemStack item = ItemUtil.createItem(Material.COOKED_FISH, 0, 1);
    ItemUtil.getMetaTag(item, true).putValue("Unbreakable", true);
    // textures.getBlockModel(opt);
    Model model = textures.getItemModel(item);
    Matrix4x4 transform = new Matrix4x4();
    // image.draw(textures.getItemTexture(item, 32, 32), 0, 0);
    transform.translate(p0.x, 0.0f, p0.y);
    transform.scale(scale);
    transform.rotateX(pitch);
    transform.rotateY(yaw);
    transform.translate(-8, -8, -8);
    // image.drawModel(textures.getBlockModel(Material.QUARTZ_BLOCK), transform);
    transform.translate(20, 0, 0);
    image.drawModel(model, transform);
    g.drawImage(image.toJavaImage(), 0, 0, null);
    int r = 8;
    g.setColor(Color.BLUE);
    g.fillOval((int) p0.x - r, (int) p0.y - r, r + r, r + r);
    g.fillOval((int) p1.x - r, (int) p1.y - r, r + r, r + r);
    g.fillOval((int) p2.x - r, (int) p2.y - r, r + r, r + r);
    g.fillOval((int) p3.x - r, (int) p3.y - r, r + r, r + r);
}
Also used : MapTexture(com.bergerkiller.bukkit.common.map.MapTexture) BlockRenderOptions(com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions) Model(com.bergerkiller.bukkit.common.map.util.Model) Vector3(com.bergerkiller.bukkit.common.math.Vector3) ItemStack(org.bukkit.inventory.ItemStack) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Aggregations

Matrix4x4 (com.bergerkiller.bukkit.common.math.Matrix4x4)8 Vector3 (com.bergerkiller.bukkit.common.math.Vector3)6 MapTexture (com.bergerkiller.bukkit.common.map.MapTexture)2 Model (com.bergerkiller.bukkit.common.map.util.Model)2 Vector (org.bukkit.util.Vector)2 Test (org.junit.Test)2 GeneratedModel (com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)1 Quaternion (com.bergerkiller.bukkit.common.math.Quaternion)1 BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)1 Point (java.awt.Point)1 Random (java.util.Random)1 ItemStack (org.bukkit.inventory.ItemStack)1