Page 1 of 1

Include 2D sprites within a 3D environment

Posted: Sun May 11, 2025 4:51 am
by JHPJHP
I've done little to nothing with the OGRE 3D engine so I'm not sure how or if this is supposed to work.

See the following Bug Report.

Re: Include 2D sprites within a 3D environment

Posted: Sun May 11, 2025 6:47 am
by miso
Hello again ;)
When you open a windowed screen together with the OGRE, it does not stretch sprites with window resize, only the 3d render.
However, there are two options:

1, Correct aspect ratio for the 3d scene too by calling resizecamera:

Code: Select all

Enumeration
  #MainWindow
EndEnumeration

InitEngine3D() : InitSprite() : InitKeyboard() : ExamineDesktops()

WindowWidth = 600 : WindowHeight = 600
nFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered | #PB_Window_Invisible

If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "Include 2D sprites within a 3D environment", nFlags)
  SetWindowColor(#MainWindow, RGB(0, 0, 0))
  WindowBounds(#MainWindow, WindowWidth, WindowHeight, #PB_Default, #PB_Default)

  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, WindowWidth, WindowHeight, #True, 0, 0)
    SetFrameRate(60) : SpriteQuality(#PB_Sprite_BilinearFiltering)
    CreateSprite(0, 20, 20, #PB_Sprite_AlphaBlending)

    If StartDrawing(SpriteOutput(0))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
    EndIf
    direction = 2

    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 5, 0) 
    CameraLookAt(0, 2, 5, 10)
    TextureSky = LoadTexture(#PB_Any, "sky.png")
    SkyDome(TextureID(TextureSky), $cc6600, $0088ff, 3, 400, -0.5, 0)
    CreateLight(0, $ff88ff, 20000, 40000, 20000)
    AmbientColor($010101)
    Fog($554488,1, 0, 1024 * 4)

    HideWindow(#MainWindow, #False)

    Repeat
      Repeat
        Event = WindowEvent()

        Select Event
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #MainWindow : Break 2
            EndSelect
          Case #PB_Event_SizeWindow
            ResizeCamera(0,0,0,100,100) ; this way it does not stretch 3D render
        EndSelect
      Until Not Event
      ExamineKeyboard()

      If KeyboardReleased(#PB_Key_Escape) : Break : EndIf
      RenderWorld()
      DisplayTransparentSprite(0, x, x)
      x + direction

      If x > WindowWidth - 20 : direction = -2 : EndIf
      If x < 0 : direction =  2 : EndIf
      
      FlipBuffers()
    ForEver
  EndIf
EndIf
2. Manually size all the sprites to the new aspect ratio using zoomsprite:

Code: Select all

Enumeration
  #MainWindow
EndEnumeration

InitEngine3D() : InitSprite() : InitKeyboard() : ExamineDesktops()

WindowWidth = 600 : WindowHeight = 600
nFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered | #PB_Window_Invisible

If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "Include 2D sprites within a 3D environment", nFlags)
  SetWindowColor(#MainWindow, RGB(0, 0, 0))
  WindowBounds(#MainWindow, WindowWidth, WindowHeight, #PB_Default, #PB_Default)

  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, WindowWidth, WindowHeight, #True, 0, 0)
    SetFrameRate(60) : SpriteQuality(#PB_Sprite_BilinearFiltering)
    CreateSprite(0, 20, 20, #PB_Sprite_AlphaBlending)

    If StartDrawing(SpriteOutput(0))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
    EndIf
    direction = 2

    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 5, 0) 
    CameraLookAt(0, 2, 5, 10)
    TextureSky = LoadTexture(#PB_Any, "sky.png")
    SkyDome(TextureID(TextureSky), $cc6600, $0088ff, 3, 400, -0.5, 0)
    CreateLight(0, $ff88ff, 20000, 40000, 20000)
    AmbientColor($010101)
    Fog($554488,1, 0, 1024 * 4)

    HideWindow(#MainWindow, #False)

    Repeat
      Repeat
        Event = WindowEvent()

        Select Event
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #MainWindow : Break 2
            EndSelect
          Case #PB_Event_SizeWindow
            ZoomSprite(0,20*WindowWidth(#MainWindow)/windowwidth,20*WindowHeight(#MainWindow)/windowheight)
        EndSelect
      Until Not Event
      ExamineKeyboard()

      If KeyboardReleased(#PB_Key_Escape) : Break : EndIf
      RenderWorld()
      DisplayTransparentSprite(0, x, x)
      x + direction

      If x > WindowWidth - 20 : direction = -2 : EndIf
      If x < 0 : direction =  2 : EndIf
      
      FlipBuffers()
    ForEver
  EndIf
EndIf
PS:I think you just found a bug, as it seems at the old window size the sprites are clipped at least with PB 6.21 beta 9
Never seen this, as I don't allow resizing my 3d screened apps.

Re: Include 2D sprites within a 3D environment

Posted: Sun May 11, 2025 3:26 pm
by JHPJHP
Hi miso,

Thank you for taking the time to post.
miso wrote:1, Correct aspect ratio for the 3d scene too by calling resizecamera:
miso wrote:2. Manually size all the sprites to the new aspect ratio using zoomsprite:
I've already tried both methods, but like you without success.
miso wrote:PS:I think you just found a bug, as it seems at the old window size the sprites are clipped at least with PB 6.21 beta 9
It's not that sprites are clipped, the 2D environment remains intact but the dimensions stay static when the 3D environment is stretched.

You might be correct that this is a bug. Previously, my 2D Death Star vs Asteroids game stretched in a 3D environment; I just don't have enough experience in this area to say if the changed behaviour is expected.

Note: I'm not sure when the behaviour changed, it's been a while since I've done much PureBasic programming.

Re: Include 2D sprites within a 3D environment

Posted: Sun May 11, 2025 3:57 pm
by miso
2d screens stretches well. Ogre windowed screens increases the viewport for the scene, while for the sprites the viewport remains intact.
Also calling screenwidth()/height() returns the values of the initial screen viewport.

(or I'm doing something wrong, it's always a possibility.)

Re: Include 2D sprites within a 3D environment

Posted: Mon May 12, 2025 2:54 pm
by JHPJHP
You know when you do something, turn away for a minute, look back, and that thing you did is gone... you're left wondering if that thing you thought you did was really done in the first place :lol: I'm almost positive I posted a bug report linking back to this thread, but it's gone, without explanation :?

Anyways, I've updated my first post to better illustrate the expected behaviour. If this has changed, no worries, I was only looking for confirmation.

Re: Include 2D sprites within a 3D environment

Posted: Mon May 12, 2025 3:02 pm
by miso
I'm thinking about just open a screen at desktop size and I resize my sprites. ;)
To be honest, I might not need window resize. I saw the report, it was just a link to here with many long posts ;)
A short clean example is preferred, I think.

Edit: No it's there. Moved to 3D bug reports ;)

Re: Include 2D sprites within a 3D environment

Posted: Mon May 12, 2025 3:11 pm
by JHPJHP
Hi miso,

Thank you again, not just for posting but also for your keen observation :)

I already tried starting the screen set to the desktop dimensions, but it doesn't resize the sprite; possibly combined with ZoomSptite, but I think that would clip the screen in a 3D environment.

At this point it's probably best to wait and hear from the powers that be.

Re: Include 2D sprites within a 3D environment

Posted: Mon May 12, 2025 4:06 pm
by miso
Zoomsprite is safe and works well, I can guarantee that. I use it a lot.

Re: Include 2D sprites within a 3D environment

Posted: Mon May 12, 2025 5:34 pm
by JHPJHP
Hi miso,

This isn't about any one command but the inclusion of sprites in a 3D environment; I'm sure ZoomSprite has no issues, I've used it myself on occasion.

If you're interested, here are a couple retro games I wrote almost ten years ago:
Invading Space Aliens
Death Star vs Asteroids
• Previously, the sprite screen would resize in the 3D environment.
• REM out line 92 to see why I originally created this thread.
• Press the Spacebar to skip the intro.

Note: Intro was based on code written by DK_PETER.

Be kind, I only wrote these games because I was board and wanted to familiarize myself with the Sprite library.

Re: Include 2D sprites within a 3D environment

Posted: Mon May 12, 2025 7:16 pm
by miso
Downloaded, these are quiet cool. ;) Please give me some time as I browse through the code.

Edit: I started to resize the sprites with 3Dscreen to test, and I just could not get the good ratio. I figured out, that there are DPI resize calls using the user32.dll (I guess PB was not natively DPI aware at the time of creation.) I removed those and the resizing with zoomsprite became correct.

Though it would be a lot of work to resize everything, as there's too many hardcoded sizes and positions. Cool programs though;)