PDA

View Full Version : Voronoi colorfull mountain



primo
10-12-2015, 16:24
just a small variation of the code http://www.thinbasic.com/community/showthread.php?12620-Question-about-TBGL_DrawFrame&p=92380&viewfull=1#post92380 in which we measure the distance of every point from the center and if it is >=300 then zz = radius/30 ie we increase z gradually

ind = (yy-1)*Width + xx
oVertexA(ind).x = xx
oVertexA(ind).y = yy
radius = sqr((width/2 - xx)^2 + (width/2 - yy)^2)
if radius <= 300 then
zz = radius/30 'gradually change the z
else
zz = 10 ' mountain bottom
end if

oVertexA(ind).z = zz

a possible future project is to isolate every region of the same color a little up or down

9449

Uses "TBGL", "math", "Oxygen"

Function TBMain()
Local hWnd As DWord
Local FrameRate As Double

' -- Create and show window
hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers- Calculate with Oxygen JIT ..Esc to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

TBGL_BackColor 255,255,255

' -- Create 3D points buffer
DWord gbPoints = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)

Global Nb As DWord = 1000000

' -- Define data for it
Global VertexA(Nb) As TBGL_TVECTOR3F ' single
Global ColorA(Nb) As TBGL_TRGB ' Byte

FillArrays()

' -- Create buffer dynamically linked to the arrays above
TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, CountOf(VertexA), VertexA(1), ColorA(1))

' -- Resets status of all keys
TBGL_ResetKeyState()
'TBGL_PointSize 2
'TBGL_Rotate 90 , 90, 0, 0

'TBGL_ClearFrame(%TBGL_CLEAR_DEPTH)
'TBGL_ClearFrame (%TBGL_RESET_MATRIX)

' -- Main loop
While TBGL_IsWindow(hWnd)
'init
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

TBGL_Camera(0, 22, 55, 0, -5, 0)

' -- Turn triangle
'TBGL_Rotate GetTickCount/50, 1, 0, 0
TBGL_Rotate 90, 1, 0, 0
TBGL_Rotate GetTickCount/50, 0, 0, 1
TBGL_Scale 0.05, 0.05, 1
'TBGL_Rotate 90 , 1, 0, 0
TBGL_Translate -320, -320
'TBGL_Rotate 10 , 0, 0, 1

' -- Render it
TBGL_GBufferRender(gbPoints)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend
' -- Destroying the buffer is not necessary,
' -- the garbage collector will take care of it

' -- Destroy window
TBGL_DestroyWindow
End Function

Function FillArrays()

String src = "
basic

' -- Re-declare types
Type TBGL_TRGB
r As Byte
g As Byte
b As Byte
End Type

Type TBGL_TVECTOR3F
x As Single
y As Single
z As Single
End Type


'RANDOMISER
'==========
'
Dim seed As Long=0x12345678
'
Function Rnd() As Single 'this is from oxygen examples in thinbasic folder
'
Static As Single f, d=1/0x7fffffff
mov eax,seed
rol eax,7
imul eax,eax,13
mov seed,eax
Push eax
fild DWord [esp]
Add esp,4
fmul DWord d
fstp DWord f
Function=f
End Function

' -- Link variables
Dim oVertexA() At #VertexA As TBGL_TVECTOR3F
Dim oColorA() At #ColorA As TBGL_TRGB

Single g
Long i

#DEF points 200
#DEF Width 640
#DEF sq Width
Single s2 = sq/2
Dim x(points) As Single
Dim y(points) As Single
Dim kl(points) As TBGL_TRGB

For i = 1 To points
x(i)=Abs(Rnd()*sq): y(i)=Abs(Rnd()*sq)
g=127-127*(Abs(s2-x(i))/s2)+127-127*(Abs(s2-y(i))/s2)
kl(i).r=255-x(i)/sq*255
kl(i).g = g
kl(i).b = y(i)/(sq*255)
Next

Single a, b, q, radius, hei, zz
Long xx, yy, d , kkl, i
For xx = 1 To sq
For yy = 1 To sq
hei = hei + 0.0
d = 307201
For i = 1 To points
a=x(i)-xx: b=y(i)-yy
q=a*a+b*b
If q < d Then d = q: kkl = i
Next i

ind = (yy-1)*Width + xx
oVertexA(ind).x = xx
oVertexA(ind).y = yy
radius = sqr((width/2 - xx)^2 + (width/2 - yy)^2)
if radius <= 300 then
zz = radius/30 'gradualy change the z
else
zz = 10 ' mountain bottom
end if

oVertexA(ind).z = zz
oColorA(ind).r = kl(kkl).r
oColorA(ind).g = kl(kkl).g
oColorA(ind).b = kl(kkl).b
'oColorA(i).r = 255:oColorA(i).g=0: oColorA(i).b=0
'pset xx,yy,kl(kkl)
Next yy
Next xx

terminate
"

' -- Pass the source
O2_Asmo src

' -- Check for errors
If Len(o2_error) Then
MsgBox 0, o2_error : Stop
End If

' -- Execute
o2_exec

End Function