Page 1 of 1

[SOLVED]Text over 3d entities

Posted: Wed Dec 07, 2011 4:14 pm
by einander
Image
I need to draw over each ball of this example:
http://www.purebasic.fr/english/viewtop ... 12&t=48467
a text label, like the one on the image. Any hint?
Thanks in advance.

Re: Text over 3d entities

Posted: Wed Dec 07, 2011 5:15 pm
by IdeasVacuum
...can't tell from the image what you want to do - just a 2d label over the image? Or a label on each ball? Couldn't the label be a small surface, 'attached' logically to it's ball but always parallel and orientated to the screen, using a texture for the text?

Re: Text over 3d entities

Posted: Wed Dec 07, 2011 6:18 pm
by einander
Couldn't the label be a small surface, 'attached' logically to it's ball but always parallel and orientated to the screen, using a texture for the text?
Yes, that is what I need.

I've tried this with Billboards; each BillBoardGroup (one for each Billboard) with the text is always facing the screen, and moving with his ball. I can move the BillBoardGroup inside the ball, or around the ball; but I'm failing to put the text always on the visible surface side of the ball (as in the image).

Attach a small surface with text to a ball seems the good way. But how?

Thanks for your interest.

Re: Text over 3d entities

Posted: Wed Dec 07, 2011 7:20 pm
by idle
guess you'll just have to texture the sphere with the symbols and rotate them to the camera
user rotates camera around the ball and stick model then you loop through the balls and rotate them
to face the camera

What does rotatematerial do? doesn't specify an axis so I guess it's only screen view.

Re: Text over 3d entities

Posted: Wed Dec 07, 2011 8:07 pm
by einander
Thanks Idle.
I'll investigate further on RotateMaterial (never used it before) and on how to found the correct angle for #PB_Material_Fixed to face the screen.

Re: Text over 3d entities

Posted: Wed Dec 07, 2011 10:00 pm
by Comtois
with a billboard
- 1 - get direction btw an entity and camera

Code: Select all

      Direction\x = CameraX(0) - EntityX(0)
      Direction\y = CameraY(0) - EntityY(0)
      Direction\z = CameraZ(0) - EntityZ(0)
- 2 - Normalize Direction
- 3 - BillBoardPosition (or what you want) = Position Entity + Direction * Distance (Distance = Rayon + Delta)

Code: Select all

#CameraSpeed = 2
Enumeration
  #MainWindow 
  #Editor
EndEnumeration

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY, Rayon = 5, Distance 
Distance = Rayon + 0.5

Define.Vector3 Direction 

Declare Normalize(*V.Vector3)

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    KeyboardMode(#PB_Keyboard_International)  
    
    ; First create materials
    ;
    CreateMaterial(0, LoadTexture(0,"Dirt.jpg"))
    CreateMaterial(1, LoadTexture(1,"viseur-jeux.png"))
    MaterialBlendingMode(1, #PB_Material_AlphaBlend)

    
    ; Meshes
    ;
    CreateSphere(0, Rayon, 40, 40)


    ; Entities
    ;
    CreateEntity(0, MeshID(0), MaterialID(0))
    
    ;-Billboard
    CreateBillboardGroup(0, MaterialID(1), Rayon/4, Rayon/4)
    AddBillboard(#PB_Any, 0, 0, 0, 0)
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    CameraLocate(0, 0, 3, 20)
    CameraLookAt(0, 0, 3, 0)
    
    ; Light
    ;
    CreateLight(0, RGB(255, 255, 255), 560, 90, 500)
    AmbientColor($888888)
    
    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

      EndIf
      
      RotateEntity(0, 1, 1, 1, #PB_Relative)
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(0, KeyX, 0, KeyY)
          
      Direction\x = CameraX(0) - EntityX(0)
      Direction\y = CameraY(0) - EntityY(0)
      Direction\z = CameraZ(0) - EntityZ(0)
      
      Normalize(@Direction)
      BillboardGroupLocate(0, EntityX(0) + Direction\x * Distance, EntityY(0) + Direction\y * Distance, EntityZ(0) + Direction\z * Distance) 
      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

Procedure Normalize(*V.Vector3)
  Define.f magSq, oneOverMag 
  
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z 
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf  
  
EndProcedure

Re: Text over 3d entities

Posted: Thu Dec 08, 2011 12:48 am
by einander
Thanks Comtois!
Your great example does what I was looking for. And more! :D
Cheers!