#include <gl.au3>
#include <Math.au3>
Opt("GUIOnEventMode", 1)

Global $gui = GUICreate("OpenGL Sparkle", 450, 450), $dc, $rc, $iFPS, $iParticles, $iProgress = 0
GUISetOnEvent(-3, "quit")
GUIRegisterMsg(0x000F, "Preserve");		==> $WM_PAINT
GUISetState(@SW_SHOW)


If Not glInit($gui, $dc, $rc) Then
    MsgBox(48, "Error", "Error bei der Initialisierung von OpenGL-Funktionen" & @CRLF & "Error code: " & @error)
    Exit
EndIf

glClear(BitOR($GL_COLOR_BUFFER_BIT, $GL_DEPTH_BUFFER_BIT))
glEnable($GL_BLEND)

$iFPSTimer = TimerInit()

While 1
	glClear(BitOR($GL_COLOR_BUFFER_BIT, $GL_DEPTH_BUFFER_BIT))
    glBegin($GL_LINES)

	$iParticles += _CreateSparkle(-.2, .5, .2)
	$iParticles += _CreateSparkle(.3, -.1, .5, 80)

	glColor3f(.5, 0, 0)
	glVertex2d(-.7, -.2)
	glVertex2d(-.7, -0.2 - .8*$iProgress)
	$iParticles += _CreateSparkle(-.7, -0.2 - .8*$iProgress, .3, 20, 0, 1, 0, .5, .5, 1)
	glColor3f(1, 0, 0)
	glVertex2d(-.7, -0.2 - .8*$iProgress)
	glVertex2d(-.7, -1)

	glEnd()
    glSwapBuffers($dc)
	$iFPS += 1
	$iProgress += 0.0015
	If $iProgress >= 1 Then $iProgress = 0
WEnd

Func _CreateSparkle($x, $y, $width = .3, $density = 10, $r1 = 1, $g1 = 0, $b1 = 0, $r2 = 1, $g2 = 1, $b2 = 0)
	For $i = 1 To $density
		$aTemp = _CircleRandom(0, $width)
		glColor3f($r1, $g1, $b1)
		glVertex2d($x, $y)
		glColor3f($r2, $g2, $b2)
		glVertex2d($x+$aTemp[0], $y+$aTemp[1])
    Next
	Return $density
EndFunc

Func _CircleRandom($Min, $Max)
	$Deg = Random(0,360,1)
	$Rad = Random($Min,$Max)
	$x = $Rad * Sin(_Radian($Deg))
	$y = $Rad * Cos(_Radian($Deg))
	Dim $aOut[2]=[$x,$y]
	Return $aOut
EndFunc

Func quit()
	$Time = TimerDiff($iFPSTimer)
	glTerminate($gui, $dc, $rc)
	MsgBox(0, "", "Diese Animation wurde Ihnen mit " & Int( ($iFPS*4/$Time)*250 ) & " echten FPS präsentiert. Es wurden "&$iParticles&" Partikel in "&Round(TimerDiff($iFPSTimer)/1000,2)&"s gerendert.")
	Exit
EndFunc

Func Preserve()
    glSwapBuffers($dc)
EndFunc