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.demos.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.Region;
37import com.jogamp.graph.curve.opengl.GLRegion;
38import com.jogamp.graph.curve.opengl.RenderState;
39import com.jogamp.graph.curve.opengl.RegionRenderer;
40import com.jogamp.graph.curve.opengl.TextRegionUtil;
41import com.jogamp.graph.font.Font;
42import com.jogamp.graph.font.FontFactory;
43import com.jogamp.graph.font.FontScale;
44import com.jogamp.graph.font.FontSet;
45import com.jogamp.math.geom.plane.AffineTransform;
46import com.jogamp.math.util.PMVMatrix4f;
47import com.jogamp.newt.Window;
48
49public abstract class TextRendererGLELBase implements GLEventListener {
50 public final int renderModes;
51
52 protected final int pass2SampleCount;
53 protected final float[] staticRGBAColor = new float[] { 1f, 1f, 1f, 1f };
54
55 private boolean exclusivePMVMatrix = true;
56 private PMVMatrix4f sharedPMVMatrix = null;
57 private RegionRenderer.GLCallback enableCallback=null, disableCallback=null;
58 protected RegionRenderer renderer = null;
60
61 protected final AffineTransform tempT1 = new AffineTransform();
62 protected final AffineTransform tempT2 = new AffineTransform();
63
64 /** scale pixel, default is 1f */
65 protected float pixelScale = 1.0f;
66
67 /** dpi display resolution, queried at {@link #init(GLAutoDrawable)} if NEWT, otherwise 96. */
68 protected float dpiH = 96;
69
70 boolean flipVerticalInGLOrientation = false;
71
72 /**
73 * @param fontSet e.g. default is {@link FontFactory#UBUNTU}
74 * @param fontFamily e.g. default is {@link FontSet#FAMILY_REGULAR}
75 * @param fontStylebits e.g. default is {@link FontSet#STYLE_NONE}
76 * @return the resulting font.
77 */
78 public static Font getFont(final int fontSet, final int fontFamily, final int fontStylebits) {
79 try {
80 return FontFactory.get(fontSet).get(fontFamily, fontStylebits);
81 } catch (final IOException e) {
82 e.printStackTrace();
83 }
84 return null;
85 }
86
87 /**
88 * @param renderModes
89 * @param sampleCount desired multisampling sample count for msaa-rendering.
90 * @see #setRendererCallbacks(com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback)
91 */
92 public TextRendererGLELBase(final int renderModes, final int sampleCount) {
93 this.renderModes = renderModes;
94 this.pass2SampleCount = sampleCount;
95 }
96
97 /**
98 * In exclusive mode, impl. uses a pixelScale of 1f and orthogonal PMV on window dimensions
99 * and renderString uses 'height' for '1'.
100 * <p>
101 * In non-exclusive mode, i.e. shared w/ custom PMV (within another 3d scene),
102 * it uses the custom pixelScale and renderString uses normalized 'height', i.e. '1'.
103 * </p>
104 * <p>
105 * Must be called before {@link #init(GLAutoDrawable)}.
106 * </p>
107 */
108 public void setSharedPMVMatrix(final PMVMatrix4f pmv) {
109 this.sharedPMVMatrix = pmv;
110 }
111
112 /**
113 * See {@link RegionRenderer#create(Vertex.Factory<? extends Vertex>, RenderState, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback)}.
114 * <p>
115 * Must be called before {@link #init(GLAutoDrawable)}.
116 * </p>
117 */
118 public void setRendererCallbacks(final RegionRenderer.GLCallback enable, final RegionRenderer.GLCallback disable) {
119 this.enableCallback = enable;
120 this.disableCallback = disable;
121 }
122
123 public void setFlipVerticalInGLOrientation(final boolean v) { flipVerticalInGLOrientation=v; }
124 public final RegionRenderer getRenderer() { return renderer; }
126
127 public PMVMatrix4f getMatrix() { return renderer.getMatrix(); };
128 public boolean isMatrixShared() { return !exclusivePMVMatrix; };
129
130 @Override
131 public void init(final GLAutoDrawable drawable) {
132 exclusivePMVMatrix = null == sharedPMVMatrix;
133 renderer = RegionRenderer.create(sharedPMVMatrix, enableCallback, disableCallback);
137 this.textRenderUtil = new TextRegionUtil(renderModes);
138 final GL2ES2 gl = drawable.getGL().getGL2ES2();
139 renderer.init(gl);
141
142 final Object upObj = drawable.getUpstreamWidget();
143 if( upObj instanceof Window ) {
144 final float[] dpi = FontScale.ppmmToPPI( ((Window)upObj).getPixelsPerMM(new float[2]) );
145 dpiH = dpi[1];
146 }
147 }
148
149 @Override
150 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
151 if( null != renderer ) {
152 final GL2ES2 gl = drawable.getGL().getGL2ES2();
153 renderer.enable(gl, true);
154 if( exclusivePMVMatrix ) {
155 // renderer.reshapePerspective(gl, 45.0f, width, height, 0.1f, 1000.0f);
156 renderer.reshapeOrtho(width, height, 0.1f, 1000.0f);
157 pixelScale = 1.0f;
158 } else {
159 renderer.reshapeNotify(x, y, width, height);
160 }
161 renderer.enable(gl, false);
162 }
163 }
164
165 @Override
166 public abstract void display(GLAutoDrawable drawable);
167
168 @Override
169 public void dispose(final GLAutoDrawable drawable) {
170 if( null != renderer ) {
171 final GL2ES2 gl = drawable.getGL().getGL2ES2();
172 renderer.destroy(gl);
173 }
174 }
175
176 int lastRow = -1;
177
178 /**
179 *
180 * @param drawable
181 * @param font
182 * @param pixelSize Use {@link Font#toPixels(float, float)} for resolution correct pixel-size.
183 * @param text
184 * @param column
185 * @param tx
186 * @param ty
187 * @param tz
188 * @param cacheRegion
189 */
190 public void renderString(final GLAutoDrawable drawable,
191 final Font font, final float pixelSize, final CharSequence text,
192 final int column, final float tx, final float ty, final float tz, final boolean cacheRegion) {
193 final int row = lastRow + 1;
194 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, cacheRegion, null);
195 }
196
197 public void renderString(final GLAutoDrawable drawable,
198 final Font font, final float pixelSize, final CharSequence text,
199 final int column, final float tx, final float ty, final float tz, final GLRegion region) {
200 final int row = lastRow + 1;
201 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, false, region);
202 }
203
204 /**
205 *
206 * @param drawable
207 * @param font
208 * @param pixelSize Use {@link Font#toPixels(float, float)} for resolution correct pixel-size.
209 * @param text
210 * @param column
211 * @param row
212 * @param tx
213 * @param ty
214 * @param tz
215 * @param cacheRegion
216 */
217 public void renderString(final GLAutoDrawable drawable,
218 final Font font, final float pixelSize, final CharSequence text,
219 final int column, final int row,
220 final float tx, final float ty, final float tz, final boolean cacheRegion) {
221 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, cacheRegion, null);
222 }
223
224 public void renderString(final GLAutoDrawable drawable,
225 final Font font, final float pixelSize, final CharSequence text,
226 final int column, final int row,
227 final float tx, final float ty, final float tz, final GLRegion region) {
228 renderStringImpl(drawable, font, pixelSize, text, column, row, tx, ty, tz, false, region);
229 }
230
231 private void renderStringImpl(final GLAutoDrawable drawable,
232 final Font font, final float pixelSize, final CharSequence text,
233 final int column, final int row,
234 final float tx, final float ty, final float tz, final boolean cacheRegion, final GLRegion region) {
235 if( null != renderer ) {
236 final GL2ES2 gl = drawable.getGL().getGL2ES2();
237
238 float dx = tx;
239 float dy;
240
241 if( !exclusivePMVMatrix ) {
242 dy = 1f-ty;
243 } else {
244 final int height = drawable.getSurfaceHeight();
245 dy = height-ty;
246 }
247 final float sxy = pixelScale * pixelSize;
248 final int newLineCount = StringUtil.getLineCount(text);
249 final float lineHeight = font.getLineHeight();
250 dx += sxy * font.getAdvanceWidth( font.getGlyphID( 'X' ) ) * column;
251 dy -= sxy * lineHeight * ( row + 1 );
252
253 final PMVMatrix4f pmvMatrix = getMatrix();
254 if( !exclusivePMVMatrix ) {
255 pmvMatrix.pushMv();
256 } else {
257 pmvMatrix.loadMvIdentity();
258 }
259 pmvMatrix.translateMv(dx, dy, tz);
260 if( flipVerticalInGLOrientation && drawable.isGLOriented() ) {
261 pmvMatrix.scaleMv(sxy, -1f*sxy, 1.0f);
262 } else {
263 pmvMatrix.scaleMv(sxy, sxy, 1.0f);
264 }
265 renderer.enable(gl, true);
266 if( cacheRegion ) {
267 textRenderUtil.drawString3D(gl, renderer, font, text, null);
268 } else if( null != region ) {
269 TextRegionUtil.drawString3D(gl, region, renderer, font, text, null, tempT1, tempT2);
270 } else {
271 TextRegionUtil.drawString3D(gl, renderModes, renderer, font, text, null, tempT1, tempT2);
272 }
273 renderer.enable(gl, false);
274
275 if( !exclusivePMVMatrix ) {
276 pmvMatrix.popMv();
277 }
278 lastRow = row + newLineCount;
279 }
280 }
281 public void renderRegion(final GLAutoDrawable drawable,
282 final Font font, final float pixelSize,
283 final int column, final int row,
284 final float tx, final float ty, final float tz, final GLRegion region) {
285 if( null != renderer ) {
286 final GL2ES2 gl = drawable.getGL().getGL2ES2();
287
288 float dx = tx;
289 float dy;
290
291 if( !exclusivePMVMatrix ) {
292 dy = 1f-ty;
293 } else {
294 final int height = drawable.getSurfaceHeight();
295 dy = height-ty;
296 }
297 final float sxy = pixelScale * pixelSize;
298 final float lineHeight = font.getLineHeight();
299 dx += sxy * font.getAdvanceWidth( font.getGlyphID( 'X' ) ) * column;
300 dy -= sxy * lineHeight * ( row + 1 );
301
302 final PMVMatrix4f pmvMatrix = getMatrix();
303 if( !exclusivePMVMatrix ) {
304 pmvMatrix.pushMv();
305 } else {
306 pmvMatrix.loadMvIdentity();
307 }
308 pmvMatrix.translateMv(dx, dy, tz);
309 if( flipVerticalInGLOrientation && drawable.isGLOriented() ) {
310 pmvMatrix.scaleMv(sxy, -1f*sxy, 1.0f);
311 } else {
312 pmvMatrix.scaleMv(sxy, sxy, 1.0f);
313 }
314 renderer.enable(gl, true);
315 region.draw(gl, renderer);
316 renderer.enable(gl, false);
317
318 if( !exclusivePMVMatrix ) {
319 pmvMatrix.popMv();
320 }
321 }
322 }
323}
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
Definition: Region.java:62
static final int DEFAULT_AA_QUALITY
Default pass2 AA-quality rendering {@value} for Graph Region AA render-modes: VBAA_RENDERING_BIT.
Definition: Region.java:168
A GLRegion is the OGL binding of one or more OutlineShapes Defined by its vertices and generated tria...
Definition: GLRegion.java:70
final void draw(final GL2ES2 gl, final RegionRenderer renderer)
Renders the associated OGL objects specifying current width/hight of window for optional multi pass r...
Definition: GLRegion.java:518
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 int setSampleCount(final int v)
Sets pass2 AA sample count clipped to the range [Region#MIN_AA_SAMPLE_COUNT..Region#MAX_AA_SAMPLE_COU...
final int setAAQuality(final int v)
Sets pass2 AA-quality rendering value clipped to the range [Region#MIN_AA_QUALITY....
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),...
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final CharSequence text, final int column, final int row, final float tx, final float ty, final float tz, final GLRegion region)
void renderRegion(final GLAutoDrawable drawable, final Font font, final float pixelSize, final int column, final int row, final float tx, final float ty, final float tz, final GLRegion region)
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final CharSequence text, final int column, final float tx, final float ty, final float tz, final boolean cacheRegion)
TextRendererGLELBase(final int renderModes, final int sampleCount)
void setRendererCallbacks(final RegionRenderer.GLCallback enable, final RegionRenderer.GLCallback disable)
See RegionRenderer#create(Vertex.Factory<? extends Vertex>, RenderState, com.jogamp....
static Font getFont(final int fontSet, final int fontFamily, final int fontStylebits)
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void setSharedPMVMatrix(final PMVMatrix4f pmv)
In exclusive mode, impl.
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.
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final CharSequence text, final int column, final int row, final float tx, final float ty, final float tz, final boolean cacheRegion)
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final CharSequence text, final int column, final float tx, final float ty, final float tz, final GLRegion region)
abstract void display(GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
float dpiH
dpi display resolution, queried at init(GLAutoDrawable) if NEWT, otherwise 96.
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.