JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TextRendererGLELBase.java
Go to the documentation of this file.
1/**
2 * Copyright 2014-2024 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.test.junit.graph;
29
30import java.io.IOException;
31
32import com.jogamp.opengl.GL2ES2;
33import com.jogamp.opengl.GLAutoDrawable;
34import com.jogamp.opengl.GLEventListener;
35import com.jogamp.common.util.StringUtil;
36import com.jogamp.graph.curve.opengl.GLRegion;
37import com.jogamp.graph.curve.opengl.RenderState;
38import com.jogamp.graph.curve.opengl.RegionRenderer;
39import com.jogamp.graph.curve.opengl.TextRegionUtil;
40import com.jogamp.graph.font.Font;
41import com.jogamp.graph.font.FontFactory;
42import com.jogamp.graph.font.FontScale;
43import com.jogamp.graph.font.FontSet;
44import com.jogamp.math.geom.plane.AffineTransform;
45import com.jogamp.math.util.PMVMatrix4f;
46import com.jogamp.newt.Window;
47
48public abstract class TextRendererGLELBase implements GLEventListener {
49 public final int renderModes;
50
51 protected final int[] vbaaSampleCount;
52 protected final float[] staticRGBAColor = new float[] { 1f, 1f, 1f, 1f };
53
54 private boolean exclusivePMVMatrix = true;
55 private PMVMatrix4f sharedPMVMatrix = null;
56 private RegionRenderer.GLCallback enableCallback=null, disableCallback=null;
57 protected RegionRenderer renderer = null;
59
60 protected final AffineTransform tempT1 = new AffineTransform();
61 protected final AffineTransform tempT2 = new AffineTransform();
62
63 /** scale pixel, default is 1f */
64 protected float pixelScale = 1.0f;
65
66 /** dpi display resolution, queried at {@link #init(GLAutoDrawable)} if NEWT, otherwise 96. */
67 protected float dpiH = 96;
68
69 boolean flipVerticalInGLOrientation = false;
70
71 /**
72 * @param fontSet e.g. default is {@link FontFactory#UBUNTU}
73 * @param fontFamily e.g. default is {@link FontSet#FAMILY_REGULAR}
74 * @param fontStylebits e.g. default is {@link FontSet#STYLE_NONE}
75 * @return the resulting font.
76 */
77 public static Font getFont(final int fontSet, final int fontFamily, final int fontStylebits) {
78 try {
79 return FontFactory.get(fontSet).get(fontFamily, fontStylebits);
80 } catch (final IOException e) {
81 e.printStackTrace();
82 }
83 return null;
84 }
85
86 /**
87 * @param renderModes
88 * @param sampleCount desired multisampling sample count for msaa-rendering.
89 * @see #setRendererCallbacks(com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback)
90 */
91 public TextRendererGLELBase(final int renderModes, final int[] sampleCount) {
92 this.renderModes = renderModes;
93 this.vbaaSampleCount = sampleCount;
94 }
95
96 /**
97 * In exclusive mode, impl. uses a pixelScale of 1f and orthogonal PMV on window dimensions
98 * and renderString uses 'height' for '1'.
99 * <p>
100 * In non-exclusive mode, i.e. shared w/ custom PMV (within another 3d scene),
101 * it uses the custom pixelScale and renderString uses normalized 'height', i.e. '1'.
102 * </p>
103 * <p>
104 * Must be called before {@link #init(GLAutoDrawable)}.
105 * </p>
106 */
107 public void setSharedPMVMatrix(final PMVMatrix4f pmv) {
108 this.sharedPMVMatrix = pmv;
109 }
110
111 /**
112 * See {@link RegionRenderer#create(Vertex.Factory<? extends Vertex>, RenderState, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback)}.
113 * <p>
114 * Must be called before {@link #init(GLAutoDrawable)}.
115 * </p>
116 */
117 public void setRendererCallbacks(final RegionRenderer.GLCallback enable, final RegionRenderer.GLCallback disable) {
118 this.enableCallback = enable;
119 this.disableCallback = disable;
120 }
121
122 public void setFlipVerticalInGLOrientation(final boolean v) { flipVerticalInGLOrientation=v; }
123 public final RegionRenderer getRenderer() { return renderer; }
125
126 @Override
127 public void init(final GLAutoDrawable drawable) {
128 exclusivePMVMatrix = null == sharedPMVMatrix;
129 this.renderer = RegionRenderer.create(sharedPMVMatrix, enableCallback, disableCallback);
131 this.textRenderUtil = new TextRegionUtil(renderModes);
132 final GL2ES2 gl = drawable.getGL().getGL2ES2();
133 renderer.init(gl);
135
136 final Object upObj = drawable.getUpstreamWidget();
137 if( upObj instanceof Window ) {
138 final float[] dpi = FontScale.ppmmToPPI( ((Window)upObj).getPixelsPerMM(new float[2]) );
139 dpiH = dpi[1];
140 }
141 }
142
143 @Override
144 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
145 if( null != renderer ) {
146 final GL2ES2 gl = drawable.getGL().getGL2ES2();
147 renderer.enable(gl, true);
148 if( exclusivePMVMatrix ) {
149 // renderer.reshapePerspective(gl, 45.0f, width, height, 0.1f, 1000.0f);
150 renderer.reshapeOrtho(width, height, 0.1f, 1000.0f);
151 pixelScale = 1.0f;
152 } else {
153 renderer.reshapeNotify(x, y, width, height);
154 }
155 renderer.enable(gl, false);
156 }
157 }
158
159 @Override
160 public abstract void display(GLAutoDrawable drawable);
161
162 @Override
163 public void dispose(final GLAutoDrawable drawable) {
164 if( null != renderer ) {
165 final GL2ES2 gl = drawable.getGL().getGL2ES2();
166 renderer.destroy(gl);
167 }
168 }
169
170 int lastRow = -1;
171
172 /**
173 *
174 * @param drawable
175 * @param font
176 * @param pixelSize Use {@link Font#toPixels(float, float)} for resolution correct pixel-size.
177 * @param text
178 * @param column
179 * @param tx
180 * @param ty
181 * @param tz
182 * @param cacheRegion
183 */
184 public void renderString(final GLAutoDrawable drawable,
185 final Font font, final float pixelSize, final String text,
186 final int column, final float tx, final float ty, final float tz, final boolean cacheRegion) {
187 final int row = lastRow + 1;
188 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, cacheRegion, null);
189 }
190
191 public void renderString(final GLAutoDrawable drawable,
192 final Font font, final float pixelSize, final String text,
193 final int column, final float tx, final float ty, final float tz, final GLRegion region) {
194 final int row = lastRow + 1;
195 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, false, region);
196 }
197
198 /**
199 *
200 * @param drawable
201 * @param font
202 * @param pixelSize Use {@link Font#toPixels(float, float)} for resolution correct pixel-size.
203 * @param text
204 * @param column
205 * @param row
206 * @param tx
207 * @param ty
208 * @param tz
209 * @param cacheRegion
210 */
211 public void renderString(final GLAutoDrawable drawable,
212 final Font font, final float pixelSize, final String text,
213 final int column, final int row,
214 final float tx, final float ty, final float tz, final boolean cacheRegion) {
215 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, cacheRegion, null);
216 }
217
218 public void renderString(final GLAutoDrawable drawable,
219 final Font font, final float pixelSize, final String text,
220 final int column, final int row,
221 final float tx, final float ty, final float tz, final GLRegion region) {
222 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, false, region);
223 }
224
225 private void renderStringImpl(final GLAutoDrawable drawable,
226 final Font font, final float pixelSize, final CharSequence text,
227 final int column, final int row,
228 final float tx, final float ty, final float tz, final boolean cacheRegion, final GLRegion region) {
229 if( null != renderer ) {
230 final GL2ES2 gl = drawable.getGL().getGL2ES2();
231
232 float dx = tx;
233 float dy;
234
235 if( !exclusivePMVMatrix ) {
236 dy = 1f-ty;
237 } else {
238 final int height = drawable.getSurfaceHeight();
239 dy = height-ty;
240 }
241 final float sxy = pixelScale * pixelSize;
242 final int newLineCount = StringUtil.getLineCount(text);
243 final float lineHeight = font.getLineHeight();
244 dx += sxy * font.getAdvanceWidth( font.getGlyphID( 'X' ) ) * column;
245 dy -= sxy * lineHeight * ( row + 1 );
246
247 final PMVMatrix4f pmvMatrix = renderer.getMatrix();
248 if( !exclusivePMVMatrix ) {
249 pmvMatrix.pushMv();
250 } else {
251 pmvMatrix.loadMvIdentity();
252 }
253 pmvMatrix.translateMv(dx, dy, tz);
254 if( flipVerticalInGLOrientation && drawable.isGLOriented() ) {
255 pmvMatrix.scaleMv(sxy, -1f*sxy, 1.0f);
256 } else {
257 pmvMatrix.scaleMv(sxy, sxy, 1.0f);
258 }
259 renderer.enable(gl, true);
260 if( cacheRegion ) {
261 textRenderUtil.drawString3D(gl, renderer, font, text, null);
262 } else if( null != region ) {
263 TextRegionUtil.drawString3D(gl, region, renderer, font, text, null, tempT1, tempT2);
264 } else {
265 TextRegionUtil.drawString3D(gl, renderModes, renderer, font, text, null, tempT1, tempT2);
266 }
267 renderer.enable(gl, false);
268
269 if( !exclusivePMVMatrix ) {
270 pmvMatrix.popMv();
271 }
272 lastRow = row + newLineCount;
273 }
274 }
275}
A GLRegion is the OGL binding of one or more OutlineShapes Defined by its vertices and generated tria...
Definition: GLRegion.java:70
final void enable(final GL2ES2 gl, final boolean enable)
Enabling or disabling the RenderState's current shader program.
final void reshapeOrtho(final int width, final int height, final float near, final float far)
Orthogonal projection, method also calls reshapeNotify(int, int, int, int).
final void setColorStatic(final Vec4f rgbaColor)
final PMVMatrix4f getMatrix()
Borrow the current PMVMatrix4f.
final void reshapeNotify(final int x, final int y, final int width, final int height)
No PMVMatrix4f operation is performed here.
final void init(final GL2ES2 gl)
Initialize shader and bindings for GPU based rendering bound to the given GL object's GLContext if no...
static RegionRenderer create()
Create a hardware accelerated RegionRenderer including its RenderState composition.
final void destroy(final GL2ES2 gl)
Deletes all ShaderPrograms and nullifies its references including RenderState#destroy(GL2ES2).
The RenderState is owned by RegionRenderer.
static final int BITHINT_GLOBAL_DEPTH_TEST_ENABLED
Bitfield hint, if set stating globally enabled GL#GL_DEPTH_TEST, otherwise disabled.
Text Type Rendering Utility Class adding the Font.Glyphs OutlineShape to a GLRegion.
AABBox drawString3D(final GL2ES2 gl, final RegionRenderer renderer, final Font font, final CharSequence str, final Vec4f rgbaColor)
Render the string in 3D space w.r.t.
The optional property jogamp.graph.font.ctor allows user to specify the FontConstructor implementatio...
static final FontSet get(final int font)
Simple static font scale methods for unit conversions.
Definition: FontScale.java:37
static float[] ppmmToPPI(final float[] ppmm)
Converts [1/mm] to [1/inch] in place.
Definition: FontScale.java:105
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
final PMVMatrix4f translateMv(final float x, final float y, final float z)
Translate the modelview matrix.
final PMVMatrix4f scaleMv(final float x, final float y, final float z)
Scale the modelview matrix.
final PMVMatrix4f loadMvIdentity()
Load the modelview matrix with the values of the given Matrix4f.
final PMVMatrix4f pushMv()
Push the modelview matrix to its stack, while preserving its values.
TextRendererGLELBase(final int renderModes, final int[] sampleCount)
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final String text, final int column, final float tx, final float ty, final float tz, final GLRegion region)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final String text, final int column, final int row, final float tx, final float ty, final float tz, final boolean cacheRegion)
void setSharedPMVMatrix(final PMVMatrix4f pmv)
In exclusive mode, impl.
static Font getFont(final int fontSet, final int fontFamily, final int fontStylebits)
abstract void display(GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
float dpiH
dpi display resolution, queried at init(GLAutoDrawable) if NEWT, otherwise 96.
void setRendererCallbacks(final RegionRenderer.GLCallback enable, final RegionRenderer.GLCallback disable)
See RegionRenderer#create(Vertex.Factory<? extends Vertex>, RenderState, com.jogamp....
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final String text, final int column, final float tx, final float ty, final float tz, final boolean cacheRegion)
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final String text, final int column, final int row, final float tx, final float ty, final float tz, final GLRegion region)
May be passed to RegionRenderer ctor, e.g.
Font get(int family, int stylebits)
Interface wrapper for font implementation.
Definition: Font.java:60
float getLineHeight()
Returns line height, baseline-to-baseline in em-size [0..1], composed from ‘hhea’ table entries.
int getGlyphID(final char codepoint)
Returns the Glyph ID mapped to given UTF16 (unicode) codepoint symbol.
float getAdvanceWidth(final int glyphID)
Returns advance-width of given glyphID in font em-size [0..1], sourced from hmtx table - same as Glyp...
Specifying NEWT's Window functionality:
Definition: Window.java:115
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
Object getUpstreamWidget()
Method may return the upstream UI toolkit object holding this GLAutoDrawable instance,...
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.