X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Frenderer%2Fraster%2Fshapes%2Fbasic%2Ftexturedpolygon%2FTexturedPolygon.java;h=85760f59f6d8660f9d446692c321eb7c509f9b70;hb=59980888bd6bba94f84e4f63ce67e5b624df8aea;hp=8d6ef266dbf8402b62b5208d570019680050c482;hpb=03447008b8ee26a6463d2cd03005dc26464863db;p=sixth-3d.git diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java index 8d6ef26..85760f5 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java @@ -1,64 +1,55 @@ /* - * Sixth 3D engine. Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - * + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon; import eu.svjatoslav.sixth.e3d.geometry.Point2D; -import eu.svjatoslav.sixth.e3d.geometry.Point3D; -import eu.svjatoslav.sixth.e3d.geometry.Polygon; import eu.svjatoslav.sixth.e3d.gui.RenderingContext; +import eu.svjatoslav.sixth.e3d.math.Vertex; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape; -import eu.svjatoslav.sixth.e3d.renderer.raster.slicer.PolygonCoordinate; import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture; import eu.svjatoslav.sixth.e3d.renderer.raster.texture.TextureBitmap; import java.awt.*; +import static eu.svjatoslav.sixth.e3d.geometry.Polygon.pointWithinPolygon; +import static java.util.Arrays.sort; + +/** + * Textured polygon. + *
+ * + *
+ * This is how perspective-correct texture rendering is implemented: + * If polygon is sufficiently small, it is rendered without perspective correction. + * Otherwise, it is sliced into smaller polygons. + *+ */ + public class TexturedPolygon extends AbstractCoordinateShape { public final Texture texture; - final PolygonBorderInterpolator[] is = new PolygonBorderInterpolator[]{ - new PolygonBorderInterpolator(), new PolygonBorderInterpolator(), - new PolygonBorderInterpolator()}; + /** - * Polygon texture coordinates. + * If
true
then polygon borders will be drawn.
+ * It is used for debugging purposes.
*/
- public Point2D texturePoint1, texturePoint2, texturePoint3;
- private boolean showBorders = false;
+ public boolean showBorders = false;
+
private double totalTextureDistance = -1;
- public TexturedPolygon(final Point3D p1, final Point3D p2,
- final Point3D p3, final Point2D tp1, final Point2D tp2,
- final Point2D tp3, final Texture texture) {
+ public TexturedPolygon(Vertex p1, Vertex p2, Vertex p3, final Texture texture) {
super(p1, p2, p3);
-
- texturePoint1 = tp1;
- texturePoint2 = tp2;
- texturePoint3 = tp3;
-
this.texture = texture;
}
- public TexturedPolygon(final PolygonCoordinate pc1,
- final PolygonCoordinate pc2, final PolygonCoordinate pc3,
- final Texture texture) {
-
- this(pc1.space, pc2.space, pc3.space, pc1.texture, pc2.texture,
- pc3.texture, texture);
- }
-
private void computeTotalTextureDistance() {
// compute total texture distance
- totalTextureDistance = texturePoint1.getDistanceTo(texturePoint2);
- totalTextureDistance += texturePoint1.getDistanceTo(texturePoint3);
- totalTextureDistance += texturePoint2.getDistanceTo(texturePoint3);
+ totalTextureDistance = coordinates[0].textureCoordinate.getDistanceTo(coordinates[1].textureCoordinate);
+ totalTextureDistance += coordinates[0].textureCoordinate.getDistanceTo(coordinates[2].textureCoordinate);
+ totalTextureDistance += coordinates[1].textureCoordinate.getDistanceTo(coordinates[2].textureCoordinate);
}
private void drawHorizontalLine(final PolygonBorderInterpolator line1,
@@ -105,7 +96,7 @@ public class TexturedPolygon extends AbstractCoordinateShape {
x2 = renderBuffer.width - 1;
int renderBufferOffset = ((y * renderBuffer.width) + x1) * 4;
- final byte[] renderBufferBytes = renderBuffer.bytes;
+ final byte[] renderBufferBytes = renderBuffer.pixels;
final double twidth = tx2 - tx1;
final double theight = ty2 - ty1;
@@ -140,11 +131,11 @@ public class TexturedPolygon extends AbstractCoordinateShape {
projectedPoint3.roundToInteger();
if (mouseInteractionController != null)
- if (renderBuffer.mouseClick != null)
- if (Polygon.pointWithinPolygon(
- renderBuffer.mouseClick.coordinate, projectedPoint1,
+ if (renderBuffer.getMouseEvent() != null)
+ if (pointWithinPolygon(
+ renderBuffer.getMouseEvent().coordinate, projectedPoint1,
projectedPoint2, projectedPoint3))
- renderBuffer.clickedItem = mouseInteractionController;
+ renderBuffer.setCurrentObjectUnderMouseCursor(mouseInteractionController);
// Show polygon boundaries (for debugging)
if (showBorders)
@@ -175,8 +166,7 @@ public class TexturedPolygon extends AbstractCoordinateShape {
yBottom = renderBuffer.height - 1;
// paint
- double totalVisibleDistance = projectedPoint1
- .getDistanceTo(projectedPoint2);
+ double totalVisibleDistance = projectedPoint1.getDistanceTo(projectedPoint2);
totalVisibleDistance += projectedPoint1.getDistanceTo(projectedPoint3);
totalVisibleDistance += projectedPoint2.getDistanceTo(projectedPoint3);
@@ -186,14 +176,21 @@ public class TexturedPolygon extends AbstractCoordinateShape {
final TextureBitmap zoomedBitmap = texture.getZoomedBitmap(scaleFactor);
- is[0].setPoints(projectedPoint1, projectedPoint2, texturePoint1,
- texturePoint2);
- is[1].setPoints(projectedPoint1, projectedPoint3, texturePoint1,
- texturePoint3);
- is[2].setPoints(projectedPoint2, projectedPoint3, texturePoint2,
- texturePoint3);
-
- java.util.Arrays.sort(is);
+ final PolygonBorderInterpolator[] is = new PolygonBorderInterpolator[]{
+ new PolygonBorderInterpolator(), new PolygonBorderInterpolator(),
+ new PolygonBorderInterpolator()};
+
+ is[0].setPoints(projectedPoint1, projectedPoint2,
+ coordinates[0].textureCoordinate,
+ coordinates[1].textureCoordinate);
+ is[1].setPoints(projectedPoint1, projectedPoint3,
+ coordinates[0].textureCoordinate,
+ coordinates[2].textureCoordinate);
+ is[2].setPoints(projectedPoint2, projectedPoint3,
+ coordinates[1].textureCoordinate,
+ coordinates[2].textureCoordinate);
+
+ sort(is);
for (int y = yTop; y < yBottom; y++)
if (is[0].containsY(y)) {