/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. ***********************************************************************/
  18. package org.mt4j.components.visibleComponents.font;
  19. import java.nio.FloatBuffer;
  20. import javax.media.opengl.GL;
  21. import org.mt4j.components.bounds.IBoundingShape;
  22. import org.mt4j.components.visibleComponents.shapes.MTRectangle;
  23. import org.mt4j.util.MT4jSettings;
  24. import org.mt4j.util.math.Vertex;
  25. import org.mt4j.util.opengl.GLTexture;
  26. import org.mt4j.util.opengl.GLTexture.EXPANSION_FILTER;
  27. import org.mt4j.util.opengl.GLTexture.SHRINKAGE_FILTER;
  28. import org.mt4j.util.opengl.GLTexture.WRAP_MODE;
  29. import processing.core.PApplet;
  30. import processing.core.PGraphics;
  31. import processing.core.PImage;
  32. /**
  33. * The Class BitmapFontCharacter.
  34. * @author Christopher Ruff
  35. */
  36. public class BitmapFontCharacter extends MTRectangle implements IFontCharacter {
  37. /** The unicode. */
  38. private String unicode;
  39. /** The horizontal dist. */
  40. private int horizontalDist;
  41. /** The left offset. */
  42. private int leftOffset;
  43. /**
  44. * Instantiates a new bitmap font character.
  45. * @param applet the applet
  46. * @param texture the texture
  47. * @param unicode the unicode
  48. * @param leftOffset the left offset
  49. * @param topOffset the top offset
  50. * @param horizontalAdvance the horizontal advance
  51. */
  52. public BitmapFontCharacter(PApplet applet, PImage texture, String unicode, int leftOffset, int topOffset, int horizontalAdvance) {
  53. super(applet, new Vertex(leftOffset, topOffset,0), texture.width, texture.height);
  54. //hm..this is for the card loading, because
  55. //when we init gl texture in other thread it breaks..
  56. // this.setUseDirectGL(false);
  57. // this.setUseDirectGL(true);
  58. this.setTexture(texture);
  59. this.setTextureEnabled(true);
  60. this.leftOffset = leftOffset;
  61. this.horizontalDist = horizontalAdvance;
  62. this.unicode = unicode;
  63. this.setNoStroke(true);
  64. this.setPickable(false);
  65. if (MT4jSettings.getInstance().isOpenGlMode()){
  66. //Set the texture to be non-repeating but clamping to the border to avoid artefacts
  67. PImage tex = this.getTexture();
  68. if (tex instanceof GLTexture) {
  69. GLTexture glTex = (GLTexture) tex;
  70. // glTex.setWrap(GL.GL_CLAMP, GL.GL_CLAMP);
  71. // glTex.setWrap(GL.GL_CLAMP_TO_EDGE, GL.GL_CLAMP_TO_EDGE);
  72. glTex.setWrapMode(WRAP_MODE.CLAMP_TO_EDGE, WRAP_MODE.CLAMP_TO_EDGE);
  73. // glTex.setFilter(SHRINKAGE_FILTER.Trilinear, EXPANSION_FILTER.Bilinear);
  74. glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.Bilinear);
  75. // glTex.setFilter(SHRINKAGE_FILTER.NearestNeighborNoMipMaps, EXPANSION_FILTER.NearestNeighbor);
  76. // glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.NearestNeighbor);
  77. }
  78. }
  79. }
  80. @Override
  81. public void drawComponent(PGraphics g) {
  82. //Draw the shape
  83. if (MT4jSettings.getInstance().isOpenGlMode() && this.isUseDirectGL()){
  84. super.drawComponent(g);
  85. }else{ //Draw with pure proccessing commands...
  86. g.strokeWeight(this.getStrokeWeight());
  87. if (this.isNoStroke())
  88. g.noStroke();
  89. drawWithProcessing(g);
  90. if (/*MT4jSettings.getInstance().isOpenGlMode() &&*/ this.isDrawSmooth())
  91. g.noSmooth(); //because of tesselation bug/lines visibile in shapes
  92. }
  93. }
  94. /* (non-Javadoc)
  95. * @see org.mt4j.components.visibleComponents.font.IFontCharacter#drawComponent(javax.media.opengl.GL)
  96. */
  97. //@Override
  98. public void drawComponent(GL gl) {
  99. // this.drawPureGl(gl);
  100. // /*
  101. if (MT4jSettings.getInstance().isOpenGlMode()){
  102. if (this.isUseDisplayList() && this.getGeometryInfo().getDisplayListIDs()[0] != -1){
  103. gl.glCallList(this.getGeometryInfo().getDisplayListIDs()[0]);
  104. // gl.glCallList(this.getGeometryInfo().getDisplayListIDs()[1]); //Outline rectangle
  105. }else{
  106. this.drawPureGl(gl);
  107. }
  108. }
  109. // */
  110. }
  111. protected void drawPureGl(GL gl){
  112. // /*
  113. //Get display array/buffer pointers
  114. FloatBuffer tbuff = this.getGeometryInfo().getTexBuff();
  115. FloatBuffer vertBuff = this.getGeometryInfo().getVertBuff();
  116. //Enable Pointers, set vertex array pointer
  117. gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
  118. if (this.isUseVBOs()){//Vertices
  119. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOVerticesName());
  120. gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
  121. }else{
  122. gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertBuff);
  123. }
  124. //Default texture target
  125. int textureTarget = GL.GL_TEXTURE_2D;
  126. /////// DRAW SHAPE ///////
  127. if (!this.isNoFill()){
  128. boolean textureDrawn = false;
  129. if (this.isTextureEnabled()
  130. && this.getTexture() != null
  131. && this.getTexture() instanceof GLTexture) //Bad for performance?
  132. {
  133. GLTexture tex = (GLTexture)this.getTexture();
  134. textureTarget = tex.getTextureTarget();
  135. //tells opengl which texture to reference in following calls from now on!
  136. //the first parameter is eigher GL.GL_TEXTURE_2D or ..1D
  137. gl.glEnable(textureTarget);
  138. gl.glBindTexture(textureTarget, tex.getTextureID());
  139. gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
  140. if (this.isUseVBOs()){//Texture
  141. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOTextureName());
  142. gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);
  143. }else
  144. gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, tbuff);
  145. textureDrawn = true;
  146. }
  147. //Normals
  148. if (this.getGeometryInfo().isContainsNormals()){
  149. gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
  150. if (this.isUseVBOs()){
  151. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBONormalsName());
  152. gl.glNormalPointer(GL.GL_FLOAT, 0, 0);
  153. }else{
  154. gl.glNormalPointer(GL.GL_FLOAT, 0, this.getGeometryInfo().getNormalsBuff());
  155. }
  156. }
  157. gl.glDrawArrays(this.getFillDrawMode(), 0, vertBuff.capacity()/3);
  158. if (this.getGeometryInfo().isContainsNormals()){
  159. gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
  160. }
  161. if (textureDrawn){
  162. gl.glBindTexture(textureTarget, 0);//Unbind texture
  163. gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
  164. gl.glDisable(textureTarget); //weiter nach unten?
  165. }
  166. }
  167. gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
  168. //TEST
  169. if (this.isUseVBOs()){
  170. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
  171. gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
  172. }
  173. // */
  174. }
  175. @Override
  176. protected void setDefaultGestureActions() {
  177. //no gestures
  178. }
  179. /* (non-Javadoc)
  180. * @see org.mt4j.components.visibleComponents.shapes.MTRectangle#computeDefaultBounds()
  181. */
  182. //@Override
  183. protected IBoundingShape computeDefaultBounds() {
  184. //We assume that font characters never get picked or anything
  185. //and hope the creation speeds up by not calculating a bounding shape
  186. return null;
  187. }
  188. /* (non-Javadoc)
  189. * @see org.mt4j.components.visibleComponents.font.IFontCharacter#getHorizontalDist()
  190. */
  191. //@Override
  192. public int getHorizontalDist() {
  193. return this.horizontalDist;
  194. }
  195. /**
  196. * Sets the horizontal dist.
  197. *
  198. * @param horizontalDist the new horizontal dist
  199. */
  200. public void setHorizontalDist(int horizontalDist) {
  201. this.horizontalDist = horizontalDist;
  202. }
  203. /* (non-Javadoc)
  204. * @see org.mt4j.components.visibleComponents.font.IFontCharacter#getUnicode()
  205. */
  206. //@Override
  207. public String getUnicode() {
  208. return this.unicode;
  209. }
  210. /**
  211. * Sets the unicode.
  212. *
  213. * @param unicode the new unicode
  214. */
  215. public void setUnicode(String unicode) {
  216. this.unicode = unicode;
  217. }
  218. public int getLeftOffset() {
  219. return this.leftOffset;
  220. }
  221. //FIXME TEST
  222. public void setTextureFiltered(boolean scalable) {
  223. if (MT4jSettings.getInstance().isOpenGlMode()){
  224. PImage tex = this.getTexture();
  225. if (tex instanceof GLTexture) {
  226. GLTexture glTex = (GLTexture) tex;
  227. //normally we would use GL_LINEAR as magnification filter but sometimes
  228. //small text is too filtered and smudged so we use NEAREST -> but this makes
  229. //scaled text very ugly and pixelated..
  230. if (scalable){
  231. // glTex.setFilter(GL.GL_LINEAR, GL.GL_LINEAR);
  232. glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.Bilinear);
  233. }else{
  234. // glTex.setFilter(GL.GL_LINEAR, GL.GL_NEAREST);
  235. glTex.setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps, EXPANSION_FILTER.NearestNeighbor);
  236. }
  237. }
  238. }
  239. }
  240. }