Page 1 of 1

How to draw an OpenGL polygon with a texture?

Posted: Sun Oct 20, 2024 7:58 pm
by SPH
Hi all,

How would you apply a texture to the entire black shape?
I guess you need an image of the texture but I don't know how to apply it to everything that is black. :arrow: :idea:

PS: the screen is in OpenGL and I draw polygons on it.

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code : Lecteur de polygones "OPTIMIZED" (datas)   ;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PB6.01 - SPH(2023) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Falsam m'a fait la structure du code OpenGL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Nemerod a codé le moteur "rotation" des polygones ;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Global chemin$,sph_cmb,Dim sph_xx(1),Dim sph_yy(1),attente,attente_ok
Global numero,echelle_xf.f,echelle_yf.f,Resx,Resy,categorie$

;EnableExplicit

;-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

ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)
mix=ddw/2
miy=ddh/2

;********************* DPI *********************
echelle_xf.f=(1920/ddw)*DesktopResolutionX()
echelle_yf.f=(1080/ddh)*DesktopResolutionY()
;**********************************************

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


;-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, "Lecteur_polygons",#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()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter

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

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

;;;;;;;;;;;
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)

timer=ElapsedMilliseconds()

glClearColor_(0,0,0, 1.0)

chemin$="f:\Polygons_OPTIMIZED\"
chemin$=""
attente=500
attente_ok=0
numero=1


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 load_poly(categorie$,pol) ; ne jamais appeller un polygon "0.pol"
  If attente_ok=0 Or attente_ok<>pol
    attente_ok=pol
    Dim sph_xx(574)
    Dim sph_yy(574)
    
    For i=1 To 574/2
      Read.w sph_xx(i-1)
      Read.w sph_yy(i-1)
    Next
  EndIf
  
EndProcedure

Procedure affiche_poly(xxx,yyy,angle.f,dkx,dky,zoom.f,alpha.f)
  sph_cmb=sph_yy(0)
  sph_la=0
  Repeat
    glBegin_(#GL_POLYGON)
    glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
    glColor4f_(sph_xx(sph_la+1)/255,sph_yy(sph_la+1)/255,sph_xx(sph_la+2)/255,(sph_yy(sph_la+2)/255)*alpha)
    ;;;;;;
    For i=3 To sph_cmb-1
      sph_xxx.f=(sph_xx(sph_la+i)/echelle_xf)-xxx
      sph_yyy.f=(sph_yy(sph_la+i)/echelle_yf)-yyy
      sq.f=Sqr(sph_xxx*sph_xxx+sph_yyy*sph_yyy)
      ff.f = ATan2(sph_xxx,sph_yyy)+angle ; par Nemerod (sans lui, je n'y serai pas arrivé)
      centre_x=xxx+Cos(ff)*sq*zoom
      centre_y=yyy+Sin(ff)*sq*zoom
      glVertex2f_(centre_x+dkx,centre_y+dky);
    Next
    ;;;;;;;
    glEnd_()                      ; 
    sph_cmb=sph_yy(sph_la+i)
    sph_la+i
  Until sph_yy(sph_la)=0
  ;;;;;;;;;;;;;;;
EndProcedure

Procedure gestion()
  Repeat
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
            Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
            ;;;;;;;         
            Select EventType()
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;             
            EndSelect
        EndSelect
      Case #PB_Event_Menu
        Select EventMenu()
          Case 666
            ;timer=ElapsedMilliseconds()-timer
            ShowCursor_(1)
            End
        EndSelect
    EndSelect
    
  Until Event = 0
EndProcedure

;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Repeat
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  gestion()
  ;##############################################
  ;load_poly(caregorie$,numero) ; categorie$=par exemple "Generique"+str(numero) appelle le fichier "Generique1.pol"  
  ;affiche_poly(axe de rotation X,axe de rotation Y,angle de rotation(.f),decallage X,decallage Y,Zoom(.f),alpha)
  ;mix=milieu de l'ecran (en X)
  ;miy=milieu de l'ecran (en Y)
  ;##############################################
  load_poly("monstre",1)
  affiche_poly(0,0,0,0,0,1,1)
  
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
  ;   If ElapsedMilliseconds()-timer>2000
  ;     timer=ElapsedMilliseconds()
  ;     ;numero+1
  ;   EndIf
  
ForEver

End


DataSection
  Data.w 28,7,37,39,60,255,1919,0,1919,1079,0,1079,0,0,0,20,51,71,115,255
  Data.w 1908,589,1919,588,1919,813,1831,815,1772,833,1717,899,1528,946,1519,966,333,961,269,883
  Data.w 259,820,0,823,0,115,74,112,139,42,246,0,1869,0,0,10,51,71,115,255
  Data.w 0,801,0,963,67,964,213,886,211,847,267,814,157,775,0,19,61,82,141,255
  Data.w 236,0,155,544,162,598,300,818,343,825,348,643,380,646,713,881,1185,857,1267,816
  Data.w 1424,846,1577,814,1689,594,1695,540,1648,320,1629,0,0,9,61,82,141,255,700,861
  Data.w 723,900,485,900,427,844,398,838,381,672,0,7,61,82,141,255,616,816,381,674
  Data.w 379,618,611,775,0,22,64,110,239,255,380,0,256,255,235,333,257,496,363,646
  Data.w 380,648,412,705,606,750,630,710,646,711,695,734,1155,749,1211,720,1234,697,1384,661
  Data.w 1512,514,1509,354,1391,199,1344,0,0,6,64,110,239,255,1200,734,1179,768,1226,732
  Data.w 0,6,64,110,239,255,1519,641,1569,429,1590,426,0,7,64,110,239,255,1590,428
  Data.w 1542,271,1524,283,1567,437,0,7,64,110,239,255,1352,2,1407,123,1420,127,1362,0
  Data.w 0,7,64,110,239,255,1408,123,1523,285,1542,274,1422,129,0,14,0,138,255,255
  Data.w 388,0,307,205,290,355,342,524,556,625,1148,689,1352,602,1517,498,1504,342,1389,195
  Data.w 1345,0,0,17,0,0,4,255,528,958,448,1010,689,1010,643,706,600,607,648,161
  Data.w 551,233,506,354,436,415,420,496,390,516,382,552,406,618,412,700,0,7,0,0
  Data.w 4,255,0,1010,0,1079,1919,1071,1919,1009,0,10,0,0,4,255,680,980,725,951
  Data.w 936,887,913,956,924,989,927,1013,678,1031,0,18,0,0,4,255,932,889,1256,590
  Data.w 1425,402,1358,342,1305,237,1226,160,1169,41,1038,12,806,11,719,42,644,171,599,556
  Data.w 632,592,745,898,722,962,0,6,0,0,4,255,642,577,588,621,586,514,0,14
  Data.w 0,0,4,255,949,945,911,812,1256,571,1257,590,1154,742,1123,893,1123,948,1174,966
  Data.w 1189,1022,935,1033,931,964,0,17,0,0,4,255,1217,975,1161,1002,1178,1069,1412,1038
  Data.w 1405,1001,1368,976,1444,765,1466,602,1494,533,1483,491,1454,477,1423,400,1279,622,1229,768
  Data.w 0,7,0,0,4,255,1280,625,1249,577,1421,400,1425,407,0,7,0,0,4,255
  Data.w 109,1065,1919,1042,1919,1079,15,1079,0,7,188,148,130,255,943,514,937,579,945,579
  Data.w 959,512,0,7,188,148,130,255,907,664,898,705,913,705,914,663,0,7,188,148
  Data.w 130,255,847,688,830,688,837,641,844,643,0,7,188,148,130,255,817,503,831,539
  Data.w 837,538,833,502,0,7,108,25,0,255,789,437,795,453,810,456,806,437,0,7
  Data.w 108,25,0,255,966,423,961,439,979,440,985,425,0,0
EndDataSection

Re: How to draw an OpenGL polygon with a texture?

Posted: Thu Oct 24, 2024 12:27 pm
by SPH
ChatGPT gave me this code but it doesn't work. Nevertheless, it's a good base but it's incomplete.

Code: Select all

; --- Déclaration et initialisation de la fenêtre ---
InitSprite()
If OpenWindow(0, 0, 0, 800, 600, "Texture OpenGL avec PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
    
    ; --- Initialisation d'OpenGL ---
    glEnable_(#GL_TEXTURE_2D)  ; Activer les textures 2D
    glShadeModel_(#GL_SMOOTH)  ; Modèle de shading pour un lissage des couleurs
    glClearColor_(0.0, 0.0, 0.0, 1.0) ; Couleur de fond (noir)
    glClearDepth_(1.0)                ; Profondeur maximale du tampon de profondeur
    glEnable_(#GL_DEPTH_TEST)         ; Activer le test de profondeur
    glDepthFunc_(#GL_LEQUAL)          ; Mode de test de profondeur
    glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST) ; Correction de perspective pour la texture
    
    ; --- Charger une texture ---
    If LoadImage(0, "c:\image.bmp")
                                            ; Générer un identifiant pour la texture OpenGL
      Define textureID
      glGenTextures_(1, @textureID)
      
      ; Lier la texture (connexion entre PureBasic et OpenGL)
      glBindTexture_(#GL_TEXTURE_2D, textureID)
      
      ; Définir les paramètres de texture (filtrage, wrapping)
      glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
      glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
      
      ; Charger l'image PureBasic dans OpenGL comme texture
      glTexImage2D_(#GL_TEXTURE_2D, 0, 4, ImageWidth(0), ImageHeight(0), 0, #GL_RGBA, #GL_UNSIGNED_BYTE, ImageID(0))
    Else
      MessageRequester("Erreur", "Impossible de charger la texture.")
      End
    EndIf
    
    ; --- Boucle principale ---
    Repeat
      Event = WindowEvent()
      If Event = #PB_Event_CloseWindow
        Break
      EndIf
      
      ; Effacer l'écran et le tampon de profondeur
      glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      glLoadIdentity_()
      
      ; Appliquer une transformation pour centrer le polygone
      glTranslatef_(0.0, 0.0, -5.0)
      
      ; Lier la texture
      glBindTexture_(#GL_TEXTURE_2D, textureID)
      
      ; Dessiner un quadrilatère avec texture
      glBegin_(#GL_QUADS)
      
      ; Coordonnées de texture et sommets du polygone
      glTexCoord2f_(0.0, 0.0) : glVertex3f_(-1.0, -1.0, 0.0) ; Bas-gauche
      glTexCoord2f_(1.0, 0.0) : glVertex3f_(1.0, -1.0, 0.0)  ; Bas-droit
      glTexCoord2f_(1.0, 1.0) : glVertex3f_(1.0, 1.0, 0.0)   ; Haut-droit
      glTexCoord2f_(0.0, 1.0) : glVertex3f_(-1.0, 1.0, 0.0)  ; Haut-gauche
      
      glEnd_()
      
      ; Mettre à jour l'écran
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)
    
    ; Libérer la texture OpenGL
    glDeleteTextures_(1, @textureID)
    
  EndIf
EndIf
End
My idea would be to have a texture as big as the OpenGL screen opened in the PB editor.
And I think that an instruction would send the texture on the drawn polygon!
If you know the answer, please tell me!
Thanks

Re: How to draw an OpenGL polygon with a texture?

Posted: Thu Oct 24, 2024 1:23 pm
by StarBootics

Code: Select all

[/co; --- Déclaration et initialisation de la fenêtre ---
If OpenWindow(0, 0, 0, 800, 600, "Texture OpenGL avec PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
    
    ; --- Initialisation d'OpenGL ---
    glEnable_(#GL_TEXTURE_2D)  ; Activer les textures 2D
    glShadeModel_(#GL_SMOOTH)  ; Modèle de shading pour un lissage des couleurs
    glClearColor_(0.0, 0.0, 0.0, 1.0) ; Couleur de fond (noir)
    glClearDepth_(1.0)                ; Profondeur maximale du tampon de profondeur
    glEnable_(#GL_DEPTH_TEST)         ; Activer le test de profondeur
    glDepthFunc_(#GL_LEQUAL)          ; Mode de test de profondeur
    glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST) ; Correction de perspective pour la texture
    
    ; --- Charger une texture ---
    If LoadImage(0, "c:\image.bmp")
      If StartDrawing(ImageOutput(0))
        ; Générer un identifiant pour la texture OpenGL
        Define textureID
        glGenTextures_(1, @textureID)
        
        ; Lier la texture (connexion entre PureBasic et OpenGL)
        glBindTexture_(#GL_TEXTURE_2D, textureID)
        
        ; Définir les paramètres de texture (filtrage, wrapping)
        glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
        glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
        
        ; Charger l'image PureBasic dans OpenGL comme texture
        glTexImage2D_(#GL_TEXTURE_2D, 0, 4, ImageWidth(0), ImageHeight(0), 0, #GL_RGBA, #GL_UNSIGNED_BYTE, DrawingBuffer())
        
        StopDrawing()
        FreeImage(0)
      EndIf
      
    Else
      MessageRequester("Erreur", "Impossible de charger la texture.")
      End
    EndIf
    
    ; --- Boucle principale ---
    Repeat
      Event = WindowEvent()
      If Event = #PB_Event_CloseWindow
        Break
      EndIf
      
      ; Effacer l'écran et le tampon de profondeur
      glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      glLoadIdentity_()
      
      ; Appliquer une transformation pour centrer le polygone
      glTranslatef_(0.0, 0.0, -5.0)
      
      ; Lier la texture
      glBindTexture_(#GL_TEXTURE_2D, textureID)
      
      ; Dessiner un quadrilatère avec texture
      glBegin_(#GL_QUADS)
      
      ; Coordonnées de texture et sommets du polygone
      glTexCoord2f_(0.0, 0.0) : glVertex3f_(-1.0, -1.0, 0.0) ; Bas-gauche
      glTexCoord2f_(1.0, 0.0) : glVertex3f_(1.0, -1.0, 0.0)  ; Bas-droit
      glTexCoord2f_(1.0, 1.0) : glVertex3f_(1.0, 1.0, 0.0)   ; Haut-droit
      glTexCoord2f_(0.0, 1.0) : glVertex3f_(-1.0, 1.0, 0.0)  ; Haut-gauche
      
      glEnd_()
      
      ; Mettre à jour l'écran
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)
    
    ; Libérer la texture OpenGL
    glDeleteTextures_(1, @textureID)
    
  EndIf
EndIf
Endde]

Re: How to draw an OpenGL polygon with a texture?

Posted: Thu Oct 24, 2024 3:40 pm
by mk-soft

Code: Select all

If LoadImage(0, "c:\image.bmp")
How can you get into the main directory ‘C:\’ files, even if it is only for testing.
I consider this to be very critical (security-wise)

In addition, you always have to adjust the code for images for testing. You can also use the path to the images from the examples.

Re: How to draw an OpenGL polygon with a texture?

Posted: Thu Oct 24, 2024 3:57 pm
by SPH
Thank you very much.

However, as is, the code contains "artifacts". I added a necessary "InitSprite".

But the code does not work: "Invalid memory access" on line 30.

I put a BMP of size 800/600 (like OpenWindows) as texture.

Question: have you really tested it?
What does the code normally do? A mapped polygon??
==

Many thanks. I only ask to succeed. I am in love with OpenGL polygons :wink:

Code: Select all

InitSprite()
; --- Déclaration et initialisation de la fenêtre ---
If OpenWindow(0, 0, 0, 800, 600, "Texture OpenGL avec PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
    
    ; --- Initialisation d'OpenGL ---
    glEnable_(#GL_TEXTURE_2D)  ; Activer les textures 2D
    glShadeModel_(#GL_SMOOTH)  ; Modèle de shading pour un lissage des couleurs
    glClearColor_(0.0, 0.0, 0.0, 1.0) ; Couleur de fond (noir)
    glClearDepth_(1.0)                ; Profondeur maximale du tampon de profondeur
    glEnable_(#GL_DEPTH_TEST)         ; Activer le test de profondeur
    glDepthFunc_(#GL_LEQUAL)          ; Mode de test de profondeur
    glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST) ; Correction de perspective pour la texture
    
    ; --- Charger une texture ---
    If LoadImage(0, "d:\1_.bmp")
      If StartDrawing(ImageOutput(0))
        ; Générer un identifiant pour la texture OpenGL
        Define textureID
        glGenTextures_(1, @textureID)
        
        ; Lier la texture (connexion entre PureBasic et OpenGL)
        glBindTexture_(#GL_TEXTURE_2D, textureID)
        
        ; Définir les paramètres de texture (filtrage, wrapping)
        glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
        glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
        
        ; Charger l'image PureBasic dans OpenGL comme texture
        glTexImage2D_(#GL_TEXTURE_2D, 0, 4, ImageWidth(0), ImageHeight(0), 0, #GL_RGBA, #GL_UNSIGNED_BYTE, DrawingBuffer())
        
        StopDrawing()
        FreeImage(0)
      EndIf
      
    Else
      MessageRequester("Erreur", "Impossible de charger la texture.")
      End
    EndIf
    
    ; --- Boucle principale ---
    Repeat
      Event = WindowEvent()
      If Event = #PB_Event_CloseWindow
        Break
      EndIf
      
      ; Effacer l'écran et le tampon de profondeur
      glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      glLoadIdentity_()
      
      ; Appliquer une transformation pour centrer le polygone
      glTranslatef_(0.0, 0.0, -5.0)
      
      ; Lier la texture
      glBindTexture_(#GL_TEXTURE_2D, textureID)
      
      ; Dessiner un quadrilatère avec texture
      glBegin_(#GL_QUADS)
      
      ; Coordonnées de texture et sommets du polygone
      glTexCoord2f_(0.0, 0.0) : glVertex3f_(-1.0, -1.0, 0.0) ; Bas-gauche
      glTexCoord2f_(1.0, 0.0) : glVertex3f_(1.0, -1.0, 0.0)  ; Bas-droit
      glTexCoord2f_(1.0, 1.0) : glVertex3f_(1.0, 1.0, 0.0)   ; Haut-droit
      glTexCoord2f_(0.0, 1.0) : glVertex3f_(-1.0, 1.0, 0.0)  ; Haut-gauche
      
      glEnd_()
      
      ; Mettre à jour l'écran
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)
    
    ; Libérer la texture OpenGL
    glDeleteTextures_(1, @textureID)
    
  EndIf
EndIf
End
===========

@mk-soft:

i put this link to an image that actually exists on my computer. And i also pointed to images on the "d:/" partition but... nothing to do. It doesn't work.

Re: How to draw an OpenGL polygon with a texture?

Posted: Fri Oct 25, 2024 5:50 am
by JHPJHP
Hi SPH,

I sent you a link to a working solution. Please let me know if it worked for you.

Re: How to draw an OpenGL polygon with a texture?

Posted: Fri Oct 25, 2024 8:08 am
by SPH
Many thanks to you JHP JHP.
Today, I'm going out but as soon as I get back, I'm going to enjoy analyzing your code.

Thanks again! :idea: