Author Topic: Flower Wheel graphic demonstration  (Read 922 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Flower Wheel graphic demonstration
« on: August 18, 2023, 06:33:27 PM »
The attached FlowerWheel.bas requires BCX 8.0.2.

I tested the translation with MSVC, Mingw, Pelles C, and Lcc-Win32.

This demonstration serves as a good learning example for how to use the new
double buffering commands that I added to BCX 8.0.2. 

Questions?   Just ask!

Video Demo Here:

https://bcxbasiccoders.com/bcxusers/mrbcx/FlowerWheel.webm





« Last Edit: September 03, 2023, 10:56:40 AM by MrBcx »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: Flower Wheel graphic demonstration
« Reply #1 on: June 24, 2024, 02:26:28 PM »
Below is an OpenGL GPU accelerated version of the Flower Wheel.

It compiles and runs using Pelles C and MSVC using BED.

Code: [Select]

'====================================================
' Flower Wheel by BPLUS using extended QB64 commands
' BCX version  by MrBcx  June 24, 2024   MIT License
' Modified by MrBcx to use OpenGL GPU Acceleration
'====================================================
'         Tested with MSCV and Pelles C
'====================================================
$BCXVERSION "8.0.2"

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

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

GUI "Flower Wheel", PIXELS

CONST MAXX = 800    ' Experiment with sizes
CONST MAXY = MAXX   ' keep it square



SUB FORMLOAD
    GLOBAL AS HWND Form1
    Form1 = BCX_FORM("Flower Wheel", 0, 0, MAXX, MAXY)
    MODSTYLE(Form1, 0, WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE)
    CENTER Form1
    SHOW Form1
    InitOpenGL()
    CALL Drawit
END SUB


SUB InitOpenGL()
    ! 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_DEPTH_TEST)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    glViewport(0, 0, MAXX, MAXY)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0, MAXX, MAXY, 0)  ' Adjusted to match the coordinate system used in GDI
    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 Drawit
    DIM RAW Theta = 0 AS SINGLE
    DIM RAW x = MAXX/2 AS INTEGER
    DIM RAW y = MAXY/2 AS INTEGER
    DIM RAW z = MAXX/5 AS INTEGER
    DO
        DIM AS HDC hdc = GetDC(Form1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        Theta = Theta + 0.0175  ' PI/180 = 0.0175 (approx)
        AnimateFlowers(x, y, z, 0.25, 4, Theta)
        SwapBuffers(hdc)
        ReleaseDC(Form1, hdc)
        DOEVENTS
    LOOP
END SUB



SUB AnimateFlowers(x AS LONG, y AS LONG, r AS LONG, a AS SINGLE, n AS LONG, Theta AS SINGLE)
    '============================================================================================
    '     This is a recursive SUB.  A recursive SUB/FUNCTION calls itself.  See below...
    '============================================================================================
    DIM RAW AS LONG xx, yy
    IF n > 0 THEN
        FOR SINGLE t = 0 TO PI * 2 STEP 1.05       ' PI/3 = 1.05 (approx)
            DOEVENTS
            xx = x + r * COS(t + Theta)
            yy = y + r * SIN(t + Theta)
            '=====================================================================================
            ' Draw circles with OpenGL
            glColor3f(1, 0, 0)
            DrawCircle(xx, yy, r)
            glColor3f(0, 1, 0)
            DrawCircle(xx, yy, r - 4)
            '=====================================================================================
            CALL AnimateFlowers(xx, yy, a * r, a, n - 1, -Theta - n * 0.0175)  ' 0.0175 = PI/180
            '=====================================================================================
        NEXT
    END IF
END SUB



SUB DrawCircle(cx AS LONG, cy AS LONG, r AS LONG)
    '============================================================================================
    '     Draw a circle with OpenGL
    '============================================================================================
    glBegin(GL_LINE_LOOP)
    FOR SINGLE angle = 0 TO 2 * PI STEP PI / 18
        glVertex2f(cx + r * COS(angle), cy + r * SIN(angle))
    NEXT
    glEnd()
END SUB


I've linked an animation that shows the speed difference between the original and the GL version.

It was recorded on a 2K screen, so it might be large on some monitors. 

My GPU is integrated Intel 530 graphics, so nothing fancy but still a big improvement in speed.

https://bcxbasiccoders.com/GL_Speedup.gif





« Last Edit: June 25, 2024, 05:10:44 PM by MrBcx »

Robert

  • Hero Member
  • *****
  • Posts: 1256
    • View Profile
Re: Flower Wheel graphic demonstration
« Reply #2 on: June 24, 2024, 03:11:49 PM »
Below is an OpenGL GPU accelerated version of the Flower Wheel.

It compiles and runs using Pelles C and MSVC using BED.


I've attached an animated gif that shows the speed difference between the original and the GL version.

It was recorded on a 2K screen, so it might be large on some monitors.

Hi MrBCX:

.gif not animated.  :(

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: Flower Wheel graphic demonstration
« Reply #3 on: June 25, 2024, 05:06:36 PM »

Hi MrBCX:

.gif not animated.  :(

Working now:  https://bcxbasiccoders.com/smf/index.php?topic=896.msg5733#msg5733

gif animations attached to posts do not animate.  They must be linked from the top level website:

                                    https://bcxbasiccoders.com/GL_Speedup.gif

Robert

  • Hero Member
  • *****
  • Posts: 1256
    • View Profile
Re: Flower Wheel graphic demonstration
« Reply #4 on: June 25, 2024, 10:06:36 PM »

Hi MrBCX:

.gif not animated.  :(

Working now:  https://bcxbasiccoders.com/smf/index.php?topic=896.msg5733#msg5733

gif animations attached to posts do not animate.  They must be linked from the top level website:

                                    https://bcxbasiccoders.com/GL_Speedup.gif

Hi MrBCX:

Thanks for that. Downloaded the .gif and it works as expected.