2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
9 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line;
11 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
17 public class Line extends AbstractCoordinateShape {
19 private static final double MINIMUM_WIDTH_THRESHOLD = 1;
21 private static final double LINE_WIDTH_MULTIPLIER = 0.2d;
23 public final double width;
24 final LineInterpolator[] li = new LineInterpolator[4];
27 public Line(final Line parentLine) {
28 this(parentLine.coordinates[0].coordinate.clone(),
29 parentLine.coordinates[1].coordinate.clone(),
30 new Color(parentLine.color), parentLine.width);
33 public Line(final Point3D point1, final Point3D point2, final Color color,
36 super(point1, point2);
41 for (int i = 0; i < li.length; i++)
42 li[i] = new LineInterpolator();
46 private void drawHorizontalLine(final LineInterpolator line1,
47 final LineInterpolator line2, final int y,
48 final RenderingContext renderBuffer) {
50 int x1 = line1.getX(y);
51 int x2 = line2.getX(y);
53 double d1 = line1.getD();
54 double d2 = line2.getD();
61 final double tmp2 = d1;
66 final int unclippedWidth = x2 - x1;
67 final double dinc = (d2 - d1) / unclippedWidth;
74 if (x2 >= renderBuffer.width)
75 x2 = renderBuffer.width - 1;
77 final int drawnWidth = x2 - x1;
79 int offset = ((y * renderBuffer.width) + x1) * 4;
80 final byte[] offSreenBufferBytes = renderBuffer.pixels;
82 final int lineAlpha = color.a;
84 final int colorB = color.b;
85 final int colorG = color.g;
86 final int colorR = color.r;
88 for (int i = 0; i < drawnWidth; i++) {
90 final double alphaMultiplier = 1d - Math.abs(d1);
92 final int realLineAlpha = (int) (lineAlpha * alphaMultiplier);
93 final int backgroundAlpha = 255 - realLineAlpha;
95 offSreenBufferBytes[offset] = (byte) 255;
97 offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + (colorB * realLineAlpha)) / 256);
99 offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + (colorG * realLineAlpha)) / 256);
101 offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + (colorR * realLineAlpha)) / 256);
109 private void drawSinglePixelHorizontalLine(final RenderingContext buffer,
112 final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate;
113 final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate;
115 int xStart = (int) onScreenPoint1.x;
116 int xEnd = (int) onScreenPoint2.x;
122 final int tmp = xStart;
125 lineHeight = (int) (onScreenPoint1.y - onScreenPoint2.y);
126 yBase = (int) onScreenPoint2.y;
128 yBase = (int) onScreenPoint1.y;
129 lineHeight = (int) (onScreenPoint2.y - onScreenPoint1.y);
132 final int lineWidth = xEnd - xStart;
136 final byte[] offSreenBufferBytes = buffer.pixels;
137 final int backgroundAlpha = 255 - alpha;
139 final int blueWithAlpha = color.b * alpha;
140 final int greenWithAplha = color.g * alpha;
141 final int redWithAlpha = color.r * alpha;
143 for (int relativeX = 0; relativeX <= lineWidth; relativeX++) {
144 final int x = xStart + relativeX;
146 if ((x >= 0) && (x < buffer.width)) {
148 final int y = yBase + ((relativeX * lineHeight) / lineWidth);
149 if ((y >= 0) && (y < buffer.height)) {
150 int ramOffset = ((y * buffer.width) + x) * 4;
152 offSreenBufferBytes[ramOffset] = (byte) 255;
154 offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256);
156 offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + greenWithAplha) / 256);
158 offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256);
165 private void drawSinglePixelVerticalLine(final RenderingContext buffer,
168 final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate;
169 final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate;
171 int yStart = (int) onScreenPoint1.y;
172 int yEnd = (int) onScreenPoint2.y;
178 final int tmp = yStart;
181 lineWidth = (int) (onScreenPoint1.x - onScreenPoint2.x);
182 xBase = (int) onScreenPoint2.x;
184 xBase = (int) onScreenPoint1.x;
185 lineWidth = (int) (onScreenPoint2.x - onScreenPoint1.x);
188 final int lineHeight = yEnd - yStart;
192 final byte[] offScreenBufferBytes = buffer.pixels;
193 final int backgroundAlpha = 255 - alpha;
195 final int blueWithAlpha = color.b * alpha;
196 final int greenWithAlpha = color.g * alpha;
197 final int redWithAlpha = color.r * alpha;
199 for (int relativeY = 0; relativeY <= lineHeight; relativeY++) {
200 final int y = yStart + relativeY;
202 if ((y >= 0) && (y < buffer.height)) {
204 final int x = xBase + ((relativeY * lineWidth) / lineHeight);
205 if ((x >= 0) && (x < buffer.width)) {
206 int ramOffset = ((y * buffer.width) + x) * 4;
208 offScreenBufferBytes[ramOffset] = (byte) 255;
210 offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256);
212 offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + greenWithAlpha) / 256);
214 offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256);
220 private int getLineInterpolator(final int startPointer, final int y) {
222 for (int i = startPointer; i < li.length; i++)
223 if (li[i].containsY(y))
229 public void paint(final RenderingContext buffer) {
231 final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate;
232 final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate;
234 final double xp = onScreenPoint2.x - onScreenPoint1.x;
235 final double yp = onScreenPoint2.y - onScreenPoint1.y;
237 final double point1radius = (buffer.width * LINE_WIDTH_MULTIPLIER * width)
238 / coordinates[0].transformedCoordinate.z;
239 final double point2radius = (buffer.width * LINE_WIDTH_MULTIPLIER * width)
240 / coordinates[1].transformedCoordinate.z;
242 if ((point1radius < MINIMUM_WIDTH_THRESHOLD)
243 || (point2radius < MINIMUM_WIDTH_THRESHOLD)) {
245 double averageRadius = (point1radius + point2radius) / 2;
247 if (averageRadius > 1)
250 final int alpha = (int) (color.a * averageRadius);
254 if (Math.abs(xp) > Math.abs(yp))
255 drawSinglePixelHorizontalLine(buffer, alpha);
257 drawSinglePixelVerticalLine(buffer, alpha);
261 final double lineLength = Math.sqrt((xp * xp) + (yp * yp));
263 final double yinc1 = (point1radius * xp) / lineLength;
264 final double yinc2 = (point2radius * xp) / lineLength;
266 final double xdec1 = (point1radius * yp) / lineLength;
267 final double xdec2 = (point2radius * yp) / lineLength;
269 final double p1x1 = onScreenPoint1.x - xdec1;
270 final double p1y1 = onScreenPoint1.y + yinc1;
272 final double p1x2 = onScreenPoint1.x + xdec1;
273 final double p1y2 = onScreenPoint1.y - yinc1;
275 final double p2x1 = onScreenPoint2.x - xdec2;
276 final double p2y1 = onScreenPoint2.y + yinc2;
278 final double p2x2 = onScreenPoint2.x + xdec2;
279 final double p2y2 = onScreenPoint2.y - yinc2;
281 li[0].setPoints(p1x1, p1y1, 1d, p2x1, p2y1, 1d);
282 li[1].setPoints(p1x2, p1y2, -1d, p2x2, p2y2, -1d);
284 li[2].setPoints(p1x1, p1y1, 1d, p1x2, p1y2, -1d);
285 li[3].setPoints(p2x1, p2y1, 1d, p2x2, p2y2, -1d);
304 if (ymax >= buffer.height)
305 ymax = buffer.height - 1;
307 for (int y = (int) ymin; y <= ymax; y++) {
308 final int li1 = getLineInterpolator(0, y);
310 final int li2 = getLineInterpolator(li1 + 1, y);
312 drawHorizontalLine(li[li1], li[li2], y, buffer);