Page 1 of 1

draw text to an image or a texture?

Posted: Thu May 15, 2008 10:57 pm
by ayman
Hi there,

in order to avoid drawing a text every frame to the screen again and again I want to draw text to a texture and display this texture instead. So I have to redraw that texture only if the text changes.

But I have no idea how to draw text to an image or a texture with Irrlicht.

I suppose I have to ...
#1. create an image in memory,
#2. draw the text to it
#3. convert the image to a texture (or copy the image's pixel-buffer to the texture's pixel/image-buffer) and
#4. tell an already displayed quad or billboard to use this texture or tell the system that the texture has been changed.

Problems:
regarding #1:
No CImage- or CTexture class found.

regarding #2:
Today I draw text with font->draw() to the screen, where font is of type gui::IGUIFont*. But this "draw" method has no parameter to indicate a target image buffer. So I suppose it is set somewhere in this strange guienvironment-thingy, but I realy have no clue how that works. Maybe it's in the video driver? (Thats the problem with a class library in c++: things that belong to each other are never easy to find using the API-docs ...)
However: Found an IImage and an ITexture. But while looking into the API docs, both seem to have no pixel- or graphicsbuffer to hold the actual image. Well, they are just interfaces. So where is the image buffer?

regarding #3:
absolutely no idea. I thought there should be a constructor to create a texture from an image ... wouldn't that be a likely approach?

regarding #4:
Well, I hoped I could find something like it in the 13.RenderToTexture example, but no, I didn't.

Any hint to solve any of my four problems is most welcome.

Ayman

Posted: Fri May 16, 2008 9:11 am
by sp00n
and there is also IGUIImage :)
search the forum and API, there is a method:

Code: Select all

virtual IImage* irr::video::IVideoDriver::createImageFromData  (  ECOLOR_FORMAT  format,  
  const core::dimension2d< s32 > &  size,  
  void *  data,  
  bool  ownForeignMemory = false,  
  bool  deleteMemory = true 
 ) 

Posted: Fri May 16, 2008 9:44 am
by JP
The render to texture example is all you really need... render the text into the texture and then display the texture in the text's place, when the text changes, update the displayed texture.

Posted: Fri May 30, 2008 1:46 pm
by ayman
Hi again

thanks for the answers.

@sp00n: I will look into createImageFromData()

@JP: I have no clue how to render a text into a texture. Do you have an example?

Posted: Fri May 30, 2008 1:51 pm
by rogerborg
You have an example.

It's cunningly hidden in your Irrlicht sdk

examples/13.RenderToTexture

The examples should be your first port of call for any "how do I?" question.

Posted: Fri May 30, 2008 2:31 pm
by ayman
Hi

@rogerborg: Thanks for you ironical hint.
I was under the impression that it should be possible to draw a font to an image or texture and use just 2D-methods to acomplish this task.
I still can't believe that I have to use a camera to take a 2d-picture of a 3d-world to move a 2d-graphic (some text drawn using a font) into a 2d-texture. That sounds like overkill for a rather simple task to me. You're realy implying that?

Posted: Fri May 30, 2008 3:11 pm
by BlindSide
No one is implying that, you can use the GUI functions on the render target texture just as anything else eg.

driver->setRenderTarget(MyRenderTargetTexture);
guienviroment->drawAll();

And you're done! (Make sure to do it inside a beginScene/endScene area).

Cheers

Posted: Fri May 30, 2008 3:21 pm
by rogerborg
If you use any of Irrlicht's '2D' methods, then they'll render a textured quad anyway.

Getting the text into the texture could be easier though. Ideally we'd want something like a IGUIFont::drawToTexture() method that just blits the font bitmaps into the target texture, but I suffered an EPIC FAIL trying to hack one up over lunch. Sorry. :(