Mintoris

Author Topic: Bitmap overlay on live camera view?  (Read 791 times)

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Bitmap overlay on live camera view?
« on: Jun 29, 2018, 03:38 AM »
Hi all, I am attempting to write some sort of dioptra/theodolite app and need some help, I can't get my bitmap over the live camera view. Can anyone point me in the right direction, thanks.

Here is a simplified version of the sort of thing I'm working on.

Graphics On
Sensors On
orientation 2
overlay=createbitmap(screenx(),screeny())
dim a(0)
cx=screenx()/2
cy=screeny()/2
f=cx*2
Dim px(1)
dim py(1)
dim pz(1)
dim sx(1)
dim sy(1)
  px(0)=-100000000
  py(0)=0
  pz(0)=100000000
  px(1)=100000000
  py(1)=0
  pz(1)=100000000

do

  a() = getAccelerometer()
  d=.1/sqrt(a(0)*a(0)+a(1)*a(1)+a(2)*a(2))
  vx=vx*.9+a(1)*d
  vy=vy*.9-a(0)*d
  vz=vz*.9+a(2)*d
  d=1/sqrt(vx*vx+vy*vy)
  ux=-vy*d
  uy=vx*d
  tx=-vz*uy
  ty=vz*ux
  tz=vx*uy-vy*ux

  for i=0 to 1
    rz=f/(py(i)*vz+pz(i)*tz)
    sx(i)=cx+(px(i)*ux+py(i)*vx+pz(i)*tx)*rz
    sy(i)=cy-(px(i)*uy+py(i)*vy+pz(i)*ty)*rz
  next i

  SetDrawingSurface overlay
  color 0,0,0
  cls
  color 100,100,100
  line cx,0,cx,cy*2
  line 0,cy,cx*2,cy
  color 100,100,0
  line sx(0),sy(0),sx(1),sy(1)
  SetDrawingSurface off

  DrawBitmap overlay,cx,cy
  wait 20

Loop

sensors off
graphics off

End
« Last Edit: Jun 29, 2018, 04:03 AM by Stonemonkey »

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #1 on: Jun 30, 2018, 03:47 AM »
Still can't get this working, I can get a static overlay working but that's no use.

Graphics On
Sensors On
orientation 2
overlay=createbitmap(screenx(),screeny())
dim a(0)
cx=screenx()/2
cy=screeny()/2
f=cx*2
Dim px(1)
dim py(1)
dim pz(1)
dim sx(1)
dim sy(1)
  px(0)=-100000000
  py(0)=0
  pz(0)=100000000
  px(1)=100000000
  py(1)=0
  pz(1)=100000000

gameengine on
atlas=gentileatlas(2048,2048)
tileset=gentileset(overlay)
genmaptoatlas atlas,tileset,0,0
genloadatlas atlas
sprite=gensprite(tileset)
genaddsprite sprite,1
GenCameraFeed on
gensetbackgroundcolor 0,0,0,0

do

  a() = getAccelerometer()
  d=.1/sqrt(a(0)*a(0)+a(1)*a(1)+a(2)*a(2))
  vx=vx*.9+a(1)*d
  vy=vy*.9-a(0)*d
  vz=vz*.9+a(2)*d
  d=1/sqrt(vx*vx+vy*vy)
  ux=-vy*d
  uy=vx*d
  tx=-vz*uy
  ty=vz*ux
  tz=vx*uy-vy*ux

  for i=0 to 1
    rz=f/(py(i)*vz+pz(i)*tz)
    sx(i)=cx+(px(i)*ux+py(i)*vx+pz(i)*tx)*rz
    sy(i)=cy-(px(i)*uy+py(i)*vy+pz(i)*ty)*rz
  next i

  SetDrawingSurface overlay
  color 0,0,0,0
  cls
  color 100,100,100
  line cx,0,cx,cy*2
  line 0,cy,cx*2,cy
  color 100,100,0
  line sx(0),sy(0),sx(1),sy(1)

  SetDrawingSurface off
 rem  DrawBitmap overlay,cx,cy

genkillsprite sprite
GenRemoveFromAtlas tileset
tileset=gentileset(overlay)
genmaptoatlas atlas,tileset,0,0
genreloadatlas atlas
sprite=gensprite(tileset)
genaddsprite sprite,1

 rem wait 20
Loop

sensors off
graphics off

End

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #2 on: Jun 30, 2018, 05:37 AM »
https://drive.google.com/file/d/1Ys3LyOmPhmugXhnS2lltJFxreJBA_U6B/view?usp=drivesdk

Short video of my overlay as it is just now, to give an idea of what I'm wanting to do. Is there any way I can do this, or is there any way to draw directly into the game engine screen?
Can someone please help.

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #3 on: Jul 09, 2018, 09:16 AM »
No one can help with this?
Is there no way to do it with backgrounds or something?

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #4 on: Jul 09, 2018, 11:08 AM »
Kind of got something working but its slow to update the overlay and it crashes after a few seconds, I suspect there's maybe some kind of memory leak going on with the way I'm doing this.

Graphics On
Sensors On
orientation 2
overlay=createbitmap(screenx(),screeny())
dim a(0)
cx=screenx()/2
cy=screeny()/2
f=cx*2
Dim px(1)
dim py(1)
dim pz(1)
dim sx(1)
dim sy(1)
  px(0)=-100000000
  py(0)=0
  pz(0)=100000000
  px(1)=100000000
  py(1)=0
  pz(1)=100000000

gameengine on
tileset=gentileset(overlay)
atlas=Gentileatlas(2048,2048)
GenMapToAtlas atlas,tileset,0,0
background=genbackground(2,tileset)
GenSetBackground background
GenCameraFeed on
GenSetBackgroundColor 0,0,0,0

do

  a() = getAccelerometer()
  d=.1/sqrt(a(0)*a(0)+a(1)*a(1)+a(2)*a(2))
  vx=vx*.9+a(1)*d
  vy=vy*.9-a(0)*d
  vz=vz*.9+a(2)*d
  d=1/sqrt(vx*vx+vy*vy)
  ux=-vy*d
  uy=vx*d
  tx=-vz*uy
  ty=vz*ux
  tz=vx*uy-vy*ux

  for i=0 to 1
    rz=f/(py(i)*vz+pz(i)*tz)
    sx(i)=cx+(px(i)*ux+py(i)*vx+pz(i)*tx)*rz
    sy(i)=cy-(px(i)*uy+py(i)*vy+pz(i)*ty)*rz
  next i

  SetDrawingSurface overlay
  color 0,0,0
cls
color 0,0,0,100,1
cls
  color 100,100,100
  line cx,0,cx,cy*2
  line 0,cy,cx*2,cy
  color 100,100,0
  line sx(0),sy(0),sx(1),sy(1)

  SetDrawingSurface off

GenloadAtlas atlas

count = GenGetPlayedTime()
atlaswait:
If (GenGetPlayedTime()-count)<3 then atlaswait

Loop

sensors off
graphics off

End
« Last Edit: Jul 09, 2018, 12:37 PM by Stonemonkey »

Mintoris

  • Administrator
  • Full Member
  • *****
  • Posts: 49
Re: Bitmap overlay on live camera view?
« Reply #5 on: Jul 09, 2018, 01:03 PM »
Here is how to do a bitmap overlay in the game engine mode. If you noticed, I've commented out the GenCameraFeed On command because I was testing in the emulator. Please un-comment that line and test the camera with the overlay. Let us know how it works.

Code: [Select]
include "Game Engine Constants.inc"

' Set the orientation and turn on the Game Engine
orientation 2
GameEngine On, 2048, 1024
' GenCameraFeed on
init()

offset = 1
inc = 4

do

  ' Draw the star on the bitmap
  SetDrawingSurface bmStar
  color 0,0,0, 100
  cls
  color 0,0,0,0
  cls
  Color rnd(100), rnd(100), rnd(100), 100
  strokewidth 4
  line 0, 0 + offset, 2048, 1024 - offset
  line 0, 1024 - offset, 2048, 0 + offset
  SetDrawingSurface off

  GenSetBackgroundColor 0,0,0,0
  GenReloadAllAtlases

  if offset < 1 or offset > 511 then inc = inc * -1
  offset = offset + inc

wait 60
loop


GameEngine off

Sub init()

  ' Create a tile atlas
  atlas = GenTileAtlas(2048, 1024)

  ' Create the star bitmap
  Global bmStar = CreateBitmap(2048, 1024)

  ' Set the drawing surface to the star bitmap
  SetDrawingSurface bmStar

  ' Draw the star on the bitmap
  Color 100,100,100,100
  StrokeWidth 4
  line 0,0, 2048, 1024
  line 0, 1024, 2048, 0
  SetDrawingSurface off

  ' Create the tileset from the star bitmap
  Global tileset = GenTileSet(bmStar)

  ' Put the font into the atlas
  GenMapToAtlas atlas, tileset, 0, 0

  ' Load the atlas into the GPU
  GenLoadAtlas atlas

  GenSetBackgroundColor 0,0,0,0
 

  Global background = GenBackground(Background_Fixed, tileset)
  GenSetBackground background

End Sub

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #6 on: Jul 09, 2018, 01:39 PM »
Hi, thanks for the reply.
I'm still having problems with that though, it starts off OK but by the time the lines meet in the middle of the screen it slows down then crashes.
I also had to change the colours+alpha values to clear the screen and have the camera view visible.
The same thing happens on both my phones, blackview bv7000 and Sony z3. The z3 runs a little longer before crashing. It does the same thing with camera enabled or not.

Any ideas what might be wrong?

Edit: if I make the bitmap and atlas smaller it runs for longer before crashing, it might not be freeing up graphics ram each reload or something like that.
« Last Edit: Jul 09, 2018, 03:00 PM by Stonemonkey »

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #7 on: Jul 10, 2018, 10:40 AM »
It seems to me there is some sort of memory leak with GenReloadAtlas and GenReloadAllAtlases where they're not releasing the graphics memory of the previous instance when reloading.
If I use GenRemoveAtlas and then create a new atlas each frame it doesn't crash any more, but the screen flashes/strobes so that's not an option.

Mintoris

  • Administrator
  • Full Member
  • *****
  • Posts: 49
Re: Bitmap overlay on live camera view?
« Reply #8 on: Jul 10, 2018, 10:45 AM »
Yes, there does seem to be a memory leak. I will look into this.

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #9 on: Jul 10, 2018, 11:52 AM »
Thankyou.

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #10 on: Jul 12, 2018, 04:23 AM »
Hi, can i make a request or suggrstion with regards to this for version 8?

Could there be a zoom function for the camera layer?
Would it be possible to either save a snapshot image of the game engine screen or copy the game engine screen to a bitmap?
Could the camera be rendered to a bitmap without gameengine?

I realise this is fairly specific to what I'm working on and I'm not sure how much use it would generally have but it could be of use for creative camera apps too.
« Last Edit: Jul 12, 2018, 01:52 PM by Stonemonkey »

Mintoris

  • Administrator
  • Full Member
  • *****
  • Posts: 49
Re: Bitmap overlay on live camera view?
« Reply #11 on: Jul 12, 2018, 08:17 PM »
I've found and fixed the memory leak issue. Graphics are moving nice and smooth and I will leave the test running for a few hours. I have a few other issues to check out but hope to have a release in a few days.

Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #12 on: Jul 12, 2018, 11:59 PM »
That's great, thanks. Can't wait to try this out.
« Last Edit: Jul 13, 2018, 02:21 AM by Stonemonkey »

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Bitmap overlay on live camera view?
« Reply #13 on: Jul 16, 2018, 09:30 AM »
Basic 7.5.17 has been released. I've also added a new example program GEN005 Bitmap Graphics Overlay in the tutorial section of the game engine board. This example shows how to use the background timer to update the overlay. Using the timer syncs the overlay update with the game engine screen refresh for a flicker free display. Please let me know how things work out.


Stonemonkey

  • Full Member
  • ***
  • Posts: 11
Re: Bitmap overlay on live camera view?
« Reply #14 on: Jul 16, 2018, 01:25 PM »
Thanks very much, that's great. I'll have a play around with it and let you know how it goes.
I take it the method of update in the example runs off interrupts, what if the drawing of the overlay takes more than 1/60th sec or if its called in the middle of the code carrying out my calculations?

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Bitmap overlay on live camera view?
« Reply #15 on: Jul 16, 2018, 01:29 PM »
The drawing can be done outside the interrupt, but continue to do the GenReloadAtlas in the interrupt to avoid flicker. If the update doesn't happen very often then you can do the whole thing outside the interrupt. You can also slow the interrupt down as much as you like. Make it run once a sec if you like.

Oh, also in the example the 60ms is the time between each execution, but that does not limit how long it takes the interrupt to run. So, your drawing can take as long as it needs.

« Last Edit: Jul 16, 2018, 01:41 PM by Chuck »

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Bitmap overlay on live camera view?
« Reply #16 on: Aug 27, 2018, 02:53 PM »
Sorry, nowhere does it say that Airplay is supported. This sounds like a cool feature for future upgrades.