Bloom Animation

Started by MrBcx, December 22, 2024, 06:13:59 PM

Previous topic - Next topic

MrBcx

'===================================================================
' Bloom Animation
' Based on https://x.com/yuruyurau/status/1327798556099416064
' Converted to BCX by MrBcx on December 21, 2024.     Public Domain
'===================================================================
$BCXVERSION "8.0.4"

GUI "Bloom", PIXELS

CONST xmax = 600
CONST ymax = 600

SUB FORMLOAD
    GLOBAL Form1 AS HWND
    Form1 = BCX_FORM ("Bloom", 0, 0, xmax+10, ymax+20)
    MODSTYLE(Form1, 0, WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE)
    CENTER Form1
    SHOW Form1
    CALL Animate
END SUB


BEGIN EVENTS
    SELECT CASE CBMSG
        CASE WM_QUIT, WM_CLOSE, WM_DESTROY
        END
    END SELECT
END EVENTS


SUB Animate
    DIM AS DWORD Kolor
    DIM AS LONG N, centerX, centerY, r, g, b
    DIM AS DOUBLE K, x, y, s, d, t, scale
    DIM AS DOUBLE mag, colorVal

    ' Initialize variables
    N = 200
    K = 2.0 / (N - 1)
    centerX = xmax / 2
    centerY = ymax / 2
    scale = MIN(xmax, ymax) / 540  ' Base scale factor
    t = 0

    DO
        BCX_BUFFER_START(xmax, ymax)
        DOEVENTS

        FOR INT i = 0 TO N * N - 1
            ' Calculate grid positions
            x = (i MOD N) * K - 1
            y = INT(i / N) * K - 1

            ' Calculate pattern value
            s = ABS(TAN(t + 15 * SQR(5 + SIN(y * 3 + t) + SIN(x * 3))))

            ' Calculate distance/magnitude
            d = SQR(x * x + y * y + (s/9 + 0.4)^2)

            ' Calculate color (using cosine cycle)
            colorVal = (COS(s + t) + 1)  ' Range 0 to 2

            ' Create color (cycling through RGB)
            r = 255 * (SIN(colorVal * PI) * 0.5 + 0.5)
            g = 255 * (SIN(colorVal * PI + PI/3) * 0.5 + 0.5)
            b = 255 * (SIN(colorVal * PI + 2*PI/3) * 0.5 + 0.5)
            Kolor = RGB(r, g, b)

            ' Draw point
            BCX_PSET(0, _
            x/d * N * scale + centerX, _
            y/d * N * scale + centerY, _
            Kolor, _
            BCX_BUFFER)
        NEXT i

        BCX_BUFFER_STOP(Form1)
        t = t + 0.13
    LOOP
END SUB




And here is an OpenGL (hardware accelerated) version that can be compiled in BED when using Microsoft Visual C++ or Pelles C.




'===================================================================
' Bloom Animation with OpenGL
' Based on https://x.com/yuruyurau/status/1327798556099416064
' Converted to OpenGL by Claude, based on code by MrBcx
'===================================================================
$BCXVERSION "8.0.4"

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

$PRAGMA comment(lib, "opengl32.lib")
$PRAGMA comment(lib, "glu32.lib")

GUI "Bloom", PIXELS

CONST xmax = 600
CONST ymax = 600

SUB FORMLOAD
    GLOBAL Form1 AS HWND
    Form1 = BCX_FORM("Bloom", 0, 0, xmax+10, ymax+20)
    MODSTYLE(Form1, 0, WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE)
    CENTER Form1
    SHOW Form1
    InitOpenGL()
    CALL Animate
END SUB

SUB InitOpenGL()
    DIM AS PIXELFORMATDESCRIPTOR pfd =                               _
    {SIZEOF(PIXELFORMATDESCRIPTOR), 1,                               _
    PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,      _
    PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 8, 0, _
    PFD_MAIN_PLANE, 0, 0, 0, 0}

    DIM AS HDC hdc = GetDC(Form1)
    DIM AS INT pixelFormat = ChoosePixelFormat(hdc, &pfd)
    SetPixelFormat(hdc, pixelFormat, &pfd)

    DIM AS HGLRC hglrc = wglCreateContext(hdc)
    wglMakeCurrent(hdc, hglrc)

    glEnable(GL_POINT_SMOOTH)  ' Enable point smoothing
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    glViewport(0, 0, xmax, ymax)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0, xmax, ymax, 0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    ReleaseDC(Form1, hdc)
END SUB

SUB CleanupOpenGL()
    DIM AS HDC hdc = GetDC(Form1)
    DIM AS HGLRC hglrc = wglGetCurrentContext()
    wglMakeCurrent(NULL, NULL)
    wglDeleteContext(hglrc)
    ReleaseDC(Form1, hdc)
END SUB

BEGIN EVENTS
    SELECT CASE CBMSG
        CASE WM_QUIT, WM_CLOSE, WM_DESTROY
            CleanupOpenGL()
            END
    END SELECT
END EVENTS

SUB Animate
    DIM AS LONG N, centerX, centerY
    DIM AS DOUBLE K, x, y, s, d, t, scale
    DIM AS DOUBLE r, g, b, colorVal

    ' Initialize variables
    N = 200
    K = 2.0 / (N - 1)
    centerX = xmax / 2
    centerY = ymax / 2
    scale = MIN(xmax, ymax) / xmax  ' Base scale factor
    t = 0

    glPointSize(2.0)  ' Set point size

    DO
        DIM AS HDC hdc = GetDC(Form1)
       
        glClear(GL_COLOR_BUFFER_BIT)
       
        glBegin(GL_POINTS)
        FOR INT i = 0 TO N * N - 1
            ' Calculate grid positions
            x = (i MOD N) * K - 1
            y = INT(i / N) * K - 1

            ' Calculate pattern value
            s = ABS(TAN(t + 15 * SQR(5 + SIN(y * 3 + t) + SIN(x * 3))))

            ' Calculate distance/magnitude
            d = SQR(x * x + y * y + (s/9 + 0.4)^2)

            ' Calculate color (using cosine cycle)
            colorVal = (COS(s + t) + 1)  ' Range 0 to 2

            ' Create color (cycling through RGB)
            r = (SIN(colorVal * PI) * 0.5 + 0.5)
            g = (SIN(colorVal * PI + PI/3) * 0.5 + 0.5)
            b = (SIN(colorVal * PI + 2*PI/3) * 0.5 + 0.5)
           
            ' Set color and draw point
            glColor3f(r, g, b)
            glVertex2f(x/d * N * scale + centerX, y/d * N * scale + centerY)
        NEXT i
        glEnd()

        SwapBuffers(hdc)
        ReleaseDC(Form1, hdc)
       
        t = t + 0.015
        DOEVENTS
    LOOP
END SUB




dragon57


MrBcx

Quote from: dragon57 on December 22, 2024, 08:51:35 PM
These are just great!

Thank you ... I get a kick getting them to run more or less like the originals.