diff options
author | Sven Gothel <[email protected]> | 2023-03-07 19:05:05 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-03-07 19:05:05 +0100 |
commit | 3bad09f6b7c7f9c93a6cf385abb51a6563e8aec0 (patch) | |
tree | 456cf2320e4d8b6970bc401f25d68efa28ad2ae6 | |
parent | 5e79fea8981a13d155e0b958aa3e20a546c533bb (diff) |
Graph Perf: Add Region.countOutlineShape(), Font.processString(Visitor2,..), TextRegionUtil.countStringRegion() allowing to use Region.setBufferCapacity()
6 files changed, 110 insertions, 13 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java index b287a833f..fc8d41660 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java +++ b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java @@ -140,6 +140,17 @@ public final class OutlineShape implements Comparable<OutlineShape> { public void visit(final OutlineShape shape, final AffineTransform t); } + /** + * Constrained {@link OutlineShape} visitor w/o {@link AffineTransform}. + */ + public static interface Visitor2 { + /** + * Visiting the given {@link OutlineShape}. + * @param shape may be used as is, otherwise a copy shall be made if intended to be modified. + */ + public void visit(final OutlineShape shape); + } + /** Initial {@link #getSharpness()} value, which can be modified via {@link #setSharpness(float)}. */ public static final float DEFAULT_SHARPNESS = 0.5f; diff --git a/src/jogl/classes/com/jogamp/graph/curve/Region.java b/src/jogl/classes/com/jogamp/graph/curve/Region.java index 5147008a1..22e63a3f7 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/Region.java +++ b/src/jogl/classes/com/jogamp/graph/curve/Region.java @@ -441,6 +441,43 @@ public abstract class Region { public PerfCounterCtrl perfCounter() { return perfCounterCtrl; } /** + * Count required number of vertices and indices adding to given int[2] `vertIndexCount` array. + * <p> + * The region's buffer can be either set using {@link Region#setBufferCapacity(int, int)} or grown using {@link Region#growBuffer(int, int)}. + * </p> + * @param shape the {@link OutlineShape} to count + * @param vertIndexCount the int[2] storage where the counted vertices and indices are added, vertices at [0] and indices at [1] + * @see #setBufferCapacity(int, int) + * @see #growBuffer(int, int) + */ + public final void countOutlineShape(final OutlineShape shape, final int[/*2*/] vertIndexCount) { + final List<Triangle> trisIn = shape.getTriangles(OutlineShape.VerticesState.QUADRATIC_NURBS); + final ArrayList<Vertex> vertsIn = shape.getVertices(); + { + final int verticeCount = vertsIn.size() + shape.getAddedVerticeCount(); + final int indexCount = trisIn.size() * 3; + vertIndexCount[0] += verticeCount; + vertIndexCount[1] += Math.min( Math.ceil(verticeCount * 0.6), indexCount ); + } + } + + /** + * Count required number of vertices and indices adding to given int[2] `vertIndexCount` array. + * <p> + * The region's buffer can be either set using {@link Region#setBufferCapacity(int, int)} or grown using {@link Region#growBuffer(int, int)}. + * </p> + * @param shapes list of {@link OutlineShape} to count + * @param vertIndexCount the int[2] storage where the counted vertices and indices are added, vertices at [0] and indices at [1] + * @see #setBufferCapacity(int, int) + * @see #growBuffer(int, int) + */ + public final void countOutlineShapes(final List<OutlineShape> shapes, final int[/*2*/] vertIndexCount) { + for (int i = 0; i < shapes.size(); i++) { + countOutlineShape(shapes.get(i), vertIndexCount); + } + } + + /** * Add the given {@link OutlineShape} to this region with the given optional {@link AffineTransform}. * <p> * In case {@link #setFrustum(Frustum) frustum culling is set}, the {@link OutlineShape} diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java index 0547d994a..8d55c6136 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java @@ -115,6 +115,27 @@ public class TextRegionUtil { } /** + * Count required number of vertices and indices adding to given int[2] `vertIndexCount` array. + * <p> + * The region's buffer can be either set using {@link Region#setBufferCapacity(int, int)} or grown using {@link Region#growBuffer(int, int)}. + * </p> + * @param region the {@link GLRegion} sink + * @param font the target {@link Font} + * @param str string text + * @param vertIndexCount the int[2] storage where the counted vertices and indices are added, vertices at [0] and indices at [1] + * @see Region#setBufferCapacity(int, int) + * @see Region#growBuffer(int, int) + */ + public static void countStringRegion(final Region region, final Font font, final CharSequence str, final int[/*2*/] vertIndexCount) { + final OutlineShape.Visitor2 visitor = new OutlineShape.Visitor2() { + @Override + public final void visit(final OutlineShape shape) { + region.countOutlineShape(shape, vertIndexCount); + } }; + font.processString(visitor, str); + } + + /** * Render the string in 3D space w.r.t. the font int font em-size [0..1] at the end of an internally cached {@link GLRegion}. * <p> * The shapes added to the GLRegion are in font em-size [0..1]. diff --git a/src/jogl/classes/com/jogamp/graph/font/Font.java b/src/jogl/classes/com/jogamp/graph/font/Font.java index 8d3ed2d8e..f296c77b3 100644 --- a/src/jogl/classes/com/jogamp/graph/font/Font.java +++ b/src/jogl/classes/com/jogamp/graph/font/Font.java @@ -27,9 +27,6 @@ */ package com.jogamp.graph.font; -import java.io.PrintStream; - -import com.jogamp.common.util.PerfCounterCtrl; import com.jogamp.graph.curve.OutlineShape; import com.jogamp.graph.geom.plane.AffineTransform; import com.jogamp.opengl.math.geom.AABBox; @@ -412,7 +409,7 @@ public interface Font { * Visit each {@link Glyph}'s {@link OutlineShape} of the string with the {@link OutlineShape.Visitor} * while passing the progressed {@link AffineTransform}. * <p> - * The produced shapes are in font em-size [0..1], but can be adjusted with the given transform, progressed and passed to the visitor. + * The processed shapes are in font em-size [0..1], but can be adjusted with the given transform, progressed and passed to the visitor. * </p> * @param visitor handling each glyph's outline shape in font em-size [0..1] and the given {@link AffineTransform} * @param transform optional given transform @@ -426,6 +423,15 @@ public interface Font { final CharSequence string, final AffineTransform temp1, final AffineTransform temp2); + /** + * Visit each {@link Glyph}'s {@link OutlineShape} of the string with the constrained {@link OutlineShape.Visitor2}. + * <p> + * The processed shapes are in font em-size [0..1]. + * </p> + * @param visitor handling each glyph's outline shape in font em-size [0..1] + * @param string string text + */ + void processString(final OutlineShape.Visitor2 visitor, final CharSequence string); /** Returns {@link #getFullFamilyName()} */ @Override diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java index c2c66bd36..003795942 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java @@ -42,12 +42,7 @@ import jogamp.graph.font.typecast.ot.table.KernTable; import jogamp.graph.font.typecast.ot.table.KerningPair; import jogamp.graph.font.typecast.ot.table.PostTable; -import java.io.PrintStream; -import java.util.concurrent.TimeUnit; - -import com.jogamp.common.os.Clock; import com.jogamp.common.util.IntObjectHashMap; -import com.jogamp.common.util.PerfCounterCtrl; import com.jogamp.graph.curve.OutlineShape; import com.jogamp.graph.font.Font; import com.jogamp.graph.font.FontFactory; @@ -425,6 +420,24 @@ class TypecastFont implements Font { } @Override + public void processString(final OutlineShape.Visitor2 visitor, final CharSequence string) { + if (null == string || 0 == string.length() ) { + return; + } + final int charCount = string.length(); + + for(int i=0; i< charCount; i++) { + final char character = string.charAt(i); + if( '\n' != character ) { + final OutlineShape glyphShape = getGlyph(getGlyphID(character)).getShape(); + if( null != glyphShape ) { // also covers 'space' and all non-contour symbols + visitor.visit(glyphShape); + } + } + } + } + + @Override final public int getNumGlyphs() { return font.getNumGlyphs(); } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/PerfTextRendererNEWT00.java b/src/test/com/jogamp/opengl/test/junit/graph/PerfTextRendererNEWT00.java index d7e55a778..46bc5cd55 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/PerfTextRendererNEWT00.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/PerfTextRendererNEWT00.java @@ -45,7 +45,6 @@ import com.jogamp.opengl.JoglVersion; import com.jogamp.opengl.fixedfunc.GLMatrixFunc; import com.jogamp.common.os.Clock; -import com.jogamp.common.os.Platform; import com.jogamp.common.util.IOUtil; import com.jogamp.common.util.VersionUtil; import com.jogamp.graph.curve.Region; @@ -249,9 +248,19 @@ public class PerfTextRendererNEWT00 { // FreeSans ~ vertices 68/char, indices 36/char // Ubuntu Light ~ vertices 100/char, indices 50/char // FreeSerif ~ vertices 115/char, indices 61/char - final int vertices_per_char = 68; // 100; - final int indices_per_char = 36; // 50; - final GLRegion region = GLRegion.create(gl.getGLProfile(), renderModes, null, text.length()*vertices_per_char, text.length()*indices_per_char); + // final int vertices_per_char = 68; // 100; + // final int indices_per_char = 36; // 50; + // final GLRegion region = GLRegion.create(gl.getGLProfile(), renderModes, null, text.length()*vertices_per_char, text.length()*indices_per_char); + final GLRegion region = GLRegion.create(gl.getGLProfile(), renderModes, null); + System.err.println("Region post ctor w/ default initial buffer size"); + region.printBufferStats(System.err); + + final int[] verticesIndicesCount = new int[] { 0, 0 }; + TextRegionUtil.countStringRegion(region, font, text, verticesIndicesCount); + System.err.println("Region count: text "+text.length()+" chars -> vertices "+verticesIndicesCount[0]+", indices "+verticesIndicesCount[1]); + region.setBufferCapacity(verticesIndicesCount[0], verticesIndicesCount[1]); + System.err.println("Region post set-buffer-size w/ matching vertices "+verticesIndicesCount[0]+", indices "+verticesIndicesCount[1]); + region.printBufferStats(System.err); final Perf perf = new Perf(); if( do_perf ) { |