User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Fri Nov 01, 2019 10:27 am

@JeDy111, that is exactly what I would like to be able to do as if follows what pi3d did on previous versions of the RPi. The code was started from the examples in /opt/vc/ which showed how to use the libELG.so and libGLESv2.so libraries for fast 3D rendering. However on the RPi4 these examples just give a message to the effect that "this doesn't work on the RPi v 4" a result which was reinforced by hosts of threads on this forum from people trying to get OpenGL projects working again on the RPi4. The /opt/vc/ hello_dispmanx example works but it doesn't use any OpenGL.

If you can find any pointers to how to get this working that would be great. Though the X11 route does make for more transportable code.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Fri Nov 01, 2019 11:18 am

I have looked for pointers on how to use DispmanX with OpenGL/ES but my searches lead to the same conclusions as yours.

It seems that DispmanX is being replaced by KMS/DRM.

Regarding KMS/DRM, there is a thread on which 6by9 mentions https://gitlab.freedesktop.org/mesa/kmscube/ as a starting point:

https://www.raspberrypi.org/forums/view ... 7#p1497481
6by9 wrote:kmscube is generally the simplest example of using GL without X. Again you need to invoke it as "kmscube -D /dev/dri/card1" to make it choose the right node.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Fri Nov 01, 2019 12:20 pm

Yes, that probably is the case :( The KMS,DRM route leads to the C example on my earlier post... but converting that to python ctypes looks pretty tough. However the use of libGLX seems a bit more feasible and the C version seemed reasonably easy to get working on the RPi4 so I will try to use that first, and resort to KMS, DRM if it fails.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7416
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi3D transparent background on Pi4

Fri Nov 01, 2019 1:01 pm

I have recently learnt of the existence of libliftoff and liboutput as lightweight helper libraries for setting stuff up in DRM/KMS. I haven't used either of them myself, but they might warrant a little investigation.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Fri Nov 01, 2019 1:41 pm

Interesting. Will definitely follow up.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Sun Nov 03, 2019 11:04 pm

Yoohoo, result! pi3d X11 window (on ubuntu laptop but should transfer to Pi4)
scrshot.jpg
scrshot.jpg (111.61 KiB) Viewed 707 times
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Mon Nov 04, 2019 7:52 am

\o/ Great! Thanks a bunch paddyg, and also 6by9

Does it use libliftoff?
is it possible to create the display with dimensions and position on the screen? If so, is it in a window?

Looking forward playing with it on a PI4 :D

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Mon Nov 04, 2019 10:54 am

It's using the xlib method that @6by9 pointed me at. i.e. libGLX.so and libXrender.so.1 both of which seem to be available on RPi4 and can give a transparent background to a normal X11 window (with xcompmgr running). It should be possible to position windows by giving the X11 server a 'hint' though I think it's not guaranteed to actually put it where you want it! Will do some tests on that.

However... pushing the modified code to github.com/tipam/pi3d develop branch I find that it doesn't 'just work' on the RPi. I can get the transparent window but nothing rendered in it! So I will make a non-fixed pipeline version of the C example and see if I can narrow down the point that GL+Xrender extension+GLX on laptop diverges from RPi4.

Annoying but hopefully soluble.

PS if you want to mess around with it on a laptop or RPi you need to clone the pi3d repository from github and switch to the develop branch. The github/pi3d/pi3d_demo repository already uses a demo.py file that 'points' to the location of pi3d if you have a local copy of the repo. Then in the program you need to add a use_glx argument

Code: Select all

...
import demo
import pi3d
DISPLAY = pi3d.Display.create(w=400, h=300, frames_per_second=10,
                                              background=(0.0, 0.0, 0.0, 0.0), use_glx=True)
...
You need to start xcompmgr on the RPi4 but on ubuntu laptop compiz is already running and will do the compositing automatically.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Mon Nov 04, 2019 9:09 pm

I have not succeeded in getting it to work.

In order to replace the pi3d and the pyxlib python3 modules, I have renamed their folders in /usr/local/lib/python3.x/dist-packages and copied the pi3d and pyxlib folders from https://github.com/tipam/pi3d/archive/develop.zip to
/usr/local/lib/python3.x/dist-packages

I have also installed glx with:
pip3 install glx

On a Debian VM, I get this error:

Code: Select all

Traceback (most recent call last):
  File "Slider.py", line 23, in <module>
    DISPLAY = pi3d.Display.create(background=BACKGROUND, frames_per_second=40, w=1200, h=800, use_glx=True)
  File "/usr/local/lib/python3.5/dist-packages/pi3d/Display.py", line 563, in create
    display_config=display_config, window_title=window_title, use_glx=use_glx)
  File "/usr/local/lib/python3.5/dist-packages/pi3d/util/DisplayOpenGL.py", line 97, in create_display
    self.create_surface(x, y, w, h, layer)
  File "/usr/local/lib/python3.5/dist-packages/pi3d/util/DisplayOpenGL.py", line 184, in create_surface
    from pyxlib import glx
  File "/usr/local/lib/python3.5/dist-packages/pyxlib/glx.py", line 329, in <module>
    glXChooseFBConfig = libGLX.glXChooseFBConfig
  File "/usr/lib/python3.5/ctypes/__init__.py", line 360, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python3.5/ctypes/__init__.py", line 365, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: python3: undefined symbol: glXChooseFBConfig
On a PI4, I run xcompmgr (with no argument) on a console before running Raspberry_Rain.py. A new window, which has no title, appears in the task ba, but it cannot be displayed. It is like this window is not rendered or cannot gain focus. There is no error message.

User avatar
Paeryn
Posts: 2702
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Pi3D transparent background on Pi4

Mon Nov 04, 2019 9:34 pm

paddyg wrote:
Mon Nov 04, 2019 10:54 am
However... pushing the modified code to github.com/tipam/pi3d develop branch I find that it doesn't 'just work' on the RPi. I can get the transparent window but nothing rendered in it! So I will make a non-fixed pipeline version of the C example and see if I can narrow down the point that GL+Xrender extension+GLX on laptop diverges from RPi4.

Annoying but hopefully soluble.
Just been looking through this, the problem is that GLX is defaulting to giving you a GL 2.1 context rather than GLES 2 context, this makes your shaders invalid due to having the precision specifiers in std_head_vs.inc and std_head_fs.inc. Making the precision lines dependent on ES profiles seems to work, allowing them to be valid under both ES and full, though some of the other shaders might need modifying too depending (I've not looked at them).

Code: Select all

#if GL_ES
	precision mediump float;
#endif
If you want to force ES2 contexts then you need to use glXCreateContextAttribsARB() to create the context instead of glXCreateNewContext() as you can then tell it what you want, though from a quick test it seems to return an ES3 even though ES profiles are not supposed to return higher than requested.

In C, your pyxlib doesn't yet expose the needed function and constants...

Code: Select all

int32_t context_attribs[] = {
    GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
    GLX_CONTEXT_MINOR_VERSION_ARB, 0,
    GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT,
    0};
render_context = glXCreateContextAttribsARB(display, fbconfig, dummy_glx_context, True, context_attribs);
She who travels light — forgot something.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Mon Nov 04, 2019 10:03 pm

@paeryn yes. I started down that route trying to get the c code (that 6by9 pointed me at) to work with the shader pipeline. On laptop then pi4 and finally hack it to ctypes python. Gratifying that your code snippet matches mine... Apart from it still not rendering... But it will do with further hacking.

JeDy111 not tried VM for years so not sure what might be missing cf rasbian or Ubuntu. Sorry. The non responsive window sounds like full screen mode. Try setting w=500,h=300. Like I say it doesn't render anything on the pi as yet but I'm hopeful!

PS the rust_pi3d version has a system to select eithe GL3 or 2 and modify the shaders accordingly. Maybe I should push those mods into python now. I was hoping to do that as second stage.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Tue Nov 05, 2019 12:37 am

Annoyingly I can't get the glXCreateContextAttribsARB() to work. But if I fix the shaders it works on the RPi4 (at last!). Commenting out the precision lines in pi3d/shaders/std_head_vs.inc and std_head_fs.inc seems sufficient (why?) but I might as well make it do 'version', 'Texture2D', 'gl_FragColor' and 'varying' while I'm at it.

I will try to get this out tomorrow as a new version in master branch and pypi (assuming it tests ok on everything)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Paeryn
Posts: 2702
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Pi3D transparent background on Pi4

Tue Nov 05, 2019 3:24 am

paddyg wrote:
Tue Nov 05, 2019 12:37 am
Commenting out the precision lines in pi3d/shaders/std_head_vs.inc and std_head_fs.inc seems sufficient (why?)
Because on the RPi4 the highest supported version of GLSL is 1.20 (GL 2.1) and the precision qualifiers weren't added until GLSL 1.30 (GL 3.0) where they appeared for compatibility with GLES. They weren't defined to have any effect outside of GLES until (...looks through each spec...) GLSL 4.10 (GL 4.1).
She who travels light — forgot something.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Tue Nov 05, 2019 8:54 am

Ah, extra confusion of GL v. GLES. My (why?) was really a note to myself to do some more investigation. In order to get rust_pi3d running (essentially the same shaders using the same GL drivers) I had to translate more things than just commenting out the precision in order to be able to switch between GL and GLES versions i.e. https://github.com/paddywwoof/rust_pi3d ... er.rs#L172 As this glx route isn't specifying the client context I will have to query the system and translate accordingly. All extra complexity. Oh well.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Tue Nov 05, 2019 5:38 pm

scrcap.jpg
scrcap.jpg (73.97 KiB) Viewed 534 times
Working version now on pi3d develop branch. Also revised shaders and examples on pi3d_demos develop branch. Tests ok on laptop and RPi4, will check out on other machines.

Thanks JeDy111 for pointing this out and 6by9 and Paeryn for pointing me in the right direction.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Tue Nov 05, 2019 10:18 pm

Test ok on my side on RPI4. Thanks again.

For compositing purposes of Pi3D along with others applications such as a video player on the screen, is there a simple way to create the Pi3D Display without the window borders and title which now appears when specifying w and h attributes (when no full screen), as it was rendered with DispmanX?
I realize that the x and y position of the Display would also be needed and might be tricky to specify when instantiating the window.
Or maybe a solution would be to use a full screen Display and 'crop' the rendering of shapes within a rectangular area?
Let me know if I am asking too much! Maybe it is very specific to my use case (mixing video and 3D shapes). PI3D transparency on RPI4 is anyway a great feature.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Tue Nov 05, 2019 11:32 pm

Hi JeDy111, thanks for testing. Yes the borders and other decoration should be fixable with xlib function calls. There are some window creation options already here but I think the removal of all decoration is even messier than the xatom and XChangeProperty() full screen and window title stuff. Positioning the window is also possible but seems like it has to be done after the initial creation process. I'm not sure exactly what the critical step is but if I get or set the window position in the general creation process it's ignored - if I do it in swap_buffers() it seems to work! Using an glScissor process (see demo NumpyBalls.py) would also work but a bit messy and only the top window would be accessible for mouse and keyboard, so probably confusing.

There are a couple of fixes I've needed to make while testing on non-fakeKMS Raspberry Pis and I might slightly automate the specification of use_glx (i.e. if pi3d is going to use X11 for the window and the background has been specified in Display.create() with alpha less than 1.0 -> use_glx is True). Also I'm not sure if I need to fix the rather strange behavior when use_glx=True and the background is set opaque (Try changing the background=(1.0, 1.0, 1.0, 1.0) in Minimal.py say).

I think there are a few people using composition features of pi3d on Raspberry Pi and I think it's pretty handy to be able to do it on the wider X11 windowing world. In my search for a way to do this I didn't come across anything else with this feature!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Wed Nov 06, 2019 9:14 am

Hi paddyg,
Opaque background with glx is indeed surprising! Can be fixed by disabling glx if alpha=1. I think it is preferable than activating automatically glx if alpha<1 because there could issues on RPI3 on which xcompmgr is not needed.
Thanks for the insights and ideas for composition features. glScissor is a good candidate for my use case since I don't need the underlying layers to get focus for mouse and keyboard.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Wed Nov 06, 2019 4:06 pm

JeDy111, I've pushed up a few fixes for things that cropped up on different machines and OSs. I've also made Display.resize(x,y,w,h) work with x11 windows. However the actual position seems to be set by the desktop window manager so (0,0) only gets to be top left corner if all the desktop stuff is positioned away from the top and left side of the screen. Border width also seems to get ignored. It's probably possible to fix these things by setting your own preferences for the window manager - being linux there is almost certainly a file you can tweak somewhere.

However, just out of curiosity; why can't you just have one full screen pi3d window and draw stuff inside that where you need it? Is there a processing overhead for having un-drawn-on GL surface? Presumably it's just glClear() that's involved.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Thu Nov 07, 2019 9:45 am

Great!
In my use case, some portions of the screen should not be overlayed by the Pi3D shapes. The Pi3D shapes must be limited to a rectangular surface. For a seamless integration, this rectangular surface cannot have visible boundaries. Just like it was with Pi3D on DispmanX. I think that a full screen Pi3D Display and the glScissor process is now the best way to achieve that. I will have to reposition the shapes and the camera based on the center of the glScissor surface rather than the center of the screen.

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Thu Nov 07, 2019 10:44 am

Ah, so you want the shapes you draw to disappear as they move off the edge of the pi3d screen? You could do that by making a single pi3d full screen window then masking off the bits you wanted to appear 'off screen' with zero alpha Plane objects like below. I also remembered that the stereo demo uses glScissor to draw each half of the screen, with a camera move between draws.

Code: Select all

import demo
import pi3d

DISPLAY = pi3d.Display.create(frames_per_second=30, background=(0.0, 0.0, 0.0, 0.0), use_glx=True)
DISPLAY.set_background      # r,g,b,alpha
cam3d = pi3d.Camera(is_3d=True, absolute=False)
cam2d = pi3d.Camera(is_3d=False)
# load shader
shader = pi3d.Shader("uv_bump")
flatsh = pi3d.Shader("uv_flat")
matflatsh = pi3d.Shader("mat_flat")

bumpimg = pi3d.Texture("textures/grasstile_n.jpg")
reflimg = pi3d.Texture("textures/stars.jpg")
floorimg = pi3d.Texture("textures/floor_nm.jpg")

FOG = ((0.3, 0.3, 0.4, 0.8), 650.0)

ectex=pi3d.loadECfiles("textures/ecubes","sbox")
myecube = pi3d.EnvironmentCube(camera=cam3d, size=900.0, maptype="FACES", name="cube")
myecube.set_draw_details(flatsh, ectex)

# Create elevation map
mapsize = 1000.0
mapheight = 60.0
mountimg1 = pi3d.Texture("textures/mountains3_512.jpg")
mymap = pi3d.ElevationMap("textures/mountainsHgt.png", camera=cam3d, name="map",
                     width=mapsize, depth=mapsize, height=mapheight,
                     divx=32, divy=32) 
mymap.set_draw_details(shader, [mountimg1, bumpimg, reflimg], 128.0, 0.0)
mymap.set_fog(*FOG)

window1 = pi3d.Plane(camera=cam2d, w=1000, h=1000, x=250, y=-250, z=1)
window1.set_shader(matflatsh)
window1.set_alpha(0.0)

#avatar camera
rot = 0.0
tilt = 0.0
avhgt = 3.5
xm = 0.0
zm = 0.0
ym = mymap.calcHeight(xm, zm) + avhgt
step = [0.0, 0.0, 0.0]
norm = None
crab = False

# Fetch key presses
mykeys = pi3d.Keyboard()
mymouse = pi3d.Mouse(restrict=False)
mymouse.start()

roll = 0.0
# Display scene and rotate cuboid
while DISPLAY.loop_running():
  xm, ym, zm = cam3d.relocate(rot, tilt, point=[xm, ym, zm], distance=step, 
                              normal=norm, crab=crab, slope_factor=1.5)
  if step != [0.0, 0.0, 0.0]: #i.e. previous loop set movmement
    ym, norm = mymap.calcHeight(xm, zm, True)
    ym += avhgt
    step = [0.0, 0.0, 0.0]
  myecube.position(xm, ym, zm)
  cam3d.rotateZ(roll)
  roll = 0.0

  # draw window first to stop blending over it (z is near)
  window1.draw()
  # then rest as required
  mymap.draw()
  myecube.draw()

  mx, my = mymouse.velocity() #change to position() if Camera switched to absolute=True (default)
  buttons = -1 #mymouse.button_status()

  rot = - mx * 0.2
  tilt = my * 0.2

  #Press ESCAPE to terminate
  k = mykeys.read()
  #print(len(DISPLAY.keys_pressed))
  if k >-1 or buttons > mymouse.BUTTON_UP:
    if k == 119 or buttons == mymouse.LEFT_BUTTON:  #key w forward
      step = [0.5, 0.0, 0.5]
      crab = False
    elif k == 115 or buttons == mymouse.RIGHT_BUTTON:  #kry s back
      step = [-0.25, 0.0, -0.25]
      crab = False
    elif k == 97:   #key a crab left
      step = [0.25, 0.0, 0.25]
      crab = True
    elif k == 100:  #key d crab right
      step = [-0.25, 0.0, -0.25]
      crab = True
    elif k == 112:  #key p picture
      pi3d.screenshot("forestWalk" + str(scshots) + ".jpg")
      scshots += 1
    elif k == 10:   #key RETURN
      mc = 0
    elif k == 27:  #Escape key
      mykeys.close()
      mymouse.stop()
      DISPLAY.stop()
      break
    elif k == ord('f'):
      roll = 1.0
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Fri Nov 08, 2019 9:59 am

Hi paddyg,

Zero alpha Plane objects are interesting to dedicate a surface free of Pi3D shapes. Good to know.

In my case, it is really glScissor that makes the trick since I want the Pi3D shapes to disappear when they leave a rectangular surface.
Based on the pi3d.sprite.ScissorBall example, here is the test I have made with a glScissor on Raspberry_Rain. It works fine, and it is like using x,y,w,h arguments on the Display as it was rendered with DispmanX, before glx. Note that to get exactly the same behavior, translating shapes might be needed if the glScissor is not centered.

Code: Select all

#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals

"""
Simple Sprite objects fall across the screen and are moved back to the top once
they hit the bottom edge.
"""

import random, time

# import sys
# sys.path.insert(1, '/home/pi/Documents/pi3d-develop/pi3d')
import pi3d
from pi3d.constants import opengles, GL_SCISSOR_TEST, GLint, GLsizei

# Set last value (alpha) to zero for a transparent background!
BACKGROUND = (0.0, 0.0, 0.0, 0)

# Setup display and initialise pi3d and a shader.
# DISPLAY = pi3d.Display.create(background=BACKGROUND, frames_per_second=20, w=800, h=400, use_glx=True)
DISPLAY = pi3d.Display.create(background=BACKGROUND, frames_per_second=20, use_glx=True)
SHADER = pi3d.Shader('uv_flat')
opengles.glEnable(GL_SCISSOR_TEST)

TEXTURE = pi3d.Texture('textures/Raspi256x256.png')
BERRY_COUNT = 20

# Setup array of random x,y,z coords and initial rotation
RASPBERRIES = []

for b in range(BERRY_COUNT):
  # Select size as a random number 0.2 and 2.5.
  size = random.uniform(0.5, 2.5)
  rasp = pi3d.ImageSprite(texture=TEXTURE, shader=SHADER, w=size, h=size)

  # distance from the camera.
  dist = random.uniform(2.0, 10.0)
  rasp.position(random.uniform(-1.0, 1.0) * dist,
                random.uniform(0.0, 4.0) * dist,
                dist)
  rasp.rotateToZ(random.uniform(0.0, 360.0))

  RASPBERRIES.append(rasp)
  DISPLAY.add_sprites(rasp)

# Fetch key presses
KEYBOARD = pi3d.Keyboard()

while DISPLAY.loop_running():
  # opengles.glScissor(GLint(0), GLint(0), GLsizei(int(DISPLAY.INSTANCE.width/2)), GLsizei(int(DISPLAY.INSTANCE.height/2)))
  opengles.glScissor(GLint(int(DISPLAY.INSTANCE.width/4)), GLint(int(DISPLAY.INSTANCE.height/4)), GLsizei(int(DISPLAY.INSTANCE.width/2)), GLsizei(int(DISPLAY.INSTANCE.height/2)))

  for b in RASPBERRIES:
    b.translateY(-0.1)
    b.rotateIncZ(1)
    if b.y() < -2 * b.z():
      b.positionX((random.uniform(0.0, 2.0) - 1) * b.z())
      b.translateY(4.0 * b.z())

  k = KEYBOARD.read()
  if k >-1:
    if k == 27:
      KEYBOARD.close()
      DISPLAY.stop()
      break
    elif k == 112:
      pi3d.screenshot('raspberryRain.jpg')

User avatar
paddyg
Posts: 2390
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D transparent background on Pi4

Fri Nov 08, 2019 12:54 pm

Looks good. Your scissor method is pretty handy. If you want multiple windows in different places then you need to redefine the scissor inside the loop before drawing what you want to show - but in your demo you could move that out of the loop. If you have a big shape that can be seen through two windows then you would need to draw it twice. Probably not too significant and compensated by the scissor rendering only working on a small area of the screen, and generally you would want to draw different things in different windows.

Just in case you ever need something more complicated (circular or L-shaped window) then you can make your own pi3d.Shape quite easily. Check out the different class definitions in pi3d/shape/ but here is a demo making a rectangular window like your scissor one. For complicated things the easiest route would be to make the mask in Blender then import it as obj... providing you find using Blender "easy":

Code: Select all

...
  RASPBERRIES.append(rasp)
  DISPLAY.add_sprites(rasp)

(W, H) = (DISPLAY.width, DISPLAY.height)
sc_verts = [[0,0,1], [W,0,1], [W,H,1], [0,H,1],
            [0.25*W, 0.25*H, 1], [0.75*W, 0.25*H, 1], [0.75*W, 0.75*H, 1], [0.25*W, 0.75*H, 1]]
sc_norms = [[0,0,-1]] * 8
sc_faces = [[0, 4, 5], [0, 5, 1], [1, 5, 6], [1, 6, 2],
            [2, 6, 7], [2, 7, 3], [3, 7, 4], [3, 4, 0]]
sc_texc = [[v[0] / W, v[1] / H] for v in sc_verts]
cam3d = pi3d.Camera(is_3d=True) # needs to be created first so it's the default for Raspberries
cam2d = pi3d.Camera(is_3d=False)
mask = pi3d.Shape(cam2d, None, "", -0.5*W, -0.5*H, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0)
mask.buf = [pi3d.Buffer(mask, sc_verts, sc_texc, sc_faces, sc_norms)]
matsh = pi3d.Shader("mat_flat")
mask.set_shader(matsh)
mask.set_alpha(0.0)

# Fetch key presses
KEYBOARD = pi3d.Keyboard()
while DISPLAY.loop_running():
  mask.draw()
  for b in RASPBERRIES:
  ...
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

JeDy111
Posts: 24
Joined: Tue Sep 03, 2019 8:35 pm

Re: Pi3D transparent background on Pi4

Fri Nov 08, 2019 4:04 pm

Very interesting. The Blender offers a wide range of possibilities.

User avatar
Gavinmc42
Posts: 3897
Joined: Wed Aug 28, 2013 3:31 am

Re: Pi3D transparent background on Pi4

Sat Nov 09, 2019 7:57 am

What is the trick to getting Pi3D working on a Pi4.
Have tested Python and OpenGL, even shaders on Gentoo64 so it works.
I know there is no GLES for Pi4 only OpenGL.

Code: Select all

Traceback (most recent call last):
  File "SpriteBalls.py", line 38, in <module>
    DISPLAY = pi3d.Display.create(background=BACKGROUND_COLOR, frames_per_second=40)
  File "/home/demouser/.local/lib64/python3.6/site-packages/pi3d/Display.py", line 560, in create
    display.opengl.create_display(x, y, w, h, depth=depth, samples=samples, layer=layer, display_config=display_config, window_title=window_title)
  File "/home/demouser/.local/lib64/python3.6/site-packages/pi3d/util/DisplayOpenGL.py", line 95, in create_display
    self.create_surface(x, y, w, h, layer)
  File "/home/demouser/.local/lib64/python3.6/site-packages/pi3d/util/DisplayOpenGL.py", line 223, in create_surface
    assert self.surface != EGL_NO_SURFACE and self.surface is not None
AssertionError
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Return to “Graphics programming”