PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/components/visibleComponents/font/BitmapFontCharacter.java

http://mt4j.googlecode.com/
Java | 288 lines | 147 code | 48 blank | 93 comment | 25 complexity | 9624694a68c8ae9905ed4d300c136cd4 MD5 | raw file
  1/***********************************************************************
  2 * mt4j Copyright (c) 2008 - 2009 C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3 *  
  4 *   This program is free software: you can redistribute it and/or modify
  5 *   it under the terms of the GNU General Public License as published by
  6 *   the Free Software Foundation, either version 3 of the License, or
  7 *   (at your option) any later version.
  8 *
  9 *   This program is distributed in the hope that it will be useful,
 10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *   GNU General Public License for more details.
 13 *
 14 *   You should have received a copy of the GNU General Public License
 15 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16 *
 17 ***********************************************************************/
 18package org.mt4j.components.visibleComponents.font;
 19
 20import java.nio.FloatBuffer;
 21
 22import javax.media.opengl.GL;
 23
 24import org.mt4j.components.bounds.IBoundingShape;
 25import org.mt4j.components.visibleComponents.shapes.MTRectangle;
 26import org.mt4j.util.MT4jSettings;
 27import org.mt4j.util.math.Vertex;
 28import org.mt4j.util.opengl.GLTexture;
 29import org.mt4j.util.opengl.GLTexture.EXPANSION_FILTER;
 30import org.mt4j.util.opengl.GLTexture.SHRINKAGE_FILTER;
 31import org.mt4j.util.opengl.GLTexture.WRAP_MODE;
 32
 33import processing.core.PApplet;
 34import processing.core.PGraphics;
 35import processing.core.PImage;
 36
 37/**
 38 * The Class BitmapFontCharacter.
 39 * @author Christopher Ruff
 40 */
 41public class BitmapFontCharacter extends MTRectangle implements IFontCharacter {
 42	
 43	/** The unicode. */
 44	private String unicode;
 45	
 46	/** The horizontal dist. */
 47	private int horizontalDist;
 48
 49	/** The left offset. */
 50	private int leftOffset;
 51	
 52	
 53	/**
 54	 * Instantiates a new bitmap font character.
 55	 * @param applet the applet
 56	 * @param texture the texture
 57	 * @param unicode the unicode
 58	 * @param leftOffset the left offset
 59	 * @param topOffset the top offset
 60	 * @param horizontalAdvance the horizontal advance
 61	 */
 62	public BitmapFontCharacter(PApplet applet, PImage texture, String unicode, int leftOffset, int topOffset, int horizontalAdvance) {
 63		super(applet, new Vertex(leftOffset, topOffset,0), texture.width, texture.height);
 64		//hm..this is for the card loading, because
 65		//when we init gl texture in other thread it breaks..
 66//		this.setUseDirectGL(false);
 67//		this.setUseDirectGL(true);
 68		
 69		this.setTexture(texture);
 70		this.setTextureEnabled(true);
 71		
 72		this.leftOffset = leftOffset;
 73		this.horizontalDist = horizontalAdvance;
 74		this.unicode = unicode;
 75		
 76		this.setNoStroke(true); 
 77		this.setPickable(false);
 78		
 79		if (MT4jSettings.getInstance().isOpenGlMode()){
 80			//Set the texture to be non-repeating but clamping to the border to avoid artefacts
 81			PImage tex = this.getTexture();
 82			if (tex instanceof GLTexture) {
 83				GLTexture glTex = (GLTexture) tex;
 84//				glTex.setWrap(GL.GL_CLAMP, GL.GL_CLAMP);
 85//				glTex.setWrap(GL.GL_CLAMP_TO_EDGE, GL.GL_CLAMP_TO_EDGE);
 86				
 87				glTex.setWrapMode(WRAP_MODE.CLAMP_TO_EDGE, WRAP_MODE.CLAMP_TO_EDGE);
 88				
 89//				glTex.setFilter(SHRINKAGE_FILTER.Trilinear, EXPANSION_FILTER.Bilinear);
 90				glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.Bilinear);
 91//				glTex.setFilter(SHRINKAGE_FILTER.NearestNeighborNoMipMaps, EXPANSION_FILTER.NearestNeighbor);
 92//				glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.NearestNeighbor);
 93			}
 94		}
 95	}
 96	
 97	
 98	
 99	@Override
100	public void drawComponent(PGraphics g) {
101		//Draw the shape
102		if (MT4jSettings.getInstance().isOpenGlMode() && this.isUseDirectGL()){
103			super.drawComponent(g);
104		}else{ //Draw with pure proccessing commands...
105			g.strokeWeight(this.getStrokeWeight());
106			if (this.isNoStroke()) 	
107				g.noStroke();
108
109				drawWithProcessing(g);
110			if (/*MT4jSettings.getInstance().isOpenGlMode() &&*/ this.isDrawSmooth())
111				g.noSmooth(); //because of tesselation bug/lines visibile in shapes
112		}
113	}
114	
115	
116
117	/* (non-Javadoc)
118	 * @see org.mt4j.components.visibleComponents.font.IFontCharacter#drawComponent(javax.media.opengl.GL)
119	 */
120	//@Override
121	public void drawComponent(GL gl) { 
122//		this.drawPureGl(gl);
123//		/*
124		if (MT4jSettings.getInstance().isOpenGlMode()){
125			if (this.isUseDisplayList() && this.getGeometryInfo().getDisplayListIDs()[0] != -1){
126				gl.glCallList(this.getGeometryInfo().getDisplayListIDs()[0]);
127//				gl.glCallList(this.getGeometryInfo().getDisplayListIDs()[1]); //Outline rectangle
128			}else{
129				this.drawPureGl(gl);
130			}
131		}
132//		*/
133	}
134	
135	protected void drawPureGl(GL gl){
136//		/*
137		//Get display array/buffer pointers
138		FloatBuffer tbuff 			= this.getGeometryInfo().getTexBuff();
139		FloatBuffer vertBuff 		= this.getGeometryInfo().getVertBuff();
140		
141		//Enable Pointers, set vertex array pointer
142		gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
143		if (this.isUseVBOs()){//Vertices
144			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOVerticesName());
145			gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
146		}else{
147			gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertBuff);
148		}
149		
150		//Default texture target
151		int textureTarget = GL.GL_TEXTURE_2D;
152		
153		/////// DRAW SHAPE ///////
154		if (!this.isNoFill()){ 
155			boolean textureDrawn = false;
156			if (this.isTextureEnabled()
157				&& this.getTexture() != null 
158				&& this.getTexture() instanceof GLTexture) //Bad for performance?
159			{
160				GLTexture tex = (GLTexture)this.getTexture();
161				textureTarget = tex.getTextureTarget();
162				
163				//tells opengl which texture to reference in following calls from now on!
164				//the first parameter is eigher GL.GL_TEXTURE_2D or ..1D
165				gl.glEnable(textureTarget);
166				gl.glBindTexture(textureTarget, tex.getTextureID());
167				
168				gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
169				
170				if (this.isUseVBOs()){//Texture
171					gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOTextureName());
172					gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);
173				}else
174					gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, tbuff);
175				
176				textureDrawn = true;
177			}
178			
179			//Normals
180			if (this.getGeometryInfo().isContainsNormals()){
181				gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
182				if (this.isUseVBOs()){
183					gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBONormalsName());
184					gl.glNormalPointer(GL.GL_FLOAT, 0, 0); 
185				}else{
186					gl.glNormalPointer(GL.GL_FLOAT, 0, this.getGeometryInfo().getNormalsBuff());
187				}
188			}
189			
190			gl.glDrawArrays(this.getFillDrawMode(), 0, vertBuff.capacity()/3);
191			
192			if (this.getGeometryInfo().isContainsNormals()){
193				gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
194			}
195			
196			if (textureDrawn){
197				gl.glBindTexture(textureTarget, 0);//Unbind texture
198				gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
199				gl.glDisable(textureTarget); //weiter nach unten?
200			}
201		}
202		gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
203		//TEST
204		if (this.isUseVBOs()){
205			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
206			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
207		}
208//		*/
209	}
210	
211	
212	@Override
213	protected void setDefaultGestureActions() {
214		//no gestures
215	}
216	
217	
218	/* (non-Javadoc)
219	 * @see org.mt4j.components.visibleComponents.shapes.MTRectangle#computeDefaultBounds()
220	 */
221	//@Override
222	protected IBoundingShape computeDefaultBounds() {
223		//We assume that font characters never get picked or anything 
224		//and hope the creation speeds up by not calculating a bounding shape
225		return null;
226	}
227
228	/* (non-Javadoc)
229	 * @see org.mt4j.components.visibleComponents.font.IFontCharacter#getHorizontalDist()
230	 */
231	//@Override
232	public int getHorizontalDist() {
233		return this.horizontalDist;
234	}
235	
236	/**
237	 * Sets the horizontal dist.
238	 * 
239	 * @param horizontalDist the new horizontal dist
240	 */
241	public void setHorizontalDist(int horizontalDist) {
242		this.horizontalDist = horizontalDist;
243	}
244
245	/* (non-Javadoc)
246	 * @see org.mt4j.components.visibleComponents.font.IFontCharacter#getUnicode()
247	 */
248	//@Override
249	public String getUnicode() {
250		return this.unicode;
251	}
252
253	/**
254	 * Sets the unicode.
255	 * 
256	 * @param unicode the new unicode
257	 */
258	public void setUnicode(String unicode) {
259		this.unicode = unicode;
260	}
261	
262	
263	public int getLeftOffset() {
264		return this.leftOffset;
265	}
266
267
268	//FIXME TEST
269	public void setTextureFiltered(boolean scalable) {
270		if (MT4jSettings.getInstance().isOpenGlMode()){
271			PImage tex = this.getTexture();
272			if (tex instanceof GLTexture) {
273				GLTexture glTex = (GLTexture) tex;
274				//normally we would use GL_LINEAR as magnification filter but sometimes
275				//small text is too filtered and smudged so we use NEAREST -> but this makes
276				//scaled text very ugly and pixelated..
277				if (scalable){
278//					glTex.setFilter(GL.GL_LINEAR, GL.GL_LINEAR);
279					glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.Bilinear);
280				}else{
281//					glTex.setFilter(GL.GL_LINEAR, GL.GL_NEAREST); 
282					glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.NearestNeighbor);
283				}
284			}
285		}
286	}
287
288}