summaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/graph/curve/text/HwTextRenderer.java
blob: f30f42cc556fa8e4e8c14a6038e34bcdd971b45b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
 * 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.GLException;
import javax.media.opengl.GLUniformData;
import javax.media.opengl.fixedfunc.GLMatrixFunc;

import jogamp.graph.curve.text.GlyphString;
import jogamp.graph.font.FontInt;
import jogamp.graph.font.typecast.TypecastFontFactory;
import jogamp.graph.geom.plane.AffineTransform;
import jogamp.graph.geom.plane.Path2D;

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.Vertex;
import com.jogamp.graph.geom.opengl.SVertex;
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 = new ShaderState();
	
	private PMVMatrix pmvMatrix = new PMVMatrix();
	private GLUniformData mgl_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 GLUniformData mgl_sharpness = new GLUniformData("p1y", 0.5f);
    private GLUniformData mgl_alpha = new GLUniformData("g_alpha", 1.0f);
    private GLUniformData mgl_color = new GLUniformData("g_color", 3, FloatBuffer.allocate(3));
    private GLUniformData mgl_strength = new GLUniformData("a_strength", 1.8f);
    	
	private boolean initialized = false;	
	
	private int regionType = Region.SINGLE_PASS;
	
	private HashMap<String, GlyphString> strings = new HashMap<String, GlyphString>();
	private final Vertex.Factory<? extends Vertex> 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 Vertex construction. Default is Vertex.Factory.
	 */
	public HwTextRenderer(Vertex.Factory<? extends Vertex> factory, int type) {
		this.pointFactory = (null != factory) ? factory : SVertex.factory();
		this.regionType = type;
	}

    public Font createFont(Vertex.Factory<? extends Vertex> factory, String name) {
    	return fontFactory.createFont(factory, name);
    }


    public Font createFont(Vertex.Factory<? extends Vertex> factory,
    		               String[] families, 
                           String style,
                           String variant,
                           String weight) {
    	return fontFactory.createFont(factory, families, style, variant, weight);
    }
	
	/** 
	 * Initialize shaders and bindings for GPU based text Rendering, 
	 * should be called only once.
	 * Leaves the renderer enables, ie ShaderState on.
	 *  
	 * @param drawable the current drawable
	 * @param shapvalue shaprness around the off-curve vertices
	 * @return true if init succeeded, false otherwise
	 */
	public boolean init(GL2ES2 gl){
		if(initialized){
			if(DEBUG) {
				System.err.println("HWTextRenderer: Already initialized!");
			}
			return true;
		}
		
		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.glEnable(GL2ES2.GL_BLEND);
		gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA);
		
        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.attachShaderProgram(gl, sp);
        gl.glBindAttribLocation(sp.id(), 0, "v_position");
        gl.glBindAttribLocation(sp.id(), 1, "texCoord");
		
		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(null);
		
		mgl_PMVMatrix = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf());		    		
		if(!st.glUniform(gl, mgl_PMVMatrix)) {
			if(DEBUG){
				System.err.println("Error setting PMVMatrix in shader: "+st);
			}
			return false;
		}
		
		if(!st.glUniform(gl, mgl_sharpness)) {
			if(DEBUG){
				System.err.println("Error setting sharpness in shader: "+st);
			}
			return false;
		}
				
		if(!st.glUniform(gl, mgl_alpha)) {
			if(DEBUG){
				System.err.println("Error setting global alpha in shader: "+st);
			}
			return false;
		}		
		
		if(!st.glUniform(gl, mgl_color)) {
			if(DEBUG){
				System.err.println("Error setting global color in shader: "+st);
			}
			return false;
		}		
		
		if(!st.glUniform(gl, mgl_strength)) {
			System.err.println("Error setting antialias strength in shader: "+st);
		}
		
		if(DEBUG) {
			System.err.println("HWTextRenderer initialized: " + Thread.currentThread()+" "+st);
		}
		initialized = true;
		return true;
	}
	
	public void dispose(GL2ES2 gl) {
	    st.destroy(gl);
	    clearCached();
	}
	
	public float getAlpha() {
		return mgl_alpha.floatValue();
	}
	
	public ShaderState getShaderState() {
	    return st;
	}
	
	public void setAlpha(GL2ES2 gl, float alpha_t) {
	    mgl_alpha.setData(alpha_t);
	    if(null != gl && st.inUse()) {
	        st.glUniform(gl, mgl_alpha);
	    }
	}
	
	public void setColor(GL2ES2 gl, float r, float g, float b){
	    FloatBuffer fb = (FloatBuffer) mgl_color.getBuffer();
	    fb.put(0, r);
	    fb.put(1, r);
	    fb.put(2, r);
	    if(null != gl && st.inUse()) {
	        st.glUniform(gl, mgl_color);
	    }
	}
	
	public void rotate(GL2ES2 gl, float angle, float x, float y, float z){
		pmvMatrix.glRotatef(angle, x, y, z);
		if(null != gl && st.inUse()) {
		    st.glUniform(gl, mgl_PMVMatrix);
		}
	}
	public void translate(GL2ES2 gl, float x, float y, float z){
		pmvMatrix.glTranslatef(x, y, z);
		if(null != gl && st.inUse()) {
		    st.glUniform(gl, mgl_PMVMatrix);
		}
	}
	
	public  void resetMatrix(GL2ES2 gl){
		pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
		pmvMatrix.glLoadIdentity();
		if(null != gl && st.inUse()) {
		    st.glUniform(gl, mgl_PMVMatrix);
		}
	}
	
    public  void updateAllShaderValues(GL2ES2 gl) {
        if(null != gl && st.inUse()) {
            st.glUniform(gl, mgl_PMVMatrix);
            st.glUniform(gl, mgl_alpha);
            st.glUniform(gl, mgl_color);
            st.glUniform(gl, mgl_strength);
        }
    }
    
	/**
	 * @param gl
	 * @param angle
	 * @param ratio
	 * @param near
	 * @param far
	 * @return
	 */
	public boolean reshape(GL2ES2 gl, 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);
		
		st.glUniform(gl, mgl_PMVMatrix);
		
		return true;
	}

	private GlyphString createString(GL2ES2 gl, Font font, int size, String str) {
		AffineTransform affineTransform = new AffineTransform(pointFactory);
		
		Path2D[] paths = new Path2D[str.length()];
		((FontInt)font).getOutline(str, size, affineTransform, paths);
		
		GlyphString glyphString = new GlyphString(pointFactory, font.getName(), str);
		glyphString.createfromFontPath(paths, affineTransform);
		
		glyphString.generateRegion(gl.getContext(), mgl_sharpness.floatValue(), st, regionType);
		return glyphString;
	}
	
	
	public void enable(GL2ES2 gl, boolean enable) {
	    if(null != gl) {
	        st.glUseProgram(gl, enable);
	    }
	}
	
	/** 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 fontSize font size
	 * @param texSize texture size for multipass render
	 * @throws Exception if TextRenderer not initialized
	 */
	public void renderString3D(GL2ES2 gl, Font font, String str, float[] position, int fontSize, int texSize) {
		if(!initialized){
			throw new GLException("HWTextRenderer: not initialized!");
		}
		String fontStrHash = getTextHashCode(font, str, fontSize);
		GlyphString glyphString = strings.get(fontStrHash);
		if(null == glyphString) {
			glyphString = createString(gl, font, fontSize, str);
			strings.put(fontStrHash, glyphString);
		}
		
		glyphString.renderString3D(pmvMatrix, win_width, win_height, texSize);
	}
	
	private String getTextHashCode(Font font, String str, int fontSize) {
	    // FIXME: use integer hash code
		return font.getName() + "." + str.hashCode() + "." + fontSize;
	}

	/** 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();
	}
}