Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / texturedpolygon / TexturedPolygon.java
index 889acda..2cd60e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sixth 3D engine. Author: Svjatoslav Agejenko. 
+ * 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;
@@ -15,18 +15,32 @@ 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.
+ * <p>
+ *
+ * <pre>
+ * 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.
+ * </pre>
+ */
 
 public class TexturedPolygon extends AbstractCoordinateShape {
 
     public final Texture texture;
-    final PolygonBorderInterpolator[] is = new PolygonBorderInterpolator[]{
-            new PolygonBorderInterpolator(), new PolygonBorderInterpolator(),
-            new PolygonBorderInterpolator()};
     /**
      * Polygon texture coordinates.
      */
     public Point2D texturePoint1, texturePoint2, texturePoint3;
-    private boolean showBorders = false;
+
+    /**
+     * If <code>true</code> then polygon borders will be drawn.
+     * It is used for debugging purposes.
+     */
+    public boolean showBorders = false;
     private double totalTextureDistance = -1;
 
     public TexturedPolygon(final Point3D p1, final Point3D p2,
@@ -46,8 +60,8 @@ public class TexturedPolygon extends AbstractCoordinateShape {
                            final PolygonCoordinate pc2, final PolygonCoordinate pc3,
                            final Texture texture) {
 
-        this(pc1.space, pc2.space, pc3.space, pc1.texture, pc2.texture,
-                pc3.texture, texture);
+        this(pc1.spaceCoordinate, pc2.spaceCoordinate, pc3.spaceCoordinate, pc1.textureCoordinate, pc2.textureCoordinate,
+                pc3.textureCoordinate, texture);
     }
 
     private void computeTotalTextureDistance() {
@@ -140,7 +154,7 @@ public class TexturedPolygon extends AbstractCoordinateShape {
                 if (pointWithinPolygon(
                         renderBuffer.getMouseEvent().coordinate, projectedPoint1,
                         projectedPoint2, projectedPoint3))
-                    renderBuffer.setObjectUnderMouse(mouseInteractionController);
+                    renderBuffer.setCurrentObjectUnderMouseCursor(mouseInteractionController);
 
         // Show polygon boundaries (for debugging)
         if (showBorders)
@@ -171,8 +185,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);
 
@@ -182,6 +195,10 @@ public class TexturedPolygon extends AbstractCoordinateShape {
 
         final TextureBitmap zoomedBitmap = texture.getZoomedBitmap(scaleFactor);
 
+        final PolygonBorderInterpolator[] is = new PolygonBorderInterpolator[]{
+                new PolygonBorderInterpolator(), new PolygonBorderInterpolator(),
+                new PolygonBorderInterpolator()};
+
         is[0].setPoints(projectedPoint1, projectedPoint2, texturePoint1,
                 texturePoint2);
         is[1].setPoints(projectedPoint1, projectedPoint3, texturePoint1,
@@ -189,7 +206,7 @@ public class TexturedPolygon extends AbstractCoordinateShape {
         is[2].setPoints(projectedPoint2, projectedPoint3, texturePoint2,
                 texturePoint3);
 
-        java.util.Arrays.sort(is);
+        sort(is);
 
         for (int y = yTop; y < yBottom; y++)
             if (is[0].containsY(y)) {