f394a2922e4ceeff0af2bc90357a6e0548b2d6e7
[sixth-3d.git] /
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.Billboard;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
11
12 /**
13  * A text label rendered as a billboard texture that always faces the camera.
14  *
15  * <p>This shape renders a single line of text onto a {@link Texture} using the font metrics
16  * defined in {@link TextCanvas} ({@link TextCanvas#FONT}, {@link TextCanvas#FONT_CHAR_WIDTH_TEXTURE_PIXELS},
17  * {@link TextCanvas#FONT_CHAR_HEIGHT_TEXTURE_PIXELS}), then displays the texture as a
18  * forward-oriented billboard via its {@link Billboard} superclass. The result
19  * is a text label that remains readable from any viewing angle.</p>
20  *
21  * <p><b>Usage example:</b></p>
22  * <pre>{@code
23  * // Create a red text label at position (0, -50, 300)
24  * ForwardOrientedTextBlock label = new ForwardOrientedTextBlock(
25  *     new Point3D(0, -50, 300),
26  *     1.0,
27  *     2,
28  *     "Hello, World!",
29  *     Color.RED
30  * );
31  * shapeCollection.addShape(label);
32  * }</pre>
33  *
34  * @see Billboard
35  * @see TextCanvas
36  * @see Texture
37  */
38 public class ForwardOrientedTextBlock extends Billboard {
39
40     /**
41      * Creates a new forward-oriented text block at the given 3D position.
42      *
43      * @param point           the 3D position where the text label is placed
44      * @param scale           the scale factor controlling the rendered size of the text
45      * @param maxUpscaleFactor the maximum mipmap upscale factor for the backing texture
46      * @param text            the text string to render
47      * @param textColor       the color of the rendered text
48      */
49     public ForwardOrientedTextBlock(final Point3D point, final double scale,
50                                     final int maxUpscaleFactor, final String text,
51                                     final eu.svjatoslav.sixth.e3d.renderer.raster.Color textColor) {
52         super(point, scale, getTexture(text, maxUpscaleFactor, textColor));
53
54     }
55
56     /**
57      * Creates a {@link Texture} containing the rendered text string.
58      *
59      * <p>The texture dimensions are calculated from the text length and the font metrics
60      * defined in {@link TextCanvas}. Each character is drawn individually at the appropriate
61      * horizontal offset using {@link TextCanvas#FONT}.</p>
62      *
63      * @param text            the text string to render into the texture
64      * @param maxUpscaleFactor the maximum mipmap upscale factor for the texture
65      * @param textColor       the color of the rendered text
66      * @return a new {@link Texture} containing the rendered text
67      */
68     public static Texture getTexture(final String text,
69                                      final int maxUpscaleFactor,
70                                      final eu.svjatoslav.sixth.e3d.renderer.raster.Color textColor) {
71
72         final Texture texture = new Texture(text.length()
73                 * TextCanvas.FONT_CHAR_WIDTH_TEXTURE_PIXELS, TextCanvas.FONT_CHAR_HEIGHT_TEXTURE_PIXELS,
74                 maxUpscaleFactor);
75
76         // texture.graphics.setColor(Color.BLUE);
77         // texture.graphics.fillRect(0, 0, texture.primaryBitmap.width,
78         // texture.primaryBitmap.width);
79
80         texture.graphics.setFont(TextCanvas.FONT);
81         texture.graphics.setColor(textColor.toAwtColor());
82
83         for (int c = 0; c < text.length(); c++)
84             texture.graphics.drawChars(new char[]{text.charAt(c),}, 0, 1,
85                     (c * TextCanvas.FONT_CHAR_WIDTH_TEXTURE_PIXELS),
86                     (int) (TextCanvas.FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.45));
87
88         return texture;
89     }
90 }