2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line;
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
9 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
16 * Line is represented by two points, width and color and width.
18 public class Line extends AbstractCoordinateShape {
20 private static final double MINIMUM_WIDTH_THRESHOLD = 1;
22 private static final double LINE_WIDTH_MULTIPLIER = 0.2d;
27 public final double width;
28 final LineInterpolator[] lineInterpolators = new LineInterpolator[4];
35 public Line(final Line parentLine) {
36 this(parentLine.coordinates[0].coordinate.clone(),
37 parentLine.coordinates[1].coordinate.clone(),
38 new Color(parentLine.color), parentLine.width);
41 public Line(final Point3D point1, final Point3D point2, final Color color,
44 super(point1, point2);
49 for (int i = 0; i < lineInterpolators.length; i++)
50 lineInterpolators[i] = new LineInterpolator();
54 private void drawHorizontalLine(final LineInterpolator line1,
55 final LineInterpolator line2, final int y,
56 final RenderingContext renderBuffer) {
58 int x1 = line1.getX(y);
59 int x2 = line2.getX(y);
61 double d1 = line1.getD();
62 double d2 = line2.getD();
69 final double tmp2 = d1;
74 final int unclippedWidth = x2 - x1;
75 final double dinc = (d2 - d1) / unclippedWidth;
82 if (x2 >= renderBuffer.width)
83 x2 = renderBuffer.width - 1;
85 final int drawnWidth = x2 - x1;
87 int offset = ((y * renderBuffer.width) + x1) * 4;
88 final byte[] offSreenBufferBytes = renderBuffer.pixels;
90 final int lineAlpha = color.a;
92 final int colorB = color.b;
93 final int colorG = color.g;
94 final int colorR = color.r;
96 for (int i = 0; i < drawnWidth; i++) {
98 final double alphaMultiplier = 1d - Math.abs(d1);
100 final int realLineAlpha = (int) (lineAlpha * alphaMultiplier);
101 final int backgroundAlpha = 255 - realLineAlpha;
103 offSreenBufferBytes[offset] = (byte) 255;
105 offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + (colorB * realLineAlpha)) / 256);
107 offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + (colorG * realLineAlpha)) / 256);
109 offSreenBufferBytes[offset] = (byte) ((((offSreenBufferBytes[offset] & 0xff) * backgroundAlpha) + (colorR * realLineAlpha)) / 256);
117 private void drawSinglePixelHorizontalLine(final RenderingContext buffer,
120 final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate;
121 final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate;
123 int xStart = (int) onScreenPoint1.x;
124 int xEnd = (int) onScreenPoint2.x;
130 final int tmp = xStart;
133 lineHeight = (int) (onScreenPoint1.y - onScreenPoint2.y);
134 yBase = (int) onScreenPoint2.y;
136 yBase = (int) onScreenPoint1.y;
137 lineHeight = (int) (onScreenPoint2.y - onScreenPoint1.y);
140 final int lineWidth = xEnd - xStart;
144 final byte[] offSreenBufferBytes = buffer.pixels;
145 final int backgroundAlpha = 255 - alpha;
147 final int blueWithAlpha = color.b * alpha;
148 final int greenWithAplha = color.g * alpha;
149 final int redWithAlpha = color.r * alpha;
151 for (int relativeX = 0; relativeX <= lineWidth; relativeX++) {
152 final int x = xStart + relativeX;
154 if ((x >= 0) && (x < buffer.width)) {
156 final int y = yBase + ((relativeX * lineHeight) / lineWidth);
157 if ((y >= 0) && (y < buffer.height)) {
158 int ramOffset = ((y * buffer.width) + x) * 4;
160 offSreenBufferBytes[ramOffset] = (byte) 255;
162 offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256);
164 offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + greenWithAplha) / 256);
166 offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256);
173 private void drawSinglePixelVerticalLine(final RenderingContext buffer,
176 final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate;
177 final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate;
179 int yStart = (int) onScreenPoint1.y;
180 int yEnd = (int) onScreenPoint2.y;
186 final int tmp = yStart;
189 lineWidth = (int) (onScreenPoint1.x - onScreenPoint2.x);
190 xBase = (int) onScreenPoint2.x;
192 xBase = (int) onScreenPoint1.x;
193 lineWidth = (int) (onScreenPoint2.x - onScreenPoint1.x);
196 final int lineHeight = yEnd - yStart;
200 final byte[] offScreenBufferBytes = buffer.pixels;
201 final int backgroundAlpha = 255 - alpha;
203 final int blueWithAlpha = color.b * alpha;
204 final int greenWithAlpha = color.g * alpha;
205 final int redWithAlpha = color.r * alpha;
207 for (int relativeY = 0; relativeY <= lineHeight; relativeY++) {
208 final int y = yStart + relativeY;
210 if ((y >= 0) && (y < buffer.height)) {
212 final int x = xBase + ((relativeY * lineWidth) / lineHeight);
213 if ((x >= 0) && (x < buffer.width)) {
214 int ramOffset = ((y * buffer.width) + x) * 4;
216 offScreenBufferBytes[ramOffset] = (byte) 255;
218 offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256);
220 offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + greenWithAlpha) / 256);
222 offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256);
228 private int getLineInterpolator(final int startPointer, final int y) {
230 for (int i = startPointer; i < lineInterpolators.length; i++)
231 if (lineInterpolators[i].containsY(y))
237 public void paint(final RenderingContext buffer) {
239 final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate;
240 final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate;
242 final double xp = onScreenPoint2.x - onScreenPoint1.x;
243 final double yp = onScreenPoint2.y - onScreenPoint1.y;
245 final double point1radius = (buffer.width * LINE_WIDTH_MULTIPLIER * width)
246 / coordinates[0].transformedCoordinate.z;
247 final double point2radius = (buffer.width * LINE_WIDTH_MULTIPLIER * width)
248 / coordinates[1].transformedCoordinate.z;
250 if ((point1radius < MINIMUM_WIDTH_THRESHOLD)
251 || (point2radius < MINIMUM_WIDTH_THRESHOLD)) {
253 double averageRadius = (point1radius + point2radius) / 2;
255 if (averageRadius > 1)
258 final int alpha = (int) (color.a * averageRadius);
262 if (Math.abs(xp) > Math.abs(yp))
263 drawSinglePixelHorizontalLine(buffer, alpha);
265 drawSinglePixelVerticalLine(buffer, alpha);
269 final double lineLength = Math.sqrt((xp * xp) + (yp * yp));
271 final double yinc1 = (point1radius * xp) / lineLength;
272 final double yinc2 = (point2radius * xp) / lineLength;
274 final double xdec1 = (point1radius * yp) / lineLength;
275 final double xdec2 = (point2radius * yp) / lineLength;
277 final double p1x1 = onScreenPoint1.x - xdec1;
278 final double p1y1 = onScreenPoint1.y + yinc1;
280 final double p1x2 = onScreenPoint1.x + xdec1;
281 final double p1y2 = onScreenPoint1.y - yinc1;
283 final double p2x1 = onScreenPoint2.x - xdec2;
284 final double p2y1 = onScreenPoint2.y + yinc2;
286 final double p2x2 = onScreenPoint2.x + xdec2;
287 final double p2y2 = onScreenPoint2.y - yinc2;
289 lineInterpolators[0].setPoints(p1x1, p1y1, 1d, p2x1, p2y1, 1d);
290 lineInterpolators[1].setPoints(p1x2, p1y2, -1d, p2x2, p2y2, -1d);
292 lineInterpolators[2].setPoints(p1x1, p1y1, 1d, p1x2, p1y2, -1d);
293 lineInterpolators[3].setPoints(p2x1, p2y1, 1d, p2x2, p2y2, -1d);
312 if (ymax >= buffer.height)
313 ymax = buffer.height - 1;
315 for (int y = (int) ymin; y <= ymax; y++) {
316 final int li1 = getLineInterpolator(0, y);
318 final int li2 = getLineInterpolator(li1 + 1, y);
320 drawHorizontalLine(lineInterpolators[li1], lineInterpolators[li2], y, buffer);