-
Notifications
You must be signed in to change notification settings - Fork 427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Viewer py add magnum text #1862
Changes from all commits
f2a2c78
825a08e
01bc510
1f46fd1
947475b
dd69059
4c613b9
e4365ba
b08ddfd
f5054a6
d7d1544
c3812e2
343dd37
3181d00
2cafaf8
eb3938a
3b49ab7
1c95c26
c1092f2
8aa8601
4660eb5
cad8e5e
78f176a
641ca4c
1124958
a5c5dd8
77d9c7f
eefc880
c6557c8
c5133da
57fe418
a882963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
---------------------------------------------------------------------- | ||
habitat-sim/data/fonts/README.txt | ||
This is the Readme dedicated to fonts.Right now just credits | ||
Tristan Grimmer for 'ProggyClean.ttf'and gives and example | ||
of how to use it in python with Magnum | ||
---------------------------------------------------------------------- | ||
|
||
--------------------------------------- | ||
CREDITS/LICENSES FOR FONTS INCLUDED IN THIS FOLDER | ||
--------------------------------------- | ||
|
||
ProggyClean.ttf | ||
|
||
Copyright (c) 2004, 2005 Tristan Grimmer | ||
MIT License | ||
recommended loading setting: Size = 13.0, DisplayOffset.Y = +1 | ||
http://www.proggyfonts.net/ | ||
|
||
--------------------------------------- | ||
FONTS PYTHON EXAMPLE WITH MAGNUM | ||
--------------------------------------- | ||
import string | ||
import magnum as mn | ||
from magnum import shaders, text | ||
|
||
. | ||
. | ||
. | ||
|
||
DISPLAY_FONT_SIZE = 16.0 | ||
viewport_size: mn.Vector2i = mn.gl.default_framebuffer.viewport.size() | ||
|
||
# how much to displace window text relative to the center of the | ||
# app window | ||
TEXT_DELTA_FROM_CENTER = 0.5 | ||
|
||
# the maximum number of chars displayable in the app window | ||
# using the magnum text module. | ||
MAX_DISPLAY_TEXT_CHARS = 256 | ||
|
||
# Load a TrueTypeFont plugin and open the font file | ||
display_font = text.FontManager().load_and_instantiate("TrueTypeFont") | ||
relative_path_to_font = "../data/fonts/ProggyClean.ttf" | ||
display_font.open_file( | ||
os.path.join(os.path.dirname(__file__), relative_path_to_font), | ||
13, | ||
) | ||
|
||
# Glyphs we need to render everything | ||
glyph_cache = text.GlyphCache(mn.Vector2i(256)) | ||
display_font.fill_glyph_cache( | ||
glyph_cache, | ||
string.ascii_lowercase | ||
+ string.ascii_uppercase | ||
+ string.digits | ||
+ ":-_+,.! %µ", | ||
) | ||
|
||
# magnum text object that displays CPU/GPU usage data in the app window | ||
window_text = text.Renderer2D( | ||
display_font, | ||
glyph_cache, | ||
DISPLAY_FONT_SIZE, | ||
text.Alignment.TOP_LEFT, | ||
) | ||
window_text.reserve(MAX_DISPLAY_TEXT_CHARS) | ||
|
||
# text object transform in window space is Projection matrix times Translation Matrix | ||
window_text_transform = mn.Matrix3.projection( | ||
mn.Vector2(viewport_size) | ||
) @ mn.Matrix3.translation( | ||
mn.Vector2( | ||
viewport_size[0] | ||
* -TEXT_DELTA_FROM_CENTER, | ||
viewport_size[1] | ||
* TEXT_DELTA_FROM_CENTER, | ||
) | ||
) | ||
shader = shaders.VectorGL2D() | ||
|
||
# make magnum text background transparent | ||
mn.gl.Renderer.enable(mn.gl.Renderer.Feature.BLENDING) | ||
mn.gl.Renderer.set_blend_function( | ||
mn.gl.Renderer.BlendFunction.ONE, | ||
mn.gl.Renderer.BlendFunction.ONE_MINUS_SOURCE_ALPHA, | ||
) | ||
mn.gl.Renderer.set_blend_equation( | ||
mn.gl.Renderer.BlendEquation.ADD, mn.gl.Renderer.BlendEquation.ADD | ||
) | ||
|
||
# draw text | ||
shader.bind_vector_texture(glyph_cache.texture) | ||
shader.transformation_projection_matrix = window_text_transform | ||
shader.color = [1.0, 1.0, 1.0] | ||
window_text.render( | ||
f""" | ||
Hello World | ||
""" | ||
) | ||
shader.draw(window_text.mesh) |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||
group=fonts | ||||||||
|
||||||||
[file] | ||||||||
filename=ProggyClean.ttf | ||||||||
filename=../../../data/fonts/ProggyClean.ttf | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid having to hardcode the ugly relative path in
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oo that's slick. Thanks, I wasn't exactly sure how the resources.conf file worked |
||||||||
alias=ProggyClean.ttf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove everything from this directory except for the
ProggyClean.ttf
. Unnecessary and irrelevant now that the font file is hardcoded anyway. Tho it might be good to creditsomewhere, although I don't know if there's even such a place in the docs or README.