Drawing text on a 3D screen?

Everything related to 3D programming
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

Drawing text on a 3D screen?

Post by mrw »

Hi,

I was wondering what the best practises are when drawing simple text on a 3D screen?
Up until now I was simply using DrawText() to print out some debugging information.
Something like this:

Code: Select all

Repeat
  Repeat
    Event = WindowEvent()
  Until Event = 0 Or #PB_Event_CloseWindow
  
  If Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    End
  EndIf

  ;-- Do 3D stuff
  ;-- here..
  RenderWorld()

  ;--2D stuff--
  StartDrawing()
  DrawText(10,100, "Hello")
  DrawText(10.115, "Hello again")
  StopDrawing()
  ;---
  
  FlipBuffers()
ForEver
This works as I want, until the framerate starts to decrease below VBL. Then the text simply disappears.
So I´m assuming that DrawText() isn´t reliable to use for this? Or is there a workaround?
I looked at CreateText3D() but since that requires a node or entity its just not what I want. I would like to just rely on 2D coordinates for this, if possible.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Drawing text on a 3D screen?

Post by Samuel »

Hello,

I've read that it's better to draw to a sprite and then display that sprite on the screen instead of directly drawing to the screen.
Here's a basic example showing the current frames per second using a sprite.

Code: Select all

Enumeration
  #Window
  #Camera
  #Mesh
  #Entity
  #Light
  #Font
  #Sprite
EndEnumeration

If InitEngine3D() = 0
  End
EndIf
If InitSprite() = 0
  End
EndIf
If InitKeyboard() = 0
  End
EndIf
 
ExamineDesktops()
Define.i DesktopW = 800
Define.i DesktopH = 600

Define.i Event
Define.i Quit
Define.i FPS

If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "Frames Per Second", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)
    
    CreateSphere(#Mesh, 2, 64, 32)
    CreateEntity(#Entity, MeshID(#Mesh), #PB_Material_None)
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    MoveCamera(#Camera, 0, 0, 10, #PB_Absolute)
    CameraLookAt(#Camera,0,0,0)
    CameraBackColor(#Camera,RGB(20,20,20))
    
    CreateLight(#Light, RGB(255,255,255), 10, 10, 10)
    
    LoadFont(#Font, "Times New Roman", 20, #PB_Font_Bold)
    
    CreateSprite(#Sprite, 128, 64)
    StartDrawing(SpriteOutput(#Sprite))
      DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0, 0, 50, 50)
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawingFont(FontID(#Font))
      DrawText(0, 0, "FPS : 60", RGB(255,255,255))
    StopDrawing()
    
    Repeat
      Event = WindowEvent()
      
      If ExamineKeyboard()
        If KeyboardReleased(#PB_Key_Escape)
          Quit = 1
        EndIf
      EndIf

      RenderWorld()
      
      StartDrawing(SpriteOutput(#Sprite))
        Box(0, 0, 128, 64, RGB(0,0,0))
        DrawingMode(#PB_2DDrawing_AlphaChannel)
        Box(0, 0, 128, 64)
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(FontID(#Font))
        FPS = Engine3DStatus(#PB_Engine3D_CurrentFPS)
        DrawText(0, 0, "FPS : "+Str(FPS), RGB(255,255,255))
      StopDrawing()
      DisplayTransparentSprite(#Sprite, 0, 0)
      
      FlipBuffers()

    Until Quit = 1 Or Event = #PB_Event_CloseWindow
  EndIf
EndIf
End
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

Re: Drawing text on a 3D screen?

Post by mrw »

Thank you Samuel!
That solved it for me perfectly!
User avatar
Roger Hågensen
User
User
Posts: 47
Joined: Wed Mar 25, 2015 1:06 pm
Location: Norway

Re: Drawing text on a 3D screen?

Post by Roger Hågensen »

If you have multiple monitors you might also want to consider a alternative way to show debug info.
Download DebugView from https://technet.microsoft.com/en-us/sys ... gview.aspx

Then you can use the Win32 API function OutputDebugString to print/show debug strings.

And example of use (with source) is found at http://www.purebasic.fr/english/viewtop ... ebugString



PS! Be aware that other software might also use OutputDebugString, and debug info via Kernel-mode DbgPrint may also show up. WHich is why DebugView let you select filters for the debug output.

DebugView is a nice way to have testers help you debug without having to include the PB debugger with your beta etc.
4 music albums under CC BY license available for free (any use, even commercial) at Skuldwyrm.no
Post Reply