We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › TTF Fonts & openGL... FANTASTIC
Page Index Toggle Pages: 1
TTF Fonts & openGL... FANTASTIC (Read 2897 times)
TTF Fonts & openGL... FANTASTIC
Aug 24th, 2008, 3:46pm
 
Hi everyone,
Figured I should make my appearance here on discourse...
I'm currently working on a project that requires a pretty extensive particle system that has 3D text labels that are drawn for each particle.  To my surprise there is really no support for this in processing/openGL.  (My colleague successfully implemented the GLUT library but its either 3 Bitmap fonts or 2 Stroke fonts... yuck)  Also wasnt sure about rendering texture maps dynamically because of the whole aliasing issue...

Finally, I took a look at the geomeritive library available in the libraries section.. sweet goodness a ttf processor!  It even has the ability to mesh out the type down to a list of TRIANGLE_STRIPS... awesome.  So now all it takes is to create a displaylist to preprocess all these points into individual characters... to later be called up dynamically.  Obviously theres a bit of overhead when starting up the sketch... but who cares.  This thing allows you to load up any truetype font you please and render it as pure scalable geometry goodness.  (Still have some issues getting the kerning/tracking straight but its getting there...)


Code:

import geomerative.*;
import java.nio.ByteBuffer;
import com.sun.opengl.util.BufferUtil;

int fontList;
pgl.beginGL();
/////BUILD FONT GEOMETRY
String fontString = " #\"#############0123456789#######ABCDEFGHIJKLMNOPQRSTUVWXYZ######abcdefghijklmnopqrstuvwxyz";
//###'s are placeholders until I get all the right chars in place
RFont font = new RFont(this, "MTBold.ttf", 72, RFont.CENTER);
float charSpace = 30.0; //SPACING OFFSET

fontList = gl.glGenLists(fontString.length());
for(int i = 0; i < fontString.length(); i++){
gl.glNewList(fontList + i, GL.GL_COMPILE);
if(i != 0){//////SPACE CHARACTER
RMesh character = font.toPolygon(fontString.charAt(i)).toMesh();
RStrip[] triangleStrips = character.strips;

for(int j = 0; j < triangleStrips.length; j++){
gl.glBegin(GL.GL_TRIANGLE_STRIP);
RPoint[] pts = triangleStrips[j].getPoints();
for(int k = 0; k < pts.length; k++){
gl.glVertex2f(pts[k].x, pts[k].y);
}
gl.glEnd();
}
float charOffset = character.getBounds().points[3].x - character.getBounds().points[1].x;////GET CHAR BOUNDS TO ADJUST SPACE
//println(charOffset);
gl.glTranslatef(charSpace+(charOffset/3), 0, 0);
gl.glEndList();
}
else{ //////SPACE CHARACTER
gl.glTranslatef(charSpace+10.0, 0, 0);
gl.glEndList();
}
}

pgl.endGL();


/////////////////////////DRAW METHOD

ByteBuffer stringBuffer = BufferUtil.newByteBuffer(256);

void drawText(String string, float _scale, color _color, float _alpha) {

gl.glPushMatrix();
gl.glRotatef(180, 1,0,0);
gl.glScalef(_scale,-_scale,_scale);
gl.glScalef(.02, .02, .02);
gl.glColor4f( red(_color), green(_color), blue(_color), _alpha );
gl.glListBase(fontList-32);

if(stringBuffer.capacity() < string.length()) {
stringBuffer = BufferUtil.newByteBuffer(string.length());
}

stringBuffer.clear();
stringBuffer.put(string.getBytes());
stringBuffer.flip();

// Write The Text To The Screen
gl.glCallLists(string.length(), GL.GL_BYTE, stringBuffer);
gl.glPopMatrix();
}



Have fun,

I'm still pretty much a novice when it comes to openGL... so one question I have is... how would I extrude the characters faces (im guessing its something like... copying the face vertices, offsetting one and running a quadstrip down the extrusion plane...



Re: TTF Fonts & openGL... FANTASTIC
Reply #1 - Feb 9th, 2009, 1:51am
 
are there any updates to this code? i get unexpected token for void drawtext() perhaps you can point me in a helpful direction?

i suspect its one of these babies

Code:

import geomerative.*;
import java.nio.ByteBuffer;
import com.sun.opengl.util.BufferUtil;
Re: TTF Fonts & openGL... FANTASTIC
Reply #2 - Feb 9th, 2009, 5:05pm
 
does this fit you?

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1213559873
Re: TTF Fonts & openGL... FANTASTIC
Reply #3 - Feb 9th, 2009, 9:26pm
 
Probably, looks tasty!
compiles

*fingers crossed*

I'll need to pull this apart to understand it.
thanks in advance
Re: TTF Fonts & openGL... FANTASTIC
Reply #4 - Feb 17th, 2009, 5:46pm
 
Hello,
Thank you for sharing the code!
I tried implementing it and I got the error; The type RRectangle is not visible with a highlight on this line:

float charOffset=character.getBounds().points[3].x-character.getBounds().points[1].x;

But even if I comment it out no text gets displayed.
I put everything inside draw() and drawText() after it in the main tab.
Can anyone help me?
Thank you!

-edit-
I still have the RRectangle problem, but other than that it finally works.
Thanks again!!

Code:
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;

import geomerative.*;
import org.apache.batik.svggen.font.table.*;
import org.apache.batik.svggen.font.*;
import java.nio.ByteBuffer;
import com.sun.opengl.util.BufferUtil;
import processing.opengl.PGraphicsOpenGL;  
import processing.core.*;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import java.util.*;
import java.nio.*;

int fontList;
ByteBuffer stringBuffer;
color c;
PGraphicsOpenGL pgl;
GL gl;
PeasyCam cam;
String fontString;
RFont font;
float charSpace;

void setup(){
 size(300,300,OPENGL);
 RG.init(this);
 fill(255);
 stroke(255);
 stringBuffer = BufferUtil.newByteBuffer(256);
 cam = new PeasyCam(this, 100);
 cam.setMinimumDistance(50);
 cam.setMaximumDistance(500);
 
 /////BUILD FONT GEOMETRY
 fontString = "################0123456789#######ABCDEFGHIJKLMNOPQRSTUVWXYZ######abcdefghijklmnopqrstuvwxyz";
 //###'s are placeholders until I get all the right chars in place
 font = new RFont("AMIENNE_.TTF", 72, RFont.CENTER);
 charSpace = 30.0;  //SPACING OFFSET
 
 pgl = (PGraphicsOpenGL)g;
 gl = pgl.gl;
 
 fontList = gl.glGenLists(fontString.length());
 
 for(int i = 0; i < fontString.length(); i++){
   gl.glNewList(fontList + i, GL.GL_COMPILE);
   if(i != 0){//////SPACE CHARACTER
RMesh character = font.toPolygon(fontString.charAt(i)).toMesh();
RStrip[] triangleStrips = character.strips;
   
for(int j = 0; j < triangleStrips.length; j++){
 gl.glBegin(GL.GL_TRIANGLE_STRIP);
 RPoint[] pts = triangleStrips[j].getPoints();
 for(int k = 0; k < pts.length; k++){
   gl.glVertex2f(pts[k].x, pts[k].y);
 }
 gl.glEnd();
}
//float charOffset = character.getBounds().points[3].x - character.getBounds().points[1].x;////GET CHAR BOUNDS TO ADJUST SPACE
//println(charOffset);
gl.glTranslatef(charSpace+(10), 0, 0);
gl.glEndList();
   }
   else{ //////SPACE CHARACTER
gl.glTranslatef(charSpace+10.0, 0, 0);
gl.glEndList();
   }
 }
// pgl.endGL();
 c = #FFCC00;
}
void draw(){
background(125);

drawText("abcdefg",4,c,0);
}

/////////////////////////DRAW METHOD

void drawText(String string, float _scale, color _color, float _alpha) {

 gl.glPushAttrib(GL.GL_LIST_BIT);
 gl.glPushMatrix();
 //gl.glRotatef(180, 1,0,0);
 //gl.glScalef(_scale,-_scale,_scale);
 //gl.glScalef(.02, .02, .02);
 //gl.glColor4f( red(_color), green(_color), blue(_color), _alpha );
 gl.glListBase(fontList-32);

 if(stringBuffer.capacity() < string.length()) {
   stringBuffer = BufferUtil.newByteBuffer(string.length());
 }

 stringBuffer.clear();
 stringBuffer.put(string.getBytes());
 stringBuffer.flip();

 pgl = (PGraphicsOpenGL)g;
 pgl.beginGL();
 // Write The Text To The Screen
 gl.glCallLists(string.length(), GL.GL_BYTE, stringBuffer);
 gl.glPopMatrix();
 gl.glPopAttrib();
  gl.glEnd();
  pgl.endGL();

}
Page Index Toggle Pages: 1