Seite 1 von 1

[FRACTAL] Sierpinski OpenGL

Verfasst: 13.05.2018 20:43
von Mijikai
Beispiel (mehrfach gerendert):
Bild

Viel Spaß :D

Infos:
https://de.wikipedia.org/wiki/Sierpinski-Dreieck

Sierpinski Fractal:

Code: Alles auswählen

Structure VECTOR
  X.f
  Y.f
EndStructure

Procedure.i SierpinskiFractal(*P0.VECTOR,*P1.VECTOR,*P2.VECTOR,Iteration.i)
  Protected.VECTOR A,B,C
  If Iteration:Iteration - 1
    glColor4ub_(255,0,80,100)
    glBegin_(#GL_LINE_LOOP)
    glVertex2f_(*P0\X,*P0\Y)
    glVertex2f_(*P1\X,*P1\Y)
    glVertex2f_(*P2\X,*P2\Y)
    glVertex2f_(*P0\X,*P0\Y)
    glEnd_()
    A\X = (*P0\X + *P1\X) / 2:A\Y = (*P0\Y + *P1\Y) / 2
    B\X = (*P1\X + *P2\X) / 2:B\Y = (*P1\Y + *P2\Y) / 2
    C\X = (*P2\X + *P0\X) / 2:C\Y = (*P2\Y + *P0\Y) / 2
    SierPinskiFractal(*P0,@A,@C,Iteration)
    SierPinskiFractal(@A,*P1,@B,Iteration)
    SierPinskiFractal(@C,@B,*P2,Iteration)
  EndIf
EndProcedure

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 14.05.2018 09:50
von NicTheQuick
Das ist kein lauffähiger Code. Ich weiß auch nicht wie ich den anwenden soll. Bitte mach ihn noch lauffähig.
Code, Tipps & Tricks hat geschrieben:Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Danke :)

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 14:06
von Mijikai
NicTheQuick hat geschrieben:Das ist kein lauffähiger Code. Ich weiß auch nicht wie ich den anwenden soll. Bitte mach ihn noch lauffähig.
Code, Tipps & Tricks hat geschrieben:Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Danke :)
Der Code ist lauffähig bzw. funktioniert ?

Ich poste trotzdem mal ein Programm um zu beweisen das der Code funktioniert.

Erklärung:
Die Funktion SierpinskiFractal() will 3 mal die XY-Positionen für die jeweiligen Ecken des Dreiecks.
Das Ursprungsdreieck muss vorgegeben werden (S0, S1 und S2).
Alle anderen benöigten Ausgangspunkte werden innerhalb der Funktion berechnet (A, B und C).
(Das eingegebene Dreieck wird also im Prinzip immer 3 mal Unterteilt.)

Hier das Programm:

Code: Alles auswählen

;PB v.5.62
;REPLICATOR by Mijikai
;Music by Phekki (Title: Comeback)

Structure VECTOR
  X.f
  Y.f
  Z.f
EndStructure

Global S0.VECTOR, S1.VECTOR, S3.VECTOR, Flags.i,Ticks.f,Iterations.i,Color.i,*ModBuffer

Procedure.i RenderBox(X.f,Y.f,Width.f,Height.f)
  Static Tick.f,C.f,Flip.b
  If Tick > 360:Tick = 0:If Flip = #True:Flip = #False:Else:Flip = #True:EndIf
    Else:Tick + 6:EndIf:If Flip:C = 1 - (Tick / 360):Else:C = Tick / 360:EndIf
  glLineWidth_(20)
  glPushMatrix_()
  glTranslated_(X,Y,0)
  glBegin_(#GL_LINE_LOOP)
  glColor4f_(C,0,C,1):glVertex2i_(X,Y)
  glColor4f_(0,0,C,1):glVertex2i_(X,Y + Height)
  glColor4f_(0,C,C,1):glVertex2i_(X + Width,Y + Height)
  glColor4f_(0,C,C,1):glVertex2i_(X + Width,Y)
  glEnd_()
  glPopMatrix_()
EndProcedure

Procedure.i SierpinskiFractal(*P0.VECTOR,*P1.VECTOR,*P2.VECTOR,Iteration.i)
  Protected.VECTOR A,B,C
  If Iteration:Iteration - 1
    glLineWidth_(3)
    glBegin_(#GL_LINE_LOOP)
    glColor4ub_(255,25,63,200)
    glVertex2f_(*P0\X,*P0\Y)
    glVertex2f_(*P1\X,*P1\Y)
    glColor4ub_(55,125,63,200)
    glVertex2f_(*P2\X,*P2\Y)
    glColor4ub_(55,125,163,200)
    glVertex2f_(*P0\X,*P0\Y)
    glEnd_()
    A\X = (*P0\X + *P1\X) / 2:A\Y = (*P0\Y + *P1\Y) / 2
    B\X = (*P1\X + *P2\X) / 2:B\Y = (*P1\Y + *P2\Y) / 2
    C\X = (*P2\X + *P0\X) / 2:C\Y = (*P2\Y + *P0\Y) / 2
    SierpinskiFractal(*P0,@A,@C,Iteration)
    SierpinskiFractal(@A,*P1,@B,Iteration)
    SierpinskiFractal(@C,@B,*P2,Iteration)
  EndIf
EndProcedure

InitNetwork():InitSound()
*ModBuffer = ReceiveHTTPMemory("https://api.modarchive.org/downloads.php?moduleid=178900#phekkis_-_comeback.xm")
If *ModBuffer:S0\X = - 200:S0\Y = 200:S0\Z = 100:S1\X = 0:S1\Y = - 200:S1\Z = 0:S3\X = 200:S3\Y = 200:S0\Z = 100
  Flags = #PB_Window_SystemMenu|#PB_Window_Tool|#PB_Window_ScreenCentered
  If OpenWindow(0,#Null,#Null,400,400,"REPLICATOR by mijikai | Music by Phekki (Comeback)",Flags) And OpenGLGadget(1,#Null,#Null,400,400)
    StickyWindow(0,#True)
    glOrtho_(0,400,400,0,-1000,1000)
    glMatrixMode_(#GL_MODELVIEW)
    Iterations = 1:Color = $31281B
    glClearColor_(Red(Color) / 255,Green(Color) / 255,Blue(Color) / 255,1)
    CatchMusic(2,*ModBuffer,MemorySize(*ModBuffer))
    MusicVolume(2,40):PlayMusic(2)
    Repeat
      Repeat:Select WindowEvent():Case #PB_Event_CloseWindow:Break 2:Case 0:Break:EndSelect:ForEver
      glClear_(#GL_COLOR_BUFFER_BIT|#GL_DEPTH_BUFFER_BIT)
      glLoadIdentity_()
      If Ticks > 360:If Iterations > 5:Iterations = 0:Else:Iterations + 0.8:EndIf:Ticks = 0:Else :Ticks + 3:EndIf 
      glTranslatef_(200,200,-200)
      glRotatef_(Ticks,0,1,1)
      SierpinskiFractal(@S0,@S1,@S3,Iterations)
      glTranslatef_(0,0,300)
      glRotatef_(Ticks,-1,1,0)
      SierpinskiFractal(@S0,@S1,@S3,6 - Iterations)
      glLoadIdentity_()
      RenderBox(0,0,400,400)
      SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
    ForEver
  EndIf
  FreeMemory(*ModBuffer)
EndIf

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 14:22
von NicTheQuick
Schade, unter Linux kommt nicht mal ein Fenster oder sowas. Ich kann das Programm nur abschießen. Aber das ist man ja auch irgendwie gewohnt von Purebasic unter Linux. :wink:

Und was ich mit lauffähig meinte, war einfach nur das, was du jetzt nachgeliefert hast. :allright: Vorher war es ja nur eine Struktur und eine Prozedur. Jetzt kann jeder mit Windows das ganze auch gleich testen.

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 14:55
von diceman
//EDIT:
Funktioniert! :)

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 17:36
von ccode_new
Ich hab es mal für Linux angepasst:

Code: Alles auswählen

;PB v.5.62
InitSprite()

Structure VECTOR
  X.f
  Y.f
EndStructure

Global ende.b = #False

Global S0.VECTOR, S1.VECTOR, S3.VECTOR, Flags.i

Procedure.i SierpinskiFractal(*P0.VECTOR,*P1.VECTOR,*P2.VECTOR,Iteration.i)
  Protected.VECTOR A,B,C
  
  If Iteration
    Iteration - 1
    glPushMatrix_()
    glMatrixMode_(#GL_MODELVIEW)
    glColor4ub_(255,25,63,100)
    glBegin_(#GL_LINE_LOOP)
    glVertex2f_(*P0\X,*P0\Y)
    glVertex2f_(*P1\X,*P1\Y)
    glVertex2f_(*P2\X,*P2\Y)
    glVertex2f_(*P0\X,*P0\Y)
    glEnd_()
    A\X = (*P0\X + *P1\X) / 2:A\Y = (*P0\Y + *P1\Y) / 2
    B\X = (*P1\X + *P2\X) / 2:B\Y = (*P1\Y + *P2\Y) / 2
    C\X = (*P2\X + *P0\X) / 2:C\Y = (*P2\Y + *P0\Y) / 2
    SierpinskiFractal(*P0,@A,@C,Iteration)
    SierpinskiFractal(@A,*P1,@B,Iteration)
    SierpinskiFractal(@C,@B,*P2,Iteration)
    glPopMatrix_()
  EndIf
EndProcedure

S0\X = - 200:S0\Y = 200:S1\X = 0:S1\Y = - 200:S3\X = 200:S3\Y = 200
Flags = #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget
If OpenWindow(0,0,0,400,400,"huhu",Flags)
  If OpenGLGadget(0,0,0,WindowWidth(0),WindowHeight(0))
    
    glMatrixMode_(#GL_MODELVIEW)
    glOrtho_(0,400,400,0,-1,0)
    glScalef_(2,2,1)
    glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
    SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)
    
    ;OpenGL muss erst vollständig initialisiert sein, sonst wird keine OpenGL-Ausgabe geöffnen.
    ;Wenn man zu schnell in eine Event-Schleife springt ist dieser Zustand nicht gewährleistet.
    AddWindowTimer(0, 1, 16)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          ende = #True
        Case #PB_Event_Timer
          If EventTimer() = 1
            glClear_(#GL_COLOR_BUFFER_BIT)
            SierpinskiFractal(@S0,@S1,@S3,8)
            SetGadgetAttribute(0,#PB_OpenGL_FlipBuffers,#True)
          EndIf
      EndSelect
    Until ende
  EndIf
EndIf

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 17:44
von NicTheQuick
Hm, ab Iteration = 3 sieht es bei mir immer gleich aus. Ein gerader Strich von links nach rechts durch die Mitte und ein Dreieck in der unteren Bildschirmhälfte. Ist das bei dir auch so, ccode_new?

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 18:04
von ccode_new
An Nic den Schnellen,

ja das Problem bestand bei mir auch.

Ich habe es angepasst!

(Es hätte sich sonst auch sehr schnell bis zur Unkenntlichkeit skaliert.)

Aber Anbei:

So etwas sollte aber dringlichst asynchron des Main-Loops laufen.

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 18:19
von Mijikai
@ccode_new danke für die Linux Version.

Bild

Hab mal das Beispielprogramm geupdated ;)
Viel Spaß beim testen :D

Re: [FRACTAL] Sierpinski OpenGL

Verfasst: 15.05.2018 18:25
von ccode_new
About the Update:
I like it!