use of com.bergerkiller.bukkit.common.math.Vector3 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);
}
use of com.bergerkiller.bukkit.common.math.Vector3 in project BKCommonLib by bergerhealer.
the class MapCanvas method drawQuad.
/**
* Draws a pseudo-3D quad onto this canvas, using the 4 3D coordinates of the quad points
* to define the projection transformation that is applied.
*
* @param canvas to draw onto this canvas
* @param p0 the top-left first point of the quad
* @param p1 the top-right second point of the quad
* @param p2 the bottom-right third point of the quad
* @param p3 the bottom-left fourth point of the quad
* @return view
*/
public final MapCanvas drawQuad(MapCanvas canvas, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) {
Vector3 fp3 = Vector3.add(p2, Vector3.subtract(p0, p1));
if (fp3.distanceSquared(p3) > 0.0001) {
// Split into two separate triangle draw calls
// We can draw the entire Quad in one go
// Triangle drawing logic? Lol, I'll just draw one half of a quad.
// Really though, this shit needs to be optimized :(
this.drawQuad(canvas, p0, p1, p2, fp3, 1);
Vector3 fp1 = Vector3.add(p2, Vector3.subtract(p0, p3));
this.drawQuad(canvas, p0, fp1, p2, p3, -1);
} else {
// We can draw the entire Quad in one go
this.drawQuad(canvas, p0, p1, p2, p3, 0);
}
return this;
}
use of com.bergerkiller.bukkit.common.math.Vector3 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);
}
use of com.bergerkiller.bukkit.common.math.Vector3 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;
}
use of com.bergerkiller.bukkit.common.math.Vector3 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);
}
Aggregations