From 5058b63364a8e8ca71f6e83d8aa7594ea828b007 Mon Sep 17 00:00:00 2001 From: paddywwoof Date: Fri, 26 Oct 2018 11:13:15 +0100 Subject: [PATCH 1/3] resize doesnt change layer and new method added - change_layer --- pi3d/Display.py | 10 +++++++++- pi3d/util/DisplayOpenGL.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pi3d/Display.py b/pi3d/Display.py index 5c8ba753..40a9a1e4 100644 --- a/pi3d/Display.py +++ b/pi3d/Display.py @@ -223,7 +223,11 @@ def resize(self, x=0, y=0, w=0, h=0): self.top = y self.right = x + w self.bottom = y + h - self.opengl.resize(x, y, w, h) + self.opengl.resize(x, y, w, h, self.layer) + + def change_layer(self, layer=0): + self.layer = layer + self.opengl.change_layer(layer) def add_sprites(self, *sprites): """Add one or more sprites to this Display.""" @@ -450,6 +454,9 @@ def create(x=None, y=None, w=None, h=None, near=None, far=None, To use pygame for display surface, mouse and keyboard - as per windows This almost certainly would conflict if attempting to use in combination with tk=True. Default False + *layer* + display layer height - used by dispmanx on Raspberry Pi only. -128 will move the + pi3d window behind the X11 desktop *display_config* Configuration of display - See pi3d.constants for DISPLAY_CONFIG options """ @@ -546,6 +553,7 @@ def update(self): display.top = y display.right = x + w display.bottom = y + h + display.layer = layer display.opengl.create_display(x, y, w, h, depth=depth, samples=samples, layer=layer, display_config=display_config) if PLATFORM == PLATFORM_ANDROID: diff --git a/pi3d/util/DisplayOpenGL.py b/pi3d/util/DisplayOpenGL.py index 748d3a62..f36267a5 100644 --- a/pi3d/util/DisplayOpenGL.py +++ b/pi3d/util/DisplayOpenGL.py @@ -194,7 +194,7 @@ def create_surface(self, x=0, y=0, w=0, h=0, layer=0): #Create viewport opengles.glViewport(0, 0, w, h) - def resize(self, x=0, y=0, w=0, h=0): + def resize(self, x=0, y=0, w=0, h=0, layer=0): # Destroy current surface and native window openegl.eglSwapBuffers(self.display, self.surface) if PLATFORM == PLATFORM_PI: @@ -207,10 +207,17 @@ def resize(self, x=0, y=0, w=0, h=0): bcm.vc_dispmanx_display_close(self.dispman_display) #Now recreate the native window and surface - self.create_surface(x, y, w, h) + self.create_surface(x, y, w, h, layer) elif PLATFORM == PLATFORM_ANDROID: pass #TODO something here + def change_layer(self, layer=0): + if PLATFORM == PLATFORM_PI: + self.dispman_update = bcm.vc_dispmanx_update_start(0) + bcm.vc_dispmanx_element_change_layer(self.dispman_update, + self.dispman_element, layer) + bcm.vc_dispmanx_update_submit_sync(self.dispman_update) + def destroy(self, display=None): if self.active: From f605bff45722cc66b76e0f8bfbd732d563215ca6 Mon Sep 17 00:00:00 2001 From: paddywwoof Date: Tue, 15 Jan 2019 20:39:35 +0000 Subject: [PATCH 2/3] allow less characters in a Font so they can be higher resolution --- ReadMe.rst | 14 +++++++++----- pi3d/util/Font.py | 25 +++++++++++++++---------- pi3d/util/PointText.py | 2 +- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/ReadMe.rst b/ReadMe.rst index 4e603d82..83c6501a 100644 --- a/ReadMe.rst +++ b/ReadMe.rst @@ -155,7 +155,7 @@ Setup on the Raspberry Pi $ sudo apt-get update $ sudo apt-get upgrade $ sudo pip3 install pi3d - $ sudo raspi-config # set gpu_mem=128 + $ sudo raspi-config # set gpu_mem=128 - keep the 'legacy' graphics driver (or ``sudo pip install`` if you want to use python 2 for some reason). @@ -178,7 +178,7 @@ Setup on the Raspberry Pi $ sudo apt-get install python3-pip $ sudo pip3 install pi3d $ sudo pip3 install Pillow - $ sudo raspi-config # set gpu_mem=128 + $ sudo raspi-config # set gpu_mem=128 - keep the 'legacy' graphics driver ####### download demos in their latest form, quicker ####### $ wget https://github.com/pi3d/pi3d_demos/archive/master.zip @@ -190,6 +190,10 @@ Setup on the Raspberry Pi $ cd ~/pi3d_demos $ python3 Earth.py + + **NB pi3d uses the original broadcom OpenGLES2 drivers** (as that's what the GPU hardware + actually runs) so if you use ``raspi-config`` to swap to the emulated + OpenGL driver it will give errors. #. **Download, Extract and install** @@ -245,9 +249,9 @@ Setup on the Raspberry Pi doesn't run on python_3. The better equivalent replacement is Pillow. As of raspbian jessie Pillow is the default imaging library. It's already - installed on the ``fully featured`` SD image, but if it's left off the - jessie lite image (rumoured to be in the pipeline) it can be installed - using the debian package manager:: + installed on the ``fully featured`` SD image, but it's left off the + lite image it can be installed using the debian package manager + (for lite image you also need numpy):: $ sudo apt-get install python3-pil diff --git a/pi3d/util/Font.py b/pi3d/util/Font.py index 9ee5a2cc..2bf6267c 100644 --- a/pi3d/util/Font.py +++ b/pi3d/util/Font.py @@ -27,10 +27,10 @@ class Font(Texture): then creates a table mapping codepoints to subrectangles of that Texture.""" def __init__(self, font, color=(255,255,255,255), codepoints=None, - add_codepoints=None, font_size=42, image_size=512, + add_codepoints=None, font_size=42, image_size=1024, italic_adjustment=1.1, background_color=None, shadow=(0,0,0,255), shadow_radius=0, spacing=None, - mipmap=True, filter=None): + mipmap=True, filter=None, grid_size=16): """Arguments: *font*: File path/name to a TrueType font file. @@ -96,6 +96,10 @@ def __init__(self, font, color=(255,255,255,255), codepoints=None, *filter*: Resulting texture filter option, default None + + *grid_size* + number rows and cols to divide 1024 pixels. For high res fonts this can be + changed 4 -> 16chars, 8 -> 64chars, 10 -> 100chars etc. """ super(Font, self).__init__(font, mipmap=mipmap, filter=filter) self.font = font @@ -113,18 +117,19 @@ def __init__(self, font, color=(255,255,255,255), codepoints=None, if spacing is None: spacing = shadow_radius self.height = ascent + descent + spacing # allow extra pixels if shadow or for certain fonts - self.spacing = 64 - - image_size = self.spacing * 16 # or 1024 TODO this may go wrong if self.height != 64 + self.grid_size = grid_size + num_char = self.grid_size ** 2 + image_size = 1024 # fixed value despite having as argument! TODO allow variable size now grid_size variable + self.spacing = image_size / self.grid_size if codepoints is not None: - codepoints = list(codepoints) + codepoints = list(codepoints)[:num_char] else: - codepoints = list(range(256)) + codepoints = list(range(num_char)) if add_codepoints is not None: add_codepoints = list(add_codepoints) - if (len(codepoints) + len(add_codepoints)) > 256: # make room at end - codepoints = codepoints[:(256 - len(add_codepoints))] + if (len(codepoints) + len(add_codepoints)) > num_char: # make room at end + codepoints = codepoints[:(num_char - len(add_codepoints))] codepoints += add_codepoints is_draw_shadows = shadow_radius > 0 @@ -183,7 +188,7 @@ def __init__(self, font, color=(255,255,255,255), codepoints=None, self.glyph_table[ch] = table_entry xindex += 1 - if xindex >= 16: + if xindex >= self.grid_size: xindex = 0 yindex += 1 diff --git a/pi3d/util/PointText.py b/pi3d/util/PointText.py index ae5b3a31..5cef5772 100644 --- a/pi3d/util/PointText.py +++ b/pi3d/util/PointText.py @@ -51,7 +51,7 @@ def __init__(self, font, camera, max_chars=100, point_size=48): self.text = Points(camera=camera, vertices=self.locations, normals=self.normals, tex_coords=self.uv, point_size=self.point_size) self.text.set_draw_details(self.shader, [self.font]) - self.text.unif[48] = 0.058 # used to hold "patch size" passed to shader + self.text.unif[48] = 0.928 / self.font.grid_size # used to hold "patch size" passed to shader - margin to allow rotating #Reset all characters to space so there are no false character shadows try: glyph = self.font.glyph_table[' '] #u' ' doesn't work on python3.2!! From 843d5aab7f6fe014c8a8e772ae1ac711455fc149 Mon Sep 17 00:00:00 2001 From: paddywwoof Date: Thu, 17 Jan 2019 13:10:21 +0000 Subject: [PATCH 3/3] make default point size vary inv proportion to grid --- pi3d/util/Font.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pi3d/util/Font.py b/pi3d/util/Font.py index 2bf6267c..cf6eb948 100644 --- a/pi3d/util/Font.py +++ b/pi3d/util/Font.py @@ -16,6 +16,11 @@ MAX_SIZE = 1920 +def _strengthen(x): + # used if shadow required to slightly harden edges + F = 0.5 + return int(x * (1 + F - F * x / 256)) + class Font(Texture): """ A Font contains a TrueType font ready to be rendered in OpenGL. @@ -27,7 +32,7 @@ class Font(Texture): then creates a table mapping codepoints to subrectangles of that Texture.""" def __init__(self, font, color=(255,255,255,255), codepoints=None, - add_codepoints=None, font_size=42, image_size=1024, + add_codepoints=None, font_size=None, image_size=1024, italic_adjustment=1.1, background_color=None, shadow=(0,0,0,255), shadow_radius=0, spacing=None, mipmap=True, filter=None, grid_size=16): @@ -103,6 +108,8 @@ def __init__(self, font, color=(255,255,255,255), codepoints=None, """ super(Font, self).__init__(font, mipmap=mipmap, filter=filter) self.font = font + if font_size is None: + font_size = int(672 / grid_size) # i.e. 16x16 has font size 42 try: imgfont = ImageFont.truetype(font, font_size) except IOError: @@ -201,6 +208,7 @@ def __init__(self, font, color=(255,255,255,255), codepoints=None, shadow_img = self._force_color(shadow_img, shadow) shadow_img = shadow_img.filter(ImageFilter.GaussianBlur(radius=shadow_radius)) + shadow_img = Image.eval(shadow_img, _strengthen) # slightly sharpen edge of blur - see func def at top self.im = Image.alpha_composite(shadow_img, self.im)