Algorithm to create random polygon?
Algorithm to create random polygon?
Does anyone have a neat PB code to create random polygons like this Python code? The goal is to generate random areas on a map.
Re: Algorithm to create random polygon?
The Python code looks like easy convert to PB!
Re: Algorithm to create random polygon?
Hi "Houseleek"
Here's a little code that demonstrates the power of OpenGL polygons and is easy to scale down and modify...
Tell me what you think. THX
Here's a little code that demonstrates the power of OpenGL polygons and is easy to scale down and modify...
Tell me what you think. THX


Code: Select all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code : Lecteur de polygones "OPTIMIZED" (datas) ;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PB6.1 - SPH(2023) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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$
Global ultimated_NANO_ALPHA.q
;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()
; Debug "Send me that :"
; Debug Str(ddw)+" / "+Str(ddh)
; Debug Str(echelle_xf)+" / "+Str(echelle_yf)
; Debug "==="
;**********************************************
;-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(2326)
Dim sph_yy(2326)
For i=1 To 1163
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("larson_",1)
;centre
affiche_poly(mix,miy,0,0,0,1,1)
affiche_poly(mix,miy,Cos(cos_f.f)*2,0,0,0.5+Sin(sin_f.f),0.8)
;gauche
affiche_poly(mix,miy,Sin(cos_f.f),120-mix,100-miy,Sin(cos_f.f)/3-0.32,1)
affiche_poly(mix,miy,Cos(cos_f.f),120-mix,-100+miy,Sin(cos_f.f)/3-0.4,1)
;droite
affiche_poly(mix,miy,Sin(cos_f.f),-120+mix,100-miy,Sin(cos_f.f)/3-0.35,1)
affiche_poly(mix,miy,Cos(cos_f.f),-120+mix,-100+miy,0.445+Sin(cos_f.f)/3-0.38,1)
;gauche centre
affiche_poly(mix,miy,Sin(cos_f.f),360-mix,300-miy,Cos(sin_f.f)/4+0.44,0.6)
affiche_poly(mix,miy,Cos(cos_f.f),360-mix,-300+miy,Cos(sin_f.f)/4+0.44,0.6)
;droite centre
affiche_poly(mix,miy,Sin(sin_f.f),-360+mix,300-miy,Cos(cos_f.f)/4+0.44,0.6)
affiche_poly(mix,miy,Cos(cos_f.f),-360+mix,-300+miy,Sin(cos_f.f)/4+0.44,0.6)
;gauche centre2
affiche_poly(mix,miy,Sin(cos_f.f),600-mix,500-miy,Cos(cos_f.f)/4+0.44,0.1)
affiche_poly(mix,miy,Cos(cos_f.f),600-mix,-500+miy,Cos(cos_f.f)/4+0.44,0.1)
;droite centre2
affiche_poly(mix,miy,Sin(cos_f.f),-600+mix,500-miy,Cos(sin_f.f)/4+0.44,0.1)
affiche_poly(mix,miy,Cos(sin_f.f),-600+mix,-500+miy,Sin(cos_f.f)/4+0.44,0.1)
cos_f.f+0.007
sin_f.f+0.009
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
; If ElapsedMilliseconds()-timer>2000
; timer=ElapsedMilliseconds()
; ;numero+1
; EndIf
ForEver
End
DataSection
Data.w 107,9,85,87,94,255,162,1079,232,1028,360,1012,510,1007,427,1055,399,1079,0,7,85,87,94,255,1530,1051,1563,1062,1589,1079,1544,1079,0,13,106,110,116,255,556,1031,574,1079,398,1079,433,1048,511,1003,626,958,703,915,732,922,735,981,570,1077,0,8,106,110,116,255,1520,1064,1508,1042,1532,1052,1547,1079,1502,1079,0,12,102,105,107,255,1401,1013,1231,1079,1305,1004,1409,913,1432,962,1506,1038,1520,1064,1505,1079,1227,1079,0,10,102,105,107,255,1408,911,1401,888,1372,878,1376,906,1384,925,1382,950,1415,932,0,8,122,93,55,255,570,1079,619,1042,731,983,750,1041,756,1079,0,16,122,93,55,255,941,1079,755,1079,730,983,721,919,788,927,856,912,983,847,1106,751,1135,715,1123,760,1105,832,1094,877,1047,953,0,19,151,127,93,255,1229,1079,1366,956,1380,921,1373,888,1368,772,1313,441,1308,436,1287,491,1257,518,1242,519,1185,555,1142,681,1108,818,1086,889,1029,975,938,1079,0,12,151,127,93,255,1196,466,1204,420,1239,424,1248,450,1261,462,1258,485,1245,523,1205,561,1178,559,0,15,122,93,55,255,1285,380,1212,355,1218,424,1241,425,1243,443,1260,460,1254,503,1252,515,1271,508,1283,493,1297,450,1312,397,0,24,122,93,55,255,618,600,667,484,692,372,693,304,646,75,531,71,512,280,544,381,551,543,585,669,645,845,668,900,699,916,730,919,716,850,740,812,764,789,764,780,777,705,764,557,629,523,0,35,151,127,93,255,878,718,768,708,708,713,697,711,696,729,698,757,704,771,727,782,765,783,728,825,716,851,720,898,728,921,764,928,813,925,874,911,951,868,1069,786,1124,735,1142,688,1161,622,1195,0,665,0,691,342,671,449,757,599,747,624,695,636,693,668,707,678,774,692,778,712,0,7,151,127,93,255,740,556,643,533,665,483,679,428,0,10,151,127,93,255,683,521,647,527,629,570,648,591,704,587,780,597,776,519,0,12,122,93,55,255,880,261,1041,263,929,300,857,396,845,400,800,370,785,338,781,312,787,288,0,15,203,191,171,255,919,349,978,341,1001,359,997,379,979,391,935,402,889,403,862,390,845,377,841,360,871,326,981,341,0,12,203,191,171,255,685,358,686,379,673,392,644,399,611,398,584,382,576,355,594,335,665,341,0,13,114,96,68,255,921,328,890,331,884,353,891,378,907,393,925,395,944,390,960,372,961,352,954,333,0,11,114,96,68,255,619,334,612,346,613,367,626,383,650,388,667,378,673,355,664,340,0,9,114,96,68,255,867,335,845,359,833,359,865,323,949,330,939,333,0,9,114,96,68,255,940,330,982,337,994,350,965,338,866,332,872,325,0,10,114,96,68,255,662,342,681,358,683,355,666,338,592,331,584,335,593,334,0,7,114,96,68,255,598,336,581,357,575,357,591,333,0,9,114,96,68,255,623,316,595,321,595,325,625,319,652,326
Data.w 652,322,0,11,114,96,68,255,877,310,939,314,976,324,913,316,864,316,846,324,845,321,862,311,0,10,200,185,155,255,934,335,927,341,929,350,934,358,946,357,954,348,954,338,0,10,200,185,155,255,664,343,668,349,664,357,654,357,645,352,644,346,649,339,0,8,114,96,68,255,796,351,797,367,802,375,800,357,800,350,0,9,114,96,68,255,729,781,701,769,699,769,713,780,745,786,763,782,0,7,114,96,68,255,777,707,837,721,839,725,781,713,0,7,114,96,68,255,782,714,710,715,713,713,776,707,0,8,114,96,68,255,709,715,688,707,685,702,709,713,715,713,0,8,114,96,68,255,687,705,671,708,674,704,688,700,693,706,0,9,114,96,68,255,707,682,690,669,692,666,709,678,723,672,722,678,0,11,114,96,68,255,735,604,749,607,746,612,734,607,720,612,712,622,711,615,726,604,0,9,114,96,68,255,676,607,696,631,691,632,673,610,648,589,658,591,0,7,114,96,68,255,734,585,651,589,660,594,734,589,0,7,114,96,68,255,733,587,757,595,757,590,733,585,0,9,114,96,68,255,650,588,631,571,630,575,645,590,658,598,658,593,0,10,114,96,68,255,672,461,657,511,631,562,631,561,663,481,686,384,690,385,0,8,114,96,68,255,1308,437,1287,490,1270,509,1293,470,1302,428,0,8,114,96,68,255,1268,508,1234,522,1215,542,1250,517,1268,515,0,8,114,96,68,255,1210,558,1218,544,1231,530,1220,531,1209,552,0,10,114,96,68,255,1113,1040,1138,990,1137,980,1112,1037,1079,1079,1087,1079,1117,1036,0,9,114,96,68,255,1145,987,1163,913,1163,892,1167,892,1155,966,1149,994,0,10,114,96,68,255,1175,894,1179,854,1179,842,1184,840,1178,898,1165,948,1163,937,0,7,114,96,68,255,1145,677,1149,677,1126,739,1120,739,0,7,114,96,68,255,1125,731,1080,778,1104,760,1128,737,0,8,114,96,68,255,1094,765,1008,833,1003,833,1060,798,1101,763,0,7,114,96,68,255,1007,831,933,878,936,880,1005,837,0,8,114,96,68,255,929,881,877,907,877,910,904,902,939,882,0,7,114,96,68,255,883,905,812,924,812,927,861,917,0,7,114,96,68,255,818,923,755,924,755,928,793
Data.w 928,0,7,114,96,68,255,756,925,714,918,715,923,766,931,0,9,114,96,68,255,1243,508,1245,520,1251,518,1250,506,1263,485,1263,475,0,7,114,96,68,255,1262,481,1261,462,1254,444,1258,479,0,7,114,96,68,255,1256,451,1241,431,1236,431,1248,450,0,7,114,96,68,255,1239,433,1242,415,1239,413,1235,433,0,10,114,96,68,255,1241,416,1254,407,1267,393,1267,387,1248,407,1238,412,1234,427,0,8,114,96,68,255,1272,494,1276,468,1271,454,1276,481,1270,495,0,11,114,96,68,255,1262,452,1273,463,1272,467,1277,463,1268,450,1251,448,1256,456,1269,456,0,11,114,96,68,255,1197,611,1190,602,1188,591,1194,590,1202,606,1221,610,1222,613,1205,615,0,7,114,96,68,255,683,356,678,336,674,336,676,354,0,9,114,96,68,255,720,922,727,983,757,1079,758,1079,748,1030,732,985,0,7,114,96,68,255,746,1049,740,1079,757,1079,748,1038,0,19,48,43,22,255,1367,815,1371,861,1382,852,1402,828,1434,746,1426,687,1419,673,1448,561,1436,538,1516,212,1508,150,1369,86,1315,445,1308,505,1303,520,1243,590,0,10,48,43,22,255,1420,798,1415,723,1435,729,1426,797,1416,828,1412,828,1420,789,0,11,48,43,22,255,1451,680,1464,723,1468,755,1464,756,1454,706,1440,686,1405,668,1395,620,0,14,48,43,22,255,1445,554,1460,591,1456,637,1451,657,1452,626,1444,646,1442,649,1435,611,1432,643,1424,646,1411,508,0,10,48,43,22,255,1460,525,1483,505,1488,495,1479,501,1455,517,1413,525,1423,542,0,11,48,43,22,255,1438,512,1458,494,1495,427,1515,374,1522,327,1523,256,1515,198,1382,472,0,10,48,43,22,255,1323,780,1332,801,1343,820,1343,815,1335,780,1348,763,1287,629,0,10,48,43,22,255,1293,777,1324,733,1309,640,1298,742,1285,775,1269,798,1272,799,0,14,48,43,22,255,1285,744,1294,728,1316,701,1289,565,1273,591,1283,724,1272,759,1265,769,1268,771,1283,751,1301,721,0,8,48,43,22,255,1284,693,1269,679,1237,593,1255,579,1305,581,0,16,48,43,22,255,1243,671,1263,690,1272,693,1272,688,1253,673,1253,660,1266,655,1245,586,1219,612,1220,626,1225,646,1246,677,1263,687,0,21,48,43,22,255,1365,174,1320,446,1304,435,1297,421,1294,408,1281,401,1265,388,1249,371,1159,349,1157,322,1130,287,1116,240,1120,221,1077,167,1058,123,1042,29,1059,0,1395,0,0,13,48,43,22,255,1029,86,1033,166,1038,191,1041,191,1037,146,1043,106,1054,70,1053,12,1040,29,1032,57,0,8,48,43,22,255,899,106,928,64,942,28,944,1,933,24,0,8,48,43,22,255,944,0,937,38,920,55,923,10,916,0,0,20,48,43,22,255,916,0,902,47,883,93,856,136,827,166,799,186,775,208,777,203,822,135,836,81,798,136,639,235,631,202,619,177,632,149,645,92,491,0,0,12,48,43,22,255,655,269,679,310,699,341,709,349,704
Data.w 338,694,300,699,222,706,205,637,231,0,8,48,43,22,255,792,138,754,173,696,217,642,235,636,208,0,15,48,43,22,255,598,106,610,158,625,189,624,152,629,111,654,80,652,41,242,0,574,101,580,141,597,182,605,193,0,14,48,43,22,255,415,0,572,90,540,155,513,260,513,439,473,396,454,370,421,344,393,289,256,101,283,0,0,12,48,43,22,255,359,303,391,356,421,397,424,395,402,360,393,314,404,267,338,173,342,253,0,16,48,43,22,255,304,282,316,320,336,357,344,370,344,363,329,321,329,278,345,217,259,87,252,141,260,194,265,218,288,262,0,10,48,43,22,255,508,279,538,364,544,397,547,465,524,438,517,446,480,399,0,14,48,43,22,255,225,60,242,21,248,0,291,0,269,61,254,68,237,104,224,165,216,180,213,155,213,113,0,24,48,43,22,255,595,263,585,241,569,237,544,244,544,249,534,252,529,269,533,284,544,283,548,287,578,277,605,283,649,308,669,316,679,326,664,307,659,291,657,276,622,262,597,244,579,240,0,22,48,43,22,255,970,250,975,234,1019,233,1065,247,1087,273,1099,302,1085,295,1085,302,1045,286,992,279,939,281,869,295,814,311,781,307,768,302,757,281,826,272,828,267,979,233,0,16,48,43,22,255,1213,343,1212,434,1212,495,1191,566,1184,566,1162,630,1149,656,1147,655,1160,590,1154,587,1154,562,1166,445,1158,296,0,13,40,43,60,255,1512,123,1508,175,1498,188,1468,192,1430,180,1403,184,1392,174,1363,47,1322,0,1472,0,0,11,40,43,60,255,1378,137,1374,158,1366,178,1353,179,1348,140,1338,71,1319,0,1411,0,0,9,40,43,60,255,1303,0,1303,29,1292,53,1284,56,1277,43,1261,0,0,9,40,43,60,255,1256,0,1257,36,1251,48,1237,52,1232,35,1231,0,0,12,40,43,60,255,830,0,807,55,784,97,753,123,727,133,725,126,730,109,779,45,801,0,0,12,40,43,60,255,705,76,682,118,678,147,689,146,700,126,728,79,761,37,760,29,737,34,0,11,40,43,60,255,688,0,731,0,706,38,682,89,665,111,659,105,659,92,687,0,0,10,40,43,60,255,666,3,658,30,646,41,636,33,638,11,640,0,663,0,0,11,45,50,68,255,1520,0,1522,71,1513,144,1501,154,1417,124,1384,85,1384,46,1393,0,0,7,41,45,48,255,557,1079,647,997,645,1003,566,1079,0,9,41,45,48,255,1516,1060,1506,1038,1511,1039,1524,1063,1511,1079,1507,1079,0,7,41,45,48,255,1547,1079,1531,1050,1527,1049,1541,1079,0,10,41,45,48,255,1389,913,1389,897,1383,896,1387,916,1381,934,1385,938,1392,913,0,9,41,45,48,255,431,1050,512,1000,507,1000,433,1043,396,1079,401,1079,0,0
EndDataSection
!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 : 6.12LTS - 64 bits
Re: Algorithm to create random polygon?
Or this :
(ps : maybe blackscreen on W11)
(ps : maybe blackscreen on W11)
Code: Select all
;;;;
Import "user32.lib"
PrintWindow(hWnd, hdc, flags.l)
EndImport
Global width,height,wallpaper
Procedure GetWallpaper(mon=0)
Protected himage,hdc,hwnd
ExamineDesktops()
width = DesktopWidth(mon)
height = DesktopHeight(mon)
hImage = CreateImage(-1,Width,Height,24)
hDC = StartDrawing(ImageOutput(himage))
printwindow(FindWindowEx_(0, 0, @"Progman", 0) ,hdc,0)
StopDrawing()
ProcedureReturn hImage
EndProcedure
wallpaper = GetWallpaper(0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code : Lightning camera ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PB6.11 - SPH(2024) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Global chemin$,sph_cmb,attente,attente_ok,mix,miy,ddw,ddh
Global numero,timer,echelle_xf.f,echelle_yf.f,Resx,Resy,categorie$
Global ultimated_NANO_ALPHA.q,cat$,Dim speedx.w(1,1),Dim speedy.w(1,1)
Global axe1f.f,axe2f.f,axe3f.f,axe4f.f
;-GLOBALS
Global.i Image1, Image2
Global.i Texture1, Texture2
Global menu,ref_ici
;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget
;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)
Procedure Render()
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glClearColor_(0,0,0,1)
glEnable_(#GL_TEXTURE_2D)
Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
glDisable_(#GL_TEXTURE_2D)
EndProcedure
Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
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 affiche_poly(attente,pol,xxx,yyy,angle.f,dkx,dky,zoom.f,alpha.f)
sph_cmb=speedy(pol,0)
sph_la=0
Repeat
glBegin_(#GL_POLYGON)
glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
glColor4f_(speedx(pol,sph_la+1)/255,speedy(pol,sph_la+1)/255,speedx(pol,sph_la+2)/255,(speedy(pol,sph_la+2)/255)*alpha)
;;;;;;
For i=3 To sph_cmb-1
sph_xxx.f=(speedx(pol,sph_la+i)/echelle_xf)-xxx
sph_yyy.f=(speedy(pol,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=speedy(pol,sph_la+i)
sph_la+i
Until speedy(pol,sph_la)=0
If attente<>0 And ElapsedMilliseconds()-timer>=attente
timer=ElapsedMilliseconds()
numero+1
EndIf
;;;;;;;;;;;;;;;
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
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
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
glClearColor_(0,0,0, 1.0)
max=21
poly_cmb=0
pol=0
Dim speedx(poly_cmb,max)
Dim speedy(poly_cmb,max)
For i=1 To max
Read.w speedx(pol,i-1)
Read.w speedy(pol,i-1)
Next
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;; I want to replace the "loadimage" with loading the desktop image **********************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
image$="f:/so0uthpark_presentateur.bmp"
Image1 = GetWallpaper(0)
Texture1 = SetupGLTexture(Image1)
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;;;;;;;;;;;;;;;;;;;;;;;
combien=6
Dim zoo1(combien)
Dim zoo2(combien)
For i=0 To combien
zoo1(i)=Random(200)+55
zoo2(i)=Random(200)+55
Next
Dim zoom.f(combien)
Dim zoom2.f(combien)
For i=0 To combien
zoom(i)=Random(100)/100
zoom2(i)=Random(100)/100
Next
Dim dist.f(combien)
For i=0 To combien
dist(i)=0.2+Random(400)/100
Next
centrex=(540/echelle_xf)
centrey=(540/echelle_yf)
ddwf.f=5.4+ddw/1920
ddhf.f=5.4+ddh/1080
Repeat
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
gestion()
Render()
glReadPixels_(Resx, ddh-1-Resy, 1 , 1, #GL_RGBA, #GL_UNSIGNED_BYTE, @pixels )
;##############################################
;affiche_poly(attente | n° de polygone | axe de rotation X | axe de rotation Y | angle de rotation(.f) | decallage X | decallage Y | Zoom(.f) | alpha.f)
;mix=milieu de l'ecran (en X)
;miy=milieu de l'ecran (en Y)
;##############################################
For nb=0 To combien
For i=0 To 20
affiche_poly(0,0,0,0,0,mix-centrex*zoom(nb)+i*ddwf-mix*dist(nb)+Resx*dist(nb),miy-centrey*zoom(nb)+i*ddhf-miy*dist(nb)+Resy*dist(nb),zoom(nb)-i/100,1/zoo1(nb))
affiche_poly(0,0,0,0,0,mix-centrex*zoom2(nb)+i*ddwf+mix*dist(nb)-Resx*dist(nb),miy-centrey*zoom2(nb)+i*ddhf+miy*dist(nb)-Resy*dist(nb),zoom2(nb)-i/100,1/zoo2(nb))
Next
Next
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
ForEver
End
DataSection
sph_data:
Data.w 1,21,255,255,255,255,1014,802,1025,775,1025,304,1012,270,990,243,566,4,540,1,511,4,80
Data.w 255,65,272,59,295,59,781,64,804,81,819,518,1077,543,1079,567,1077,993,829
Data.w 0,0
EndDataSection
!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 : 6.12LTS - 64 bits
Re: Algorithm to create random polygon?
I'm not really good at pure OpenGL, but knows a lot of procgen techniques. (comes from my love to oldschool roguelikes) I think what you need is the Voronoi diagram algorithm. Search for that in youtube, there will be countless explanations.The goal is to generate random areas on a map.