Page 1 of 1

SwitchCamera glitching and splitting the screen, bug?

Posted: Fri Mar 27, 2015 11:10 am
by Henry00
Hello everyone!

The command SwitchCamera with description "Switch the currently displayed camera." makes little sense to me. I had hoped it would literally switch some camera with another camera internally. Say you have two screen halves, camera A taking the left 50% and camera B taking the right 50% of the screen that SwitchCamera would simply exchange those two internally now showing camera B on the left and camera A on the right. Instead I get this undefined behavior:

Image
Image

I modified the purebasic camera example to easily show how it immediately breaks itself, simply press F anytime:
You'd expect this would just flip the upper and lower camera views.

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Camera
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 1

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()

  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    AmbientColor(RGB(0, 200, 0))  ; Green 'HUD' like color 
 
    CreateMaterial(0, LoadTexture(0, "r2skin.jpg"))
    CreateEntity(0, LoadMesh(0, "robot.mesh"), MaterialID(0))
    StartEntityAnimation(0, "Walk")
    
    CreateCamera(0, 0, 0, 100, 50)  ; Front camera
    MoveCamera(0, 0, 20, 250, #PB_Absolute)
    CameraBackColor(0, RGB(55, 0, 0))
    
    CreateCamera(1, 0, 50, 100, 50) ; Back camera
    MoveCamera(1, 0, 20, -250, #PB_Absolute)
    CameraBackColor(1, RGB(25, 25, 25))
    RotateCamera(1, 180, 0, 0)
    
    CameraRenderMode(1, #PB_Camera_Wireframe)  ; Wireframe for this camera
 
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
             
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
                  
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
        
        ; henry's bug proving:
        If KeyboardReleased(#PB_Key_F)
          SwitchCamera(0, 1)
        EndIf

      EndIf
      
      
      RotateEntity(0, 0, 0.1, 0, #PB_Relative)
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RotateCamera(1, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (1, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
Any information regarding this command would be very appreciated!

Re: SwitchCamera glitching and splitting the screen, bug?

Posted: Fri Mar 27, 2015 10:16 pm
by DK_PETER
The SwitchCamera() has a different purpose, than what you're expecting.
It switches the active camera to another one which in turn becomes the new active one.
It doesn't switch the camera positions or properties.

Here's an example:

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

Global CamActive.i = 0

OpenWindow(0, 0, 0, 1024, 768, "Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #False, 0, 0)

CreateSphere(0, 1, 10, 10)
CreateTexture(0, 64, 64)
StartDrawing(TextureOutput(0))
Box(0, 0, 64, 64, $888888)
For x = 0 To 100
  Circle(Random(55,10), Random(55,10), Random(9, 4), RGB(Random(255,50),Random(255,50),Random(255,50)))
Next x
StopDrawing()
CreateMaterial(0, TextureID(0))
CreateEntity(0, MeshID(0), MaterialID(0), 0, 0, -4)

For x = 0 To 4
  CreateCamera(x, 0, 0, 100, 100)
  MoveCamera(x, 9 * x, 9 * x, 9 * x)
  CameraLookAt(x, 0, 0, 0)
Next x

Repeat
  
  
  Repeat
    ev = WindowEvent()
    
  Until ev = 0
 
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_1)
    SwitchCamera(CamActive, 0)
    CamActive = 0
  ElseIf  KeyboardPushed(#PB_Key_2)
    SwitchCamera(CamActive, 1)
    CamActive = 1
  ElseIf KeyboardPushed(#PB_Key_3)
    SwitchCamera(CamActive, 2)
    CamActive = 2
  ElseIf KeyboardPushed(#PB_Key_4)
    SwitchCamera(CamActive, 3)
    CamActive = 3
  ElseIf KeyboardPushed(#PB_Key_5)
    SwitchCamera(CamActive, 4)
    CamActive = 4
  EndIf   

  RenderWorld()
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)

Re: SwitchCamera glitching and splitting the screen, bug?

Posted: Fri Mar 27, 2015 11:05 pm
by Henry00
Your example works but I am still a little confused, aren't you supposed to be able to make multiple cameras? The help file is all proud:
Many cameras can be used at the same time with different positions and views to allow cool effects like: split-screen, rear view, etc.

That's a bit of a contradiction if SwitchCamera throws every camera on the screen all over the place and causes some to completely stop working...

edit:
What exactly is an "active" camera, for me they all render at the same time and they are all active...

edit:
I have messed around with your code and this really must be a bug, when you set the height to be 100% and the width to be 25% and call this function the height also becomes 25%!

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

Global CamActive.i = 0

OpenWindow(0, 0, 0, 1024, 768, "Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #False, 0, 0)

CreateSphere(0, 1, 10, 10)
CreateTexture(0, 64, 64)
StartDrawing(TextureOutput(0))
Box(0, 0, 64, 64, $888888)
For x = 0 To 100
  Circle(Random(55,10), Random(55,10), Random(9, 4), RGB(Random(255,50),Random(255,50),Random(255,50)))
Next x
StopDrawing()
CreateMaterial(0, TextureID(0))
CreateEntity(0, MeshID(0), MaterialID(0), 0, 0, -4)

For x = 0 To 3
  CreateCamera(x, x*25, 0, (1+x)*25, 100) ; make some cameras with 25% offset
  CameraBackColor(x, RGB(x * 50, 0, 0)) ; make different background colors
  MoveCamera(x, 9 * x, 9 * x, 9 * x)
  CameraLookAt(x, 0, 0, 0)
Next x

Repeat
 
 
  Repeat
    ev = WindowEvent()
   
  Until ev = 0
 
  ExamineKeyboard()
 
  If KeyboardPushed(#PB_Key_1)
    SwitchCamera(CamActive, 0)
    CamActive = 0
  ElseIf  KeyboardPushed(#PB_Key_2)
    SwitchCamera(CamActive, 1)
    CamActive = 1
  ElseIf KeyboardPushed(#PB_Key_3)
    SwitchCamera(CamActive, 2)
    CamActive = 2
  ElseIf KeyboardPushed(#PB_Key_4)
    SwitchCamera(CamActive, 3)
    CamActive = 3
  ElseIf KeyboardPushed(#PB_Key_5)
    SwitchCamera(CamActive, 4)
    CamActive = 4
  EndIf   

  RenderWorld()
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape)

Re: SwitchCamera glitching and splitting the screen, bug?

Posted: Sat Apr 04, 2015 10:15 am
by DK_PETER
@Henry00

You're right..It doesn't keep a fixed height percentage.

I might have gotten a (mis)understanding of how the cameras should function
when displaying multiple views at once....

The info about SwitchCamera() is extremely poor and gives a lot of room for (mis)interpretation.
If you examine the nonsense example below, you'll see, that something is terribly wrong.
Should you be able to set an 'active' camera, you would also need a way to examine if a current cammera is
active such as : activeCam = GetActiveCamera() - which is non-existant.

Personally, I would like a clear definition of the exact purpose and usage of the SwitchCamera() command.

Nonsense example to test:

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

Global CamActive.i = 0

OpenWindow(0, 0, 0, 1024, 768, "Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #False, 0, 0)

CreateSphere(0, 1, 10, 10)
CreateTexture(0, 64, 64)
StartDrawing(TextureOutput(0))
Box(0, 0, 64, 64, $888888)
For x = 0 To 100
  Circle(Random(55,10), Random(55,10), Random(9, 4), RGB(Random(255,50),Random(255,50),Random(255,50)))
Next x
StopDrawing()
CreateMaterial(0, TextureID(0))
CreateEntity(0, MeshID(0), MaterialID(0), 0, 0, -4)
For z = 1 To 3
  CopyEntity(0, z)
Next z
;Copy it
MoveEntity(1, -4, 0, 0)
MoveEntity(2, 4, 0, 0)
MoveEntity(3, 0, 0, 4)

For x = 0 To 3
  CreateCamera(x, x*25, 0, 25, 100)
  MoveCamera(x, 0, 0, 0)
Next x
;set view direction
CameraDirection(0, 1, 0, 0)
CameraDirection(1, -1, 0, 0)
CameraDirection(2, 0, 0, 1)
CameraDirection(3, 0, 0, -1)

Repeat
 
 
  Repeat
    ev = WindowEvent()
   
  Until ev = 0
 
  ExamineKeyboard()
 
  If KeyboardPushed(#PB_Key_1)
    SwitchCamera(CamActive, 0)
    CamActive = 0
  ElseIf  KeyboardPushed(#PB_Key_2)
    SwitchCamera(CamActive, 1)
    CamActive = 1
  ElseIf KeyboardPushed(#PB_Key_3)
    SwitchCamera(CamActive, 2)
    CamActive = 2
  ElseIf KeyboardPushed(#PB_Key_4)
    SwitchCamera(CamActive, 3)
    CamActive = 3
  EndIf   
  RotateEntity(0, 0.3, 0, 0, #PB_Relative)
  RotateEntity(1, -0.3, 0, 0, #PB_Relative)
  RotateEntity(2, 0,-0.5, 0, #PB_Relative)
  RotateEntity(3, 0,0,8, #PB_Relative)
  
  RenderWorld()
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape)

Re: SwitchCamera glitching and splitting the screen, bug?

Posted: Sun Apr 05, 2015 7:57 am
by Comtois
What are you trying to do exactly? I do not understand myself this feature.

Re: SwitchCamera glitching and splitting the screen, bug?

Posted: Sun Apr 05, 2015 12:08 pm
by DK_PETER
Comtois wrote:What are you trying to do exactly? I do not understand myself this feature.
ROFLMAO!

So...The SwitchCamera() doesn't make any sense to you too or is it our usage of it?

If there should be any advanges - this is it:

When setting each camera to a different direction and you're setting all the cameras' width and height to 100% -
it would be a nice way to swap cameras z-order (If you can refer it as such).

ShowCamera(id.i) or a simple HideCamera(id.i, true/false) would be a nice addition.
(edit: or simply skip the idea entirely and kill the SwitchCamera() command) :D

Swapping positions doesn't make any sense to me.

Re: SwitchCamera glitching and splitting the screen, bug?

Posted: Sun Apr 05, 2015 5:15 pm
by Henry00
One usage example is if you have the camera attached to an entity (say a security camera mesh) and you want to swap that camera with an other security camera to show a different location on your security monitor. Having to detach, attach, delete, create, move a camera into place or whatever other workaround you can find is a lot more work then swapping the security cameras internally. If SwitchCamera(CamA, CamB) does that and either of these is on the render texture it sure saves the day.

Edit:
And yes having more control instead of only swapping would be great, show/hide, re-position, re-size everything is missing and I see all those functions in OGRE. Currently you'd have to keep deleting and creating a new camera whenever you want to re-position it.