OpenGLGadget + txt = impossible [ok]

Just starting out? Need help? Post your questions and find answers here.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

OpenGLGadget + txt = impossible [ok]

Post by SPH »

Hello,

to write text on an OpenGLGadget (1,0,0,1024,768), it's the obstacle course.
I was offered different solutions which are more than 100 lines of code and which do not satisfy me.
Here is an EXTREMELY simplified code.
Can you register text?

Thank you

Code: Select all

If ExamineDesktops()
  ddw=DesktopWidth(0)
  ddh=DesktopHeight(0)
Else
  ddw=1024
  ddh=768
EndIf


If OpenWindow(0, 0, 0, 0, 0, "texte openGL",#PB_Window_Maximize|#PB_Window_BorderLess)=0

  Beep_(500,250) : Delay(150) : Beep_(500,250)
  Debug ("OpenWindow() impossible")
  End
EndIf
SetWindowColor(0, #Black)
GL=OpenGLGadget(1,0,0,ddw,ddh)
If GL=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  Debug ("OpenGLGadget() impossible")
  End
EndIf




; write a text on OpenGLGadget...




  SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
Delay(2000)
Last edited by SPH on Mon Jan 04, 2021 12:01 pm, edited 2 times in total.
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible

Post by infratec »

Hi,
opengl is ... opengl.
To draw text you need a few things.

Meanwhile you can read:
https://users.cs.jmu.edu/bernstdh/web/c ... l-text.php
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible

Post by infratec »

A 'simple way' is to draw the text into an image and use glDrawPixels_()

Mouse right click to close the program.

Code: Select all

If ExamineDesktops()
  ddw=DesktopWidth(0)
  ddh=DesktopHeight(0)
Else
  ddw=1024
  ddh=768
EndIf


If OpenWindow(0, 0, 0, 0, 0, "texte openGL",#PB_Window_Maximize|#PB_Window_BorderLess)
  
  SetWindowColor(0, #Black)
  GL = OpenGLGadget(1,0,0,ddw,ddh)
  If GL
    
    If CreateImage(0, 200, 40) And StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText(0, 0, "Hello World!", #White)
      
      glRasterPos2i_(0, 0)
      glDrawPixels_(200, 40, #GL_RGB, #GL_UNSIGNED_BYTE, DrawingBuffer())
      
      StopDrawing()
      FreeImage(0)
    EndIf

    
    ; write a text on OpenGLGadget...
    
    
    
    SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
    
    Repeat
      Event = WaitWindowEvent()
      Select Event
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              If EventType() = #PB_EventType_RightClick
                Exit = #True
              EndIf
          EndSelect
      EndSelect
    Until Exit
    
  EndIf
EndIf
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible

Post by infratec »

As a procedure

Code: Select all

Procedure.i OpenGLDrawText(x.i, y.i, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          glRasterPos2i_(x, y)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible

Post by infratec »

With floats and a try to correct the coordinates:

Code: Select all

Procedure.i OpenGLDrawText(Gadget.i, x.f, y.f, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
          
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




If ExamineDesktops()
  ddw=DesktopWidth(0)
  ddh=DesktopHeight(0)
Else
  ddw=1024
  ddh=768
EndIf



If OpenWindow(0, 0, 0, 0, 0, "texte openGL",#PB_Window_Maximize|#PB_Window_BorderLess)
  
  SetWindowColor(0, #Black)
  GL = OpenGLGadget(1,0,0,ddw,ddh)
  If GL
    
    OpenGLDrawText(1, -1.0, 1.0, "Hello World", #Green)
    
    SetGadgetAttribute(1, #PB_OpenGL_FlipBuffers, #True)
    
    Repeat
      Event = WaitWindowEvent()
      Select Event
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              If EventType() = #PB_EventType_RightClick
                Exit = #True
              EndIf
          EndSelect
      EndSelect
    Until Exit
    
  EndIf
EndIf
P.S.: I have nothing to do with opengl, so please don't ask about more details :cry:
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: OpenGLGadget + txt = impossible

Post by Mijikai »

Text with wgl:

Code: Select all

EnableExplicit

;Minimal (2D) OpenGLGadget() Setup by Mijikai

Procedure.i oglContext(Window.i)
  Protected w.i
  Protected h.i
  Protected *font
  Protected lst.i
  Protected hdc.i
  *font = AllocateMemory($18)
  If *font
    w = WindowWidth(Window)
    h = WindowHeight(Window)
    If OpenGLGadget(0,0,0,w,h,#PB_OpenGL_NoDepthBuffer)
      glOrtho_(0,w,h,0,-1,1)
      glMatrixMode_(#GL_MODELVIEW)
      glLoadIdentity_()
      lst = glGenLists_(96)
      hdc = wglGetCurrentDC_()
      PokeI(*font,lst)
      PokeI(*font + $8,hdc)
      PokeI(*font + $10,SelectObject_(hdc,GetStockObject_(#SYSTEM_FIXED_FONT)))
      wglUseFontBitmaps_(hdc,32,96,lst)
      SetGadgetData(0,*font)
      ProcedureReturn #True
    EndIf
    FreeMemory(*font)
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure.i oglText(X.f,Y.f,Text.s,Color.i)
  Protected *font.Integer
  *font = GetGadgetData(0)
  glPushAttrib_(#GL_LIST_BIT)
  glListBase_(*font\i - 32)
  glColor3ubv_(@Color)
  glRasterPos2f_(X,Y + 10)
  glCallLists_(StringByteLength(Text),#GL_UNSIGNED_BYTE,Text)
  glPopAttrib_()
  ProcedureReturn
EndProcedure

Macro oglClear()
  glClear_(#GL_COLOR_BUFFER_BIT)
EndMacro

Macro oglSwapBuffer()
  SetGadgetAttribute(0,#PB_OpenGL_FlipBuffers,#True)
  Repeat
    Select WindowEvent()
      Case #PB_Event_CloseWindow:Break 2
      Case #Null:Break
    EndSelect
  ForEver
EndMacro

Procedure.i oglRelease()
  Protected *font.Integer
  *font = GetGadgetData(0)
  SelectObject_(PeekI(*font + $8),PeekI(*font + $10))
  glDeleteLists_(*font\i,96)
  FreeMemory(*font)
  FreeGadget(0)
  ProcedureReturn #Null
EndProcedure

Procedure.i Main()
  If OpenWindow(0,0,0,960,600,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
    If oglContext(0)
      Repeat
        oglClear()
        oglText(10,10,"Hello World!",$FF00FF) 
        oglSwapBuffer()
      ForEver
      oglRelease()
    EndIf
    CloseWindow(0)
  EndIf 
  ProcedureReturn #Null
EndProcedure

Main()

End
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: OpenGLGadget + txt = impossible

Post by falsam »

SPH wrote:I was offered different solutions which are more than 100 lines of code and which do not satisfy me.
Strange. It seemed to me that this request was resolved. You had a simple solution on December 11th ;)

< 100 Lines

Code: Select all

; Texte affiché sur une image RGBA
Procedure glDrawtext(x.f, y.f, Text.s, Font, Color = $FFEEF5FF)
  Protected Tmp = CreateImage(#PB_Any, 1, 1, 32, #PB_Image_Transparent)
  Protected iw, ih        ; Largeur auteur de l'image
  Protected DrawingBuffer ; Pointeur de données de l'image
 
  Protected TextureID = 0 ; Il restera unique
 
  ; Préparation de l'image format RGBA au dimension du texte
  ; Déterminer les dimensions du texte à afficher (iw & ih)
  StartDrawing(ImageOutput(tmp))
  DrawingFont(FontID(Font))
  iw = TextWidth(Text)
  ih = TextHeight(Text) 
  StopDrawing()
 
  ; On connait maintenant les dimensions du texte (iw & ih)
  ; Création de l'image
  ResizeImage(tmp, iw, ih)
 
  StartDrawing(ImageOutput(tmp))
  DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
  DrawingFont(FontID(Font))
  DrawText(0, 0, Text, Color, RGBA(0, 0, 0, 0))
  DrawingBuffer = DrawingBuffer() ;Récupération adresse mémoire de l'image
  StopDrawing()
 
  ; Préparation de la texture OpenGL
  glEnable_(#GL_TEXTURE_2D)
 
  ; Selection de la texture
  glBindTexture_(#GL_TEXTURE_2D, TextureID)
 
  ; Méthode de placage
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
 
  ; Création de la texture
  glTexImage2D_(#GL_TEXTURE_2D, 0, 4, iw, ih, 0, #GL_BGRA_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer)
 
  ; La texture est prete. On a plus besoin de l'image
  FreeImage(tmp)
 
  ; Modelisation texture 
  glMatrixMode_(#GL_TEXTURE)
  glLoadIdentity_()
  glPushMatrix_()
 
  ; Le coin 0,0 étnt en bas à droite de l'image, Flip vertical de la texture
  glScalef_(1, -1, 0)       
 
  ; Modelisation de la texture   
  glMatrixMode_(#GL_MODELVIEW)
  glLoadIdentity_()
  glPushMatrix_()
 
  ; Reset couleur
  glColor4f_(255, 255, 255, 255)
 
  ; Dessin
  glBegin_(#GL_QUADS)
 
  ; Coin haut gauche
  glTexCoord2f_(0, 0) : glVertex2i_(x, y)
 
  ; Coin haut droit
  glTexCoord2f_(1, 0) : glVertex2i_(x + iw, y)
 
  ; Coin bas droit
  glTexCoord2f_(1, 1) : glVertex2i_(x + iw, y + ih)
 
  ; Coin bas gauche
  glTexCoord2f_(0, 1) : glVertex2i_(x, y + ih)
 
  glEnd_() 
 
  glPopMatrix_()
 
  glDisable_(#GL_TEXTURE_2D)
EndProcedure
:arrow: Source https://www.purebasic.fr/french/viewtop ... 76#p209376

■ Demonstration

Code: Select all

EnableExplicit

Global Scene, ww = 800, wh = 600

Global Font0, Font1

; Plan de l'application
Declare Start()
Declare Render()
Declare Exit()

Start()

; Texte affiché sur une image RGBA
Procedure glDrawtext(x.f, y.f, Text.s, Font, Color = $FFEEF5FF)
  Protected Tmp = CreateImage(#PB_Any, 1, 1, 32, #PB_Image_Transparent)
  Protected iw, ih        ; Largeur auteur de l'image
  Protected DrawingBuffer ; Pointeur de données de l'image
 
  Protected TextureID = 0 ; Il restera unique
 
  ; Préparation de l'image format RGBA au dimension du texte
  ; Déterminer les dimensions du texte à afficher (iw & ih)
  StartDrawing(ImageOutput(tmp))
  DrawingFont(FontID(Font))
  iw = TextWidth(Text)
  ih = TextHeight(Text) 
  StopDrawing()
 
  ; On connait maintenant les dimensions du texte (iw & ih)
  ; Création de l'image
  ResizeImage(tmp, iw, ih)
 
  StartDrawing(ImageOutput(tmp))
  DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
  DrawingFont(FontID(Font))
  DrawText(0, 0, Text, Color, RGBA(0, 0, 0, 0))
  DrawingBuffer = DrawingBuffer() ;Récupération adresse mémoire de l'image
  StopDrawing()
 
  ; Préparation de la texture OpenGL
  glEnable_(#GL_TEXTURE_2D)
 
  ; Selection de la texture
  glBindTexture_(#GL_TEXTURE_2D, TextureID)
 
  ; Méthode de placage
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
 
  ; Création de la texture
  glTexImage2D_(#GL_TEXTURE_2D, 0, 4, iw, ih, 0, #GL_BGRA_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer)
 
  ; La texture est prete. On a plus besoin de l'image
  FreeImage(tmp)
 
  ; Modelisation texture 
  glMatrixMode_(#GL_TEXTURE)
  glLoadIdentity_()
  glPushMatrix_()
 
  ; Le coin 0,0 étnt en bas à droite de l'image, Flip vertical de la texture
  glScalef_(1, -1, 0)       
 
  ; Modelisation de la texture   
  glMatrixMode_(#GL_MODELVIEW)
  glLoadIdentity_()
  glPushMatrix_()
 
  ; Reset couleur
  glColor4f_(255, 255, 255, 255)
 
  ; Dessin
  glBegin_(#GL_QUADS)
 
  ; Coin haut gauche
  glTexCoord2f_(0, 0) : glVertex2i_(x, y)
 
  ; Coin haut droit
  glTexCoord2f_(1, 0) : glVertex2i_(x + iw, y)
 
  ; Coin bas droit
  glTexCoord2f_(1, 1) : glVertex2i_(x + iw, y + ih)
 
  ; Coin bas gauche
  glTexCoord2f_(0, 1) : glVertex2i_(x, y + ih)
 
  glEnd_() 
 
  glPopMatrix_()
 
  glDisable_(#GL_TEXTURE_2D)
EndProcedure

;-
Procedure Start()
  ; Chargement des fonts d'affichage du texte
  Font0 = LoadFont(#PB_Any, "Arial", 25)
  Font1 = LoadFont(#PB_Any, "Arial", 15)
 
  OpenWindow(#PB_Any, 0, 0, ww, wh, "OpenGL : Création d'une scene", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
 
  ; Fenetre openGl
  Scene = OpenGLGadget(#PB_Any, 0, 0, ww, wh, #PB_OpenGL_Keyboard)
 
  ; Activation textures et transparence
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
 
  ; Couleur d'arrière plan
  ; Chaque composant RGBA est divisé par 255
  glClearColor_(0.5, 0.5, 0.5, 1.0)
 
  ; Sélectionner la pile des matrices de projection
  glMatrixMode_(#GL_PROJECTION)
 
  ; Ré-initialise la matrice de projection
  ; et assure ainsi qu’une seule projection soit effectuée.
  glLoadIdentity_()
 
  ; glOrtho_(gauche, droit, bas, haut, proche, loin);
  glOrtho_(0, ww, wh, 0, -1, 1)
  glDisable_(#GL_DEPTH_TEST)
 
  glMatrixMode_(#GL_MODELVIEW);
  glLoadIdentity_()
 
  ; Déclencheur 
  BindEvent(#PB_Event_CloseWindow, @Exit())
 
  ; Boucle de rendu visuel
  Render()
EndProcedure

; Visualisation du résultat
Procedure Render() 
  Static N
 
  Repeat
   
    ; Efface le frame buffer et le Z-buffer
    glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
   
    ; Un peu de polygone pour SPH ;)
    glColor4f_(1, 0, 0, 1)
    glBegin_(#GL_POLYGON)
    glVertex2f_(300, 10)
    glVertex2f_(500, 10)
    glVertex2f_(400, 200)
    glEnd_()   
   
    ; Affichage d'un titre
    glDrawtext(10, 10, "Texte sur un OpenGLGadget", Font0, RGBA(255, 255, 255, 255))
   
    ; Dessin d'un compteur
    glDrawtext(10, 100, "Compteur", Font1, RGBA(255, 255, 255, 255))
    glDrawtext(130, 100, Str(N), Font1, RGBA(227, 139, 226, 255))
   
    ; Compteur + 1
    N + 1
   
    ; FlipBuffers
    SetGadgetAttribute(Scene, #PB_OpenGL_FlipBuffers, #True)
   
  Until WindowEvent() = #PB_Event_CloseWindow
EndProcedure

Procedure Exit() 
  End
EndProcedure
Enjoy SPH

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: OpenGLGadget + txt = impossible

Post by kernadec »

hello SPH
Did you retry my code which I rewrote
for a DrawText () mode or with a text under OpenGLGadget
with a key break?
now it should work on all windows version

https://www.purebasic.fr/french/viewtop ... 5&start=20
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: OpenGLGadget + txt = impossible

Post by SPH »

infratec wrote:With floats and a try to correct the coordinates:

Code: Select all

Procedure.i OpenGLDrawText(Gadget.i, x.f, y.f, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
          
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




If ExamineDesktops()
  ddw=DesktopWidth(0)
  ddh=DesktopHeight(0)
Else
  ddw=1024
  ddh=768
EndIf



If OpenWindow(0, 0, 0, 0, 0, "texte openGL",#PB_Window_Maximize|#PB_Window_BorderLess)
  
  SetWindowColor(0, #Black)
  GL = OpenGLGadget(1,0,0,ddw,ddh)
  If GL
    
    OpenGLDrawText(1, -1.0, 1.0, "Hello World", #Green)
    
    SetGadgetAttribute(1, #PB_OpenGL_FlipBuffers, #True)
    
    Repeat
      Event = WaitWindowEvent()
      Select Event
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              If EventType() = #PB_EventType_RightClick
                Exit = #True
              EndIf
          EndSelect
      EndSelect
    Until Exit
    
  EndIf
EndIf
P.S.: I have nothing to do with opengl, so please don't ask about more details :cry:
Hi, your procedure is working fine if it is alone. But when I incorporate it into my code, I no longer see the "HELLO WORLD".
A little idea (please)?
fill in an image on line 4 first :idea:

Code: Select all

UsePNGImageDecoder()
UseJPEGImageDecoder()

image$="f:\9332.jpg"

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration

;These two GL constants are used for texture creation. Don't change their values.
#GL_BGR = $80E0
#GL_BGRA = $80E1

If ExamineDesktops()
  ddw=DesktopWidth(0)
  ddh=DesktopHeight(0)
Else
  ddw=1024
  ddh=768
EndIf

;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-GLOBALS
Global.i Image1, Image2
Global.i Texture1, Texture2

;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Polygons_Maker",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh)
If screenGL=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf


SetupOpenGL()

;Load images.
Image1 = LoadImage(#PB_Any,image$)

Texture1 = SetupGLTexture(Image1)

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter

Dim poly_x(1000)
Dim poly_y(1000)
;;;;;;;;;;;;;;;;;;;

glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************

Procedure.i OpenGLDrawText(Gadget.i, x.f, y.f, Text$, Textcolor=#White)
 
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
 
 
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
     
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
         
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
         
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
         
          Result = #True
         
          StopDrawing()
        EndIf
      EndIf
     
    EndIf
    FreeImage(Image)
  EndIf
 
  ProcedureReturn Result
 
EndProcedure

;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************



Repeat ;- ..... Repeat
  
  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  Render()
  glReadPixels_(Resx, ddh-1-Resy, 1 , 1, #GL_RGBA, #GL_UNSIGNED_BYTE, @pixels )
  
  
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
          Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
          ;;;;;;;          
          Select EventType()
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_LeftClick
              Beep_(1500,33) ;;debug "Clic avec le bouton gauche de la souris"
              poly_x(0)+1
              poly_x(poly_x(0))=Resx 
              poly_y(poly_x(0))=Resy
              
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            Case #PB_EventType_RightDoubleClick 
              If poly_x(0)>0
                Beep_(500,400) ;: ;debug "Double-clic avec le bouton droit de la souris"
                Dim poly_x(1000)
                Dim poly_y(1000)
                
              EndIf
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_RightClick
              If poly_x(0)>0
                Beep_(500,33) ;;debug "Clic avec le bouton droit de la souris"
                poly_x(0)-1
                
              EndIf
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_MiddleButtonDown
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
          EndSelect
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;        
        Case 666
          End
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
      EndSelect
  EndSelect
  
  
    glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
    glBegin_(#GL_POLYGON)

    glVertex2f_(0,0)
    glVertex2f_(192,0)
    glVertex2f_(128,128)
    glVertex2f_(0,128)
    glEnd_()
    
  
    OpenGLDrawText(1, 0, 0, "Hello World", #Green) ;-**********************  HELLO WORLD
    
    
    
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
ForEver

;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************


Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure

Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure

Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure

Procedure SetupGLTexture(ImageHandle.i)
  
  Define.i ImageW, ImageH, ImageD
  Define.i MemoryAddress
  Define.i TextureHandle
  
  If IsImage(ImageHandle) = 0
    ProcedureReturn #False
  EndIf
  
  ImageD = ImageDepth(ImageHandle, #PB_Image_InternalDepth)
  
  StartDrawing(ImageOutput(ImageHandle))
  MemoryAddress = DrawingBuffer()
  StopDrawing()
  
  If MemoryAddress = 0
    ProcedureReturn #False
  EndIf
  
  glGenTextures_(1, @TextureHandle)
  glBindTexture_(#GL_TEXTURE_2D, TextureHandle)
  
  ImageW = ImageWidth(ImageHandle)
  ImageH = ImageHeight(ImageHandle)
  
  If ImageD = 32
    glTexImage2D_(#GL_TEXTURE_2D, 0, 4, ImageW, ImageH, 0, #GL_BGRA, #GL_UNSIGNED_BYTE, MemoryAddress)
  Else
    glTexImage2D_(#GL_TEXTURE_2D, 0, 3, ImageW, ImageH, 0, #GL_BGR, #GL_UNSIGNED_BYTE, MemoryAddress)
  EndIf
  
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
  ;   glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
  
  ProcedureReturn TextureHandle
  
EndProcedure
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible

Post by infratec »

Hi,

you had already answers to your problem. So why asking again?
My solution was only an idea how to do it. As I told you, I have no experience with OpenGL.

Also: I don't start your codes anymore, because I have always trouble to terminate your black fullscreen programs.
And I don't want to do hard PC resets only because I want to help someone.

Main point: I have no image "f:\9332.jpg"

And use EnableExplicit as your firts command.
And (horrible for me) don't put procdures inside of the normal main code.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: OpenGLGadget + txt = impossible

Post by Mijikai »

Make the code properly runnable... include the image.
Add EnableExplicit so we dont wast time checking variables.
Also add errorhandling - procedures/functions often return results!
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible

Post by infratec »

If I change the coordinates to 100, 100 I see my text.

You have to use correct coordinates or change my code to your needs.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: OpenGLGadget + txt = impossible

Post by SPH »

HO, nice, if I change coordonate, I see it !!

Thx! 8)
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible [Resolve]

Post by infratec »

Now you can use PB coordinates:

Code: Select all

Procedure.i OpenGLDrawTextI(Gadget.i, x.i, y.i, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          glOrtho_(0.0, GadgetWidth(Gadget), GadgetHeight(Gadget), 0.0, 0.0, 1.0)
          glRasterPos2i_(x, y + TextHeight)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible [Resolve]

Post by infratec »

Added push and pop and real transparence:

Code: Select all

EnableExplicit

Procedure.i OpenGLDrawTextF(Gadget.i, x.f, y.f, Text$, FrontColor.i=#White, BackColor.i=-1)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
  
  
  Image = CreateImage(#PB_Any, 1, 1, 32, #PB_Image_Transparent)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_AllChannels)
          If BackColor = -1
            BackColor = $00000000
          Else
            BackColor = $FF000000 | BackColor
          EndIf
          DrawText(0, 0, Text$, $FF000000 | FrontColor, BackColor)
          
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
          
          glPushMatrix_()
          
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGRA_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          glPopMatrix_()
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure.i OpenGLDrawTextI(Gadget.i, x.i, y.i, Text$, FrontColor.i=#White, BackColor.i=-1)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, *PixelBuffer
  
  
  Image = CreateImage(#PB_Any, 1, 1, 32, #PB_Image_Transparent)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_AllChannels)
          If BackColor = -1
            BackColor = $00000000
          Else
            BackColor = $FF000000 | BackColor
          EndIf
          DrawText(0, 0, Text$, $FF000000 | FrontColor, BackColor)
          
          glPushMatrix_()
          
          glOrtho_(0.0, GadgetWidth(Gadget), GadgetHeight(Gadget), 0.0, 0.0, 1.0)
          glRasterPos2i_(x, y + TextHeight)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGRA_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          glPopMatrix_()
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




If OpenWindow(0, 0, 0, 800, 600, "Text and OpenGL", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  
  If OpenGLGadget(1, 0, 0, 800, 600)
    
    glEnable_(#GL_BLEND)
    glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
    glClearColor_(1.0, 0.0, 1.0, 0.0)
    glClear_(#GL_COLOR_BUFFER_BIT)
    
    OpenGLDrawTextF(1, 0.0, 0.0, "Hello World", #Blue)
    
    OpenGLDrawTextI(1, 0, 0, "Hello World", #Green)
    
    OpenGLDrawTextI(1, 50, 50, "Hello World", #Yellow, #Green)
    
    SetGadgetAttribute(1, #PB_OpenGL_FlipBuffers, #True)
    
    
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
    
  EndIf
EndIf
I leave the Float coordinates as they are.
The Integer coordinates are translated to PB coordinates.
I don't know if this is good or bad.

But as already written, I have no knowledge about OpenGL
Post Reply