'
' Plasma demonstration using GL and GLUT - learning 2D GL/GLUT programming.
'
' Inspired by Cybermonkey's PLASMA demo:
'   http://forum.basicprogramming.org/index.php/topic,1509.msg9440.html#msg9440
'
' October 2010, PvE - GPL
'
'--------------------------------------------------------------------------------------------

INCLUDE "glut.bac"
INCLUDE "gl.bac"

CONST XSize = 640
CONST YSize = 480

DECLARE cols[3][256]

SUB draw

    LOCAL r, g, b, x, y TYPE float
    LOCAL c

    glMatrixMode (GL_PROJECTION)
    glLoadIdentity

    glOrtho(0, XSize, 0, YSize, 0, 1)
    glDisable(GL_DEPTH_TEST)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity

    glClearColor(0.2, 0.2, 0.2, 0)
    glClear(GL_COLOR_BUFFER_BIT)

    FOR y = 1 TO YSize
        FOR x = 1 TO XSize

            c = FLOOR(SIN(x/35)*128 + SIN(y/28)*32 + SIN((x+y)/16)*64)

            IF c > 255 THEN DECR c, 256
            IF c < 0 THEN c = 256 + c

            r = cols[0][c]

            IF r > 255 THEN DECR r, 256
            IF r < 0 THEN r = 256 + c

            g = cols[1][c]

            IF g > 255 THEN DECR g, 256
            IF g < 0 THEN g = 256 + c

            b = cols[2][c]

            IF b > 255 THEN DECR b, 256
            IF b < 0 THEN b = 256 + c

            glColor3f(r/256, g/256, b/256)

            glBegin(GL_POINTS)
                glVertex2i(x, y)
            glEnd
        NEXT
    NEXT

    glutSwapBuffers

END SUB

SUB key (NUMBER k, NUMBER x, NUMBER y)

    END

END SUB

SUB mainprog

    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE)
    glutInitWindowSize(XSize, YSize)
    glutCreateWindow ("Plasma")

    glutDisplayFunc (ADDRESS (draw))
    glutKeyboardFunc (ADDRESS(key))

    glutMainLoop

END SUB

FOR i = 0 TO 255
    cols[0][i] = ABS(128 - 127 * SIN(i * PI / 32))
    cols[1][i] = ABS(128 - 127 * SIN(i * PI/ 64))
    cols[2][i] = ABS(128 - 127 * SIN(i * PI / 128))
NEXT

mainprog