Voici des vagues d'ondes en OpenGL.
Code : Tout sélectionner
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code de demonstration OpenGL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PB6.0 - SPH(2022) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;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
If ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)
Else
ddw=1024
ddh=768
EndIf
dw2=ddw/2
dh2=ddh/2
;-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, "Titre",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
MessageRequester("Erreur","OpenWindow() impossible")
End
EndIf
screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh)
If screenGL=0
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)
;*********************************************************************************************************************************
dw1=dw/4
dw3=(dw/4)*3
rr1.f=Random(5)+Random(1000)/1000
rr2.f=Random(5)+Random(1000)/1000
vv1.f=Random(5)+Random(1000)/1000
vv2.f=Random(5)+Random(1000)/1000
bb1.f=Random(5)+Random(1000)/1000
bb2.f=Random(5)+Random(1000)/1000
or1.f=rr1
or2.f=rr2
ov1.f=vv1
ov2.f=vv2
ob1.f=bb1
ob2.f=bb2
cr1.f=0.001+Random(1000)/10000
cr2.f=0.001+Random(1000)/10000
cv1.f=0.001+Random(1000)/10000
cv2.f=0.001+Random(1000)/10000
cb1.f=0.001+Random(1000)/10000
cb2.f=0.001+Random(1000)/10000
a1.f=0.001+Random(1000)/35000
a2.f=0.002+Random(1000)/35000
a3.f=0.003+Random(1000)/35000
a4.f=0.001+Random(1000)/35000
a5.f=0.002+Random(1000)/35000
a6.f=0.003+Random(1000)/35000
;;;;;;;;;;
arr1.f=Random(5)+Random(1000)/1000
arr2.f=Random(5)+Random(1000)/1000
avv1.f=Random(5)+Random(1000)/1000
avv2.f=Random(5)+Random(1000)/1000
abb1.f=Random(5)+Random(1000)/1000
abb2.f=Random(5)+Random(1000)/1000
aor1.f=arr1
aor2.f=arr2
aov1.f=avv1
aov2.f=avv2
aob1.f=abb1
aob2.f=abb2
acr1.f=0.001+Random(1000)/10000
acr2.f=0.001+Random(1000)/10000
acv1.f=0.001+Random(1000)/10000
acv2.f=0.001+Random(1000)/10000
acb1.f=0.001+Random(1000)/10000
acb2.f=0.001+Random(1000)/10000
aa1.f=0.001+Random(1000)/35000
aa2.f=0.002+Random(1000)/35000
aa3.f=0.003+Random(1000)/35000
aa4.f=0.001+Random(1000)/35000
aa5.f=0.002+Random(1000)/35000
aa6.f=0.003+Random(1000)/35000
;;;;;;;;;;;
timer=ElapsedMilliseconds()
glClearColor_(0,0,0, 1.0)
; ShowCursor_(0)
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;### D E B U T ###;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Repeat
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
;*****
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
End
EndSelect
EndSelect
Until Event = 0
;##############################################
;##############################################
;##############################################
;##############################################
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; Traçage de vos polygones ;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
For i=0 To ddh-1
r1=63+Cos(rr1)*63
r2=63+Sin(rr2)*63
v1=63+Cos(vv1)*63
v2=63+Cos(vv2)*63
b1=63+Sin(bb1)*63
b2=63+Sin(bb2)*63
;;;
rr=r1+v1+b1
vv=r2+v2+b2
;LineXY(0,i,dw,i,RGB(b1+b2,r1+r2,v1+v2))
;LineXY(dw2,i,dw,i,RGB(b1+b2,r1+r2,v1+v2))
; LineXY(ddw2-rr,i,ddw2+vv,i,RGB(r1+r2,v1+v2,b1+b2))
glBegin_(#GL_LINES);glBegin_(#GL_POLYGON)
glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
glColor4f_((r1+r2)/255,(v1+v2)/255,(b1+b2)/255,1);(rr+vv)/768)
glVertex2f_(dw2-rr,i)
glVertex2f_(dw2+vv,i)
glEnd_()
rr1+a1
rr2+a2
vv1+a3
vv2+a4
bb1+a5
bb2+a6
Next
;;;;;;;;;;;;;;;;;;
For i=0 To ddw-1
;;;r1=63+Cos(rr1)*63
ar1=63+Cos(arr1)*63
ar2=63+Sin(arr2)*63
av1=63+Cos(avv1)*63
av2=63+Cos(avv2)*63
ab1=63+Sin(abb1)*63
ab2=63+Sin(abb2)*63
arr=ar1+av1+ab1
avv=ar2+av2+ab2
;LineXY(0,i,dw,i,RGB(b1+b2,r1+r2,v1+v2))
;LineXY(dw2,i,dw,i,RGB(b1+b2,r1+r2,v1+v2))
; LineXY(ddw2-rr,i,ddw2+vv,i,RGB(r1+r2,v1+v2,b1+b2))
glBegin_(#GL_LINES);glBegin_(#GL_POLYGON)
glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
glColor4f_((ar1+ar2)/255,(av1+av2)/255,(ab1+ab2)/255,0.5);(rr+vv)/768)
glVertex2f_(i,dh2-arr)
glVertex2f_(i,dh2+avv)
glEnd_()
arr1+aa1
arr2+aa2
avv1+aa3
avv2+aa4
abb1+aa5
abb2+aa6
Next
or1+cr1
rr1=or1
or2+cr2
rr2=or2
ov1+cv1
vv1=ov1
ov2+cv2
vv2=ob2
ob1+cb1
bb1=ob1
ob2+cb2
bb2=ob2
;;;
aor1+acr1
arr1=aor1
aor2+acr2
arr2=aor2
aov1+acv1
avv1=aov1
aov2+acv2
avv2=aob2
aob1+acb1
abb1=aob1
aob2+acb2
abb2=aob2
;;;;;;;;;;;;;;;
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
;Delay à virer :
; Delay(1000)
ForEver
End
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