using font as a texture on a 3d surface

apologies for the simpleness, i did a search but couldnt find anything to suit my needs.

i need to make a simple cube and have a letter showing on one face, the letter needs to change freely. i understand .ttf files are basically a picture with individual texture coordinates mapped out, but how can i access each individual slice and map it to a primitive surface?

not much of a graphics programmer me.

I think the fastest way would be drawing your fonts into ofFbo objects (so you can change them dynamically) and use their textures (yourFBO.getTextureReference()) on your cube/mesh/whatever

i have a little addon called ofxWordPalette which let’s you specify a list of input words, it renders them to an fbo canvas, and then you can draw them and get texture coordinates for each word. Maybe not exactly what you need, but it gives a specific example of what you’d trying to do:

https://github.com/FlightPhase/ofxWordPalette

You can also take a look at this discussion, there might be some useful stuff that NickHardemann made in here: http://forum.openframeworks.cc/t/extrude-text-into-3d/6938/0

thank you chaps, some stuff to try there. i was hoping though to be able to load a ttf as a texture, then somehow query it for which texture coordinates based on a character. i could do it by making an array of texture coordinates then retrieving them in a readable way but i thought this sort of thing may have been done before, i thought this sort of data would be held within a ttf?

edit: FTGL has a charmap?

http://ftgl.sourceforge.net/docs/html/ftgl-tutorial.html

i could do with using that as somekind of tex coord for a surface on my primitive? i dunno, im at work and away from my machine.

I think I can summarise my problem into the question, “how do I access glyph metrics within a font file”?

This page does what im trying to do http://home.pacific.net.hk/~edx/texfont.htm so i think its possible.

if you just want to use a char as a texture, the easiest right now as naus3a said is drawing it to an fbo and using the fbo as a texture.

Internally ofTrueTypeFont creates a texture atlas (a big texture with all the fonts) and then creates quads with vertexes and texture coordinates to draw each char in a string in the right position.

It could be useful to be able to get access to that texture atlas and the right texture coordinates for each char but that doesn’t exist yet and want be much different from drawing the char to an fbo except it will be faster

You don’t really need to access the glyph metrics for this but if you want to access them take a look at how ofTrueTypeFont works internally, you’ll need to use FreeType directly for that.

[quote=“arturo, post:7, topic:8153”]
if you just want to use a char as a texture, the easiest right now as naus3a said is drawing it to an fbo and using the fbo as a texture.

Internally ofTrueTypeFont creates a texture atlas (a big texture with all the fonts) and then creates quads with vertexes and texture coordinates to draw each char in a string in the right position.

It could be useful to be able to get access to that texture atlas and the right texture coordinates for each char but that doesn’t exist yet and want be much different from drawing the char to an fbo except it will be faster

You don’t really need to access the glyph metrics for this but if you want to access them take a look at how ofTrueTypeFont works internally, you’ll need to use FreeType directly for that.
[/quote] cool, ill give the drawing to fbo a go, cheers.

ok im having problems with the fbo, i can’t find any documentation or an example that is trying to do what im doing. i tried editing the ofbox example and just got what looked like a screenshot of what was in my web browser at the time. ive set up this empty example so i could paste it in and ask if theres anything im doing wrong.

  
  
void testApp::setup(){  
	verdana14.loadFont("verdana.ttf", 14, true, true);  
	someText = "some text";  
	bounds = verdana14.getStringBoundingBox(someText, 0, 0);  
	  
	fbo1.allocate(bounds.width, bounds.height);  
	fbo1.begin();  
		ofSetColor(255);  
		verdana14.drawString(someText, 0, bounds.height);  
	fbo1.end();  
	tex1=fbo1.getTextureReference();  
  
void testApp::draw(){  
	ofBackground(112, 112, 112);  
	ofSetColor(255, 255, 255);  
	tex1.draw(0,100);  
}  
}  
  

this nearly works but i still get some junk pixels.

You can just draw the FBO directly, also I think you need to call ofClear() in between ofFbo.begin() and ofFbo.end() to avoid anything else getting glommed into your fbo.

  
ofEnableAlphaBlending(); // make sure we can alpha blend  
	fbo1.allocate(bounds.width, bounds.height);    
	fbo1.begin();   
	ofClear(0, 0, 0, 0); // clear it out  
        ofSetColor(255);   
        verdana14.drawString(someText, 0, bounds.height);  // this causes some stretching  
	fbo1.end();    
  

hope that helps.

1 Like

sweeeeeet. cheers mate, look out world i got cubes with chars on them!