Skip to content
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

Merged
merged 32 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f2a2c78
commiting changes made during magnum text PR merge
jrreyna Sep 13, 2022
825a08e
updated settings.py and imported examples.settings instead of habitat…
jrreyna Sep 13, 2022
01bc510
updating submodules
jrreyna Sep 13, 2022
1f46fd1
importing text from magnum now works
jrreyna Sep 14, 2022
947475b
default to ycb dataset and simple_room scene when you run 'python exa…
jrreyna Sep 14, 2022
dd69059
wrote static string on top left corner of window
jrreyna Sep 14, 2022
4c613b9
removed redundant comments
jrreyna Sep 14, 2022
e4365ba
whoops, forgot to add the settings.py file to the commit list
jrreyna Sep 14, 2022
b08ddfd
static string displayed, but using the format template that is in the…
jrreyna Sep 14, 2022
f5054a6
changed in-window text background from black to transparent
jrreyna Sep 14, 2022
d7d1544
removed some placeholder text from the magnum in-window text until we…
jrreyna Sep 14, 2022
c3812e2
cleaned up variable names, improved style a bit, and added comments
jrreyna Sep 15, 2022
343dd37
restored ./examples/settings.py and now use habitat_sim.utils.setting…
jrreyna Sep 15, 2022
3181d00
removed redundant comments from 'src_python/habitat_sim/utils/setting…
jrreyna Sep 15, 2022
2cafaf8
added True Type fonts to examples directory
jrreyna Sep 15, 2022
eb3938a
fixed a typo
jrreyna Sep 15, 2022
3b49ab7
added a comment
jrreyna Sep 15, 2022
1c95c26
removed magnum.text.DistanceFieldGlyphCache constructor as it wasn't …
jrreyna Sep 15, 2022
c1092f2
adjusting display font and rendering parameters to look more like wha…
jrreyna Sep 16, 2022
8aa8601
removed some unnecessary fonts from the examples directory
jrreyna Sep 16, 2022
4660eb5
Update examples/viewer.py with simpler glyph representation
jrreyna Sep 19, 2022
cad8e5e
used simpler arguments for the fill_glyph_cache function call
jrreyna Sep 20, 2022
78f176a
resolving a merge conflict when I commited a change on github vs via …
jrreyna Sep 20, 2022
641ca4c
removed ttf font from the directories storing viewer.py and viewer.cp…
jrreyna Sep 23, 2022
1124958
removed a semicolon I added by accident
jrreyna Sep 24, 2022
a5c5dd8
updated the resources.conf file in the viewer.cpp directory to point …
jrreyna Sep 24, 2022
77d9c7f
added file alias in resources.conf so that I didn't have to use an ug…
jrreyna Sep 26, 2022
eefc880
Merge branch 'main' of https://github.com/facebookresearch/habitat-si…
jrreyna Sep 27, 2022
c6557c8
deleted unused fonts, updated README.txt accordingly and to give an e…
jrreyna Sep 27, 2022
c5133da
added comment
jrreyna Sep 27, 2022
57fe418
forgot to add a variable to example in README
jrreyna Sep 27, 2022
a882963
Merge branch 'main' of https://github.com/facebookresearch/habitat-si…
jrreyna Sep 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
100 changes: 100 additions & 0 deletions data/fonts/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
----------------------------------------------------------------------
Copy link
Collaborator

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 credit

ProggyClean.ttf by Tristan Grimmer

somewhere, although I don't know if there's even such a place in the docs or README.

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)
108 changes: 104 additions & 4 deletions examples/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ctypes
import math
import os
import string
import sys
import time
from enum import Enum
Expand All @@ -15,6 +16,7 @@

import magnum as mn
import numpy as np
from magnum import shaders, text
from magnum.platform.glfw import Application

import habitat_sim
Expand All @@ -25,12 +27,31 @@


class HabitatSimInteractiveViewer(Application):

# the maximum number of chars displayable in the app window
# using the magnum text module. These chars are used to
# display the CPU/GPU usage data
MAX_DISPLAY_TEXT_CHARS = 256

# how much to displace window text relative to the center of the
# app window (e.g if you want the display text in the top left of
# the app window, you will displace the text
# window width * -TEXT_DELTA_FROM_CENTER in the x axis and
# widnow height * TEXT_DELTA_FROM_CENTER in the y axis, as the text
# position defaults to the middle of the app window)
TEXT_DELTA_FROM_CENTER = 0.49

# font size of the magnum in-window display text that displays
# CPU and GPU usage info
DISPLAY_FONT_SIZE = 16.0

def __init__(self, sim_settings: Dict[str, Any]) -> None:
configuration = self.Configuration()
configuration.title = "Habitat Sim Interactive Viewer"
Application.__init__(self, configuration)
self.sim_settings: Dict[str:Any] = sim_settings
self.fps: float = 60.0

# draw Bullet debug line visualizations (e.g. collision meshes)
self.debug_bullet_draw = False
# draw active contact point debug line visualizations
Expand Down Expand Up @@ -73,6 +94,60 @@ def __init__(self, sim_settings: Dict[str, Any]) -> None:
key.Z: "move_up",
}

# Load a TrueTypeFont plugin and open the font file
self.display_font = text.FontManager().load_and_instantiate("TrueTypeFont")
relative_path_to_font = "../data/fonts/ProggyClean.ttf"
self.display_font.open_file(
os.path.join(os.path.dirname(__file__), relative_path_to_font),
13,
)

# Glyphs we need to render everything
self.glyph_cache = text.GlyphCache(mn.Vector2i(256))
self.display_font.fill_glyph_cache(
self.glyph_cache,
string.ascii_lowercase
+ string.ascii_uppercase
+ string.digits
+ ":-_+,.! %µ",
)

# magnum text object that displays CPU/GPU usage data in the app window
self.window_text = text.Renderer2D(
self.display_font,
self.glyph_cache,
HabitatSimInteractiveViewer.DISPLAY_FONT_SIZE,
text.Alignment.TOP_LEFT,
)
self.window_text.reserve(HabitatSimInteractiveViewer.MAX_DISPLAY_TEXT_CHARS)

# text object transform in window space is Projection matrix times Translation Matrix
# put text in top left of window
self.window_text_transform = mn.Matrix3.projection(
mn.Vector2(self.viewport_size)
) @ mn.Matrix3.translation(
mn.Vector2(
self.viewport_size[0]
* -HabitatSimInteractiveViewer.TEXT_DELTA_FROM_CENTER,
self.viewport_size[1]
* HabitatSimInteractiveViewer.TEXT_DELTA_FROM_CENTER,
)
)
self.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
)

# variables that track app data and CPU/GPU usage
self.num_frames_to_track = 60

# Cycle mouse utilities
self.mouse_interaction = MouseMode.LOOK
self.mouse_grabber: Optional[MouseGrabber] = None
Expand Down Expand Up @@ -195,6 +270,9 @@ def draw_event(
self.render_camera.render_target.blit_rgba_to_default()
mn.gl.default_framebuffer.bind()

# draw CPU/GPU usage data and other info to the app window
self.draw_text(self.render_camera.specification())

self.swap_buffers()
Timer.next_frame()
self.redraw()
Expand Down Expand Up @@ -733,6 +811,27 @@ def exit_event(self, event: Application.ExitEvent):
event.accepted = True
exit(0)

def draw_text(self, sensor_spec):
self.shader.bind_vector_texture(self.glyph_cache.texture)
self.shader.transformation_projection_matrix = self.window_text_transform
self.shader.color = [1.0, 1.0, 1.0]

sensor_type_string = str(sensor_spec.sensor_type.name)
sensor_subtype_string = str(sensor_spec.sensor_subtype.name)
if self.mouse_interaction == MouseMode.LOOK:
mouse_mode_string = "LOOK"
elif self.mouse_interaction == MouseMode.GRAB:
mouse_mode_string = "GRAB"
self.window_text.render(
f"""
{self.fps} FPS
Sensor Type: {sensor_type_string}
Sensor Subtype: {sensor_subtype_string}
Mouse Interaction Mode: {mouse_mode_string}
"""
)
self.shader.draw(self.window_text.mesh)

def print_help_text(self) -> None:
"""
Print the Key Command help text.
Expand Down Expand Up @@ -916,16 +1015,16 @@ def next_frame() -> None:
# optional arguments
parser.add_argument(
"--scene",
default="NONE",
default="./data/test_assets/scenes/simple_room.glb",
type=str,
help='scene/stage file to load (default: "NONE")',
help='scene/stage file to load (default: "./data/test_assets/scenes/simple_room.glb")',
)
parser.add_argument(
"--dataset",
default="default",
default="./data/objects/ycb/ycb.scene_dataset_config.json",
type=str,
metavar="DATASET",
help="dataset configuration file to use (default: default)",
help='dataset configuration file to use (default: "./data/objects/ycb/ycb.scene_dataset_config.json")',
)
parser.add_argument(
"--disable_physics",
Expand All @@ -947,4 +1046,5 @@ def next_frame() -> None:
sim_settings["enable_physics"] = not args.disable_physics
sim_settings["stage_requires_lighting"] = args.stage_requires_lighting

# start the application
HabitatSimInteractiveViewer(sim_settings).exec()
3 changes: 2 additions & 1 deletion src/utils/viewer/resources.conf
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid having to hardcode the ugly relative path in viewer.cpp, add an alias here (docs):

Suggested change
filename=../../../data/fonts/ProggyClean.ttf
filename=../../../data/fonts/ProggyClean.ttf
alias=ProggyClean.ttf

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
12 changes: 6 additions & 6 deletions src_python/habitat_sim/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"default_agent": 0,
"sensor_height": 1.5,
"hfov": 90,
"color_sensor": True, # RGB sensor (default: ON)
"semantic_sensor": False, # semantic sensor (default: OFF)
"depth_sensor": False, # depth sensor (default: OFF)
"ortho_rgba_sensor": False, # Orthographic RGB sensor (default: OFF)
"ortho_depth_sensor": False, # Orthographic depth sensor (default: OFF)
"ortho_semantic_sensor": False, # Orthographic semantic sensor (default: OFF)
"color_sensor": True,
Skylion007 marked this conversation as resolved.
Show resolved Hide resolved
"semantic_sensor": False,
"depth_sensor": False,
"ortho_rgba_sensor": False,
"ortho_depth_sensor": False,
"ortho_semantic_sensor": False,
"fisheye_rgba_sensor": False,
"fisheye_depth_sensor": False,
"fisheye_semantic_sensor": False,
Expand Down