How to draw an OpenGL polygon with a texture?
Posted: Sun Oct 20, 2024 7:58 pm
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.
PS: the screen is in OpenGL and I draw polygons on it.
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.


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