Here is an abstract paintings generator. It is entirely made in OpenGL. I had fun doing it.
Know that it works with many Cos and Sin; so maybe you won't understand the code.
The code is ugly because it didn't have that purpose. The tables are formed after about 1 minute. Then it becomes anything.
Thanks to Falsam without whom I would never have managed to code in OpenGL (the code base is from him (maybe 2 years ago))
Code: Select all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;; p i c a s s o _ S P H ;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code : SPH ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PB6.0 (2022) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;EnableExplicit
;-CONSTANTS
Enumeration
#MainWindow
#OpenGLGadget
EndEnumeration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Réglage ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;nb=30 entre 0 (1 ligne) et x milliards (+++) ;D
nb=30
;alpha.f entre 0.005 (tres transparent) et 1 (pas transparent)
alpha.f=0.01
;rayon.f entre 1 (plein ecran) et 6 (cadrage serré)
rayon.f=2.5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;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
;********************* DPI *********************
dpix.f=DesktopResolutionX()
dpiy.f=DesktopResolutionY()
If dpix > 1
xf.f=(1920/ddw)*dpix
Else
xf = 1
EndIf
If dpiy > 1
yf.f=(1024/ddh)*dpiy
Else
yf = 1
EndIf
;**********************************************
;-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, "Picasso_SPH",#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)
;*********************************************************************************************************************************
;;;;;;;;;;;
timer=ElapsedMilliseconds()
glClearColor_(0,0,0, 1.0)
ShowCursor_(0)
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Dim sph_x1.f(nb)
Dim sph_y1.f(nb)
Dim sph_x2.f(nb)
Dim sph_y2.f(nb)
Dim sph_x3.f(nb)
Dim sph_y3.f(nb)
Dim sph_x4.f(nb)
Dim sph_y4.f(nb)
Dim sph_x11.f(nb);=(50+Random(50))/20000
Dim sph_y11.f(nb);=(50+Random(50))/20000
Dim sph_x21.f(nb);=(50+Random(50))/20000
Dim sph_y21.f(nb);=(50+Random(50))/20000
Dim sph_x31.f(nb);=(50+Random(50))/20000
Dim sph_y31.f(nb);=(50+Random(50))/20000
Dim sph_x41.f(nb);=(50+Random(50))/20000
Dim sph_y41.f(nb);=(50+Random(50))/20000
Dim sph_xx(nb)
Dim sph_yy(nb)
Dim sph_xx2(nb);=Cos(sph_x3(i))*ddw31
Dim sph_yy2(nb);=Sin(sph_y3(i))*ddh31
Dim sph_xx2(nb);+Cos(sph_x4(i))*ddw32
Dim sph_yy2(nb);+Sin(sph_y4(i))*ddh32
Dim r(4)
Dim v(4)
Dim b(4)
; reglage des couleurs ici (mais c'est délicat)
r(0)=255
v(0)=0
b(0)=0
r(1)=255
v(1)=255
b(1)=0
r(2)=0
v(2)=255
b(2)=0
r(3)=0
v(3)=0
b(3)=255
r(4)=255
v(4)=0
b(4)=255
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
For i=0 To nb
sph_x1.f(i)=(0+Random(200000))/100000;+0.13
sph_y1.f(i)=(0+Random(200000))/100000;0.152
sph_x2.f(i)=(0+Random(200000))/100000;0.1233
sph_y2.f(i)=(0+Random(200000))/100000;0.1402
sph_x3.f(i)=(0+Random(200000))/100000;+0.13
sph_y3.f(i)=(0+Random(200000))/100000;0.152
sph_x4.f(i)=(0+Random(200000))/100000;0.1233
sph_y4.f(i)=(0+Random(200000))/100000;0.1402
sph_x11.f(i)=(50+Random(100))/30000
sph_y11.f(i)=(50+Random(100))/30000
sph_x21.f(i)=(50+Random(100))/30000
sph_y21.f(i)=(50+Random(100))/30000
sph_x31.f(i)=(50+Random(100))/30000
sph_y31.f(i)=(50+Random(100))/30000
sph_x41.f(i)=(50+Random(100))/30000
sph_y41.f(i)=(50+Random(100))/30000
Next
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Repeat
For i=0 To nb
ddw21=Sin(sph_x1(i))*ddw/rayon.f
ddh21=Cos(sph_y1(i))*ddh/rayon.f
ddw22=Sin(sph_x2(i))*ddw/rayon.f
ddh22=Cos(sph_x2(i))*ddh/rayon.f
sph_xx(i)=Cos(sph_x1(i))*ddw21
sph_yy(i)=Sin(sph_y1(i))*ddh21
sph_xx(i)+Cos(sph_x2(i))*ddw22
sph_yy(i)+Sin(sph_y2(i))*ddh22
ddw31=Sin(sph_x3(i))*ddw/rayon.f
ddh31=Cos(sph_y3(i))*ddh/rayon.f
ddw32=Cos(sph_x4(i))*ddw/rayon.f
ddh32=Sin(sph_y4(i))*ddh/rayon.f
sph_xx2(i)=Cos(sph_x3(i))*ddw31
sph_yy2(i)=Sin(sph_y3(i))*ddh31
sph_xx2(i)+Sin(sph_x4(i))*ddw32
sph_yy2(i)+Cos(sph_y4(i))*ddh32
Next
;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
;MessageRequester("Timer", Str(timer)+" ms - "+Str(scene)+" frames",2)
; Debug scene
; Debug timer
ShowCursor_(1)
End
EndSelect
EndSelect
Until Event = 0
;##############################################
;##############################################
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; L I N E S ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
For i=0 To nb
glBegin_(#GL_LINES);
glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
glColor4f_(r(i%5),v(i%5),b(i%5),alpha)
glVertex2f_(ddw/2+sph_xx(i),ddh/2+sph_yy(i));
glVertex2f_(ddw/2-sph_xx2(i),ddh/2-sph_yy2(i));
glEnd_();
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sph_x1(i)+sph_x11(i);0.00951
sph_y1(i)+sph_y11(i);(50+Random(50))/10000;0.00732
sph_x2(i)+sph_x21(i);(50+Random(50))/10000;0.00621
sph_y2(i)+sph_y21(i);(50+Random(50))/10000;0.01
sph_x3(i)+sph_x31(i);0.00951
sph_y3(i)+sph_y31(i);(50+Random(50))/10000;0.00732
sph_x4(i)+sph_x41(i);(50+Random(50))/10000;0.00621
sph_y4(i)+sph_y41(i);(50+Random(50))/10000;0.01
Next
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