aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/graph/curve
diff options
context:
space:
mode:
authorRami Santina <rami.santina@gmail.com>2011-03-25 03:58:05 +0100
committerSven Gothel <sgothel@jausoft.com>2011-03-25 03:58:05 +0100
commit7f745c32c5734bc9549a16a98d158cdc01215bf5 (patch)
treead798e477be7ff9903a98e3ee3ee51e130453d2e /src/com/jogamp/graph/curve
parent6c07da79c276abef7a7c2f51b05fb1e04f7666db (diff)
Add initial GPU based curve rendering implementation, utilizing TTF fonts and manual shapes
Diffstat (limited to 'src/com/jogamp/graph/curve')
-rwxr-xr-xsrc/com/jogamp/graph/curve/HwRegionRenderer.java385
-rwxr-xr-xsrc/com/jogamp/graph/curve/OutlineShape.java244
-rwxr-xr-xsrc/com/jogamp/graph/curve/Region.java125
-rwxr-xr-xsrc/com/jogamp/graph/curve/RegionFactory.java58
-rw-r--r--src/com/jogamp/graph/curve/shader/curverenderer.fp99
-rw-r--r--src/com/jogamp/graph/curve/shader/curverenderer.vp13
-rw-r--r--src/com/jogamp/graph/curve/tess/CDTriangulator2D.java213
-rw-r--r--src/com/jogamp/graph/curve/text/HwTextRenderer.java367
8 files changed, 1504 insertions, 0 deletions
diff --git a/src/com/jogamp/graph/curve/HwRegionRenderer.java b/src/com/jogamp/graph/curve/HwRegionRenderer.java
new file mode 100755
index 000000000..3c3142061
--- /dev/null
+++ b/src/com/jogamp/graph/curve/HwRegionRenderer.java
@@ -0,0 +1,385 @@
+/**
+ * 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;
+
+import java.nio.FloatBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import javax.media.opengl.GL2ES2;
+import javax.media.opengl.GLAutoDrawable;
+import javax.media.opengl.GLContext;
+import javax.media.opengl.GLException;
+import javax.media.opengl.GLUniformData;
+import javax.media.opengl.fixedfunc.GLMatrixFunc;
+
+import com.jogamp.graph.geom.Triangle;
+import com.jogamp.graph.geom.PointTex;
+
+import jogamp.opengl.Debug;
+import com.jogamp.opengl.util.PMVMatrix;
+import com.jogamp.opengl.util.glsl.ShaderCode;
+import com.jogamp.opengl.util.glsl.ShaderProgram;
+import com.jogamp.opengl.util.glsl.ShaderState;
+
+public class HwRegionRenderer {
+ protected static final boolean DEBUG = Debug.debug("RegionRenderer");
+
+ private ShaderState st;
+ private PMVMatrix pmvMatrix = new PMVMatrix();
+
+ /**Sharpness is equivalent to the value of t value of texture coord
+ * on the off-curve vertex. The high value of sharpness will
+ * result in high curvature.
+ */
+ private float sharpness = 0.5f;
+ private float alpha = 1.0f;
+ private float strength = 3.0f;
+ private boolean initialized = false;
+
+ private int regionType = Region.SINGLE_PASS;
+
+ private GLContext context;
+ private FloatBuffer color = FloatBuffer.allocate(3);
+ private HashMap<Integer, Region> regions = new HashMap<Integer, Region>();
+
+ /** Create a Hardware accelerated Region Renderer
+ * @param context OpenGL rendering context
+ * @param factory optional Point.Factory for PointTex construction. Default is Vertex.Factory.
+ */
+ public HwRegionRenderer(GLContext context) {
+ this.context = context;
+ init(context, 0.5f);
+ }
+ /** Create a Hardware accelerated Region Renderer
+ * @param context OpenGL rendering context
+ * @param type region type (single or multipass)
+ */
+ public HwRegionRenderer(GLContext context, int type) {
+ this.context = context;
+ this.regionType = type;
+ init(context, 0.5f);
+ }
+
+ private boolean init(GLContext context, float sharpvalue){
+ if(initialized){
+ if(DEBUG) {
+ System.err.println("HWRegionRenderer: Already initialized!");
+ }
+ return true;
+ }
+ sharpness = sharpvalue;
+
+ GL2ES2 gl = context.getGL().getGL2ES2();
+
+ boolean VBOsupported = gl.isFunctionAvailable("glGenBuffers") &&
+ gl.isFunctionAvailable("glBindBuffer") &&
+ gl.isFunctionAvailable("glBufferData") &&
+ gl.isFunctionAvailable("glDrawElements") &&
+ gl.isFunctionAvailable("glVertexAttribPointer") &&
+ gl.isFunctionAvailable("glDeleteBuffers");
+
+ if(DEBUG) {
+ System.err.println("HWRegionRenderer: VBO Supported = " + VBOsupported);
+ }
+
+ if(!VBOsupported){
+ return false;
+ }
+
+ gl.setSwapInterval(1);
+
+ gl.glEnable(GL2ES2.GL_BLEND);
+ gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA);
+
+ initShader(gl);
+
+ st.glUseProgram(gl, true);
+
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ pmvMatrix.glLoadIdentity();
+
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ resetMatrix();
+
+ if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()))) {
+ if(DEBUG){
+ System.err.println("Error setting PMVMatrix in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("p1y", sharpness))) {
+ if(DEBUG){
+ System.err.println("Error setting sharpness in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_alpha", alpha))) {
+ if(DEBUG){
+ System.err.println("Error setting global alpha in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_color", 3, color))) {
+ if(DEBUG){
+ System.err.println("Error setting global color in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("a_strength", strength))) {
+ System.err.println("Error setting antialias strength in shader: "+st);
+ }
+ st.glUseProgram(gl, false);
+
+ if(DEBUG) {
+ System.err.println("HWRegionRenderer initialized: " + Thread.currentThread()+" "+st);
+ }
+ initialized = true;
+ return true;
+ }
+
+ public float getAlpha() {
+ return alpha;
+ }
+ public void setAlpha(float alpha_t) {
+ alpha = alpha_t;
+ }
+
+ public void setColor(float r, float g, float b){
+ color.put(r);
+ color.put(g);
+ color.put(b);
+ color.rewind();
+ }
+
+ public void rotate(float angle, float x, float y, float z){
+ pmvMatrix.glRotatef(angle, x, y, z);
+ }
+ public void translate(float x, float y, float z){
+ pmvMatrix.glTranslatef(x, y, z);
+ }
+
+ public void resetMatrix(){
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ pmvMatrix.glLoadIdentity();
+ }
+
+ /**
+ * @param drawable
+ * @param angle
+ * @param ratio
+ * @param near
+ * @param far
+ * @return
+ */
+ public boolean reshape(GLAutoDrawable drawable, float angle, float ratio, float near, float far){
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ pmvMatrix.gluPerspective(angle, ratio, near, far);
+
+ if(null==st) {
+ if(DEBUG){
+ System.err.println("HWRegionRenderer: Shader State is null, or not");
+ }
+ return false;
+ }
+ GL2ES2 gl = drawable.getGL().getGL2ES2();
+
+ st.glUseProgram(gl, true);
+ GLUniformData ud = st.getUniform("mgl_PMVMatrix");
+ if(null!=ud) {
+ st.glUniform(gl, ud);
+ }
+ st.glUseProgram(gl, false);
+ return true;
+ }
+
+ private void initShader(GL2ES2 gl) {
+ ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, HwRegionRenderer.class,
+ "shader", "shader/bin", "curverenderer");
+ ShaderCode rsFp = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, 1, HwRegionRenderer.class,
+ "shader", "shader/bin", "curverenderer");
+
+ ShaderProgram sp = new ShaderProgram();
+ sp.add(rsVp);
+ sp.add(rsFp);
+
+ if(!sp.link(gl, System.err)) {
+ throw new GLException("HWRegionRenderer: Couldn't link program: "+sp);
+ }
+
+ st = new ShaderState();
+ st.attachShaderProgram(gl, sp);
+ gl.glBindAttribLocation(sp.id(), 0, "v_position");
+ gl.glBindAttribLocation(sp.id(), 1, "texCoord");
+ }
+
+ private Region createRegion(OutlineShape outlineShape) {
+ Region region = RegionFactory.create(context, st, regionType);
+
+ outlineShape.transformOutlines(OutlineShape.QUADRATIC_NURBS);
+
+ ArrayList<Triangle<PointTex>> triangles = (ArrayList<Triangle<PointTex>>) outlineShape.triangulate(sharpness);
+ ArrayList<PointTex> vertices = (ArrayList<PointTex>) outlineShape.getVertices();
+ region.addVertices(vertices);
+ region.addTriangles(triangles);
+
+ region.update();
+ return region;
+ }
+
+ private Region createRegion(OutlineShape[] outlineShapes) {
+ Region region = RegionFactory.create(context, st, regionType);
+
+ int numVertices = region.getNumVertices();
+
+ for(OutlineShape outlineShape:outlineShapes){
+ outlineShape.transformOutlines(OutlineShape.QUADRATIC_NURBS);
+
+ ArrayList<Triangle<PointTex>> triangles = outlineShape.triangulate(sharpness);
+ region.addTriangles(triangles);
+
+ ArrayList<PointTex> vertices = outlineShape.getVertices();
+ for(PointTex vert:vertices){
+ vert.setId(numVertices++);
+ }
+ region.addVertices(vertices);
+ }
+
+ region.update();
+ return region;
+ }
+
+
+ /** Render outline in 3D space at the position provided
+ * the triangles of the shapes will be generated, if not yet generated
+ * @param outlineShape the OutlineShape to Render.
+ * @param position the initial translation of the outlineShape.
+ * @throws Exception if HwRegionRenderer not initialized
+ */
+ public void renderOutlineShape(OutlineShape outlineShape, float[] position) throws Exception{
+ if(!initialized){
+ throw new Exception("HWRegionRenderer: not initialized!");
+ }
+ int hashCode = getHashCode(outlineShape);
+ Region region = regions.get(hashCode);
+
+ if(null == region) {
+ region = createRegion(outlineShape);
+ regions.put(hashCode, region);
+ }
+
+ GL2ES2 gl = context.getGL().getGL2ES2();
+ st.glUseProgram(gl, true);
+ GLUniformData ud = st.getUniform("mgl_PMVMatrix");
+ if(null!=ud) {
+ st.glUniform(gl, ud);
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_alpha", alpha))) {
+ System.err.println("Error setting global alpha in shader: "+st);
+ }
+ GLUniformData gcolorUD = st.getUniform("g_color");
+ if(null!=gcolorUD) {
+ st.glUniform(gl, gcolorUD);
+ }
+ if(!st.glUniform(gl, new GLUniformData("a_strength", strength))) {
+ System.err.println("Error setting antialias strength in shader: "+st);
+ }
+
+ region.render(null, 0, 0, 0);
+ st.glUseProgram(gl, false);
+ }
+
+ /** Render a list of Outline shapes combined in one region
+ * at the position provided the triangles of the
+ * shapes will be generated, if not yet generated
+ * @param outlineShapes the list of OutlineShapes to Render.
+ * @param position the initial translation of the outlineShapes.
+ * @throws Exception if HwRegionRenderer not initialized
+ */
+ public void renderOutlineShapes(OutlineShape[] outlineShapes, float[] position) throws Exception{
+ if(!initialized){
+ throw new Exception("HWRegionRenderer: not initialized!");
+ }
+
+ int hashCode = getHashCode(outlineShapes);
+ Region region = regions.get(hashCode);
+
+ if(null == region) {
+ region = createRegion(outlineShapes);
+ regions.put(hashCode, region);
+ }
+
+ GL2ES2 gl = context.getGL().getGL2ES2();
+ st.glUseProgram(gl, true);
+ GLUniformData ud = st.getUniform("mgl_PMVMatrix");
+ if(null!=ud) {
+ st.glUniform(gl, ud);
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_alpha", alpha))) {
+ System.err.println("Error setting global alpha in shader: "+st);
+ }
+ GLUniformData gcolorUD = st.getUniform("g_color");
+ if(null!=gcolorUD) {
+ st.glUniform(gl, gcolorUD);
+ }
+ if(!st.glUniform(gl, new GLUniformData("a_strength", strength))) {
+ System.err.println("Error setting antialias strength in shader: "+st);
+ }
+ region.render(null, 0, 0, 0);
+ st.glUseProgram(gl, false);
+ }
+
+ private int getHashCode(OutlineShape outlineShape){
+ return outlineShape.hashCode();
+ }
+
+ private int getHashCode(OutlineShape[] outlineShapes){
+ int hashcode = 0;
+ for(OutlineShape outlineShape:outlineShapes){
+ hashcode += getHashCode(outlineShape);
+ }
+ return hashcode;
+ }
+
+ /** Clears the cached string curves
+ * and destorys underlying buffers
+ */
+ public void clearCached() {
+ Iterator<Region> iterator = regions.values().iterator();
+ while(iterator.hasNext()){
+ Region region = iterator.next();
+ region.destroy();
+ }
+ regions.clear();
+ }
+}
diff --git a/src/com/jogamp/graph/curve/OutlineShape.java b/src/com/jogamp/graph/curve/OutlineShape.java
new file mode 100755
index 000000000..d939d7427
--- /dev/null
+++ b/src/com/jogamp/graph/curve/OutlineShape.java
@@ -0,0 +1,244 @@
+/**
+ * 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;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+import jogamp.graph.math.VectorFloatUtil;
+
+import com.jogamp.graph.geom.Outline;
+import com.jogamp.graph.geom.Line;
+import com.jogamp.graph.geom.Triangle;
+import com.jogamp.graph.geom.Point;
+import com.jogamp.graph.geom.PointTex;
+
+import com.jogamp.graph.curve.tess.CDTriangulator2D;
+
+/** 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.
+ *
+ * One or more OutlineShape Object can be associated to a region
+ * this is left high-level representation of the Objects. For
+ * possible Optimizations.
+ *
+ * @see Region
+ */
+public class OutlineShape {
+ public static final int QUADRATIC_NURBS = 10;
+ private final Point.Factory<? extends PointTex> pointFactory;
+ private ArrayList<Outline<PointTex>> outlines = new ArrayList<Outline<PointTex>>(3);
+
+ /** Create a new Outline based Shape
+ */
+ public OutlineShape(Point.Factory<? extends PointTex> factory) {
+ pointFactory = factory;
+ outlines.add(new Outline<PointTex>());
+ }
+
+ public final Point.Factory<? extends PointTex> pointFactory() { return pointFactory; }
+
+ /** Add a new empty outline
+ * to the shape, this new outline will
+ * be placed at the end of the outline list.
+ */
+ public void addEmptyOutline(){
+ outlines.add(new Outline<PointTex>());
+ }
+
+ /** Adds an outline to the OutlineShape object
+ * if last outline of the shape is empty, it will replace
+ * that last Outline with the new one. If outline is empty,
+ * it will do nothing.
+ * @param outline an Outline object
+ */
+ public void addOutline(Outline<PointTex> outline){
+ if(outline.isEmpty()){
+ return;
+ }
+ if(getLastOutline().isEmpty()){
+ outlines.remove(getLastOutline());
+ }
+ outlines.add(outline);
+ }
+
+ /** Adds a vertex to the last open outline in the
+ * shape
+ * @param point
+ */
+ public final void addVertex(PointTex point){
+ getLastOutline().addVertex(point);
+ }
+
+ public final void addVertex(float x, float y, boolean onCurve) {
+ getLastOutline().addVertex(pointFactory, x, y, onCurve);
+ }
+
+ public final void addVertex(float x, float y, float z, boolean onCurve) {
+ getLastOutline().addVertex(pointFactory, x, y, z, onCurve);
+ }
+
+ public final void addVertex(float[] coordsBuffer, int offset, int length, boolean onCurve) {
+ getLastOutline().addVertex(pointFactory, coordsBuffer, offset, length, onCurve);
+ }
+
+ /** Closes the last outline in the shape
+ * if last vertex is not equal to first vertex.
+ * A new temp vertex is added at the end which
+ * is equal to the first.
+ */
+ public void closeLastOutline(){
+ getLastOutline().setClosed(true);
+ }
+
+ /** Get the last added outline to the list
+ * of outlines that define the shape
+ * @return the last outline
+ */
+ public final Outline<PointTex> getLastOutline(){
+ return outlines.get(outlines.size()-1);
+ }
+ /** Make sure that the outlines represent
+ * the specified destinationType, if not
+ * transform outlines to destinationType.
+ * @param destinationType The curve type needed
+ */
+ public void transformOutlines(int destinationType){
+ if(destinationType == QUADRATIC_NURBS){
+ transformOutlinesQuadratic();
+ }
+ }
+
+ private void transformOutlinesQuadratic(){
+ ArrayList<Outline<PointTex>> newOutlines = new ArrayList<Outline<PointTex>>(3);
+
+ /**loop over the outlines and make sure no
+ * adj off-curve vertices
+ */
+ for(Outline<PointTex> outline:outlines){
+ Outline<PointTex> newOutline = new Outline<PointTex>();
+
+ ArrayList<PointTex> vertices = outline.getVertices();
+ int size =vertices.size()-1;
+ for(int i=0;i<size;i++){
+ PointTex currentVertex = vertices.get(i);
+ PointTex nextVertex = vertices.get((i+1)%size);
+ if(!(currentVertex.isOnCurve()) && !(nextVertex.isOnCurve())) {
+ newOutline.addVertex(currentVertex);
+
+ float[] newCoords = VectorFloatUtil.mid(currentVertex.getCoord(), nextVertex.getCoord());
+ newOutline.addVertex(pointFactory, newCoords, 0, 3, true);
+ }
+ else {
+ newOutline.addVertex(currentVertex);
+ }
+ }
+ newOutlines.add(newOutline);
+ }
+ outlines = newOutlines;
+ }
+
+ private void generateVertexIds(){
+ int maxVertexId = 0;
+ for(Outline<PointTex> outline:outlines){
+ ArrayList<PointTex> vertices = outline.getVertices();
+ for(PointTex vert:vertices){
+ vert.setId(maxVertexId);
+ maxVertexId++;
+ }
+ }
+ }
+
+ /** @return the list of vertices associated with the
+ * {@code Outline} list of this object
+ */
+ public ArrayList<PointTex> getVertices(){
+ ArrayList<PointTex> vertices = new ArrayList<PointTex>();
+ for(Outline<PointTex> polyline:outlines){
+ vertices.addAll(polyline.getVertices());
+ }
+ return vertices;
+ }
+
+
+ /** Generates the lines the define the noncurved
+ * parts of this graph
+ * @return arraylist of lines
+ */
+ public ArrayList<Line<PointTex>> getLines(){
+ ArrayList<Line<PointTex>> lines = new ArrayList<Line<PointTex>>();
+ for(Outline<PointTex> outline:outlines){
+ ArrayList<PointTex> outVertices = outline.getVertices();
+ int size = outVertices.size();
+ for(int i=0; i < size; i++) {
+ PointTex currentVertex = outVertices.get(i);
+ if(currentVertex.isOnCurve()) {
+ PointTex v2 = outVertices.get((i+1)%size);
+ if(v2.isOnCurve()){
+ lines.add(new Line<PointTex>(currentVertex, v2));
+ }
+ }
+ }
+ }
+ return lines;
+ }
+
+ /** Triangluate the graph object
+ * @param sharpness sharpness of the curved regions default = 0.5
+ */
+ public ArrayList<Triangle<PointTex>> triangulate(float sharpness){
+ if(outlines.size() == 0){
+ return null;
+ }
+ sortOutlines();
+ generateVertexIds();
+
+ CDTriangulator2D<PointTex> triangulator2d = new CDTriangulator2D<PointTex>(sharpness);
+
+ for(int index = 0; index< outlines.size();index++){
+ Outline<PointTex> outline = outlines.get(index);
+ triangulator2d.addCurve(outline);
+ }
+
+ ArrayList<Triangle<PointTex>> triangles = triangulator2d.generateTriangulation();
+ triangulator2d.reset();
+
+ return triangles;
+ }
+
+ /** Sort the outlines from large
+ * to small depending on the AABox
+ */
+ private void sortOutlines() {
+ Collections.sort(outlines);
+ Collections.reverse(outlines);
+ }
+}
diff --git a/src/com/jogamp/graph/curve/Region.java b/src/com/jogamp/graph/curve/Region.java
new file mode 100755
index 000000000..44f426313
--- /dev/null
+++ b/src/com/jogamp/graph/curve/Region.java
@@ -0,0 +1,125 @@
+/**
+ * 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;
+
+import java.util.ArrayList;
+
+import com.jogamp.graph.geom.Triangle;
+import com.jogamp.graph.geom.PointTex;
+import com.jogamp.opengl.util.PMVMatrix;
+
+/** A Region is the OGL binding of one or more OutlineShapes
+ * Defined by its vertices and generated triangles. The Region
+ * defines the final shape of the OutlineShape(s), which shall produced a shaded
+ * region on the screen.
+ *
+ * Implementations of the Region shall take care of the OGL
+ * binding of the depending on its context, profile.
+ *
+ * @see RegionFactory, OutlineShape
+ */
+public interface Region {
+ /** The vertices index in an OGL object
+ */
+ public static int VERTEX_POS_INDX = 0;
+
+ /** The Texture Coord index in an OGL object
+ */
+ public static int TEX_COORD = 1;
+
+ public static int SINGLE_PASS = 10;
+ public static int TWO_PASS = 20;
+
+ /** Updates a graph region by updating the ogl related
+ * objects for use in rendering. if called for the first time
+ * it initialize the objects.
+ */
+ public void update();
+
+ /** Renders the associated OGL objects specifying
+ * current width/hight of window for multi pass rendering
+ * of the region.
+ * @param matrix current pmv matrix.
+ * @param vp_width current screen width
+ * @param vp_height current screen height
+ * @param width texture width for mp rendering
+ *
+ * @see update()
+ */
+ public void render(PMVMatrix matrix, int vp_width, int vp_height, int width);
+
+ /** Adds a list of {@code 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()
+ */
+ public void addTriangles(ArrayList<Triangle<PointTex>> tris);
+
+ /** Get the current number of vertices associated
+ * with this region. This number is not necessary equal to
+ * the OGL binded number of vertices.
+ * @return vertices count
+ *
+ * @see isDirty()
+ */
+ public int getNumVertices();
+
+ /** Adds a list of {@code 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()
+ */
+ public void addVertices(ArrayList<PointTex> verts);
+
+ /** 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();
+ */
+ public boolean isDirty();
+
+ /** Delete and clean the associated OGL
+ * objects
+ */
+ public void destroy();
+
+ public boolean isFlipped();
+
+ /** Set if the y coordinate of the region should be flipped
+ * {@code y=-y} used mainly for fonts since they use opposite vertex
+ * as origion
+ * @param flipped flag if the coordinate is flipped defaults to false.
+ */
+ public void setFlipped(boolean flipped);
+}
diff --git a/src/com/jogamp/graph/curve/RegionFactory.java b/src/com/jogamp/graph/curve/RegionFactory.java
new file mode 100755
index 000000000..158f9db5b
--- /dev/null
+++ b/src/com/jogamp/graph/curve/RegionFactory.java
@@ -0,0 +1,58 @@
+/**
+ * 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;
+
+import javax.media.opengl.GLContext;
+
+import com.jogamp.opengl.util.glsl.ShaderState;
+
+import jogamp.graph.curve.opengl.VBORegionSPES2;
+import jogamp.graph.curve.opengl.VBORegion2PGL3;
+
+
+/** RegionFactory to create a Context specific Region implementation.
+ *
+ * @see Region
+ */
+public class RegionFactory {
+
+ /**Create a Region based on the GLContext attached
+ * @param context the current opengl context
+ * @param st the shader state object
+ * @param type can be one of Region.SINGLE_PASS or Region.TWO_PASS
+ * @return region
+ */
+ public static Region create(GLContext context, ShaderState st, int type){
+ if(Region.TWO_PASS == type && context.isGL3()){
+ return new VBORegion2PGL3(context, st);
+ }
+ else{
+ return new VBORegionSPES2(context);
+ }
+ }
+}
diff --git a/src/com/jogamp/graph/curve/shader/curverenderer.fp b/src/com/jogamp/graph/curve/shader/curverenderer.fp
new file mode 100644
index 000000000..2b3a0ce1d
--- /dev/null
+++ b/src/com/jogamp/graph/curve/shader/curverenderer.fp
@@ -0,0 +1,99 @@
+//#version 100
+
+uniform float p1y;
+uniform float g_alpha;
+uniform vec3 g_color;
+uniform float a_strength;
+
+varying vec2 v_texCoord;
+
+vec3 b_color = vec3(0.0, 0.0, 0.0);
+
+uniform sampler2D texture;
+vec4 weights = vec4(0.075, 0.06, 0.045, 0.025);
+
+void main (void)
+{
+ vec2 rtex = vec2(abs(v_texCoord.x),abs(v_texCoord.y));
+ vec3 c = g_color;
+
+ float alpha = 0.0;
+
+ if((v_texCoord.x == 0.0) && (v_texCoord.y == 0.0)){
+ alpha = g_alpha;
+ }
+ else if((v_texCoord.x >= 5.0)){
+ vec2 dfx = dFdx(v_texCoord);
+ vec2 dfy = dFdy(v_texCoord);
+
+ vec2 size = 1.0/textureSize(texture,0); //version 130
+ rtex -= 5.0;
+ vec4 t = texture2D(texture, rtex)* 0.18;
+
+ t += texture2D(texture, rtex + size*(vec2(1, 0)))*weights.x;
+ t += texture2D(texture, rtex - size*(vec2(1, 0)))*weights.x;
+ t += texture2D(texture, rtex + size*(vec2(0, 1)))*weights.x;
+ t += texture2D(texture, rtex - size*(vec2(0, 1)))*weights.x;
+
+ t += texture2D(texture, rtex + 2.0*size*(vec2(1, 0))) *weights.y;
+ t += texture2D(texture, rtex - 2.0*size*(vec2(1, 0)))*weights.y;
+ t += texture2D(texture, rtex + 2.0*size*(vec2(0, 1)))*weights.y;
+ t += texture2D(texture, rtex - 2.0*size*(vec2(0, 1)))*weights.y;
+
+ t += texture2D(texture, rtex + 3.0*size*(vec2(1, 0))) *weights.z;
+ t += texture2D(texture, rtex - 3.0*size*(vec2(1, 0)))*weights.z;
+ t += texture2D(texture, rtex + 3.0*size*(vec2(0, 1)))*weights.z;
+ t += texture2D(texture, rtex - 3.0*size*(vec2(0, 1)))*weights.z;
+
+ t += texture2D(texture, rtex + 4.0*size*(vec2(1, 0))) *weights.w;
+ t += texture2D(texture, rtex - 4.0*size*(vec2(1, 0)))*weights.w;
+ t += texture2D(texture, rtex + 4.0*size*(vec2(0, 1)))*weights.w;
+ t += texture2D(texture, rtex - 4.0*size*(vec2(0, 1)))*weights.w;
+
+ if(t.w == 0.0){
+ discard;
+ }
+
+ c = t.xyz;
+ alpha = g_alpha* t.w;
+ }
+ ///////////////////////////////////////////////////////////
+ else if ((v_texCoord.x > 0.0) && (rtex.y > 0.0 || rtex.x == 1.0)){
+ vec2 dtx = dFdx(rtex);
+ vec2 dty = dFdy(rtex);
+
+ rtex.y -= 0.1;
+ if(rtex.y < 0.0) {
+ if(v_texCoord.y < 0.0)
+ discard;
+ else{
+ rtex.y = 0.0;
+ }
+ }
+
+ vec2 f = vec2((dtx.y - 2.0*p1y*dtx.x + 4.0*p1y*rtex.x*dtx.x), (dty.y - 2.0*p1y*dty.x + 4.0*p1y*rtex.x*dty.x));
+
+ float position = rtex.y - ((2.0 * rtex.x * p1y) * (1.0 - rtex.x));
+ float d = position/(length(f));
+
+ float a = (0.5 - d * sign(v_texCoord.y));
+
+
+ if (a >= 1.0) {
+ alpha = g_alpha;
+ // c = vec3(1.0,1.0,1.0);
+ }
+ else if (a <= 0.0) {
+ alpha = 0.0;//discard;
+ // c = vec3(0.0,0.0,0.0);
+
+ }
+ else {
+ alpha = g_alpha*a;
+ // c = vec3(a,a,a);
+ mix(b_color,g_color, a);
+ }
+ }
+
+ gl_FragColor = vec4(c, alpha);
+}
diff --git a/src/com/jogamp/graph/curve/shader/curverenderer.vp b/src/com/jogamp/graph/curve/shader/curverenderer.vp
new file mode 100644
index 000000000..bc9ecb41e
--- /dev/null
+++ b/src/com/jogamp/graph/curve/shader/curverenderer.vp
@@ -0,0 +1,13 @@
+//#version 100
+
+uniform mat4 mgl_PMVMatrix[2];
+attribute vec4 v_position;
+attribute vec2 texCoord;
+
+varying vec2 v_texCoord;
+
+void main(void)
+{
+ gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * v_position;
+ v_texCoord = texCoord.st;
+} \ No newline at end of file
diff --git a/src/com/jogamp/graph/curve/tess/CDTriangulator2D.java b/src/com/jogamp/graph/curve/tess/CDTriangulator2D.java
new file mode 100644
index 000000000..936965f0c
--- /dev/null
+++ b/src/com/jogamp/graph/curve/tess/CDTriangulator2D.java
@@ -0,0 +1,213 @@
+/**
+ * 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.tess;
+
+import java.util.ArrayList;
+
+import jogamp.graph.curve.tess.GraphOutline;
+import jogamp.graph.curve.tess.GraphPoint;
+import jogamp.graph.curve.tess.Loop;
+import jogamp.graph.math.VectorFloatUtil;
+
+import com.jogamp.graph.geom.Outline;
+import com.jogamp.graph.geom.Triangle;
+import com.jogamp.graph.geom.PointTex;
+import jogamp.opengl.Debug;
+
+/** Constrained Delaunay Triangulation
+ * implementation of a list of Outlines that define a set of
+ * Closed Regions with optional n holes.
+ *
+ */
+public class CDTriangulator2D <T extends PointTex> {
+
+ protected static final boolean DEBUG = Debug.debug("Triangulation");
+
+ private float sharpness = 0.5f;
+ private ArrayList<Loop<T>> loops;
+ private ArrayList<T> vertices;
+
+ private ArrayList<Triangle<T>> triangles;
+ private int maxTriID = 0;
+
+
+ public CDTriangulator2D() {
+ this(0.5f);
+ }
+
+ /** Constructor for a new Delaunay triangulator
+ * @param curveSharpness the curvature around
+ * the off-curve vertices
+ */
+ public CDTriangulator2D(float curveSharpness) {
+ this.sharpness = curveSharpness;
+ reset();
+ }
+
+ /** Reset the triangulation to initial state
+ * Clearing cached data
+ */
+ public void reset() {
+ maxTriID = 0;
+ vertices = new ArrayList<T>();
+ triangles = new ArrayList<Triangle<T>>(3);
+ loops = new ArrayList<Loop<T>>();
+ }
+
+ /** Add a curve to the list of profiles provided
+ * @param polyline a bounding Outline
+ */
+ public void addCurve(Outline<T> polyline){
+ Loop<T> loop = null;
+
+ if(!loops.isEmpty()){
+ loop = getContainerLoop(polyline);
+ }
+
+ if(loop == null) {
+ GraphOutline<T> outline = new GraphOutline<T>(polyline);
+ GraphOutline<T> innerPoly = extractBoundaryTriangles(outline, false);
+ vertices.addAll(polyline.getVertices());
+ loop = new Loop<T>(innerPoly, VectorFloatUtil.CCW);
+ loops.add(loop);
+ }
+ else {
+ GraphOutline<T> outline = new GraphOutline<T>(polyline);
+ GraphOutline<T> innerPoly = extractBoundaryTriangles(outline, true);
+ vertices.addAll(innerPoly.getPoints());
+ loop.addConstraintCurve(innerPoly);
+ }
+ }
+
+ /** Generate the triangulation of the provided
+ * List of Outlines
+ */
+ public ArrayList<Triangle<T>> generateTriangulation(){
+ for(int i=0;i<loops.size();i++) {
+ Loop<T> loop = loops.get(i);
+ int numTries = 0;
+ int size = loop.computeLoopSize();
+ while(!loop.isSimplex()){
+ Triangle<T> tri = null;
+ if(numTries > size){
+ tri = loop.cut(false);
+ }
+ else{
+ tri = loop.cut(true);
+ }
+ numTries++;
+
+ if(tri != null) {
+ numTries = 0;
+ size--;
+ tri.setId(maxTriID++);
+ triangles.add(tri);
+ if(DEBUG){
+ System.err.println(tri);
+ }
+ }
+ if(numTries > size*2){
+ if(DEBUG){
+ System.err.println("Triangulation not complete!");
+ }
+ break;
+ }
+ }
+ Triangle<T> tri = loop.cut(true);
+ if(tri != null)
+ triangles.add(tri);
+ }
+ return triangles;
+ }
+
+ @SuppressWarnings("unchecked")
+ private GraphOutline<T> extractBoundaryTriangles(GraphOutline<T> outline, boolean hole){
+ GraphOutline<T> innerOutline = new GraphOutline<T>();
+ ArrayList<GraphPoint<T>> outVertices = outline.getGraphPoint();
+ int size = outVertices.size();
+ for(int i=0; i < size; i++) {
+ GraphPoint<T> currentVertex = outVertices.get(i);
+ GraphPoint<T> gv0 = outVertices.get((i+size-1)%size);
+ GraphPoint<T> gv2 = outVertices.get((i+1)%size);
+ GraphPoint<T> gv1 = currentVertex;
+
+ if(!currentVertex.getPoint().isOnCurve()) {
+ T v0 = (T) gv0.getPoint().clone();
+ T v2 = (T) gv2.getPoint().clone();
+ T v1 = (T) gv1.getPoint().clone();
+
+ gv0.setBoundaryContained(true);
+ gv1.setBoundaryContained(true);
+ gv2.setBoundaryContained(true);
+
+ Triangle<T> t= null;
+ boolean holeLike = false;
+ if(VectorFloatUtil.ccw(v0,v1,v2)){
+ t = new Triangle<T>(v0, v1, v2);
+ }
+ else {
+ holeLike = true;
+ t = new Triangle<T>(v2, v1, v0);
+ }
+ t.setId(maxTriID++);
+ triangles.add(t);
+
+ if(hole || holeLike) {
+ v0.setTexCoord(0, -0.1f);
+ v2.setTexCoord(1, -0.1f);
+ v1.setTexCoord(0.5f, -1*sharpness -0.1f);
+ innerOutline.addVertex(currentVertex);
+ }
+ else {
+ v0.setTexCoord(0, 0.1f);
+ v2.setTexCoord(1, 0.1f);
+ v1.setTexCoord(0.5f, sharpness+0.1f);
+ }
+ }
+ else {
+ if(!gv2.getPoint().isOnCurve() || !gv0.getPoint().isOnCurve()){
+ currentVertex.setBoundaryContained(true);
+ }
+ innerOutline.addVertex(currentVertex);
+ }
+ }
+ return innerOutline;
+ }
+
+ private Loop<T> getContainerLoop(Outline<T> polyline){
+ T v = polyline.getVertex(0);
+
+ for (Loop<T> loop:loops){
+ if(loop.checkInside(v)){
+ return loop;
+ }
+ }
+ return null;
+ }
+}
diff --git a/src/com/jogamp/graph/curve/text/HwTextRenderer.java b/src/com/jogamp/graph/curve/text/HwTextRenderer.java
new file mode 100644
index 000000000..5813225e4
--- /dev/null
+++ b/src/com/jogamp/graph/curve/text/HwTextRenderer.java
@@ -0,0 +1,367 @@
+/**
+ * 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.text;
+
+import java.nio.FloatBuffer;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import javax.media.opengl.GL2ES2;
+import javax.media.opengl.GLAutoDrawable;
+import javax.media.opengl.GLContext;
+import javax.media.opengl.GLException;
+import javax.media.opengl.GLUniformData;
+import javax.media.opengl.fixedfunc.GLMatrixFunc;
+
+import jogamp.graph.curve.text.GlyphString;
+import jogamp.graph.font.typecast.TypecastFontFactory;
+
+import com.jogamp.common.util.ReflectionUtil;
+import com.jogamp.graph.curve.Region;
+import com.jogamp.graph.font.Font;
+import com.jogamp.graph.font.FontFactory;
+import com.jogamp.graph.geom.plane.AffineTransform;
+import com.jogamp.graph.geom.plane.Path2D;
+import com.jogamp.graph.geom.Point;
+import com.jogamp.graph.geom.PointTex;
+import com.jogamp.graph.geom.opengl.Vertex;
+import jogamp.opengl.Debug;
+import com.jogamp.opengl.util.PMVMatrix;
+import com.jogamp.opengl.util.glsl.ShaderCode;
+import com.jogamp.opengl.util.glsl.ShaderProgram;
+import com.jogamp.opengl.util.glsl.ShaderState;
+
+public class HwTextRenderer {
+ protected static final boolean DEBUG = Debug.debug("TextRenderer");
+ static final boolean FONTTOOL_CUSTOM = false;
+
+ private static FontFactory fontFactory;
+
+ static {
+ FontFactory _fontFactory = null;
+
+ if(FONTTOOL_CUSTOM) {
+ _fontFactory = (FontFactory) ReflectionUtil.createInstance("jogamp.graph.font.ttf.TTFFontFactory", HwTextRenderer.class.getClassLoader());
+ if(null!=_fontFactory) {
+ System.err.println("Using custom font tool");
+ }
+ }
+ if(null==_fontFactory) {
+ _fontFactory = new TypecastFontFactory();
+ }
+ fontFactory = _fontFactory;
+ }
+
+
+ public static FontFactory getFontFactory() {
+ return fontFactory;
+ }
+
+ private ShaderState st;
+ private PMVMatrix pmvMatrix = new PMVMatrix();
+
+ /**Sharpness is equivalent to the value of t value of texture coord
+ * on the off-curve vertex. The high value of sharpness will
+ * result in high curvature.
+ */
+ private float sharpness = 0.5f;
+ private float alpha = 1.0f;
+ private float strength = 1.8f;
+ private boolean initialized = false;
+
+
+ private int regionType = Region.SINGLE_PASS;
+ private GLContext context;
+ private FloatBuffer color = FloatBuffer.allocate(3);
+ private HashMap<String, GlyphString> strings = new HashMap<String, GlyphString>();
+ private final Point.Factory<? extends PointTex> pointFactory;
+
+ int win_width = 0;
+ int win_height = 0;
+
+ /** Create a Hardware accelerated Text Renderer
+ * @param context OpenGL rendering context
+ * @param factory optional Point.Factory for PointTex construction. Default is Vertex.Factory.
+ */
+ public HwTextRenderer(GLContext context, Point.Factory<? extends PointTex> factory, int type) {
+ this.pointFactory = (null != factory) ? factory : Vertex.factory();
+ this.context = context;
+ this.regionType = type;
+ init(context, 0.5f);
+ }
+
+ public Font createFont(Point.Factory<? extends PointTex> factory, String name, int size) {
+ return fontFactory.createFont(factory, name, size);
+ }
+
+
+ public Font createFont(Point.Factory<? extends PointTex> factory,
+ String[] families,
+ String style,
+ String variant,
+ String weight,
+ String size) {
+ return fontFactory.createFont(factory, families, style, variant, weight, size);
+ }
+
+ /** initialize shaders and bindings for GPU based text Rendering, should
+ * be called only onceangle
+ * @param drawable the current drawable
+ * @param shapvalue shaprness around the off-curve vertices
+ * @return true if init succeeded, false otherwise
+ */
+ private boolean init(GLContext context, float sharpvalue){
+ if(initialized){
+ if(DEBUG) {
+ System.err.println("HWTextRenderer: Already initialized!");
+ }
+ return true;
+ }
+ sharpness = sharpvalue;
+
+ GL2ES2 gl = context.getGL().getGL2ES2();
+
+ boolean VBOsupported = gl.isFunctionAvailable("glGenBuffers") &&
+ gl.isFunctionAvailable("glBindBuffer") &&
+ gl.isFunctionAvailable("glBufferData") &&
+ gl.isFunctionAvailable("glDrawElements") &&
+ gl.isFunctionAvailable("glVertexAttribPointer") &&
+ gl.isFunctionAvailable("glDeleteBuffers");
+
+ if(DEBUG) {
+ System.err.println("HWTextRenderer: VBO Supported = " + VBOsupported);
+ }
+
+ if(!VBOsupported){
+ return false;
+ }
+
+ gl.setSwapInterval(1);
+
+ gl.glEnable(GL2ES2.GL_BLEND);
+ gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA);
+
+ initShader(gl);
+
+ st.glUseProgram(gl, true);
+
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ pmvMatrix.glLoadIdentity();
+
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ resetMatrix();
+
+ if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()))) {
+ if(DEBUG){
+ System.err.println("Error setting PMVMatrix in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("p1y", sharpness))) {
+ if(DEBUG){
+ System.err.println("Error setting sharpness in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_alpha", alpha))) {
+ if(DEBUG){
+ System.err.println("Error setting global alpha in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_color", 3, color))) {
+ if(DEBUG){
+ System.err.println("Error setting global color in shader: "+st);
+ }
+ return false;
+ }
+ if(!st.glUniform(gl, new GLUniformData("a_strength", strength))) {
+ System.err.println("Error setting antialias strength in shader: "+st);
+ }
+
+ st.glUseProgram(gl, false);
+
+ if(DEBUG) {
+ System.err.println("HWTextRenderer initialized: " + Thread.currentThread()+" "+st);
+ }
+ initialized = true;
+ return true;
+ }
+
+ public float getAlpha() {
+ return alpha;
+ }
+ public void setAlpha(float alpha_t) {
+ alpha = alpha_t;
+ }
+
+ public void setColor(float r, float g, float b){
+ color.put(r);
+ color.put(g);
+ color.put(b);
+ color.rewind();
+ }
+
+ public void rotate(float angle, float x, float y, float z){
+ pmvMatrix.glRotatef(angle, x, y, z);
+ }
+ public void translate(float x, float y, float z){
+ pmvMatrix.glTranslatef(x, y, z);
+ }
+
+ public void resetMatrix(){
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ pmvMatrix.glLoadIdentity();
+ }
+
+ /**
+ * @param drawable
+ * @param angle
+ * @param ratio
+ * @param near
+ * @param far
+ * @return
+ */
+ public boolean reshape(GLAutoDrawable drawable, float angle, int width, int height, float near, float far){
+ win_width = width;
+ win_height = height;
+ float ratio = (float)width/(float)height;
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ pmvMatrix.gluPerspective(angle, ratio, near, far);
+
+ if(null==st) {
+ if(DEBUG){
+ System.err.println("HWTextRenderer: Shader State is null, or not");
+ }
+ return false;
+ }
+ GL2ES2 gl = drawable.getGL().getGL2ES2();
+
+ st.glUseProgram(gl, true);
+ GLUniformData ud = st.getUniform("mgl_PMVMatrix");
+ if(null!=ud) {
+ st.glUniform(gl, ud);
+ }
+ st.glUseProgram(gl, false);
+ return true;
+ }
+
+ private void initShader(GL2ES2 gl) {
+ ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, HwTextRenderer.class,
+ "../shader", "../shader/bin", "curverenderer");
+ ShaderCode rsFp = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, 1, HwTextRenderer.class,
+ "../shader", "../shader/bin", "curverenderer");
+
+ ShaderProgram sp = new ShaderProgram();
+ sp.add(rsVp);
+ sp.add(rsFp);
+
+ if(!sp.link(gl, System.err)) {
+ throw new GLException("HWTextRenderer: Couldn't link program: "+sp);
+ }
+
+ st = new ShaderState();
+ st.attachShaderProgram(gl, sp);
+ gl.glBindAttribLocation(sp.id(), 0, "v_position");
+ gl.glBindAttribLocation(sp.id(), 1, "texCoord");
+ }
+
+ private GlyphString createString(Font font, String str) {
+ AffineTransform affineTransform = new AffineTransform(pointFactory);
+
+ Path2D[] paths = new Path2D[str.length()];
+ font.getOutline(str, affineTransform, paths);
+
+ GlyphString glyphString = new GlyphString(pointFactory, font.getName(), str);
+ glyphString.createfromFontPath(paths, affineTransform);
+
+ glyphString.generateRegion(context, sharpness, st, regionType);
+ return glyphString;
+ }
+
+
+ /** 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 font font to be used
+ * @param str text to be rendered
+ * @param position the lower left corner of the string
+ * @param size texture size for multipass render
+ * @throws Exception if TextRenderer not initialized
+ */
+ public void renderString3D(Font font, String str, float[] position, int size) throws Exception{
+ if(!initialized){
+ throw new Exception("HWTextRenderer: not initialized!");
+ }
+ String fontStrHash = getTextHashCode(font, str);
+ GlyphString glyphString = strings.get(fontStrHash);
+ if(null == glyphString) {
+ glyphString = createString(font, str);
+ strings.put(fontStrHash, glyphString);
+ }
+
+ GL2ES2 gl = context.getGL().getGL2ES2();
+ st.glUseProgram(gl, true);
+ GLUniformData ud = st.getUniform("mgl_PMVMatrix");
+ if(null!=ud) {
+ st.glUniform(gl, ud);
+ }
+ if(!st.glUniform(gl, new GLUniformData("g_alpha", alpha))) {
+ System.err.println("Error setting global alpha in shader: "+st);
+ }
+ GLUniformData gcolorUD = st.getUniform("g_color");
+ if(null!=gcolorUD) {
+ st.glUniform(gl, gcolorUD);
+ }
+
+ if(!st.glUniform(gl, new GLUniformData("a_strength", strength))) {
+ System.err.println("Error setting antialias strength in shader: "+st);
+ }
+ glyphString.renderString3D(pmvMatrix, win_width, win_height, size);
+ st.glUseProgram(gl, false);
+ }
+
+ private String getTextHashCode(Font font, String str){
+ return "" + str.hashCode() + font.getSize();
+ }
+
+ /** Clears the cached string curves
+ * and destorys underlying buffers
+ */
+ public void clearCached() {
+ Iterator<GlyphString> iterator = strings.values().iterator();
+ while(iterator.hasNext()){
+ GlyphString glyphString = iterator.next();
+ glyphString.destroy();
+ }
+ strings.clear();
+ }
+}