Differences between revisions 1 and 26 (spanning 25 versions)
Revision 1 as of 2008-07-17 01:08:00
Size: 317
Comment:
Revision 26 as of 2008-09-29 17:29:54
Size: 2886
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[TableOfContents]]
Line 2: Line 3:
{{{ === Compiling EMAN2 with FTGL support ===
Line 4: Line 5:
self.render_mode = EMImageMXCore.FTGL To compile EMAN2 with FTGL support set ENABLE_FTGL to ON in cmake setup. This may also require you to install FTGL on your machine. For more information contact an EMAN2 developer.

=== Using FTGL in python ===

Programmers have to create an instance of an EMFTGL object, for instance as a member variable:

{{{#!python
from EMAN2 import EMFTGL
Line 8: Line 16:
self.font_renderer.set_font_mode(FTGLFontMode.TEXTURE)
self.font_renderer.set_font_file_name("/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf")
self.font_renderer.set_depth(32) # only useful if font mode is EXTRUDE
self.font_renderer.set_font_mode(FTGLFontMode.TEXTURE) # or EXTRUDE, PIXMAP, BITMAP, POLYGON or OUTLINE
self.font_renderer.set_font_file_name("/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf") # be wary of platform dependence
}}}
Line 11: Line 21:
A platform independent solution to getting a font renderer is available and is the recommended approach

{{{#!python
from EMAN2 import get_3d_font_renderer
self.font_renderer = get_3d_font_renderer() # loads a platform independent font file that is shipped with EMAN2
self.font_renderer.set_face_size(16) # etc
Line 12: Line 28:

Note that the EMFTGL has default values for all of its parameters and that in general you want to limit the number of times you call the setter functions, because they cause FTGL to regenerate an FTFont class and this is not a trivial expense. However the EMFTGL uses a cache so any FTFonts (which are characterized by face size, depth, display list use, mode, and font file) which have previously been created will automatically be reused (if parameters match).

The EMFTGL cache is not static. This is related to OpenGL contexts and display lists...

Then to render text use a command similar to the following in your paint function:

{{{#!python
glPushMatrix() # FTGL does transformations - must push
self.font_renderer.render_string("Important Information")
glPopMatrix()
}}}

To get the bounding box of the string use:

{{{#!python
bbox = self.font_renderer.bounding_box("Important Information")
}}}

If you want to rotate the font as though it is centered on the origin you might use

{{{#!python
rotation = 45
rot_axis = [1,0,0]
bbox = self.font_renderer.bounding_box(message)

glPushMatrix()
glTranslate(-(bbox[0]-bbox[3])/2,-(bbox[1]-bbox[4])/2,-(bbox[2]-bbox[5])/2)
glRotate(rotation,*rot_axis)
glTranslate((bbox[0]-bbox[3])/2,(bbox[1]-bbox[4])/2,(bbox[2]-bbox[5])/2)
self.font_renderer.render_string(message)
glPopMatrix()
}}}


=== Recommendations ===

TEXTURE fonts are probably the most versatile types. They are fast, antialiased, and can be transformed just like any OpenGL primitive. See http://ftgl.sourceforge.net/docs/html/ftgl-tutorial.html

=== C++ ===

Have a look at the C++ code [http://blake.bcm.edu/eman2/doxygen_html/classEMAN_1_1EMFTGL.html here]

TableOfContents

Compiling EMAN2 with FTGL support

To compile EMAN2 with FTGL support set ENABLE_FTGL to ON in cmake setup. This may also require you to install FTGL on your machine. For more information contact an EMAN2 developer.

Using FTGL in python

Programmers have to create an instance of an EMFTGL object, for instance as a member variable:

   1 from EMAN2 import EMFTGL
   2 self.font_renderer = EMFTGL()
   3 self.font_renderer.set_face_size(16)
   4 self.font_renderer.set_using_display_lists(True)
   5 self.font_renderer.set_depth(32) # only useful if font mode is EXTRUDE
   6 self.font_renderer.set_font_mode(FTGLFontMode.TEXTURE) # or EXTRUDE, PIXMAP, BITMAP, POLYGON or OUTLINE
   7 self.font_renderer.set_font_file_name("/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf") # be wary of platform dependence

A platform independent solution to getting a font renderer is available and is the recommended approach

   1 from EMAN2 import get_3d_font_renderer
   2 self.font_renderer = get_3d_font_renderer() # loads a platform independent font file that is shipped with EMAN2
   3 self.font_renderer.set_face_size(16) # etc

Note that the EMFTGL has default values for all of its parameters and that in general you want to limit the number of times you call the setter functions, because they cause FTGL to regenerate an FTFont class and this is not a trivial expense. However the EMFTGL uses a cache so any FTFonts (which are characterized by face size, depth, display list use, mode, and font file) which have previously been created will automatically be reused (if parameters match).

The EMFTGL cache is not static. This is related to OpenGL contexts and display lists...

Then to render text use a command similar to the following in your paint function:

   1 glPushMatrix() # FTGL does transformations - must push
   2 self.font_renderer.render_string("Important Information")
   3 glPopMatrix()

To get the bounding box of the string use:

   1 bbox = self.font_renderer.bounding_box("Important Information")

If you want to rotate the font as though it is centered on the origin you might use

   1 rotation = 45
   2 rot_axis = [1,0,0]
   3 bbox = self.font_renderer.bounding_box(message)
   4 
   5 glPushMatrix()
   6 glTranslate(-(bbox[0]-bbox[3])/2,-(bbox[1]-bbox[4])/2,-(bbox[2]-bbox[5])/2)
   7 glRotate(rotation,*rot_axis)
   8 glTranslate((bbox[0]-bbox[3])/2,(bbox[1]-bbox[4])/2,(bbox[2]-bbox[5])/2)
   9 self.font_renderer.render_string(message)
  10 glPopMatrix()

Recommendations

TEXTURE fonts are probably the most versatile types. They are fast, antialiased, and can be transformed just like any OpenGL primitive. See http://ftgl.sourceforge.net/docs/html/ftgl-tutorial.html

C++

Have a look at the C++ code [http://blake.bcm.edu/eman2/doxygen_html/classEMAN_1_1EMFTGL.html here]

Eman2UsingFTGL (last edited 2022-02-18 00:29:09 by TunayDurmaz)