diff options
Diffstat (limited to 'src/jogl/classes/com/jogamp')
22 files changed, 1650 insertions, 982 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java index 226e57f07..cb99fbfa4 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java +++ b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java @@ -39,8 +39,9 @@ import com.jogamp.opengl.math.VectorUtil; import com.jogamp.opengl.math.geom.AABBox; -/** A Generic shape objects which is defined by a list of Outlines. - * This Shape can be transformed to Triangulations. +/** + * A Generic shape objects which is defined by a list of Outlines. + * This Shape can be transformed to triangulations. * The list of triangles generated are render-able by a Region object. * The triangulation produced by this Shape will define the * closed region defined by the outlines. @@ -107,19 +108,37 @@ public class OutlineShape implements Comparable<OutlineShape> { } public static final int DIRTY_BOUNDS = 1 << 0; + /** + * Modified shape, requires to update the vertices and triangles, here: vertices. + */ + public static final int DIRTY_VERTICES = 1 << 1; + /** + * Modified shape, requires to update the vertices and triangles, here: triangulation. + */ + public static final int DIRTY_TRIANGLES = 1 << 2; private final Vertex.Factory<? extends Vertex> vertexFactory; - private VerticesState outlineState; /** The list of {@link Outline}s that are part of this * outline shape. */ - private ArrayList<Outline> outlines; - private AABBox bbox; + /* pp */ final ArrayList<Outline> outlines; + + private final AABBox bbox; + private final ArrayList<Triangle> triangles; + private final ArrayList<Vertex> vertices; + + private VerticesState outlineState; /** dirty bits DIRTY_BOUNDS */ private int dirtyBits; + private float sharpness; + + private final float[] tmpV1 = new float[3]; + private final float[] tmpV2 = new float[3]; + private final float[] tmpV3 = new float[3]; + /** Create a new Outline based Shape */ public OutlineShape(Vertex.Factory<? extends Vertex> factory) { @@ -128,7 +147,19 @@ public class OutlineShape implements Comparable<OutlineShape> { this.outlines.add(new Outline()); this.outlineState = VerticesState.UNDEFINED; this.bbox = new AABBox(); + this.triangles = new ArrayList<Triangle>(); + this.vertices = new ArrayList<Vertex>(); this.dirtyBits = 0; + this.sharpness = 0.5f; + } + + public float getSharpness() { return sharpness; } + + public void setSharpness(final float s) { + if( this.sharpness != s ) { + clearCache(); + sharpness=s; + } } /** Clears all data and reset all states as if this instance was newly created */ @@ -137,32 +168,44 @@ public class OutlineShape implements Comparable<OutlineShape> { outlines.add(new Outline()); outlineState = VerticesState.UNDEFINED; bbox.reset(); + vertices.clear(); + triangles.clear(); dirtyBits = 0; } - /** Returns the associated vertex factory of this outline shape + /** Clears cached triangulated data, i.e. {@link #getTriangles(VerticesState)} and {@link #getVertices()}. */ + public void clearCache() { + vertices.clear(); + triangles.clear(); + dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES; + } + + /** + * Returns the associated vertex factory of this outline shape * @return Vertex.Factory object */ public final Vertex.Factory<? extends Vertex> vertexFactory() { return vertexFactory; } - public int getOutlineNumber() { + public final int getOutlineNumber() { return outlines.size(); } - /** Add a new empty {@link Outline} + /** + * Add a new empty {@link Outline} * to the end of this shape's outline list. * <p>If the {@link #getLastOutline()} is empty already, no new one will be added.</p> * * After a call to this function all new vertices added * will belong to the new outline */ - public void addEmptyOutline() { + public final void addEmptyOutline() { if( !getLastOutline().isEmpty() ) { outlines.add(new Outline()); } } - /** Appends the {@link Outline} element to the end, + /** + * Appends the {@link Outline} element to the end, * ensuring a clean tail. * * <p>A clean tail is ensured, no double empty Outlines are produced @@ -171,11 +214,12 @@ public class OutlineShape implements Comparable<OutlineShape> { * @param outline Outline object to be added * @throws NullPointerException if the {@link Outline} element is null */ - public void addOutline(Outline outline) throws NullPointerException { + public final void addOutline(Outline outline) throws NullPointerException { addOutline(outlines.size(), outline); } - /** Insert the {@link Outline} element at the given {@code position}. + /** + * Insert the {@link Outline} element at the given {@code position}. * * <p>If the {@code position} indicates the end of this list, * a clean tail is ensured, no double empty Outlines are produced @@ -186,7 +230,7 @@ public class OutlineShape implements Comparable<OutlineShape> { * @throws NullPointerException if the {@link Outline} element is null * @throws IndexOutOfBoundsException if position is out of range (position < 0 || position > getOutlineNumber()) */ - public void addOutline(int position, Outline outline) throws NullPointerException, IndexOutOfBoundsException { + public final void addOutline(int position, Outline outline) throws NullPointerException, IndexOutOfBoundsException { if (null == outline) { throw new NullPointerException("outline is null"); } @@ -200,6 +244,8 @@ public class OutlineShape implements Comparable<OutlineShape> { if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) { bbox.resize(outline.getBounds()); } + // vertices.addAll(outline.getVertices()); // FIXME: can do and remove DIRTY_VERTICES ? + dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES; return; } } @@ -207,26 +253,29 @@ public class OutlineShape implements Comparable<OutlineShape> { if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) { bbox.resize(outline.getBounds()); } + dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES; } - /** Insert the {@link OutlineShape} elements of type {@link Outline}, .. at the end of this shape, + /** + * Insert the {@link OutlineShape} elements of type {@link Outline}, .. at the end of this shape, * using {@link #addOutline(Outline)} for each element. - * <p>Closes the current last outline via {@link #closeLastOutline()} before adding the new ones.</p> + * <p>Closes the current last outline via {@link #closeLastOutline(boolean)} before adding the new ones.</p> * @param outlineShape OutlineShape elements to be added. * @throws NullPointerException if the {@link OutlineShape} is null * @throws IndexOutOfBoundsException if position is out of range (position < 0 || position > getOutlineNumber()) */ - public void addOutlineShape(OutlineShape outlineShape) throws NullPointerException { + public final void addOutlineShape(OutlineShape outlineShape) throws NullPointerException { if (null == outlineShape) { throw new NullPointerException("OutlineShape is null"); } - closeLastOutline(); + closeLastOutline(true); for(int i=0; i<outlineShape.getOutlineNumber(); i++) { addOutline(outlineShape.getOutline(i)); } } - /** Replaces the {@link Outline} element at the given {@code position}. + /** + * Replaces the {@link Outline} element at the given {@code position}. * <p>Sets the bounding box dirty, hence a next call to {@link #getBounds()} will validate it.</p> * * @param position of the replaced Outline @@ -234,26 +283,28 @@ public class OutlineShape implements Comparable<OutlineShape> { * @throws NullPointerException if the {@link Outline} element is null * @throws IndexOutOfBoundsException if position is out of range (position < 0 || position >= getOutlineNumber()) */ - public void setOutline(int position, Outline outline) throws NullPointerException, IndexOutOfBoundsException { + public final void setOutline(int position, Outline outline) throws NullPointerException, IndexOutOfBoundsException { if (null == outline) { throw new NullPointerException("outline is null"); } outlines.set(position, outline); - dirtyBits |= DIRTY_BOUNDS; + dirtyBits |= DIRTY_BOUNDS | DIRTY_TRIANGLES | DIRTY_VERTICES; } - /** Removes the {@link Outline} element at the given {@code position}. + /** + * Removes the {@link Outline} element at the given {@code position}. * <p>Sets the bounding box dirty, hence a next call to {@link #getBounds()} will validate it.</p> * * @param position of the to be removed Outline * @throws IndexOutOfBoundsException if position is out of range (position < 0 || position >= getOutlineNumber()) */ public final Outline removeOutline(int position) throws IndexOutOfBoundsException { - dirtyBits |= DIRTY_BOUNDS; + dirtyBits |= DIRTY_BOUNDS | DIRTY_TRIANGLES | DIRTY_VERTICES; return outlines.remove(position); } - /** Get the last added outline to the list + /** + * Get the last added outline to the list * of outlines that define the shape * @return the last outline */ @@ -261,15 +312,16 @@ public class OutlineShape implements Comparable<OutlineShape> { return outlines.get(outlines.size()-1); } - /** @return the {@code Outline} at {@code position} + /** + * Returns the {@code Outline} at {@code position} * @throws IndexOutOfBoundsException if position is out of range (position < 0 || position >= getOutlineNumber()) */ - public Outline getOutline(int position) throws IndexOutOfBoundsException { + public final Outline getOutline(int position) throws IndexOutOfBoundsException { return outlines.get(position); } - /** Adds a vertex to the last open outline in the - * shape. + /** + * Adds a vertex to the last open outline to the shape's tail. * @param v the vertex to be added to the OutlineShape */ public final void addVertex(Vertex v) { @@ -278,10 +330,12 @@ public class OutlineShape implements Comparable<OutlineShape> { if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) { bbox.resize(lo.getBounds()); } + // vertices.add(v); // FIXME: can do and remove DIRTY_VERTICES ? + dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES; } - /** Adds a vertex to the last open outline in the shape. - * at {@code position} + /** + * Adds a vertex to the last open outline to the shape at {@code position} * @param position indx at which the vertex will be added * @param v the vertex to be added to the OutlineShape */ @@ -291,9 +345,11 @@ public class OutlineShape implements Comparable<OutlineShape> { if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) { bbox.resize(lo.getBounds()); } + dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES; } - /** Add a 2D {@link Vertex} to the last outline by defining the coordniate attribute + /** + * Add a 2D {@link Vertex} to the last outline by defining the coordinate attribute * of the vertex. The 2D vertex will be represented as Z=0. * * @param x the x coordinate @@ -305,7 +361,8 @@ public class OutlineShape implements Comparable<OutlineShape> { addVertex(vertexFactory.create(x, y, 0f, onCurve)); } - /** Add a 3D {@link Vertex} to the last outline by defining the coordniate attribute + /** + * Add a 3D {@link Vertex} to the last outline by defining the coordniate attribute * of the vertex. * @param x the x coordinate * @param y the y coordinate @@ -317,7 +374,8 @@ public class OutlineShape implements Comparable<OutlineShape> { addVertex(vertexFactory.create(x, y, z, onCurve)); } - /** Add a vertex to the last outline by passing a float array and specifying the + /** + * Add a vertex to the last outline by passing a float array and specifying the * offset and length in which. The attributes of the vertex are located. * The attributes should be continuous (stride = 0). * Attributes which value are not set (when length less than 3) @@ -332,29 +390,37 @@ public class OutlineShape implements Comparable<OutlineShape> { addVertex(vertexFactory.create(coordsBuffer, offset, length, onCurve)); } - /** Closes the last outline in the shape. - * <p>If last vertex is not equal to first vertex. - * A new temp vertex is added at the end which - * is equal to the first.</p> + /** + * Closes the last outline in the shape. + * <p> + * Checks whether the last vertex equals to the first of the last outline. + * If not equal, it either appends a clone of the first vertex + * or prepends a clone of the last vertex, depending on <code>closeTail</code>. + * </p> + * @param closeTail if true, a clone of the first vertex will be appended, + * otherwise a clone of the last vertex will be prepended. */ - public void closeLastOutline() { - getLastOutline().setClosed(true); + public final void closeLastOutline(boolean closeTail) { + if( getLastOutline().setClosed(true) ) { + dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES; + } } /** - * @return the outline's vertices state, {@link OutlineShape.VerticesState} + * Return the outline's vertices state, {@link OutlineShape.VerticesState} */ public final VerticesState getOutlineState() { return outlineState; } - /** Ensure the outlines represent + /** + * Ensure the outlines represent * the specified destinationType. * and removes all overlaps in boundary triangles * @param destinationType the target outline's vertices state. Currently only * {@link OutlineShape.VerticesState#QUADRATIC_NURBS} are supported. */ - public void transformOutlines(VerticesState destinationType) { + protected final void transformOutlines(VerticesState destinationType) { if(outlineState != destinationType){ if(destinationType == VerticesState.QUADRATIC_NURBS){ transformOutlines2Quadratic(); @@ -366,29 +432,30 @@ public class OutlineShape implements Comparable<OutlineShape> { } private void subdivideTriangle(final Outline outline, Vertex a, Vertex b, Vertex c, int index){ - float[] v1 = VectorUtil.mid(a.getCoord(), b.getCoord()); - float[] v3 = VectorUtil.mid(b.getCoord(), c.getCoord()); - float[] v2 = VectorUtil.mid(v1, v3); + VectorUtil.mid(tmpV1, a.getCoord(), b.getCoord()); + VectorUtil.mid(tmpV3, b.getCoord(), c.getCoord()); + VectorUtil.mid(tmpV2, tmpV1, tmpV3); //drop off-curve vertex to image on the curve - b.setCoord(v2, 0, 3); + b.setCoord(tmpV2, 0, 3); b.setOnCurve(true); - outline.addVertex(index, vertexFactory.create(v1, 0, 3, false)); - outline.addVertex(index+2, vertexFactory.create(v3, 0, 3, false)); + outline.addVertex(index, vertexFactory.create(tmpV1, 0, 3, false)); + outline.addVertex(index+2, vertexFactory.create(tmpV3, 0, 3, false)); } - /** Check overlaps between curved triangles - * first check if any vertex in triangle a is in triangle b - * second check if edges of triangle a intersect segments of triangle b - * if any of the two tests is true we divide current triangle - * and add the other to the list of overlaps + /** + * Check overlaps between curved triangles + * first check if any vertex in triangle a is in triangle b + * second check if edges of triangle a intersect segments of triangle b + * if any of the two tests is true we divide current triangle + * and add the other to the list of overlaps * - * Loop until overlap array is empty. (check only in first pass) + * Loop until overlap array is empty. (check only in first pass) */ private void checkOverlaps() { - ArrayList<Vertex> overlaps = new ArrayList<Vertex>(3); - int count = getOutlineNumber(); + final ArrayList<Vertex> overlaps = new ArrayList<Vertex>(3); + final int count = getOutlineNumber(); boolean firstpass = true; do { for (int cc = 0; cc < count; cc++) { @@ -416,8 +483,9 @@ public class OutlineShape implements Comparable<OutlineShape> { vertexCount+=2; if(overlap != null && !overlap.isOnCurve()) { - if(!overlaps.contains(overlap)) + if(!overlaps.contains(overlap)) { overlaps.add(overlap); + } } } } @@ -445,15 +513,14 @@ public class OutlineShape implements Comparable<OutlineShape> { continue; } - if(VectorUtil.vertexInTriangle(a.getCoord(), b.getCoord(), c.getCoord(), current.getCoord()) - || VectorUtil.vertexInTriangle(a.getCoord(), b.getCoord(), c.getCoord(), nextV.getCoord()) - || VectorUtil.vertexInTriangle(a.getCoord(), b.getCoord(), c.getCoord(), prevV.getCoord())) { - + if( VectorUtil.vertexInTriangle3(a.getCoord(), b.getCoord(), c.getCoord(), + current.getCoord(), nextV.getCoord(), prevV.getCoord(), + tmpV1, tmpV2, tmpV3) ) { return current; } - if(VectorUtil.tri2SegIntersection(a, b, c, prevV, current) - || VectorUtil.tri2SegIntersection(a, b, c, current, nextV) - || VectorUtil.tri2SegIntersection(a, b, c, prevV, nextV)) { + if(VectorUtil.testTri2SegIntersection(a, b, c, prevV, current) || + VectorUtil.testTri2SegIntersection(a, b, c, current, nextV) || + VectorUtil.testTri2SegIntersection(a, b, c, prevV, nextV)) { return current; } } @@ -471,9 +538,8 @@ public class OutlineShape implements Comparable<OutlineShape> { final Vertex currentVertex = outline.getVertex(i); final Vertex nextVertex = outline.getVertex((i+1)%vertexCount); if ( !currentVertex.isOnCurve() && !nextVertex.isOnCurve() ) { - final float[] newCoords = VectorUtil.mid(currentVertex.getCoord(), - nextVertex.getCoord()); - final Vertex v = vertexFactory.create(newCoords, 0, 3, true); + VectorUtil.mid(tmpV1, currentVertex.getCoord(), nextVertex.getCoord()); + final Vertex v = vertexFactory.create(tmpV1, 0, 3, true); i++; vertexCount++; outline.addVertex(i, v); @@ -496,49 +562,84 @@ public class OutlineShape implements Comparable<OutlineShape> { outlineState = VerticesState.QUADRATIC_NURBS; } - private void generateVertexIds() { + private int generateVertexIds() { int maxVertexId = 0; for(int i=0; i<outlines.size(); i++) { final ArrayList<Vertex> vertices = outlines.get(i).getVertices(); for(int pos=0; pos<vertices.size(); pos++) { - Vertex vert = vertices.get(pos); - vert.setId(maxVertexId); - maxVertexId++; + vertices.get(pos).setId(maxVertexId++); } } + return maxVertexId; } - /** @return the list of concatenated vertices associated with all - * {@code Outline}s of this object + /** + * Return list of concatenated vertices associated with all + * {@code Outline}s of this object. + * <p> + * Vertices are cached until marked dirty. + * </p> + * <p> + * Should always be called <i>after</i> {@link #getTriangles(VerticesState)}, + * since the latter will mark all cached vertices dirty! + * </p> */ - public ArrayList<Vertex> getVertices() { - ArrayList<Vertex> vertices = new ArrayList<Vertex>(); - for(int i=0; i<outlines.size(); i++) { - vertices.addAll(outlines.get(i).getVertices()); + public final ArrayList<Vertex> getVertices() { + final boolean updated; + if( 0 != ( DIRTY_VERTICES & dirtyBits ) ) { + vertices.clear(); + for(int i=0; i<outlines.size(); i++) { + vertices.addAll(outlines.get(i).getVertices()); + } + dirtyBits &= ~DIRTY_VERTICES; + updated = true; + } else { + updated = false; + } + if(Region.DEBUG_INSTANCE) { + System.err.println("OutlineShape.getVertices(): o "+outlines.size()+", v "+vertices.size()+", updated "+updated); } return vertices; } + private void triangulateImpl() { + if( 0 < outlines.size() ) { + sortOutlines(); + generateVertexIds(); + + triangles.clear(); + final Triangulator triangulator2d = Triangulation.create(); + for(int index = 0; index<outlines.size(); index++) { + triangulator2d.addCurve(triangles, outlines.get(index), sharpness); + } + triangulator2d.generate(triangles); + triangulator2d.reset(); + } + } + /** - * Triangulate the {@link OutlineShape} generating a list of triangles + * Triangulate the {@link OutlineShape} generating a list of triangles, + * while {@link #transformOutlines(VerticesState)} beforehand. + * <p> + * Triangles are cached until marked dirty. + * </p> * @return an arraylist of triangles representing the filled region * which is produced by the combination of the outlines */ - public ArrayList<Triangle> triangulate() { - if(outlines.size() == 0){ - return null; + public ArrayList<Triangle> getTriangles(VerticesState destinationType) { + final boolean updated; + if( 0 != ( DIRTY_TRIANGLES & dirtyBits ) ) { + transformOutlines(destinationType); + triangulateImpl(); + updated = true; + dirtyBits |= DIRTY_VERTICES; + dirtyBits &= ~DIRTY_TRIANGLES; + } else { + updated = false; } - sortOutlines(); - generateVertexIds(); - - Triangulator triangulator2d = Triangulation.create(); - for(int index = 0; index<outlines.size(); index++) { - triangulator2d.addCurve(outlines.get(index)); + if(Region.DEBUG_INSTANCE) { + System.err.println("OutlineShape.getTriangles().2: "+triangles.size()+", updated "+updated); } - - ArrayList<Triangle> triangles = triangulator2d.generate(); - triangulator2d.reset(); - return triangles; } @@ -550,24 +651,24 @@ public class OutlineShape implements Comparable<OutlineShape> { Collections.reverse(outlines); } - /** Compare two outline shapes with Bounding Box area + /** + * Compare two outline shapes with Bounding Box area * as criteria. * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override - public final int compareTo(OutlineShape outline) { - float size = getBounds().getSize(); - float newSize = outline.getBounds().getSize(); - if(size < newSize){ + public final int compareTo(final OutlineShape other) { + final float thisSize = getBounds().getSize(); + final float otherSize = other.getBounds().getSize(); + if( thisSize < otherSize ){ return -1; - } - else if(size > newSize){ + } else if( thisSize > otherSize ) { return 1; } - return 0; + return 0; // FIXME: No epsilon, i.e. smallest accurate float value ? } - private final void validateBoundingBox() { + private void validateBoundingBox() { dirtyBits &= ~DIRTY_BOUNDS; bbox.reset(); for (int i=0; i<outlines.size(); i++) { diff --git a/src/jogl/classes/com/jogamp/graph/curve/Region.java b/src/jogl/classes/com/jogamp/graph/curve/Region.java index a9779523a..8e68b7913 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/Region.java +++ b/src/jogl/classes/com/jogamp/graph/curve/Region.java @@ -28,159 +28,248 @@ package com.jogamp.graph.curve; import java.util.ArrayList; +import java.util.List; +import jogamp.graph.curve.opengl.VBORegion2PVBAAES2; +import jogamp.graph.curve.opengl.VBORegion2PMSAAES2; +import jogamp.graph.curve.opengl.VBORegionSPES2; +import jogamp.graph.geom.plane.AffineTransform; import jogamp.opengl.Debug; +import com.jogamp.graph.curve.opengl.GLRegion; import com.jogamp.graph.geom.Triangle; import com.jogamp.graph.geom.Vertex; import com.jogamp.opengl.math.geom.AABBox; -/** Abstract Outline shape GL representation - * define the method an OutlineShape(s) is - * binded rendered. +/** + * Abstract Outline shape representation define the method an OutlineShape(s) + * is bound and rendered. * - * @see GLRegion - */ + * @see GLRegion */ public abstract class Region { - /** Debug flag for region impl (graph.curve) - */ + /** Debug flag for region impl (graph.curve) */ public static final boolean DEBUG = Debug.debug("graph.curve"); + public static final boolean DEBUG_INSTANCE = Debug.debug("graph.curve.instance"); - public static final boolean DEBUG_INSTANCE = false; + /** + * MSAA based Anti-Aliasing, a two pass region rendering, slower and more + * resource hungry (FBO), but providing fast MSAA in case + * the whole scene is not rendered with MSAA. + */ + public static final int MSAA_RENDERING_BIT = 1 << 0; - /** View based Anti-Aliasing, A Two pass region rendering, slower - * and more resource hungry (FBO), but AA is perfect. - * Otherwise the default fast one pass MSAA region rendering is being used. + /** + * View based Anti-Aliasing, a two pass region rendering, slower and more + * resource hungry (FBO), but AA is perfect. Otherwise the default fast one + * pass MSAA region rendering is being used. */ - public static final int VBAA_RENDERING_BIT = 1 << 0; + public static final int VBAA_RENDERING_BIT = 1 << 1; - /** Use non uniform weights [0.0 .. 1.9] for curve region rendering. - * Otherwise the default weight 1.0 for uniform curve region rendering is being applied. + /** + * Use non uniform weights [0.0 .. 1.9] for curve region rendering. + * Otherwise the default weight 1.0 for uniform curve region rendering is + * being applied. */ - public static final int VARIABLE_CURVE_WEIGHT_BIT = 1 << 1; + public static final int VARIABLE_CURVE_WEIGHT_BIT = 1 << 8; public static final int TWO_PASS_DEFAULT_TEXTURE_UNIT = 0; private final int renderModes; private boolean dirty = true; - protected int numVertices = 0; + private int numVertices = 0; protected final AABBox box = new AABBox(); - protected ArrayList<Triangle> triangles = new ArrayList<Triangle>(); - protected ArrayList<Vertex> vertices = new ArrayList<Vertex>(); public static boolean isVBAA(int renderModes) { - return 0 != ( renderModes & Region.VBAA_RENDERING_BIT ); + return 0 != (renderModes & Region.VBAA_RENDERING_BIT); + } + public static boolean isMSAA(int renderModes) { + return 0 != (renderModes & Region.MSAA_RENDERING_BIT); + } + public static String getRenderModeString(int renderModes) { + if( Region.isVBAA(renderModes) ) { + return "vbaa"; + } else if( Region.isMSAA(renderModes) ) { + return "msaa"; + } else { + return "norm" ; + } } - /** Check if render mode capable of non uniform weights - * @param renderModes bit-field of modes, e.g. {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, - * {@link Region#VBAA_RENDERING_BIT} - * @return true of capable of non uniform weights - */ + /** + * Check if render mode capable of non uniform weights + * + * @param renderModes + * bit-field of modes, e.g. + * {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, + * {@link Region#VBAA_RENDERING_BIT} + * @return true of capable of non uniform weights */ public static boolean isNonUniformWeight(int renderModes) { - return 0 != ( renderModes & Region.VARIABLE_CURVE_WEIGHT_BIT ); + return 0 != (renderModes & Region.VARIABLE_CURVE_WEIGHT_BIT); + } + + /** + * Create a Region using the passed render mode + * + * <p> In case {@link Region#VBAA_RENDERING_BIT} is being requested the default texture unit + * {@link Region#TWO_PASS_DEFAULT_TEXTURE_UNIT} is being used.</p> + * + * @param rs the RenderState to be used + * @param renderModes bit-field of modes, e.g. {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, {@link Region#VBAA_RENDERING_BIT} + */ + public static GLRegion create(int renderModes) { + if( isVBAA(renderModes) ) { + return new VBORegion2PVBAAES2(renderModes, Region.TWO_PASS_DEFAULT_TEXTURE_UNIT); + } else if( isMSAA(renderModes) ) { + return new VBORegion2PMSAAES2(renderModes, Region.TWO_PASS_DEFAULT_TEXTURE_UNIT); + } else { + return new VBORegionSPES2(renderModes); + } } protected Region(int regionRenderModes) { this.renderModes = regionRenderModes; } - /** Get current Models - * @return bit-field of render modes + // FIXME: Better handling of impl. buffer growth .. ! + + protected abstract void pushVertex(float[] coords, float[] texParams); + protected abstract void pushIndex(int idx); + + /** + * Return bit-field of render modes, see {@link #create(int)}. */ public final int getRenderModes() { return renderModes; } - /** Check if current Region is using VBAA - * @return true if capable of two pass rendering - VBAA - */ - public boolean isVBAA() { - return Region.isVBAA(renderModes); + protected void clearImpl() { + dirty = true; + numVertices = 0; + box.reset(); } - /** Check if current instance uses non uniform weights - * @return true if capable of nonuniform weights + /** + * Return true if capable of two pass rendering - VBAA, otherwise false. */ - public boolean isNonUniformWeight() { - return Region.isNonUniformWeight(renderModes); + public final boolean isVBAA() { + return isVBAA(renderModes); } - /** Get the current number of vertices associated - * with this region. This number is not necessary equal to - * the OGL bound number of vertices. - * @return vertices count + /** + * Return true if capable of two pass rendering - MSAA, otherwise false. */ - public final int getNumVertices(){ - return numVertices; + public final boolean isMSAA() { + return isMSAA(renderModes); } - /** Adds a {@link Triangle} object to the Region - * This triangle will be bound to OGL objects - * on the next call to {@code update} - * @param tri a triangle object - * - * @see update(GL2ES2) + /** + * Return true if capable of nonuniform weights, otherwise false. */ - public void addTriangle(Triangle tri) { - triangles.add(tri); - setDirty(true); + public final boolean isNonUniformWeight() { + return Region.isNonUniformWeight(renderModes); } - /** Adds a list of {@link Triangle} objects to the Region - * These triangles are to be binded to OGL objects - * on the next call to {@code update} - * @param tris an arraylist of triangle objects - * - * @see update(GL2ES2) - */ - public void addTriangles(ArrayList<Triangle> tris) { - triangles.addAll(tris); - setDirty(true); - } + final float[] coordsEx = new float[3]; - /** Adds a {@link Vertex} object to the Region - * This vertex will be bound to OGL objects - * on the next call to {@code update} - * @param vert a vertex objects - * - * @see update(GL2ES2) - */ - public void addVertex(Vertex vert) { - vertices.add(vert); + private void pushNewVertexImpl(final Vertex vertIn, final AffineTransform transform) { + if( null != transform ) { + final float[] coordsIn = vertIn.getCoord(); + transform.transform(coordsIn, coordsEx); + coordsEx[2] = coordsIn[2]; + box.resize(coordsEx[0], coordsEx[1], coordsEx[2]); + pushVertex(coordsEx, vertIn.getTexCoord()); + } else { + box.resize(vertIn.getX(), vertIn.getY(), vertIn.getZ()); + pushVertex(vertIn.getCoord(), vertIn.getTexCoord()); + } numVertices++; - setDirty(true); } - /** Adds a list of {@link Vertex} objects to the Region - * These vertices are to be binded to OGL objects - * on the next call to {@code update} - * @param verts an arraylist of vertex objects - * - * @see update(GL2ES2) - */ - public void addVertices(ArrayList<Vertex> verts) { - vertices.addAll(verts); - numVertices = vertices.size(); + private void pushNewVertexIdxImpl(final Vertex vertIn, final AffineTransform transform) { + pushIndex(numVertices); + pushNewVertexImpl(vertIn, transform); + } + + public final void addOutlineShape(final OutlineShape shape, final AffineTransform transform) { + final List<Triangle> trisIn = shape.getTriangles(OutlineShape.VerticesState.QUADRATIC_NURBS); + final ArrayList<Vertex> vertsIn = shape.getVertices(); + if(DEBUG_INSTANCE) { + System.err.println("Region.addOutlineShape().0: tris: "+trisIn.size()+", verts "+vertsIn.size()+", transform "+transform); + } + final int idxOffset = numVertices; + int vertsVNewIdxCount = 0, vertsTMovIdxCount = 0, vertsTNewIdxCount = 0, tris = 0; + int vertsDupCountV = 0, vertsDupCountT = 0, vertsKnownMovedT = 0; + if( vertsIn.size() >= 3 ) { + if(DEBUG_INSTANCE) { + System.err.println("Region.addOutlineShape(): Processing Vertices"); + } + for(int i=0; i<vertsIn.size(); i++) { + pushNewVertexImpl(vertsIn.get(i), transform); + vertsVNewIdxCount++; + } + if(DEBUG_INSTANCE) { + System.err.println("Region.addOutlineShape(): Processing Triangles"); + } + for(int i=0; i<trisIn.size(); i++) { + final Triangle triIn = trisIn.get(i); + if(Region.DEBUG_INSTANCE) { + System.err.println("T["+i+"]: "+triIn); + } + // triEx.addVertexIndicesOffset(idxOffset); + // triangles.add( triEx ); + final Vertex[] triInVertices = triIn.getVertices(); + final int tv0Idx = triInVertices[0].getId(); + if( Integer.MAX_VALUE-idxOffset > tv0Idx ) { // Integer.MAX_VALUE != i0 // FIXME: renderer uses SHORT! + // valid 'known' idx - move by offset + if(Region.DEBUG_INSTANCE) { + System.err.println("T["+i+"]: Moved "+tv0Idx+" + "+idxOffset+" -> "+(tv0Idx+idxOffset)); + } + pushIndex(tv0Idx+idxOffset); + pushIndex(triInVertices[1].getId()+idxOffset); + pushIndex(triInVertices[2].getId()+idxOffset); + vertsTMovIdxCount+=3; + } else { + // invalid idx - generate new one + if(Region.DEBUG_INSTANCE) { + System.err.println("T["+i+"]: New Idx "+numVertices); + } + pushNewVertexIdxImpl(triInVertices[0], transform); + pushNewVertexIdxImpl(triInVertices[1], transform); + pushNewVertexIdxImpl(triInVertices[2], transform); + vertsTNewIdxCount+=3; + } + tris++; + } + } + if(DEBUG_INSTANCE) { + System.err.println("Region.addOutlineShape().X: idxOffset "+idxOffset+", tris: "+tris+", verts [idx "+vertsTNewIdxCount+", add "+vertsTNewIdxCount+" = "+(vertsVNewIdxCount+vertsTNewIdxCount)+"]"); + System.err.println("Region.addOutlineShape().X: verts: idx[v-new "+vertsVNewIdxCount+", t-new "+vertsTNewIdxCount+" = "+(vertsVNewIdxCount+vertsTNewIdxCount)+"]"); + System.err.println("Region.addOutlineShape().X: verts: idx t-moved "+vertsTMovIdxCount+", numVertices "+numVertices); + System.err.println("Region.addOutlineShape().X: verts: v-dups "+vertsDupCountV+", t-dups "+vertsDupCountT+", t-known "+vertsKnownMovedT); + // int vertsDupCountV = 0, vertsDupCountT = 0; + System.err.println("Region.addOutlineShape().X: box "+box); + } setDirty(true); } - /** - * @return the AxisAligned bounding box of - * current region - */ - public final AABBox getBounds(){ + public final void addOutlineShapes(final List<OutlineShape> shapes, final AffineTransform transform) { + for (int i = 0; i < shapes.size(); i++) { + addOutlineShape(shapes.get(i), transform); + } + } + + /** @return the AxisAligned bounding box of current region */ + public final AABBox getBounds() { return box; } - /** Check if this region is dirty. A region is marked dirty - * when new Vertices, Triangles, and or Lines are added after a - * call to update() + /** Check if this region is dirty. A region is marked dirty when new + * Vertices, Triangles, and or Lines are added after a call to update() + * * @return true if region is Dirty, false otherwise * - * @see update(GL2ES2) - */ + * @see update(GL2ES2) */ public final boolean isDirty() { return dirty; } diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java index dfb7a95b3..defb7722a 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java @@ -27,17 +27,11 @@ */
package com.jogamp.graph.curve.opengl;
-
-import java.util.ArrayList;
-
+import javax.media.opengl.GL;
import javax.media.opengl.GL2ES2;
-import com.jogamp.opengl.util.PMVMatrix;
-import com.jogamp.graph.curve.OutlineShape;
+import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.graph.curve.Region;
-import com.jogamp.graph.geom.Triangle;
-import com.jogamp.graph.geom.Vertex;
-import jogamp.graph.curve.opengl.RegionFactory;
/** A GLRegion is the OGL binding of one or more OutlineShapes
* Defined by its vertices and generated triangles. The Region
@@ -51,79 +45,87 @@ import jogamp.graph.curve.opengl.RegionFactory; */
public abstract class GLRegion extends Region {
- /** Create an ogl {@link GLRegion} defining the list of {@link OutlineShape}.
- * Combining the Shapes into single buffers.
- * @return the resulting Region inclusive the generated region
+ /**
+ * Create a GLRegion using the passed render mode
+ *
+ * <p> In case {@link Region#VBAA_RENDERING_BIT} is being requested the default texture unit
+ * {@link Region#TWO_PASS_DEFAULT_TEXTURE_UNIT} is being used.</p>
+ *
+ * @param rs the RenderState to be used
+ * @param renderModes bit-field of modes, e.g. {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, {@link Region#VBAA_RENDERING_BIT}
*/
- public static GLRegion create(OutlineShape[] outlineShapes, int renderModes) {
- final GLRegion region = RegionFactory.create(renderModes);
-
- int numVertices = region.getNumVertices();
-
- for(int index=0; index<outlineShapes.length; index++) {
- OutlineShape outlineShape = outlineShapes[index];
- outlineShape.transformOutlines(OutlineShape.VerticesState.QUADRATIC_NURBS);
-
- ArrayList<Triangle> triangles = outlineShape.triangulate();
- region.addTriangles(triangles);
-
- ArrayList<Vertex> vertices = outlineShape.getVertices();
- for(int pos=0; pos < vertices.size(); pos++){
- Vertex vert = vertices.get(pos);
- vert.setId(numVertices++);
- }
- region.addVertices(vertices);
- }
+ public static GLRegion create(int renderModes) {
+ return Region.create(renderModes);
+ }
- return region;
+ protected GLRegion(int renderModes) {
+ super(renderModes);
}
/**
- * Create an ogl {@link GLRegion} defining this {@link OutlineShape}
- * @return the resulting Region.
+ * Updates a graph region by updating the ogl related
+ * objects for use in rendering if {@link #isDirty()}.
+ * <p>Allocates the ogl related data and initializes it the 1st time.<p>
+ * <p>Called by {@link #draw(GL2ES2, RenderState, int, int, int)}.</p>
*/
- public static GLRegion create(OutlineShape outlineShape, int renderModes) {
- final GLRegion region = RegionFactory.create(renderModes);
+ protected abstract void update(GL2ES2 gl, RegionRenderer renderer);
- outlineShape.transformOutlines(OutlineShape.VerticesState.QUADRATIC_NURBS);
- ArrayList<Triangle> triangles = (ArrayList<Triangle>) outlineShape.triangulate();
- ArrayList<Vertex> vertices = (ArrayList<Vertex>) outlineShape.getVertices();
- region.addVertices(vertices);
- region.addTriangles(triangles);
- return region;
- }
+ protected abstract void destroyImpl(GL2ES2 gl, RegionRenderer renderer);
- protected GLRegion(int renderModes) {
- super(renderModes);
- }
+ protected abstract void clearImpl(final GL2ES2 gl, final RegionRenderer renderer);
- /** Updates a graph region by updating the ogl related
- * objects for use in rendering if {@link #isDirty()}.
- * <p>Allocates the ogl related data and initializes it the 1st time.<p>
- * <p>Called by {@link #draw(GL2ES2, RenderState, int, int, int)}.</p>
- * @param rs TODO
+ /**
+ * Clears all data, i.e. triangles, vertices etc.
*/
- protected abstract void update(GL2ES2 gl, RenderState rs);
+ public void clear(final GL2ES2 gl, final RegionRenderer renderer) {
+ clearImpl(gl, renderer);
+ clearImpl();
+ }
- /** Delete and clean the associated OGL
- * objects
+ /**
+ * Delete and clear the associated OGL objects.
*/
- public abstract void destroy(GL2ES2 gl, RenderState rs);
+ public final void destroy(GL2ES2 gl, RegionRenderer renderer) {
+ clear(gl, renderer);
+ destroyImpl(gl, renderer);
+ }
- /** Renders the associated OGL objects specifying
+ /**
+ * Renders the associated OGL objects specifying
* current width/hight of window for multi pass rendering
* of the region.
+ * <p>
+ * User shall consider {@link RegionRenderer#enable(GL2ES2, boolean) enabling}
+ * the renderer beforehand and {@link RegionRenderer#enable(GL2ES2, boolean) disabling}
+ * it afterwards when used in conjunction with other renderer.
+ * </p>
+ * <p>
+ * Users shall also consider setting the {@link GL#glClearColor(float, float, float, float) Clear Color}
+ * appropriately:
+ * <ul>
+ * <li>If {@link GL#GL_BLEND blending} is enabled, <i>RGB</i> shall be set to text color, otherwise
+ * blending will reduce the alpha seam's contrast and the font will appear thinner.</li>
+ * <li>If {@link GL#GL_BLEND blending} is disabled, <i>RGB</i> shall be set to the actual desired background.</li>
+ * </ul>
+ * The <i>alpha</i> component shall be set to zero.
+ * Note: If {@link GL#GL_BLEND blending} is enabled, the
+ * {@link RegionRenderer} might need to be
+ * {@link RegionRenderer#create(RenderState, int, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback, com.jogamp.graph.curve.opengl.RegionRenderer.GLCallback) created}
+ * with the appropriate {@link {@link RegionRenderer.GLCallback callbacks}.
+ * </p>
* @param matrix current {@link PMVMatrix}.
- * @param rs the RenderState to be used
- * @param vp_width current screen width
- * @param vp_height current screen height
- * @param texWidth desired texture width for multipass-rendering.
- * The actual used texture-width is written back when mp rendering is enabled, otherwise the store is untouched.
+ * @param renderer the {@link RegionRenderer} to be used
+ * @param sampleCount desired multisampling sample count for msaa-rendering.
+ * The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched.
+ * @see RegionRenderer#enable(GL2ES2, boolean)
*/
- public final void draw(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int[/*1*/] texWidth) {
- update(gl, rs);
- drawImpl(gl, rs, vp_width, vp_height, texWidth);
+ public final void draw(GL2ES2 gl, RegionRenderer renderer, int[/*1*/] sampleCount) {
+ if(isDirty()) {
+ update(gl, renderer);
+ setDirty(false);
+ }
+ drawImpl(gl, renderer, sampleCount);
}
- protected abstract void drawImpl(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int[/*1*/] texWidth);
+ protected abstract void drawImpl(GL2ES2 gl, RegionRenderer renderer, int[/*1*/] sampleCount);
}
diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java index f7d4bfd2f..1f6e532d3 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java @@ -27,58 +27,373 @@ */ package com.jogamp.graph.curve.opengl; +import java.nio.FloatBuffer; + +import javax.media.opengl.GL; import javax.media.opengl.GL2ES2; import javax.media.opengl.GLException; +import javax.media.opengl.fixedfunc.GLMatrixFunc; -import com.jogamp.graph.curve.OutlineShape; +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderState; +import com.jogamp.opengl.util.PMVMatrix; import com.jogamp.graph.curve.Region; -public abstract class RegionRenderer extends Renderer { +/** + * OpenGL {@link Region} renderer + * <p> + * All OpenGL related operations regarding {@link Region}s + * are passed through an instance of this class. + * </p> + */ +public abstract class RegionRenderer { + protected static final boolean DEBUG = Region.DEBUG; + protected static final boolean DEBUG_INSTANCE = Region.DEBUG_INSTANCE; + + public interface GLCallback { + /** + * @param gl a current GL object + * @param renderer {@link RegionRenderer} calling this method. + */ + void run(GL gl, RegionRenderer renderer); + } + + /** + * Default {@link GL#GL_BLEND} <i>enable</i> {@link GLCallback}, + * turning on the {@link GL#GL_BLEND} state and setting up + * {@link GL#glBlendFunc(int, int) glBlendFunc}({@link GL#GL_SRC_ALPHA}, {@link GL#GL_ONE_MINUS_SRC_ALPHA}). + * @see #setEnableCallback(GLCallback, GLCallback) + * @see #enable(GL2ES2, boolean) + */ + public static final GLCallback defaultBlendEnable = new GLCallback() { + @Override + public void run(final GL gl, final RegionRenderer args) { + gl.glEnable(GL.GL_BLEND); + gl.glBlendEquation(GL.GL_FUNC_ADD); // default + gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + } + }; + + /** + * Default {@link GL#GL_BLEND} <i>disable</i> {@link GLCallback}, + * simply turning off the {@link GL#GL_BLEND} state. + * @see #setEnableCallback(GLCallback, GLCallback) + * @see #enable(GL2ES2, boolean) + */ + public static final GLCallback defaultBlendDisable = new GLCallback() { + @Override + public void run(final GL gl, final RegionRenderer args) { + gl.glDisable(GL.GL_BLEND); + } + }; + + public static boolean isWeightValid(float v) { + return 0.0f <= v && v <= 1.9f ; + } /** * Create a Hardware accelerated Region Renderer. + * <p> + * The optional {@link GLCallback}s <code>enableCallback</code> and <code>disableCallback</code> + * maybe used to issue certain tasks at {@link #enable(GL2ES2, boolean)}.<br/> + * For example, instances {@link #defaultBlendEnable} and {@link #defaultBlendDisable} + * can be utilized to enable and disable {@link GL#GL_BLEND}. + * </p> * @param rs the used {@link RenderState} * @param renderModes bit-field of modes, e.g. {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, {@link Region#VBAA_RENDERING_BIT} + * @param enableCallback optional {@link GLCallback}, if not <code>null</code> will be issued at + * {@link #init(GL2ES2) init(gl)} and {@link #enable(GL2ES2, boolean) enable(gl, true)}. + * @param disableCallback optional {@link GLCallback}, if not <code>null</code> will be issued at + * {@link #enable(GL2ES2, boolean) enable(gl, false)}. * @return an instance of Region Renderer + * @see #enable(GL2ES2, boolean) */ - public static RegionRenderer create(RenderState rs, int renderModes) { - return new jogamp.graph.curve.opengl.RegionRendererImpl01(rs, renderModes); + public static RegionRenderer create(final RenderState rs, final int renderModes, + final GLCallback enableCallback, final GLCallback disableCallback) { + return new jogamp.graph.curve.opengl.RegionRendererImpl01(rs, renderModes, enableCallback, disableCallback); } - protected RegionRenderer(RenderState rs, int renderModes) { - super(rs, renderModes); + protected final int renderModes; + protected final RenderState rs; + + protected final GLCallback enableCallback; + protected final GLCallback disableCallback; + + protected int vp_width; + protected int vp_height; + protected boolean initialized; + private boolean vboSupported = false; + + public final boolean isInitialized() { return initialized; } + + public final int getWidth() { return vp_width; } + public final int getHeight() { return vp_height; } + + public final float getWeight() { return rs.getWeight().floatValue(); } + public final float getAlpha() { return rs.getAlpha().floatValue(); } + public final PMVMatrix getMatrix() { return rs.pmvMatrix(); } + + /** + * Implementation shall load, compile and link the shader program and leave it active. + * @param gl referencing the current GLContext to which the ShaderState is bound to + * @return + */ + protected abstract boolean initImpl(GL2ES2 gl); + + /** Delete and clean the associated OGL objects */ + protected abstract void destroyImpl(GL2ES2 gl); + + ////////////////////////////////////// + + /** + * @param rs the used {@link RenderState} + * @param renderModes bit-field of modes + */ + protected RegionRenderer(final RenderState rs, final int renderModes, final GLCallback enableCallback, final GLCallback disableCallback) { + this.rs = rs; + this.renderModes = renderModes; + this.enableCallback = enableCallback; + this.disableCallback = disableCallback; + } + + public final int getRenderModes() { + return renderModes; } + public final boolean usesVariableCurveWeight() { return Region.isNonUniformWeight(renderModes); } - /** Render an {@link OutlineShape} in 3D space at the position provided - * the triangles of the shapes will be generated, if not yet generated - * @param region the OutlineShape to Render. - * @param position the initial translation of the outlineShape. - * @param texWidth desired texture width for multipass-rendering. - * The actual used texture-width is written back when mp rendering is enabled, otherwise the store is untouched. - * @throws Exception if HwRegionRenderer not initialized + /** + * @return true if Region's renderModes contains all bits as this Renderer's renderModes + * except {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, otherwise false. + */ + public final boolean areRenderModesCompatible(final Region region) { + final int cleanRenderModes = getRenderModes() & ( Region.VARIABLE_CURVE_WEIGHT_BIT ); + return cleanRenderModes == ( region.getRenderModes() & cleanRenderModes ); + } + + public final boolean isVBOSupported() { return vboSupported; } + + /** + * Initialize shader and bindings for GPU based rendering bound to the given GL object's GLContext + * if not initialized yet. + * <p>Leaves the renderer enabled, ie ShaderState.</p> + * <p>Shall be called by a {@code draw()} method, e.g. {@link RegionRenderer#draw(GL2ES2, Region, int)}</p> + * + * @param gl referencing the current GLContext to which the ShaderState is bound to + * @throws GLException if initialization failed */ - public final void draw(GL2ES2 gl, Region region, float[] position, int[/*1*/] texWidth) { - if(!isInitialized()) { - throw new GLException("RegionRenderer: not initialized!"); + public final void init(GL2ES2 gl) throws GLException { + if(initialized){ + return; } - if( !areRenderModesCompatible(region) ) { - throw new GLException("Incompatible render modes, : region modes "+region.getRenderModes()+ - " doesn't contain renderer modes "+this.getRenderModes()); + vboSupported = gl.isFunctionAvailable("glGenBuffers") && + gl.isFunctionAvailable("glBindBuffer") && + gl.isFunctionAvailable("glBufferData") && + gl.isFunctionAvailable("glDrawElements") && + gl.isFunctionAvailable("glVertexAttribPointer") && + gl.isFunctionAvailable("glDeleteBuffers"); + + if(DEBUG) { + System.err.println("TextRendererImpl01: VBO Supported = " + isVBOSupported()); + } + + if(!vboSupported){ + throw new GLException("VBO not supported"); + } + + rs.attachTo(gl); + + if( null != enableCallback ) { + enableCallback.run(gl, this); + } + + initialized = initImpl(gl); + if(!initialized) { + throw new GLException("Shader initialization failed"); + } + + if(!rs.getShaderState().uniform(gl, rs.getPMVMatrix())) { + throw new GLException("Error setting PMVMatrix in shader: "+rs.getShaderState()); + } + + if( Region.isNonUniformWeight( getRenderModes() ) ) { + if(!rs.getShaderState().uniform(gl, rs.getWeight())) { + throw new GLException("Error setting weight in shader: "+rs.getShaderState()); + } } - drawImpl(gl, region, position, texWidth); + + if(!rs.getShaderState().uniform(gl, rs.getAlpha())) { + throw new GLException("Error setting global alpha in shader: "+rs.getShaderState()); + } + + if(!rs.getShaderState().uniform(gl, rs.getColorStatic())) { + throw new GLException("Error setting global color in shader: "+rs.getShaderState()); + } + } + + public final void destroy(GL2ES2 gl) { + if(!initialized){ + if(DEBUG_INSTANCE) { + System.err.println("TextRenderer: Not initialized!"); + } + return; + } + rs.getShaderState().useProgram(gl, false); + destroyImpl(gl); + rs.destroy(gl); + initialized = false; } + public final RenderState getRenderState() { return rs; } + public final ShaderState getShaderState() { return rs.getShaderState(); } + /** - * Usually just dispatched the draw call to the Region's draw implementation, - * e.g. {@link com.jogamp.graph.curve.opengl.GLRegion#draw(GL2ES2, RenderState, int, int, int[]) GLRegion#draw(GL2ES2, RenderState, int, int, int[])}. + * Enabling or disabling the {@link RenderState#getShaderState() RenderState}'s + * {@link ShaderState#useProgram(GL2ES2, boolean) ShaderState program}. + * <p> + * In case enable and disable {@link GLCallback}s are setup via {@link #create(RenderState, int, GLCallback, GLCallback)}, + * they will be called before toggling the shader program. + * </p> + * @see #create(RenderState, int, GLCallback, GLCallback) */ - protected abstract void drawImpl(GL2ES2 gl, Region region, float[] position, int[] texWidth); + public final void enable(GL2ES2 gl, boolean enable) { + if( enable ) { + if( null != enableCallback ) { + enableCallback.run(gl, this); + } + } else { + if( null != disableCallback ) { + disableCallback.run(gl, this); + } + } + rs.getShaderState().useProgram(gl, enable); + } + + public final void setWeight(GL2ES2 gl, float v) { + if( !isWeightValid(v) ) { + throw new IllegalArgumentException("Weight out of range"); + } + rs.getWeight().setData(v); + if(null != gl && rs.getShaderState().inUse() && Region.isNonUniformWeight( getRenderModes() ) ) { + rs.getShaderState().uniform(gl, rs.getWeight()); + } + } + + public final void setAlpha(GL2ES2 gl, float alpha_t) { + rs.getAlpha().setData(alpha_t); + if(null != gl && rs.getShaderState().inUse()) { + rs.getShaderState().uniform(gl, rs.getAlpha()); + } + + } + + public final void getColorStatic(GL2ES2 gl, float[] rgb) { + FloatBuffer fb = (FloatBuffer) rs.getColorStatic().getBuffer(); + rgb[0] = fb.get(0); + rgb[1] = fb.get(1); + rgb[2] = fb.get(2); + } + + public final void setColorStatic(GL2ES2 gl, float r, float g, float b){ + FloatBuffer fb = (FloatBuffer) rs.getColorStatic().getBuffer(); + fb.put(0, r); + fb.put(1, g); + fb.put(2, b); + if(null != gl && rs.getShaderState().inUse()) { + rs.getShaderState().uniform(gl, rs.getColorStatic()); + } + } + + public final void rotate(GL2ES2 gl, float angle, float x, float y, float z) { + rs.pmvMatrix().glRotatef(angle, x, y, z); + updateMatrix(gl); + } + + public final void translate(GL2ES2 gl, float x, float y, float z) { + rs.pmvMatrix().glTranslatef(x, y, z); + updateMatrix(gl); + } + + public final void scale(GL2ES2 gl, float x, float y, float z) { + rs.pmvMatrix().glScalef(x, y, z); + updateMatrix(gl); + } + + public final void resetModelview(GL2ES2 gl) { + rs.pmvMatrix().glMatrixMode(GLMatrixFunc.GL_MODELVIEW); + rs.pmvMatrix().glLoadIdentity(); + updateMatrix(gl); + } + + public final void updateMatrix(GL2ES2 gl) { + if(initialized && null != gl && rs.getShaderState().inUse()) { + rs.getShaderState().uniform(gl, rs.getPMVMatrix()); + } + } + + /** No PMVMatrix operation is performed here. PMVMatrix will be updated if gl is not null. */ + public final boolean reshapeNotify(GL2ES2 gl, int width, int height) { + this.vp_width = width; + this.vp_height = height; + updateMatrix(gl); + return true; + } - @Override - protected void destroyImpl(GL2ES2 gl) { - // nop + public final boolean reshapePerspective(GL2ES2 gl, float angle, int width, int height, float near, float far) { + this.vp_width = width; + this.vp_height = height; + final float ratio = (float)width/(float)height; + final PMVMatrix p = rs.pmvMatrix(); + p.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + p.glLoadIdentity(); + p.gluPerspective(angle, ratio, near, far); + updateMatrix(gl); + return true; } + public final boolean reshapeOrtho(GL2ES2 gl, int width, int height, float near, float far) { + this.vp_width = width; + this.vp_height = height; + final PMVMatrix p = rs.pmvMatrix(); + p.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + p.glLoadIdentity(); + p.glOrthof(0, width, 0, height, near, far); + updateMatrix(gl); + return true; + } + + protected String getVertexShaderName() { + return "curverenderer" + getImplVersion(); + } + + protected String getFragmentShaderName() { + final String version = getImplVersion(); + final String pass; + if( Region.isVBAA(renderModes) ) { + pass = "-2pass_vbaa"; + } else if( Region.isMSAA(renderModes) ) { + pass = "-2pass_msaa"; + } else { + pass = "-1pass_norm" ; + } + final String weight = Region.isNonUniformWeight(renderModes) ? "-weight" : "" ; + return "curverenderer" + version + pass + weight; + } -} + // FIXME: Really required to have sampler2D def. precision ? If not, we can drop getFragmentShaderPrecision(..) and use default ShaderCode .. + public static final String es2_precision_fp = "\nprecision mediump float;\nprecision mediump int;\nprecision mediump sampler2D;\n"; + + protected String getFragmentShaderPrecision(GL2ES2 gl) { + if( gl.isGLES() ) { + return es2_precision_fp; + } + if( ShaderCode.requiresGL3DefaultPrecision(gl) ) { + return ShaderCode.gl3_default_precision_fp; + } + return null; + } + + protected String getImplVersion() { + return "01"; + } +}
\ No newline at end of file diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java deleted file mode 100644 index 029286601..000000000 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java +++ /dev/null @@ -1,302 +0,0 @@ -/** - * Copyright 2010 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ -package com.jogamp.graph.curve.opengl; - -import java.nio.FloatBuffer; - -import javax.media.opengl.GL2ES2; -import javax.media.opengl.GLException; -import javax.media.opengl.fixedfunc.GLMatrixFunc; - -import com.jogamp.opengl.util.glsl.ShaderCode; -import com.jogamp.opengl.util.glsl.ShaderState; -import com.jogamp.opengl.util.PMVMatrix; - -import com.jogamp.graph.curve.Region; - -public abstract class Renderer { - protected static final boolean DEBUG = Region.DEBUG; - protected static final boolean DEBUG_INSTANCE = Region.DEBUG_INSTANCE; - - public static boolean isWeightValid(float v) { - return 0.0f <= v && v <= 1.9f ; - } - - protected final int renderModes; - protected int vp_width; - protected int vp_height; - protected boolean initialized; - protected final RenderState rs; - private boolean vboSupported = false; - - public final boolean isInitialized() { return initialized; } - - public final int getWidth() { return vp_width; } - public final int getHeight() { return vp_height; } - - public float getWeight() { return rs.getWeight().floatValue(); } - public float getAlpha() { return rs.getAlpha().floatValue(); } - public final PMVMatrix getMatrix() { return rs.pmvMatrix(); } - - /** - * Implementation shall load, compile and link the shader program and leave it active. - * @param gl referencing the current GLContext to which the ShaderState is bound to - * @return - */ - protected abstract boolean initShaderProgram(GL2ES2 gl); - - protected abstract void destroyImpl(GL2ES2 gl); - - /** - * @param rs the used {@link RenderState} - * @param renderModes bit-field of modes - */ - protected Renderer(RenderState rs, int renderModes) { - this.rs = rs; - this.renderModes = renderModes; - } - - public final int getRenderModes() { - return renderModes; - } - - public boolean usesVariableCurveWeight() { return Region.isNonUniformWeight(renderModes); } - - /** - * @return true if Region's renderModes contains all bits as this Renderer's renderModes - * except {@link Region#VARIABLE_CURVE_WEIGHT_BIT}, otherwise false. - */ - public final boolean areRenderModesCompatible(Region region) { - final int cleanRenderModes = getRenderModes() & ( Region.VARIABLE_CURVE_WEIGHT_BIT ); - return cleanRenderModes == ( region.getRenderModes() & cleanRenderModes ); - } - - public final boolean isVBOSupported() { return vboSupported; } - - /** - * Initialize shader and bindings for GPU based rendering bound to the given GL object's GLContext - * if not initialized yet. - * <p>Leaves the renderer enabled, ie ShaderState.</p> - * <p>Shall be called by a {@code draw()} method, e.g. {@link RegionRenderer#draw(GL2ES2, Region, float[], int)}</p> - * - * @param gl referencing the current GLContext to which the ShaderState is bound to - * @throws GLException if initialization failed - */ - public final void init(GL2ES2 gl) throws GLException { - if(initialized){ - return; - } - vboSupported = gl.isFunctionAvailable("glGenBuffers") && - gl.isFunctionAvailable("glBindBuffer") && - gl.isFunctionAvailable("glBufferData") && - gl.isFunctionAvailable("glDrawElements") && - gl.isFunctionAvailable("glVertexAttribPointer") && - gl.isFunctionAvailable("glDeleteBuffers"); - - if(DEBUG) { - System.err.println("TextRendererImpl01: VBO Supported = " + isVBOSupported()); - } - - if(!vboSupported){ - throw new GLException("VBO not supported"); - } - - rs.attachTo(gl); - - gl.glEnable(GL2ES2.GL_BLEND); - gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA); // FIXME: alpha blending stage ? - - initialized = initShaderProgram(gl); - if(!initialized) { - throw new GLException("Shader initialization failed"); - } - - if(!rs.getShaderState().uniform(gl, rs.getPMVMatrix())) { - throw new GLException("Error setting PMVMatrix in shader: "+rs.getShaderState()); - } - - if( Region.isNonUniformWeight( getRenderModes() ) ) { - if(!rs.getShaderState().uniform(gl, rs.getWeight())) { - throw new GLException("Error setting weight in shader: "+rs.getShaderState()); - } - } - - if(!rs.getShaderState().uniform(gl, rs.getAlpha())) { - throw new GLException("Error setting global alpha in shader: "+rs.getShaderState()); - } - - if(!rs.getShaderState().uniform(gl, rs.getColorStatic())) { - throw new GLException("Error setting global color in shader: "+rs.getShaderState()); - } - } - - public final void flushCache(GL2ES2 gl) { - // FIXME: REMOVE ! - } - - public void destroy(GL2ES2 gl) { - if(!initialized){ - if(DEBUG_INSTANCE) { - System.err.println("TextRenderer: Not initialized!"); - } - return; - } - rs.getShaderState().useProgram(gl, false); - destroyImpl(gl); - rs.destroy(gl); - initialized = false; - } - - public final RenderState getRenderState() { return rs; } - public final ShaderState getShaderState() { return rs.getShaderState(); } - - public final void enable(GL2ES2 gl, boolean enable) { - rs.getShaderState().useProgram(gl, enable); - } - - public void setWeight(GL2ES2 gl, float v) { - if( !isWeightValid(v) ) { - throw new IllegalArgumentException("Weight out of range"); - } - rs.getWeight().setData(v); - if(null != gl && rs.getShaderState().inUse() && Region.isNonUniformWeight( getRenderModes() ) ) { - rs.getShaderState().uniform(gl, rs.getWeight()); - } - } - - public void setAlpha(GL2ES2 gl, float alpha_t) { - rs.getAlpha().setData(alpha_t); - if(null != gl && rs.getShaderState().inUse()) { - rs.getShaderState().uniform(gl, rs.getAlpha()); - } - - } - - public void getColorStatic(GL2ES2 gl, float[] rgb) { - FloatBuffer fb = (FloatBuffer) rs.getColorStatic().getBuffer(); - rgb[0] = fb.get(0); - rgb[1] = fb.get(1); - rgb[2] = fb.get(2); - } - - public void setColorStatic(GL2ES2 gl, float r, float g, float b){ - FloatBuffer fb = (FloatBuffer) rs.getColorStatic().getBuffer(); - fb.put(0, r); - fb.put(1, g); - fb.put(2, b); - if(null != gl && rs.getShaderState().inUse()) { - rs.getShaderState().uniform(gl, rs.getColorStatic()); - } - } - - public void rotate(GL2ES2 gl, float angle, float x, float y, float z) { - rs.pmvMatrix().glRotatef(angle, x, y, z); - updateMatrix(gl); - } - - public void translate(GL2ES2 gl, float x, float y, float z) { - rs.pmvMatrix().glTranslatef(x, y, z); - updateMatrix(gl); - } - - public void scale(GL2ES2 gl, float x, float y, float z) { - rs.pmvMatrix().glScalef(x, y, z); - updateMatrix(gl); - } - - public void resetModelview(GL2ES2 gl) { - rs.pmvMatrix().glMatrixMode(GLMatrixFunc.GL_MODELVIEW); - rs.pmvMatrix().glLoadIdentity(); - updateMatrix(gl); - } - - public void updateMatrix(GL2ES2 gl) { - if(initialized && null != gl && rs.getShaderState().inUse()) { - rs.getShaderState().uniform(gl, rs.getPMVMatrix()); - } - } - - /** No PMVMatrix operation is performed here. PMVMatrix will be updated if gl is not null. */ - public boolean reshapeNotify(GL2ES2 gl, int width, int height) { - this.vp_width = width; - this.vp_height = height; - updateMatrix(gl); - return true; - } - - public boolean reshapePerspective(GL2ES2 gl, float angle, int width, int height, float near, float far) { - this.vp_width = width; - this.vp_height = height; - final float ratio = (float)width/(float)height; - final PMVMatrix p = rs.pmvMatrix(); - p.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - p.glLoadIdentity(); - p.gluPerspective(angle, ratio, near, far); - updateMatrix(gl); - return true; - } - - public boolean reshapeOrtho(GL2ES2 gl, int width, int height, float near, float far) { - this.vp_width = width; - this.vp_height = height; - final PMVMatrix p = rs.pmvMatrix(); - p.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - p.glLoadIdentity(); - p.glOrthof(0, width, 0, height, near, far); - updateMatrix(gl); - return true; - } - - protected String getVertexShaderName() { - return "curverenderer" + getImplVersion(); - } - - protected String getFragmentShaderName() { - final String version = getImplVersion(); - final String pass = Region.isVBAA(renderModes) ? "-2pass" : "-1pass" ; - final String weight = Region.isNonUniformWeight(renderModes) ? "-weight" : "" ; - return "curverenderer" + version + pass + weight; - } - - // FIXME: Really required to have sampler2D def. precision ? If not, we can drop getFragmentShaderPrecision(..) and use default ShaderCode .. - public static final String es2_precision_fp = "\nprecision mediump float;\nprecision mediump int;\nprecision mediump sampler2D;\n"; - - protected String getFragmentShaderPrecision(GL2ES2 gl) { - if( gl.isGLES() ) { - return es2_precision_fp; - } - if( ShaderCode.requiresGL3DefaultPrecision(gl) ) { - return ShaderCode.gl3_default_precision_fp; - } - return null; - } - - protected String getImplVersion() { - return "01"; - } -}
\ No newline at end of file diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java new file mode 100644 index 000000000..0721c4726 --- /dev/null +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java @@ -0,0 +1,316 @@ +/** + * Copyright 2014 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.graph.curve.opengl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLException; + +import jogamp.graph.geom.plane.AffineTransform; + +import com.jogamp.graph.curve.OutlineShape; +import com.jogamp.graph.curve.Region; +import com.jogamp.graph.font.Font; +import com.jogamp.graph.font.Font.Glyph; +import com.jogamp.graph.geom.Vertex; +import com.jogamp.graph.geom.Vertex.Factory; + +/** + * Text {@link GLRegion} Utility Class + */ +public class TextRegionUtil { + + public final RegionRenderer renderer; + + public TextRegionUtil(final RegionRenderer renderer) { + this.renderer = renderer; + } + + public static interface ShapeVisitor { + /** + * Visiting the given {@link OutlineShape} with it's corresponding {@link AffineTransform}. + * @param shape may be used as is, otherwise a copy shall be made if intended to be modified. + * @param t may be used immediately as is, otherwise a copy shall be made if stored. + */ + public void visit(final OutlineShape shape, final AffineTransform t); + } + + /** + * Visit each {@link Font.Glyph}'s {@link OutlineShape} with the given {@link ShapeVisitor} + * additionally passing the progressed {@link AffineTransform}. + * The latter reflects the given font metric, pixelSize and hence character position. + * @param visitor + * @param transform + * @param font the target {@link Font} + * @param pixelSize Use {@link Font#getPixelSize(float, float)} for resolution correct pixel-size. + * @param str string text + */ + public static void processString(final ShapeVisitor visitor, final AffineTransform transform, + final Font font, final float pixelSize, final CharSequence str) { + final int charCount = str.length(); + + // region.setFlipped(true); + final Font.Metrics metrics = font.getMetrics(); + final float lineHeight = font.getLineHeight(pixelSize); + + final float scale = metrics.getScale(pixelSize); + final AffineTransform t = new AffineTransform(transform); + + float y = 0; + float advanceTotal = 0; + + for(int i=0; i< charCount; i++) { + final char character = str.charAt(i); + if( '\n' == character ) { + y -= lineHeight; + advanceTotal = 0; + } else if (character == ' ') { + advanceTotal += font.getAdvanceWidth(Glyph.ID_SPACE, pixelSize); + } else { + if(Region.DEBUG_INSTANCE) { + System.err.println("XXXXXXXXXXXXXXx char: "+character+", scale: "+scale+"; translate: "+advanceTotal+", "+y); + } + t.setTransform(transform); // reset transform + t.translate(advanceTotal, y); + t.scale(scale, scale); + + final Font.Glyph glyph = font.getGlyph(character); + final OutlineShape glyphShape = glyph.getShape(); + if( null == glyphShape ) { + continue; + } + visitor.visit(glyphShape, t); + + advanceTotal += glyph.getAdvance(pixelSize, true); + } + } + } + + /** + * Add the string in 3D space w.r.t. the font and pixelSize at the end of the {@link GLRegion}. + * @param region the {@link GLRegion} sink + * @param vertexFactory vertex impl factory {@link Factory} + * @param font the target {@link Font} + * @param pixelSize Use {@link Font#getPixelSize(float, float)} for resolution correct pixel-size. + * @param str string text + */ + public static void addStringToRegion(final GLRegion region, final Factory<? extends Vertex> vertexFactory, + final Font font, final float pixelSize, final CharSequence str) { + final ShapeVisitor visitor = new ShapeVisitor() { + public final void visit(final OutlineShape shape, final AffineTransform t) { + region.addOutlineShape(shape, t); + } }; + processString(visitor, new AffineTransform(vertexFactory), font, pixelSize, str); + } + + /** + * Render the string in 3D space w.r.t. the font and pixelSize + * using a cached {@link GLRegion} for reuse. + * <p> + * Cached {@link GLRegion}s will be destroyed w/ {@link #clear(GL2ES2)} or to free memory. + * </p> + * @param gl the current GL state + * @param font {@link Font} to be used + * @param pixelSize Use {@link Font#getPixelSize(float, float)} for resolution correct pixel-size. + * @param str text to be rendered + * @param sampleCount desired multisampling sample count for msaa-rendering. + * The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched. + * @throws Exception if TextRenderer not initialized + */ + public void drawString3D(final GL2ES2 gl, + final Font font, final float pixelSize, final CharSequence str, + final int[/*1*/] sampleCount) { + if( !renderer.isInitialized() ) { + throw new GLException("TextRendererImpl01: not initialized!"); + } + final int special = 0; + GLRegion region = getCachedRegion(font, str, pixelSize, special); + if(null == region) { + region = GLRegion.create(renderer.getRenderModes()); + addStringToRegion(region, renderer.getRenderState().getVertexFactory(), font, pixelSize, str); + addCachedRegion(gl, font, str, pixelSize, special, region); + } + region.draw(gl, renderer, sampleCount); + } + + /** + * Render the string in 3D space w.r.t. the font and pixelSize + * using a temporary {@link GLRegion}, which will be destroyed afterwards. + * <p> + * In case of a multisampling region renderer, i.e. {@link Region#VBAA_RENDERING_BIT}, recreating the {@link GLRegion} + * is a huge performance impact. + * In such case better use {@link #drawString3D(GLRegion, RegionRenderer, GL2ES2, Font, float, CharSequence, int[])} + * instead. + * </p> + * @param gl the current GL state + * @param font {@link Font} to be used + * @param pixelSize Use {@link Font#getPixelSize(float, float)} for resolution correct pixel-size. + * @param str text to be rendered + * @param sampleCount desired multisampling sample count for msaa-rendering. + * The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched. + * @throws Exception if TextRenderer not initialized + */ + public static void drawString3D(final RegionRenderer renderer, final GL2ES2 gl, + final Font font, final float pixelSize, final CharSequence str, + final int[/*1*/] sampleCount) { + if(!renderer.isInitialized()){ + throw new GLException("TextRendererImpl01: not initialized!"); + } + final GLRegion region = GLRegion.create(renderer.getRenderModes()); + addStringToRegion(region, renderer.getRenderState().getVertexFactory(), font, pixelSize, str); + region.draw(gl, renderer, sampleCount); + region.destroy(gl, renderer); + } + + /** + * Render the string in 3D space w.r.t. the font and pixelSize + * using the given {@link GLRegion}, which will {@link GLRegion#clear(GL2ES2, RegionRenderer) cleared} beforehand. + * @param gl the current GL state + * @param font {@link Font} to be used + * @param pixelSize Use {@link Font#getPixelSize(float, float)} for resolution correct pixel-size. + * @param str text to be rendered + * @param sampleCount desired multisampling sample count for msaa-rendering. + * The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched. + * @throws Exception if TextRenderer not initialized + */ + public static void drawString3D(final GLRegion region, final RegionRenderer renderer, final GL2ES2 gl, + final Font font, final float pixelSize, final CharSequence str, + final int[/*1*/] sampleCount) { + if(!renderer.isInitialized()){ + throw new GLException("TextRendererImpl01: not initialized!"); + } + region.clear(gl, renderer); + addStringToRegion(region, renderer.getRenderState().getVertexFactory(), font, pixelSize, str); + region.draw(gl, renderer, sampleCount); + } + + /** + * Clear all cached {@link GLRegions}. + */ + public void clear(GL2ES2 gl) { + // fluchCache(gl) already called + final Iterator<GLRegion> iterator = stringCacheMap.values().iterator(); + while(iterator.hasNext()){ + final GLRegion region = iterator.next(); + region.destroy(gl, renderer); + } + stringCacheMap.clear(); + stringCacheArray.clear(); + } + + /** + * <p>Sets the cache limit for reusing GlyphString's and their Region. + * Default is {@link #DEFAULT_CACHE_LIMIT}, -1 unlimited, 0 turns cache off, >0 limited </p> + * + * <p>The cache will be validate when the next string rendering happens.</p> + * + * @param newLimit new cache size + * + * @see #DEFAULT_CACHE_LIMIT + */ + public final void setCacheLimit(int newLimit ) { stringCacheLimit = newLimit; } + + /** + * Sets the cache limit, see {@link #setCacheLimit(int)} and validates the cache. + * + * @see #setCacheLimit(int) + * + * @param gl current GL used to remove cached objects if required + * @param newLimit new cache size + */ + public final void setCacheLimit(GL2ES2 gl, int newLimit ) { stringCacheLimit = newLimit; validateCache(gl, 0); } + + /** + * @return the current cache limit + */ + public final int getCacheLimit() { return stringCacheLimit; } + + /** + * @return the current utilized cache size, <= {@link #getCacheLimit()} + */ + public final int getCacheSize() { return stringCacheArray.size(); } + + protected final void validateCache(GL2ES2 gl, int space) { + if ( getCacheLimit() > 0 ) { + while ( getCacheSize() + space > getCacheLimit() ) { + removeCachedRegion(gl, 0); + } + } + } + + protected final GLRegion getCachedRegion(Font font, CharSequence str, float pixelSize, int special) { + return stringCacheMap.get(getKey(font, str, pixelSize, special)); + } + + protected final void addCachedRegion(GL2ES2 gl, Font font, CharSequence str, float pixelSize, int special, GLRegion glyphString) { + if ( 0 != getCacheLimit() ) { + final String key = getKey(font, str, pixelSize, special); + final GLRegion oldRegion = stringCacheMap.put(key, glyphString); + if ( null == oldRegion ) { + // new entry .. + validateCache(gl, 1); + stringCacheArray.add(stringCacheArray.size(), key); + } /// else overwrite is nop .. + } + } + + protected final void removeCachedRegion(GL2ES2 gl, Font font, CharSequence str, int pixelSize, int special) { + final String key = getKey(font, str, pixelSize, special); + final GLRegion region = stringCacheMap.remove(key); + if(null != region) { + region.destroy(gl, renderer); + } + stringCacheArray.remove(key); + } + + protected final void removeCachedRegion(GL2ES2 gl, int idx) { + final String key = stringCacheArray.remove(idx); + if( null != key ) { + final GLRegion region = stringCacheMap.remove(key); + if(null != region) { + region.destroy(gl, renderer); + } + } + } + + protected final String getKey(Font font, CharSequence str, float pixelSize, int special) { + final StringBuilder sb = new StringBuilder(); + return font.getName(sb, Font.NAME_UNIQUNAME) + .append(".").append(str.hashCode()).append(".").append(Float.floatToIntBits(pixelSize)).append(special).toString(); + } + + /** Default cache limit, see {@link #setCacheLimit(int)} */ + public static final int DEFAULT_CACHE_LIMIT = 256; + + private final HashMap<String, GLRegion> stringCacheMap = new HashMap<String, GLRegion>(DEFAULT_CACHE_LIMIT); + private final ArrayList<String> stringCacheArray = new ArrayList<String>(DEFAULT_CACHE_LIMIT); + private int stringCacheLimit = DEFAULT_CACHE_LIMIT; +}
\ No newline at end of file diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java deleted file mode 100644 index f6ce852d8..000000000 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java +++ /dev/null @@ -1,193 +0,0 @@ -/** - * Copyright 2010 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ -package com.jogamp.graph.curve.opengl; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; - -import javax.media.opengl.GL2ES2; - -import jogamp.graph.curve.text.GlyphString; - -import com.jogamp.graph.font.Font; - -public abstract class TextRenderer extends Renderer { - /** - * Create a Hardware accelerated Text Renderer. - * @param rs the used {@link RenderState} - * @param renderModes either {@link com.jogamp.graph.curve.opengl.GLRegion#SINGLE_PASS} or {@link com.jogamp.graph.curve.Region#VBAA_RENDERING_BIT} - */ - public static TextRenderer create(RenderState rs, int renderModes) { - return new jogamp.graph.curve.opengl.TextRendererImpl01(rs, renderModes); - } - - protected TextRenderer(RenderState rs, int type) { - super(rs, type); - } - - - /** Render the String in 3D space wrt to the font provided at the position provided - * the outlines will be generated, if not yet generated - * @param gl the current GL state - * @param font {@link Font} to be used - * @param str text to be rendered - * @param position the lower left corner of the string - * @param fontSize font size - * @param texWidth desired texture width for multipass-rendering. - * The actual used texture-width is written back when mp rendering is enabled, otherwise the store is untouched. - * @throws Exception if TextRenderer not initialized - */ - public abstract void drawString3D(GL2ES2 gl, Font font, - String str, float[] position, int fontSize, int[/*1*/] texSize); - - /**Create the resulting {@link GlyphString} that represents - * the String wrt to the font. - * @param font {@link Font} to be used - * @param size font size - * @param str {@link String} to be created - * @return the resulting GlyphString inclusive the generated region - */ - public GlyphString createString(GL2ES2 gl, Font font, int size, String str) { - if(DEBUG_INSTANCE) { - System.err.println("createString: "+getCacheSize()+"/"+getCacheLimit()+" - "+Font.NAME_UNIQUNAME + " - " + str + " - " + size); - } - final GlyphString glyphString = GlyphString.createString(null, rs.getVertexFactory(), font, size, str); - glyphString.createRegion(gl, renderModes); - return glyphString; - } - - /** FIXME - public void flushCache(GL2ES2 gl) { - Iterator<GlyphString> iterator = stringCacheMap.values().iterator(); - while(iterator.hasNext()){ - GlyphString glyphString = iterator.next(); - glyphString.destroy(gl, rs); - } - stringCacheMap.clear(); - stringCacheArray.clear(); - } */ - - @Override - protected void destroyImpl(GL2ES2 gl) { - // fluchCache(gl) already called - Iterator<GlyphString> iterator = stringCacheMap.values().iterator(); - while(iterator.hasNext()){ - GlyphString glyphString = iterator.next(); - glyphString.destroy(gl, rs); - } - stringCacheMap.clear(); - stringCacheArray.clear(); - } - - /** - * <p>Sets the cache limit for reusing GlyphString's and their Region. - * Default is {@link #DEFAULT_CACHE_LIMIT}, -1 unlimited, 0 turns cache off, >0 limited </p> - * - * <p>The cache will be validate when the next string rendering happens.</p> - * - * @param newLimit new cache size - * - * @see #DEFAULT_CACHE_LIMIT - */ - public final void setCacheLimit(int newLimit ) { stringCacheLimit = newLimit; } - - /** - * Sets the cache limit, see {@link #setCacheLimit(int)} and validates the cache. - * - * @see #setCacheLimit(int) - * - * @param gl current GL used to remove cached objects if required - * @param newLimit new cache size - */ - public final void setCacheLimit(GL2ES2 gl, int newLimit ) { stringCacheLimit = newLimit; validateCache(gl, 0); } - - /** - * @return the current cache limit - */ - public final int getCacheLimit() { return stringCacheLimit; } - - /** - * @return the current utilized cache size, <= {@link #getCacheLimit()} - */ - public final int getCacheSize() { return stringCacheArray.size(); } - - protected final void validateCache(GL2ES2 gl, int space) { - if ( getCacheLimit() > 0 ) { - while ( getCacheSize() + space > getCacheLimit() ) { - removeCachedGlyphString(gl, 0); - } - } - } - - protected final GlyphString getCachedGlyphString(Font font, String str, int fontSize) { - return stringCacheMap.get(getKey(font, str, fontSize)); - } - - protected final void addCachedGlyphString(GL2ES2 gl, Font font, String str, int fontSize, GlyphString glyphString) { - if ( 0 != getCacheLimit() ) { - final String key = getKey(font, str, fontSize); - GlyphString oldGlyphString = stringCacheMap.put(key, glyphString); - if ( null == oldGlyphString ) { - // new entry .. - validateCache(gl, 1); - stringCacheArray.add(stringCacheArray.size(), key); - } /// else overwrite is nop .. - } - } - - protected final void removeCachedGlyphString(GL2ES2 gl, Font font, String str, int fontSize) { - final String key = getKey(font, str, fontSize); - GlyphString glyphString = stringCacheMap.remove(key); - if(null != glyphString) { - glyphString.destroy(gl, rs); - } - stringCacheArray.remove(key); - } - - protected final void removeCachedGlyphString(GL2ES2 gl, int idx) { - final String key = stringCacheArray.remove(idx); - final GlyphString glyphString = stringCacheMap.remove(key); - if(null != glyphString) { - glyphString.destroy(gl, rs); - } - } - - protected final String getKey(Font font, String str, int fontSize) { - final StringBuilder sb = new StringBuilder(); - return font.getName(sb, Font.NAME_UNIQUNAME) - .append(".").append(str.hashCode()).append(".").append(fontSize).toString(); - } - - /** Default cache limit, see {@link #setCacheLimit(int)} */ - public static final int DEFAULT_CACHE_LIMIT = 256; - - private HashMap<String, GlyphString> stringCacheMap = new HashMap<String, GlyphString>(DEFAULT_CACHE_LIMIT); - private ArrayList<String> stringCacheArray = new ArrayList<String>(DEFAULT_CACHE_LIMIT); - private int stringCacheLimit = DEFAULT_CACHE_LIMIT; -}
\ No newline at end of file diff --git a/src/jogl/classes/com/jogamp/graph/curve/tess/Triangulator.java b/src/jogl/classes/com/jogamp/graph/curve/tess/Triangulator.java index 4e8c400e0..96ff4bbb8 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/tess/Triangulator.java +++ b/src/jogl/classes/com/jogamp/graph/curve/tess/Triangulator.java @@ -28,7 +28,7 @@ package com.jogamp.graph.curve.tess; -import java.util.ArrayList; +import java.util.List; import com.jogamp.graph.geom.Outline; import com.jogamp.graph.geom.Triangle; @@ -49,18 +49,20 @@ import com.jogamp.graph.geom.Triangle; */ public interface Triangulator { - /** Add a curve to the list of Outlines + /** + * Add a curve to the list of Outlines * describing the shape + * @param sink list where the generated triangles will be added * @param outline a bounding {@link Outline} + * @param sharpness TODO */ - public void addCurve(Outline outline); + public void addCurve(List<Triangle> sink, Outline outline, float sharpness); /** Generate the triangulation of the provided * List of {@link Outline}s - * @return an arraylist of {@link Triangle}s resembling the - * final shape. + * @param sink list where the generated triangles will be added */ - public ArrayList<Triangle> generate(); + public void generate(List<Triangle> sink); /** Reset the triangulation to initial state * Clearing cached data diff --git a/src/jogl/classes/com/jogamp/graph/font/Font.java b/src/jogl/classes/com/jogamp/graph/font/Font.java index a4a8fd53d..ac7a904e7 100644 --- a/src/jogl/classes/com/jogamp/graph/font/Font.java +++ b/src/jogl/classes/com/jogamp/graph/font/Font.java @@ -27,16 +27,33 @@ */ package com.jogamp.graph.font; +import com.jogamp.graph.curve.OutlineShape; import com.jogamp.opengl.math.geom.AABBox; /** * Interface wrapper for font implementation. - * + * <p> * TrueType Font Specification: - * http://developer.apple.com/fonts/ttrefman/rm06/Chap6.html - * + * <ul> + * <li>http://www.freetype.org/freetype2/documentation.html</li> + * <li>http://developer.apple.com/fonts/ttrefman/rm06/Chap6.html</li> + * <li>http://www.microsoft.com/typography/SpecificationsOverview.mspx</li> + * <li>http://www.microsoft.com/typography/otspec/</li> + * </ul> + * </p> + * <p> * TrueType Font Table Introduction: - * http://scripts.sil.org/cms/scripts/page.php?item_id=IWS-Chapter08 + * <ul> + * <li>http://scripts.sil.org/cms/scripts/page.php?item_id=IWS-Chapter08</li> + * </ul> + * </p> + * <p> + * Misc.: + * <ul> + * <li>Treatis on Font <code>Rasterization https://freddie.witherden.org/pages/font-rasterisation/</code></li> + * <li>Glyph Hell <code>http://walon.org/pub/ttf/ttf_glyphs.htm</code></li> + * </ul> + * </p> */ public interface Font { @@ -67,11 +84,20 @@ public interface Font { float getLineGap(float pixelSize); float getMaxExtend(float pixelSize); float getScale(float pixelSize); - AABBox getBBox(float pixelSize); + /** + * @param pixelSize + * @param tmpV3 caller provided temporary 3-component vector + * @return + */ + AABBox getBBox(float pixelSize, float[] tmpV3); } /** * Glyph for font + * + * http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6cmap.html + * http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6glyf.html + * http://www.microsoft.com/typography/otspec/glyf.htm */ public interface Glyph { // reserved special glyph IDs @@ -82,8 +108,18 @@ public interface Font { public Font getFont(); public char getSymbol(); - public AABBox getBBox(float pixelSize); + public short getID(); + public AABBox getBBox(); + public float getScale(float pixelSize); + /** + * @param pixelSize + * @param tmpV3 caller provided temporary 3-component vector + * @return + */ + public AABBox getBBox(float pixelSize, float[] tmpV3); public float getAdvance(float pixelSize, boolean useFrationalMetrics); + public OutlineShape getShape(); + public int hashCode(); } @@ -97,11 +133,28 @@ public interface Font { public StringBuilder getAllNames(StringBuilder string, String separator); - public float getAdvanceWidth(int i, float pixelSize); + /** + * <pre> + Font Scale Formula: + inch: 25.4 mm + pointSize: [point] = [1/72 inch] + + [1] Scale := pointSize * resolution / ( 72 points per inch * units_per_em ) + [2] PixelSize := pointSize * resolution / ( 72 points per inch ) + [3] Scale := PixelSize / units_per_em + * </pre> + * @param fontSize in point-per-inch + * @param resolution display resolution in dots-per-inch + * @return pixel-per-inch, pixelSize scale factor for font operations. + */ + public float getPixelSize(float fontSize /* points per inch */, float resolution); + + public float getAdvanceWidth(int glyphID, float pixelSize); public Metrics getMetrics(); public Glyph getGlyph(char symbol); public int getNumGlyphs(); + public float getLineHeight(float pixelSize); public float getStringWidth(CharSequence string, float pixelSize); public float getStringHeight(CharSequence string, float pixelSize); public AABBox getStringBounds(CharSequence string, float pixelSize); @@ -111,4 +164,4 @@ public interface Font { /** Shall return {@link #getFullFamilyName()} */ @Override public String toString(); -}
\ No newline at end of file +} diff --git a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java index 884662e6e..948600a4a 100644 --- a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java +++ b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java @@ -29,10 +29,13 @@ package com.jogamp.graph.font; import java.io.File; import java.io.IOException; +import java.net.URI; import java.net.URLConnection; +import com.jogamp.common.util.IOUtil; import com.jogamp.common.util.PropertyAccess; import com.jogamp.common.util.ReflectionUtil; +import com.jogamp.common.util.cache.TempJarCache; import jogamp.graph.font.FontConstructor; import jogamp.graph.font.JavaFontLoader; @@ -50,10 +53,10 @@ public class FontFactory { private static final String FontConstructorPropKey = "jogamp.graph.font.ctor"; private static final String DefaultFontConstructor = "jogamp.graph.font.typecast.TypecastFontConstructor"; - /** Ubuntu is the default font family */ + /** Ubuntu is the default font family, {@value} */ public static final int UBUNTU = 0; - /** Java fonts are optional */ + /** Java fonts are optional, {@value} */ public static final int JAVA = 1; private static final FontConstructor fontConstr; @@ -92,6 +95,24 @@ public class FontFactory { return fontConstr.create(conn); } + public static final Font get(final Class<?> context, final String fname, final boolean useTempJarCache) throws IOException { + URLConnection conn = null; + if( useTempJarCache ) { + try { + final URI uri = TempJarCache.getResource(fname); + conn = null != uri ? uri.toURL().openConnection() : null; + } catch (Exception e) { + throw new IOException(e); + } + } else { + conn = IOUtil.getResource(context, fname); + } + if(null != conn) { + return FontFactory.get ( conn ) ; + } + return null; + } + public static boolean isPrintableChar( char c ) { if( Character.isWhitespace(c) ) { return true; diff --git a/src/jogl/classes/com/jogamp/graph/font/FontSet.java b/src/jogl/classes/com/jogamp/graph/font/FontSet.java index 17b8b2136..60a16b241 100644 --- a/src/jogl/classes/com/jogamp/graph/font/FontSet.java +++ b/src/jogl/classes/com/jogamp/graph/font/FontSet.java @@ -32,30 +32,38 @@ import java.io.IOException; public interface FontSet { - /** Font family REGULAR **/ + /** Font family REGULAR, {@value} **/ public static final int FAMILY_REGULAR = 0; - /** Font family LIGHT **/ + /** Font family LIGHT, {@value} **/ public static final int FAMILY_LIGHT = 1; - /** Font family MEDIUM **/ + /** Font family MEDIUM, {@value} **/ public static final int FAMILY_MEDIUM = 2; - /** Font family CONDENSED **/ + /** Font family CONDENSED, {@value} **/ public static final int FAMILY_CONDENSED = 3; - /** Font family MONO **/ + /** Font family MONO, {@value} **/ public static final int FAMILY_MONOSPACED = 4; - /** SERIF style/family bit flag. Fallback to Sans Serif. */ + /** Zero style, {@value} */ + public static final int STYLE_NONE = 0; + + /** SERIF style/family bit flag. Fallback to Sans Serif, {@value} */ public static final int STYLE_SERIF = 1 << 1; - /** BOLD style bit flag */ + /** BOLD style bit flag, {@value} */ public static final int STYLE_BOLD = 1 << 2; - /** ITALIC style bit flag */ + /** ITALIC style bit flag, {@value} */ public static final int STYLE_ITALIC = 1 << 3; + /** + * Returns the family {@link #FAMILY_REGULAR} with {@link #STYLE_NONE} + * as retrieved with {@link #get(int, int)}. + * @throws IOException + */ Font getDefault() throws IOException ; Font get(int family, int stylebits) throws IOException ; diff --git a/src/jogl/classes/com/jogamp/graph/geom/Outline.java b/src/jogl/classes/com/jogamp/graph/geom/Outline.java index 77a318078..2d9d74966 100644 --- a/src/jogl/classes/com/jogamp/graph/geom/Outline.java +++ b/src/jogl/classes/com/jogamp/graph/geom/Outline.java @@ -62,7 +62,8 @@ public class Outline implements Cloneable, Comparable<Outline> { return vertices.size(); } - /** Appends a vertex to the outline loop/strip. + /** + * Appends a vertex to the outline loop/strip. * @param vertex Vertex to be added * @throws NullPointerException if the {@link Vertex} element is null */ @@ -70,7 +71,8 @@ public class Outline implements Cloneable, Comparable<Outline> { addVertex(vertices.size(), vertex); } - /** Insert the {@link Vertex} element at the given {@code position} to the outline loop/strip. + /** + * Insert the {@link Vertex} element at the given {@code position} to the outline loop/strip. * @param position of the added Vertex * @param vertex Vertex object to be added * @throws NullPointerException if the {@link Vertex} element is null @@ -151,22 +153,32 @@ public class Outline implements Cloneable, Comparable<Outline> { return closed; } - /** define if this outline is closed or not. - * if set to closed, checks if the last vertex is - * equal to the first vertex. If not Equal adds a - * vertex at the end to the list. - * @param closed + /** + * Ensure this outline is closed. + * <p> + * Checks whether the last vertex equals to the first. + * If not equal, it either appends a clone of the first vertex + * or prepends a clone of the last vertex, depending on <code>closeTail</code>. + * </p> + * @param closeTail if true, a clone of the first vertex will be appended, + * otherwise a clone of the last vertex will be prepended. + * @return true if closing performed, otherwise false for NOP */ - public final void setClosed(boolean closed) { - this.closed = closed; - if( closed && !isEmpty() ) { - Vertex first = vertices.get(0); - Vertex last = getLastVertex(); - if(!VectorUtil.checkEquality(first.getCoord(), last.getCoord())){ - Vertex v = first.clone(); - vertices.add(v); + public final boolean setClosed(boolean closeTail) { + this.closed = true; + if( !isEmpty() ) { + final Vertex first = vertices.get(0); + final Vertex last = getLastVertex(); + if( !VectorUtil.checkEquality( first.getCoord(), last.getCoord() ) ) { + if( closeTail ) { + vertices.add(first.clone()); + } else { + vertices.add(0, last.clone()); + } + return true; } } + return false; } /** Compare two outlines with Bounding Box area diff --git a/src/jogl/classes/com/jogamp/graph/geom/opengl/SVertex.java b/src/jogl/classes/com/jogamp/graph/geom/SVertex.java index b27604a44..99f10a694 100644 --- a/src/jogl/classes/com/jogamp/graph/geom/opengl/SVertex.java +++ b/src/jogl/classes/com/jogamp/graph/geom/SVertex.java @@ -25,9 +25,8 @@ * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ -package com.jogamp.graph.geom.opengl; +package com.jogamp.graph.geom; -import com.jogamp.graph.geom.Vertex; import com.jogamp.opengl.math.VectorUtil; /** A Simple Vertex Implementation. Where the coordinates, and other attributes are @@ -35,10 +34,10 @@ import com.jogamp.opengl.math.VectorUtil; * */ public class SVertex implements Vertex { - private int id = Integer.MAX_VALUE; - protected float[] coord = new float[3]; + private int id; + protected final float[] coord = new float[3]; protected boolean onCurve; - private float[] texCoord = new float[2]; + private final float[] texCoord = new float[2]; static final Factory factory = new Factory(); @@ -50,8 +49,17 @@ public class SVertex implements Vertex { return new SVertex(); } + public SVertex create(final Vertex src) { + return new SVertex(src); + } + + @Override + public SVertex create(final int id, final boolean onCurve, final float[] texCoordsBuffer) { + return new SVertex(id, onCurve, texCoordsBuffer); + } + @Override - public SVertex create(float x, float y, float z, boolean onCurve) { + public SVertex create(final float x, final float y, final float z, final boolean onCurve) { return new SVertex(x, y, z, onCurve); } @@ -62,30 +70,46 @@ public class SVertex implements Vertex { } public SVertex() { + this.id = Integer.MAX_VALUE; + } + + public SVertex(final Vertex src) { + this.id = src.getId(); + System.arraycopy(src.getCoord(), 0, coord, 0, 3); + setOnCurve(src.isOnCurve()); + System.arraycopy(src.getTexCoord(), 0, texCoord, 0, 2); } - public SVertex(float x, float y, float z, boolean onCurve) { + public SVertex(final int id, final boolean onCurve, final float[] texCoordsBuffer) { + this.id = id; + this.onCurve = onCurve; + System.arraycopy(texCoordsBuffer, 0, texCoord, 0, 2); + } + + public SVertex(final float x, final float y, final float z, final boolean onCurve) { + this.id = Integer.MAX_VALUE; setCoord(x, y, z); setOnCurve(onCurve); } - public SVertex(float[] coordsBuffer, int offset, int length, boolean onCurve) { + public SVertex(final float[] coordsBuffer, final int offset, final int length, final boolean onCurve) { + this.id = Integer.MAX_VALUE; setCoord(coordsBuffer, offset, length); setOnCurve(onCurve); } - public SVertex(float[] coordsBuffer, int offset, int length, - float[] texCoordsBuffer, int offsetTC, int lengthTC, boolean onCurve) { - setCoord(coordsBuffer, offset, length); - setTexCoord(texCoordsBuffer, offsetTC, lengthTC); + public SVertex(float[] coordsBuffer, float[] texCoordsBuffer, boolean onCurve) { + this.id = Integer.MAX_VALUE; + System.arraycopy(coordsBuffer, 0, coord, 0, 3); + System.arraycopy(texCoordsBuffer, 0, texCoord, 0, 2); setOnCurve(onCurve); } @Override public final void setCoord(float x, float y, float z) { - this.coord[0] = x; - this.coord[1] = y; - this.coord[2] = z; + coord[0] = x; + coord[1] = y; + coord[2] = z; } @Override @@ -175,8 +199,8 @@ public class SVertex implements Vertex { @Override public final void setTexCoord(float s, float t) { - this.texCoord[0] = s; - this.texCoord[1] = t; + texCoord[0] = s; + texCoord[1] = t; } @Override @@ -185,11 +209,11 @@ public class SVertex implements Vertex { } /** - * @return deep clone of this Vertex, but keeping the id blank + * @return deep clone of this Vertex elements */ @Override public SVertex clone(){ - return new SVertex(this.coord, 0, 3, this.texCoord, 0, 2, this.onCurve); + return new SVertex(this.coord, this.texCoord, this.onCurve); } @Override diff --git a/src/jogl/classes/com/jogamp/graph/geom/Triangle.java b/src/jogl/classes/com/jogamp/graph/geom/Triangle.java index a01cd834f..e353dd061 100644 --- a/src/jogl/classes/com/jogamp/graph/geom/Triangle.java +++ b/src/jogl/classes/com/jogamp/graph/geom/Triangle.java @@ -27,14 +27,52 @@ */ package com.jogamp.graph.geom; +import com.jogamp.graph.curve.Region; + +import jogamp.graph.geom.plane.AffineTransform; + public class Triangle { - private int id = Integer.MAX_VALUE; - final private Vertex[] vertices; - private boolean[] boundaryEdges = new boolean[3]; + private final Vertex[] vertices = new Vertex[3]; + private final boolean[] boundaryEdges = new boolean[3]; private boolean[] boundaryVertices = null; + private int id; + + public Triangle(Vertex v1, Vertex v2, Vertex v3) { + id = Integer.MAX_VALUE; + vertices[0] = v1; + vertices[1] = v2; + vertices[2] = v3; + } + + public Triangle(Triangle src) { + id = src.id; + vertices[0] = src.vertices[0].clone(); + vertices[1] = src.vertices[1].clone(); + vertices[2] = src.vertices[2].clone(); + System.arraycopy(src.boundaryEdges, 0, boundaryEdges, 0, 3); + boundaryVertices = src.boundaryVertices; + } - public Triangle(Vertex ... v123){ - vertices = v123; + private Triangle(final int id, final boolean[] boundaryEdges, final boolean[] boundaryVertices){ + this.id = id; + System.arraycopy(boundaryEdges, 0, this.boundaryEdges, 0, 3); + this.boundaryVertices = boundaryVertices; + /** + if( null != boundaryVertices ) { + this.boundaryVertices = new boolean[3]; + System.arraycopy(boundaryVertices, 0, this.boundaryVertices, 0, 3); + } */ + } + + /** + * Returns a transformed a clone of this instance using the given AffineTransform. + */ + public Triangle transform(AffineTransform t) { + final Triangle tri = new Triangle(id, boundaryEdges, boundaryVertices); + tri.vertices[0] = t.transform(vertices[0], null); + tri.vertices[1] = t.transform(vertices[1], null); + tri.vertices[2] = t.transform(vertices[2], null); + return tri; } public int getId() { @@ -45,6 +83,7 @@ public class Triangle { this.id = id; } + /** Returns array of 3 vertices, denominating the triangle. */ public Vertex[] getVertices() { return vertices; } @@ -57,10 +96,6 @@ public class Triangle { return boundaryVertices[0] || boundaryVertices[1] || boundaryVertices[2]; } - public void setEdgesBoundary(boolean[] boundary) { - this.boundaryEdges = boundary; - } - public boolean[] getEdgeBoundary() { return boundaryEdges; } @@ -75,6 +110,6 @@ public class Triangle { @Override public String toString() { - return "Tri ID: " + id + "\n" + vertices[0] + "\n" + vertices[1] + "\n" + vertices[2]; + return "Tri ID: " + id + "\n\t" + vertices[0] + "\n\t" + vertices[1] + "\n\t" + vertices[2]; } } diff --git a/src/jogl/classes/com/jogamp/graph/geom/Vertex.java b/src/jogl/classes/com/jogamp/graph/geom/Vertex.java index 994253f71..fc9590ae7 100644 --- a/src/jogl/classes/com/jogamp/graph/geom/Vertex.java +++ b/src/jogl/classes/com/jogamp/graph/geom/Vertex.java @@ -37,6 +37,10 @@ public interface Vertex extends Vert3fImmutable, Cloneable { public static interface Factory <T extends Vertex> { T create(); + T create(Vertex src); + + T create(int id, boolean onCurve, float[] texCoordsBuffer); + T create(float x, float y, float z, boolean onCurve); T create(float[] coordsBuffer, int offset, int length, boolean onCurve); diff --git a/src/jogl/classes/com/jogamp/opengl/FBObject.java b/src/jogl/classes/com/jogamp/opengl/FBObject.java index 90a8dc073..33751eab9 100644 --- a/src/jogl/classes/com/jogamp/opengl/FBObject.java +++ b/src/jogl/classes/com/jogamp/opengl/FBObject.java @@ -2316,8 +2316,8 @@ public class FBObject { return "FBO[name r/w "+fbName+"/"+getReadFramebuffer()+", init "+initialized+", bound "+bound+", size "+width+"x"+height+ ", samples "+samples+"/"+maxSamples+", depth "+depth+", stencil "+stencil+ ", color attachments: "+colorAttachmentCount+"/"+maxColorAttachments+ - ": "+caps+", msaa-sink "+samplingSinkTexture+", hasSamplesSink "+(null != samplingSink)+ - ", state "+getStatusString()+", obj "+toHexString(objectHashCode())+"]"; + ": "+caps+", msaa["+samplingSinkTexture+", hasSink "+(null != samplingSink)+ + ", dirty "+samplingSinkDirty+"], state "+getStatusString()+", obj "+toHexString(objectHashCode())+"]"; } private final void updateStatus(GL gl) { diff --git a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java index 191a83241..d2976357d 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java @@ -29,23 +29,39 @@ package com.jogamp.opengl.math; import java.nio.FloatBuffer; +import jogamp.opengl.Debug; + import com.jogamp.common.os.Platform; /** * Basic Float math utility functions. * <p> * Implementation assumes linear matrix layout in column-major order - * matching OpenGL's implementation. + * matching OpenGL's implementation, translation matrix example: + * <pre> + Row-Major Order: + 1 0 0 x + 0 1 0 y + 0 0 1 z + 0 0 0 1 + * </pre> + * <pre> + Column-Major Order: + 1 0 0 0 + 0 1 0 0 + 0 0 1 0 + x y z 1 + * </pre> * </p> * <p> * Derived from ProjectFloat.java - Created 11-jan-2004 * </p> * - * @author Erik Duijs - * @author Kenneth Russell - * @author Sven Gothel + * @author Erik Duijs, Kenneth Russell, et al. */ public class FloatUtil { + public static final boolean DEBUG = Debug.debug("Math"); + private static final float[] IDENTITY_MATRIX = new float[] { 1.0f, 0.0f, 0.0f, 0.0f, @@ -558,7 +574,7 @@ public class FloatUtil { public static final float PI = 3.14159265358979323846f; - public static float abs(float a) { return (float) java.lang.Math.abs(a); } + public static float abs(float a) { return java.lang.Math.abs(a); } public static float pow(float a, float b) { return (float) java.lang.Math.pow(a, b); } diff --git a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java index 52a59c599..3c3510b7f 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java @@ -56,7 +56,8 @@ public class Quaternion { */ public Quaternion(float[] vector1, float[] vector2) { final float theta = FloatUtil.acos(VectorUtil.dot(vector1, vector2)); - final float[] cross = VectorUtil.cross(vector1, vector2); + final float[] cross = new float[3]; + VectorUtil.cross(cross, vector1, vector2); fromAxis(cross, theta); } @@ -79,7 +80,7 @@ public class Quaternion { public void fromAxis(float[] vector, float angle) { final float halfangle = angle * 0.5f; final float sin = FloatUtil.sin(halfangle); - final float[] nv = VectorUtil.normalize(vector); + final float[] nv = VectorUtil.normalize(vector, vector); x = (nv[0] * sin); y = (nv[1] * sin); z = (nv[2] * sin); @@ -220,7 +221,7 @@ public class Quaternion { * Normalize a quaternion required if to be used as a rotational quaternion */ public void normalize() { - final float norme = (float) FloatUtil.sqrt(w * w + x * x + y * y + z * z); + final float norme = FloatUtil.sqrt(w * w + x * x + y * y + z * z); if (norme == 0.0f) { setIdentity(); } else { @@ -355,7 +356,7 @@ public class Quaternion { public void setFromMatrix(float[] m) { final float T = m[0] + m[4] + m[8] + 1; if (T > 0) { - final float S = 0.5f / (float) FloatUtil.sqrt(T); + final float S = 0.5f / FloatUtil.sqrt(T); w = 0.25f / S; x = (m[5] - m[7]) * S; y = (m[6] - m[2]) * S; diff --git a/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java b/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java index e1e797088..734b7459b 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java @@ -52,41 +52,26 @@ public class VectorUtil { { return (vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2]); } - /** Normalize a vector + + /** + * Normalize a vector * @param vector input vector * @return normalized vector */ - public static float[] normalize(float[] vector) + public static float[] normalize(final float[] result, float[] vector) { - final float[] newVector = new float[3]; - final float d = FloatUtil.sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]); if(d> 0.0f) { - newVector[0] = vector[0]/d; - newVector[1] = vector[1]/d; - newVector[2] = vector[2]/d; + result[0] = vector[0]/d; + result[1] = vector[1]/d; + result[2] = vector[2]/d; } - return newVector; - } - - /** Scales a vector by param creating a new float[] for the result! - * @param vector input vector - * @param scale constant to scale by - * @return new scaled vector - * @deprecated Use {@link #scale(float[], float[], float)} - */ - public static float[] scale(float[] vector, float scale) - { - final float[] newVector = new float[3]; - - newVector[0] = vector[0] * scale; - newVector[1] = vector[1] * scale; - newVector[2] = vector[2] * scale; - return newVector; + return result; } - /** Scales a vector by param using given result float[] + /** + * Scales a vector by param using given result float[] * @param result vector for the result * @param vector input vector * @param scale single scale constant for all vector components @@ -113,67 +98,61 @@ public class VectorUtil { return result; } - /** Adds to vectors + /** + * Adds to vectors * @param v1 vector 1 * @param v2 vector 2 * @return v1 + v2 */ - public static float[] vectorAdd(float[] v1, float[] v2) + public static float[] vectorAdd(float[] result, float[] v1, float[] v2) { - final float[] newVector = new float[3]; - - newVector[0] = v1[0] + v2[0]; - newVector[1] = v1[1] + v2[1]; - newVector[2] = v1[2] + v2[2]; - return newVector; + result[0] = v1[0] + v2[0]; + result[1] = v1[1] + v2[1]; + result[2] = v1[2] + v2[2]; + return result; } - /** cross product vec1 x vec2 + /** + * cross product vec1 x vec2 * @param vec1 vector 1 * @param vec2 vecttor 2 * @return the resulting vector */ - public static float[] cross(float[] vec1, float[] vec2) + public static float[] cross(final float[] result, float[] vec1, float[] vec2) { - final float[] out = new float[3]; - - out[0] = vec2[2]*vec1[1] - vec2[1]*vec1[2]; - out[1] = vec2[0]*vec1[2] - vec2[2]*vec1[0]; - out[2] = vec2[1]*vec1[0] - vec2[0]*vec1[1]; + result[0] = vec2[2]*vec1[1] - vec2[1]*vec1[2]; + result[1] = vec2[0]*vec1[2] - vec2[2]*vec1[0]; + result[2] = vec2[1]*vec1[0] - vec2[0]*vec1[1]; - return out; + return result; } /** Column Matrix Vector multiplication * @param colMatrix column matrix (4x4) * @param vec vector(x,y,z) - * @return result new float[3] + * @return result */ - public static float[] colMatrixVectorMult(float[] colMatrix, float[] vec) + public static float[] colMatrixVectorMult(final float[] result, float[] colMatrix, float[] vec) { - final float[] out = new float[3]; - - out[0] = vec[0]*colMatrix[0] + vec[1]*colMatrix[4] + vec[2]*colMatrix[8] + colMatrix[12]; - out[1] = vec[0]*colMatrix[1] + vec[1]*colMatrix[5] + vec[2]*colMatrix[9] + colMatrix[13]; - out[2] = vec[0]*colMatrix[2] + vec[1]*colMatrix[6] + vec[2]*colMatrix[10] + colMatrix[14]; + result[0] = vec[0]*colMatrix[0] + vec[1]*colMatrix[4] + vec[2]*colMatrix[8] + colMatrix[12]; + result[1] = vec[0]*colMatrix[1] + vec[1]*colMatrix[5] + vec[2]*colMatrix[9] + colMatrix[13]; + result[2] = vec[0]*colMatrix[2] + vec[1]*colMatrix[6] + vec[2]*colMatrix[10] + colMatrix[14]; - return out; + return result; } /** Matrix Vector multiplication * @param rawMatrix column matrix (4x4) * @param vec vector(x,y,z) - * @return result new float[3] + * @return result */ - public static float[] rowMatrixVectorMult(float[] rawMatrix, float[] vec) + public static float[] rowMatrixVectorMult(final float[] result, float[] rawMatrix, float[] vec) { - final float[] out = new float[3]; - - out[0] = vec[0]*rawMatrix[0] + vec[1]*rawMatrix[1] + vec[2]*rawMatrix[2] + rawMatrix[3]; - out[1] = vec[0]*rawMatrix[4] + vec[1]*rawMatrix[5] + vec[2]*rawMatrix[6] + rawMatrix[7]; - out[2] = vec[0]*rawMatrix[8] + vec[1]*rawMatrix[9] + vec[2]*rawMatrix[10] + rawMatrix[11]; + result[0] = vec[0]*rawMatrix[0] + vec[1]*rawMatrix[1] + vec[2]*rawMatrix[2] + rawMatrix[3]; + result[1] = vec[0]*rawMatrix[4] + vec[1]*rawMatrix[5] + vec[2]*rawMatrix[6] + rawMatrix[7]; + result[2] = vec[0]*rawMatrix[8] + vec[1]*rawMatrix[9] + vec[2]*rawMatrix[10] + rawMatrix[11]; - return out; + return result; } /** Calculate the midpoint of two values @@ -186,19 +165,19 @@ public class VectorUtil { return (p1+p2)/2.0f; } - /** Calculate the midpoint of two points + /** + * Calculate the midpoint of two points * @param p1 first point * @param p2 second point * @return midpoint */ - public static float[] mid(float[] p1, float[] p2) + public static float[] mid(final float[] result, float[] p1, float[] p2) { - final float[] midPoint = new float[3]; - midPoint[0] = (p1[0] + p2[0])*0.5f; - midPoint[1] = (p1[1] + p2[1])*0.5f; - midPoint[2] = (p1[2] + p2[2])*0.5f; + result[0] = (p1[0] + p2[0])*0.5f; + result[1] = (p1[1] + p2[1])*0.5f; + result[2] = (p1[2] + p2[2])*0.5f; - return midPoint; + return result; } /** Compute the norm of a vector @@ -271,17 +250,14 @@ public class VectorUtil { } /** Compute Vector + * @param vector storage for resulting Vector V1V2 * @param v1 vertex 1 * @param v2 vertex2 2 - * @return Vector V1V2 */ - public static float[] computeVector(float[] v1, float[] v2) - { - final float[] vector = new float[3]; + public static void computeVector(float[] vector, float[] v1, float[] v2) { vector[0] = v2[0] - v1[0]; vector[1] = v2[1] - v1[1]; vector[2] = v2[2] - v1[2]; - return vector; } /** Check if vertices in triangle circumcircle @@ -292,11 +268,15 @@ public class VectorUtil { * @return true if the vertex d is inside the circle defined by the * vertices a, b, c. from paper by Guibas and Stolfi (1985). */ - public static boolean inCircle(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d){ - return (a.getX() * a.getX() + a.getY() * a.getY()) * triArea(b, c, d) - - (b.getX() * b.getX() + b.getY() * b.getY()) * triArea(a, c, d) + - (c.getX() * c.getX() + c.getY() * c.getY()) * triArea(a, b, d) - - (d.getX() * d.getX() + d.getY() * d.getY()) * triArea(a, b, c) > 0; + public static boolean inCircle(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { + final float[] A = a.getCoord(); + final float[] B = b.getCoord(); + final float[] C = c.getCoord(); + final float[] D = d.getCoord(); + return (A[0] * A[0] + A[1] * A[1]) * triArea(B, C, D) - + (B[0] * B[0] + B[1] * B[1]) * triArea(A, C, D) + + (C[0] * C[0] + C[1] * C[1]) * triArea(A, B, D) - + (D[0] * D[0] + D[1] * D[1]) * triArea(A, B, C) > 0; } /** Computes oriented area of a triangle @@ -306,8 +286,22 @@ public class VectorUtil { * @return compute twice the area of the oriented triangle (a,b,c), the area * is positive if the triangle is oriented counterclockwise. */ - public static float triArea(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c) { - return (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY())*(c.getX() - a.getX()); + public static float triArea(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c){ + final float[] A = a.getCoord(); + final float[] B = b.getCoord(); + final float[] C = c.getCoord(); + return (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0]); + } + + /** Computes oriented area of a triangle + * @param A first vertex + * @param B second vertex + * @param C third vertex + * @return compute twice the area of the oriented triangle (a,b,c), the area + * is positive if the triangle is oriented counterclockwise. + */ + public static float triArea(float[] A, float[] B, float[] C){ + return (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1])*(C[0] - A[0]); } /** Check if a vertex is in triangle using @@ -318,11 +312,13 @@ public class VectorUtil { * @param p the vertex in question * @return true if p is in triangle (a, b, c), false otherwise. */ - public static boolean vertexInTriangle(float[] a, float[] b, float[] c, float[] p){ + public static boolean vertexInTriangle(float[] a, float[] b, float[] c, + float[] p, + float[] ac, float[] ab, float[] ap){ // Compute vectors - final float[] ac = computeVector(a, c); //v0 - final float[] ab = computeVector(a, b); //v1 - final float[] ap = computeVector(a, p); //v2 + computeVector(ac, a, c); //v0 + computeVector(ab, a, b); //v1 + computeVector(ap, a, p); //v2 // Compute dot products final float dot00 = dot(ac, ac); @@ -340,6 +336,76 @@ public class VectorUtil { return (u >= 0) && (v >= 0) && (u + v < 1); } + /** + * Check if one of three vertices are in triangle using + * barycentric coordinates computation. + * @param a first triangle vertex + * @param b second triangle vertex + * @param c third triangle vertex + * @param p1 the vertex in question + * @param p2 the vertex in question + * @param p3 the vertex in question + * @param tmpAC + * @param tmpAB + * @param tmpAP + * @return true if p1 or p2 or p3 is in triangle (a, b, c), false otherwise. + */ + public static boolean vertexInTriangle3(float[] a, float[] b, float[] c, + float[] p1, float[] p2, float[] p3, + float[] tmpAC, float[] tmpAB, float[] tmpAP){ + // Compute vectors + computeVector(tmpAC, a, c); //v0 + computeVector(tmpAB, a, b); //v1 + + // Compute dot products + final float dotAC_AC = dot(tmpAC, tmpAC); + final float dotAC_AB = dot(tmpAC, tmpAB); + final float dotAB_AB = dot(tmpAB, tmpAB); + + // Compute barycentric coordinates + final float invDenom = 1 / (dotAC_AC * dotAB_AB - dotAC_AB * dotAC_AB); + { + computeVector(tmpAP, a, p1); //v2 + final float dotAC_AP1 = dot(tmpAC, tmpAP); + final float dotAB_AP1 = dot(tmpAB, tmpAP); + final float u1 = (dotAB_AB * dotAC_AP1 - dotAC_AB * dotAB_AP1) * invDenom; + final float v1 = (dotAC_AC * dotAB_AP1 - dotAC_AB * dotAC_AP1) * invDenom; + + // Check if point is in triangle + if ( (u1 >= 0) && (v1 >= 0) && (u1 + v1 < 1) ) { + return true; + } + } + + { + computeVector(tmpAP, a, p2); //v2 + final float dotAC_AP2 = dot(tmpAC, tmpAP); + final float dotAB_AP2 = dot(tmpAB, tmpAP); + final float u = (dotAB_AB * dotAC_AP2 - dotAC_AB * dotAB_AP2) * invDenom; + final float v = (dotAC_AC * dotAB_AP2 - dotAC_AB * dotAC_AP2) * invDenom; + + // Check if point is in triangle + if ( (u >= 0) && (v >= 0) && (u + v < 1) ) { + return true; + } + } + + { + computeVector(tmpAP, a, p3); //v2 + final float dotAC_AP3 = dot(tmpAC, tmpAP); + final float dotAB_AP3 = dot(tmpAB, tmpAP); + final float u = (dotAB_AB * dotAC_AP3 - dotAC_AB * dotAB_AP3) * invDenom; + final float v = (dotAC_AC * dotAB_AP3 - dotAC_AB * dotAC_AP3) * invDenom; + + // Check if point is in triangle + if ( (u >= 0) && (v >= 0) && (u + v < 1) ) { + return true; + } + } + + return false; + } + /** Check if points are in ccw order * @param a first vertex * @param b second vertex @@ -390,10 +456,9 @@ public class VectorUtil { * @param b vertex 2 of first segment * @param c vertex 1 of second segment * @param d vertex 2 of second segment - * @return the intersection coordinates if the segments intersect, otherwise - * returns null + * @return the intersection coordinates if the segments intersect, otherwise returns null */ - public static float[] seg2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { + public static float[] seg2SegIntersection(final float[] result, Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { final float determinant = (a.getX()-b.getX())*(c.getY()-d.getY()) - (a.getY()-b.getY())*(c.getX()-d.getX()); if (determinant == 0) @@ -409,7 +474,42 @@ public class VectorUtil { if(gamma <= 0 || gamma >= 1) return null; if(gamma1 <= 0 || gamma1 >= 1) return null; - return new float[]{xi,yi,0}; + result[0] = xi; + result[1] = yi; + result[2] = 0; + return result; + } + + /** Compute intersection between two segments + * @param a vertex 1 of first segment + * @param b vertex 2 of first segment + * @param c vertex 1 of second segment + * @param d vertex 2 of second segment + * @return true if the segments intersect, otherwise returns false + */ + public static boolean testSeg2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { + final float[] A = a.getCoord(); + final float[] B = b.getCoord(); + final float[] C = c.getCoord(); + final float[] D = d.getCoord(); + + final float determinant = (A[0]-B[0])*(C[1]-D[1]) - (A[1]-B[1])*(C[0]-D[0]); + + if (determinant == 0) { + return false; + } + + final float alpha = (A[0]*B[1]-A[1]*B[0]); + final float beta = (C[0]*D[1]-C[1]*D[1]); + final float xi = ((C[0]-D[0])*alpha-(A[0]-B[0])*beta)/determinant; + + final float gamma = (xi - A[0])/(B[0] - A[0]); + final float gamma1 = (xi - C[0])/(D[0] - C[0]); + if(gamma <= 0 || gamma >= 1 || gamma1 <= 0 || gamma1 >= 1) { + return false; + } + + return true; } /** Compute intersection between two lines @@ -420,7 +520,7 @@ public class VectorUtil { * @return the intersection coordinates if the lines intersect, otherwise * returns null */ - public static float[] line2lineIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { + public static float[] line2lineIntersection(final float[] result, Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { final float determinant = (a.getX()-b.getX())*(c.getY()-d.getY()) - (a.getY()-b.getY())*(c.getX()-d.getX()); if (determinant == 0) @@ -431,7 +531,10 @@ public class VectorUtil { final float xi = ((c.getX()-d.getX())*alpha-(a.getX()-b.getX())*beta)/determinant; final float yi = ((c.getY()-d.getY())*alpha-(a.getY()-b.getY())*beta)/determinant; - return new float[]{xi,yi,0}; + result[0] = xi; + result[1] = yi; + result[2] = 0; + return result; } /** Check if a segment intersects with a triangle @@ -442,14 +545,9 @@ public class VectorUtil { * @param e vertex 2 of first segment * @return true if the segment intersects at least one segment of the triangle, false otherwise */ - public static boolean tri2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d, Vert2fImmutable e){ - if(seg2SegIntersection(a, b, d, e) != null) - return true; - if(seg2SegIntersection(b, c, d, e) != null) - return true; - if(seg2SegIntersection(a, c, d, e) != null) - return true; - - return false; + public static boolean testTri2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d, Vert2fImmutable e){ + return testSeg2SegIntersection(a, b, d, e) || + testSeg2SegIntersection(b, c, d, e) || + testSeg2SegIntersection(a, c, d, e) ; } } diff --git a/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java b/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java index d48677da5..c28b36f82 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java +++ b/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java @@ -27,7 +27,9 @@ */ package com.jogamp.opengl.math.geom; +import com.jogamp.opengl.math.FloatUtil; import com.jogamp.opengl.math.VectorUtil; +import com.jogamp.opengl.util.PMVMatrix; /** @@ -37,9 +39,10 @@ import com.jogamp.opengl.math.VectorUtil; * */ public class AABBox implements Cloneable { - private float[] low = new float[3]; - private float[] high = new float[3]; - private float[] center = new float[3]; + private static final boolean DEBUG = FloatUtil.DEBUG; + private final float[] low = new float[3]; + private final float[] high = new float[3]; + private final float[] center = new float[3]; /** Create a Axis Aligned bounding box (AABBox) * where the low and and high MAX float Values. @@ -167,20 +170,26 @@ public class AABBox implements Cloneable { */ public final void resize(float x, float y, float z) { /** test low */ - if (x < low[0]) + if (x < low[0]) { low[0] = x; - if (y < low[1]) + } + if (y < low[1]) { low[1] = y; - if (z < low[2]) + } + if (z < low[2]) { low[2] = z; + } /** test high */ - if (x > high[0]) + if (x > high[0]) { high[0] = x; - if (y > high[1]) + } + if (y > high[1]) { high[1] = y; - if (z > high[2]) + } + if (z > high[2]) { high[2] = z; + } computeCenter(); } @@ -275,26 +284,25 @@ public class AABBox implements Cloneable { return center; } - /** Scale the AABBox by a constant + /** + * Scale the AABBox by a constant * @param size a constant float value + * @param tmpV3 caller provided temporary 3-component vector */ - public final void scale(float size) { - float[] diffH = new float[3]; - diffH[0] = high[0] - center[0]; - diffH[1] = high[1] - center[1]; - diffH[2] = high[2] - center[2]; + public final void scale(float size, float[] tmpV3) { + tmpV3[0] = high[0] - center[0]; + tmpV3[1] = high[1] - center[1]; + tmpV3[2] = high[2] - center[2]; - diffH = VectorUtil.scale(diffH, size); + VectorUtil.scale(tmpV3, tmpV3, size); // in-place scale + VectorUtil.vectorAdd(high, center, tmpV3); - float[] diffL = new float[3]; - diffL[0] = low[0] - center[0]; - diffL[1] = low[1] - center[1]; - diffL[2] = low[2] - center[2]; + tmpV3[0] = low[0] - center[0]; + tmpV3[1] = low[1] - center[1]; + tmpV3[2] = low[2] - center[2]; - diffL = VectorUtil.scale(diffL, size); - - high = VectorUtil.vectorAdd(center, diffH); - low = VectorUtil.vectorAdd(center, diffL); + VectorUtil.scale(tmpV3, tmpV3, size); // in-place scale + VectorUtil.vectorAdd(low, center, tmpV3); } public final float getMinX() { @@ -351,9 +359,66 @@ public class AABBox implements Cloneable { VectorUtil.checkEquality(high, other.high) ; } + /** + * Assume this bounding box as being in object space and + * compute the window bounding box. + * <p> + * If <code>useCenterZ</code> is <code>true</code>, + * only 4 {@link PMVMatrix#gluProject(float, float, float, int[], int, float[], int) gluProject} + * operations are made on points [1..4] using {@link #getCenter()}'s z-value. + * Otherwise 8 {@link PMVMatrix#gluProject(float, float, float, int[], int, float[], int) gluProject} + * operation on all 8 points are performed. + * </p> + * <pre> + * [2] ------ [4] + * | | + * | | + * [1] ------ [3] + * </pre> + * @param pmv + * @param view + * @param useCenterZ + * @param tmpV3 TODO + * @return + */ + public AABBox mapToWindow(final AABBox result, final PMVMatrix pmv, final int[] view, final boolean useCenterZ, float[] tmpV3) { + // System.err.printf("AABBox.mapToWindow.0: view[%d, %d, %d, %d], this %s%n", view[0], view[1], view[2], view[3], toString()); + float objZ = useCenterZ ? center[2] : getMinZ(); + pmv.gluProject(getMinX(), getMinY(), objZ, view, 0, tmpV3, 0); + // System.err.printf("AABBox.mapToWindow.p1: %f, %f, %f -> %f, %f, %f%n", getMinX(), getMinY(), objZ, tmpV3[0], tmpV3[1], tmpV3[2]); + // System.err.printf("AABBox.mapToWindow.p1: %s%n", pmv.toString()); + result.reset(); + result.resize(tmpV3, 0); + pmv.gluProject(getMinX(), getMaxY(), objZ, view, 0, tmpV3, 0); + // System.err.printf("AABBox.mapToWindow.p2: %f, %f, %f -> %f, %f, %f%n", getMinX(), getMaxY(), objZ, tmpV3[0], tmpV3[1], tmpV3[2]); + result.resize(tmpV3, 0); + pmv.gluProject(getMaxX(), getMinY(), objZ, view, 0, tmpV3, 0); + // System.err.printf("AABBox.mapToWindow.p3: %f, %f, %f -> %f, %f, %f%n", getMaxX(), getMinY(), objZ, tmpV3[0], tmpV3[1], tmpV3[2]); + result.resize(tmpV3, 0); + pmv.gluProject(getMaxX(), getMaxY(), objZ, view, 0, tmpV3, 0); + // System.err.printf("AABBox.mapToWindow.p4: %f, %f, %f -> %f, %f, %f%n", getMaxX(), getMaxY(), objZ, tmpV3[0], tmpV3[1], tmpV3[2]); + result.resize(tmpV3, 0); + if( !useCenterZ ) { + objZ = getMaxZ(); + pmv.gluProject(getMinX(), getMinY(), objZ, view, 0, tmpV3, 0); + result.resize(tmpV3, 0); + pmv.gluProject(getMinX(), getMaxY(), objZ, view, 0, tmpV3, 0); + result.resize(tmpV3, 0); + pmv.gluProject(getMaxX(), getMinY(), objZ, view, 0, tmpV3, 0); + result.resize(tmpV3, 0); + pmv.gluProject(getMaxX(), getMaxY(), objZ, view, 0, tmpV3, 0); + result.resize(tmpV3, 0); + } + if( DEBUG ) { + System.err.printf("AABBox.mapToWindow: view[%d, %d], this %s -> %s%n", view[0], view[1], toString(), result.toString()); + } + return result; + } + @Override public final String toString() { - return "[ "+low[0]+"/"+low[1]+"/"+low[1]+" .. "+high[0]+"/"+high[0]+"/"+high[0]+", ctr "+ - center[0]+"/"+center[1]+"/"+center[1]+" ]"; + return "[ dim "+getWidth()+" x "+getHeight()+" x "+getDepth()+ + ", box "+low[0]+" / "+low[1]+" / "+low[2]+" .. "+high[0]+" / "+high[1]+" / "+high[2]+ + ", ctr "+center[0]+" / "+center[1]+" / "+center[2]+" ]"; } } diff --git a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java index 218897ffe..270bf34f6 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java +++ b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java @@ -65,6 +65,7 @@ import com.jogamp.opengl.math.geom.Frustum; * <p> * All matrices are provided in column-major order, * as specified in the OpenGL fixed function pipeline, i.e. compatibility profile. + * See {@link FloatUtil}. * </p> * <p> * PMVMatrix can supplement {@link GL2ES2} applications w/ the @@ -486,7 +487,7 @@ public class PMVMatrix implements GLMatrixFunc { public final void glGetFloatv(int matrixGetName, FloatBuffer params) { int pos = params.position(); if(matrixGetName==GL_MATRIX_MODE) { - params.put((float)matrixMode); + params.put(matrixMode); } else { final FloatBuffer matrix = glGetMatrixf(matrixGetName); params.put(matrix); // matrix -> params @@ -498,7 +499,7 @@ public class PMVMatrix implements GLMatrixFunc { @Override public final void glGetFloatv(int matrixGetName, float[] params, int params_offset) { if(matrixGetName==GL_MATRIX_MODE) { - params[params_offset]=(float)matrixMode; + params[params_offset]=matrixMode; } else { final FloatBuffer matrix = glGetMatrixf(matrixGetName); matrix.get(params, params_offset, 16); // matrix -> params diff --git a/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java b/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java index b964245ad..f4ea29084 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java +++ b/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java @@ -133,7 +133,7 @@ public interface AudioSink { * @param sampleCount sample count per frame and channel */ public final float getSamplesDuration(int sampleCount) { - return ( 1000f * (float) sampleCount ) / (float)sampleRate; + return ( 1000f * sampleCount ) / sampleRate; } /** @@ -152,7 +152,7 @@ public interface AudioSink { * @param frameDuration duration per frame in milliseconds. */ public final int getFrameCount(int millisecs, float frameDuration) { - return Math.max(1, (int) ( (float)millisecs / frameDuration + 0.5f )); + return Math.max(1, (int) ( millisecs / frameDuration + 0.5f )); } /** @@ -187,7 +187,7 @@ public interface AudioSink { * <p> * Byte Count -> Sample Count * </p> - * @param sampleCount sample count + * @param byteCount number of bytes */ public final int getBytesSampleCount(int byteCount) { return ( byteCount << 3 ) / sampleSize; |