PDA

View Full Version : Plasma



peter
10-10-2012, 17:47
Hi,
A bit plasma.


Uses "tbgl"
Dim hwnd,hfnt As DWord


hwnd=TBGL_CreateWindowEx("PLASMA",400,400,32, %TBGL_WS_WINDOWED | %TBGL_WS_DONTSIZE | %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_RenderMatrix2D(0,0,400,400)
TBGL_BackColor(0,0,0)

Single m1,m2,m4,m5,m3,y,x,r,g,b


TBGL_ClearFrame
For y=0 To 399
m5=m5+1
m1=m1+9
m2=m1
m3=m5
For x=0 To 399
m4=m4 +19
m2=m2 +15
m3=m3+1
r = Abs(Sin(m1/100)+Sin(m2/100)+Sin((m1+m4)/600))*(255/3)
g = Abs(Sin((m3)/50) + Sin(m5/100))*250
b = Abs(Sin(x*y/100000)*200)
TBGL_Color(r,g*.6,b)
TBGL_Point(x,y)
Next
Next
TBGL_DrawFrame


While TBGL_IsWindow(hWnd)
If TBGL_GetAsyncKeyState(27) Then Exit While
Sleep (10)
Wend
TBGL_DestroyWindow

Petr Schreiber
10-10-2012, 19:12
I see you like a lot the per pixel effects :) That is a little deadly for interpreter, but there is solution - using TBGL Geometry Buffers (AKA GBuffers).
They offload the loops from CPU and pass the data based on arrays to GPU in huge blocks.

Here little demo - it first creates your plasma, and then it selectively fades away random rows and columns. On my low end 16 core GPU it runs at about 230FPS:


Uses "tbgl"
Dim hwnd,hfnt As DWord

%width = 400
%height = 400

hwnd=TBGL_CreateWindowEx("PLASMA",%width,%height,32, %TBGL_WS_WINDOWED | %TBGL_WS_DONTSIZE | %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

TBGL_RenderMatrix2D(0,0,%width,%height)
TBGL_BackColor(0,0,0)

Single m1,m2,m4,m5,m3,y,x,r,g,b

' -- Create GBuffer
DWord gbPoints = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_2D)

' -- Prepare arrays
Dim totalPoints As Long = %width * %height
Dim pointCounter As Long
Dim pointCoordinates(totalPoints) As TBGL_TVECTOR2F
Dim pointColors(totalPoints) As TBGL_TRGB

TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, totalPoints, pointCoordinates, pointColors)

TBGL_SetWindowTitle(hWnd, "Calculating positions")
' -- The positions will not change, let's prefill them
pointCounter = 0
For y = 1 To %height
For x = 1 To %width
pointCounter += 1

pointCoordinates(pointCounter).x = x
pointCoordinates(pointCounter).y = y
Next
Next

TBGL_SetWindowTitle(hWnd, "Calculating colors")
' -- Recalculate the colors
pointCounter = 0
For y = 1 To %height
m5 += 1
m1 += 9
m2 = m1
m3 = m5

For x = 1 To %width
pointCounter += 1

m4 += 19
m2 += 15
m3 += 1

r = Abs(Sin(m1/100)+Sin(m2/100)+Sin((m1+m4)/600))*(255/3)
g = Abs(Sin((m3)/50) + Sin(m5/100))*250
b = Abs(Sin(x*y/100000)*200)

pointColors(pointCounter).r = MinMax(r, 0, 255)
pointColors(pointCounter).g = MinMax(g*0.6, 0, 255)
pointColors(pointCounter).b = MinMax(b, 0, 255)

Next
Next

TBGL_ResetKeyState()

Long sheep
Double fps

TBGL_SetWindowTitle(hWnd, "Rendering...")

' -- Main loop
While TBGL_IsWindow(hWnd)
fps = TBGL_GetFrameRate

sheep += 1
If sheep > 60 Then
sheep = 0
TBGL_SetWindowTitle(hWnd, Format$(fps, "#.0") + "FPS")
End If

TBGL_ClearFrame
' -- Change randomly one row to fade away
y = Rnd(1, %height)

For x = 1 To %width
pointCounter = (y-1) * %width + x

pointColors(pointCounter).r *= 0.9
pointColors(pointCounter).g *= 0.9
pointColors(pointCounter).b *= 0.9

Next

' -- Change randomly one column to fade away
x = Rnd(1, %width)

For y = 1 To %height
pointCounter = (y-1) * %width + x

pointColors(pointCounter).r *= 0.9
pointColors(pointCounter).g *= 0.9
pointColors(pointCounter).b *= 0.9

Next

' -- Draw all point in single call - very fast, no loops in interpreter :)
TBGL_GBufferRender(gbPoints)
TBGL_DrawFrame

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend
TBGL_DestroyWindow


Petr

peter
10-10-2012, 20:52
Not bad Petr,


if I had time, I would it do on the same way. I have to little time to learn TBGL things.
There are a lot of new constants and commands. That must be first tested.


Thank you.