3 Attachment(s)
glDrawArrays example + GBuffers example
1- glDrawArrays : to draw simple flat shark data, the purpose of Z = RND(0,1) in line 73 is to draw two parallel sharks. the shark is too tall so i have used gluPerspective(60.0, 800/600, 1.0, 100.0) to see it all
i have added data to the code because it is not too big
Code:
Uses "tbgl", "MATH"
#INCLUDE "%app_includepath%\thinbasic_gl.inc"
#INCLUDE "%app_includepath%\thinbasic_glu.inc"
Type Point3D
x As Single
y As Single
z As Single
red As Single
green As Single
blue As Single
End Type
Dim hwnd As DWord
Global Nb As DWord = 10053
Global Vertex(Nb) As Point3D
'msgbox 0, SizeOf(Point3D))
hwnd = TBGL_CreateWindowEx("glDrawArrays example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
TBGL_ResetKeyState()
'TBGL_SetDrawDistance 550
glPointSize( 2.0 ) ' just to show the curve in bold
FillArray ' fill Vertex(...) with position data and color data
While TBGL_IsWindow(hwnd)
TBGL_ClearFrame
display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
'=================================================================================
'--------------------------------------------------------
'--------------------------------------------------------
Sub display()
glMatrixMode(%GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, 800/600, 1.0, 100.0)
glMatrixMode(%GL_MODELVIEW)
glTranslatef(0, 0, -40)
glShadeModel(%GL_SMOOTH)
glEnable(%GL_DEPTH_TEST)
gluLookAt( 0, 1, 1,
0, 1, 0,
0, 1, 0 )
glclear(%gl_color_buffer_bit)
TBGL_Rotate GetTickCount/30,0,1,0
'glRotatef(1, 0, 1, 0)' -- 0,1,0 rotate around Y
glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)
'glRotatef_(1, 0, 1, 0)
glEnableClientState(%GL_VERTEX_ARRAY )
glEnableClientState(%GL_COLOR_ARRAY)
glVertexPointer(3, %GL_FLOAT,SizeOf(Point3D),VarPtr(Vertex(1).x))
glColorPointer(3, %GL_FLOAT, SizeOf(Point3D), VarPtr(Vertex(1).red))
'glDrawArrays(%GL_LINE_STRIP, 1, 10053)
glDrawArrays(%GL_POINTS, 1, 10053 )
glDisableClientState(%GL_COLOR_ARRAY)
glDisableClientState(%GL_VERTEX_ARRAY)
'**************************************************
End Sub
Sub FillArray()
DWord n = 1 : Double t = 0
Dim x, y, z As Single
While t <= 2 * Pi
If n>10053 Then Exit While
x = -18/5 *Sin(3/4-30 *t)-7/2 *Sin(2/3-20 *t)-129/4 *Sin(6/7-6 *t)-285/11 *Sin(1/15-5 *t)+2749/3 *Sin(t+11/6)+556/3 *Sin(2 *t+8/3)+625/12 *Sin(3 *t+6/5)+318/5 *Sin(4 *t+7/2)+257/6 *Sin(7 *t+20/7)+35 *Sin(8 *t+3/5)+118/7 *Sin(9 *t+3/5)+48/5 *Sin(10 *t+1/3)+96/5 *Sin(11 *t+5/4)+77/6 *Sin(12 *t+9/2)+91/4 *Sin(13 *t+7/3)+19/3 *Sin(14 *t+8/5)+56/11 *Sin(15 *t+9/5)+55/7 *Sin(16 *t+13/3)+37/9 *Sin(17 *t+13/5)+13/4 *Sin(18 *t+1/4)+27/5 *Sin(19 *t+7/3)+25/6 *Sin(21 *t+8/5)+5/6 *Sin(22 *t+7/3)+49/5 *Sin(23 *t+12/5)+11/4 *Sin(24 *t+5/3)+18/5 *Sin(25 *t+3)+23/4 *Sin(26 *t+5/2)+8/3 *Sin(27 *t+3/4)+5/3 *Sin(28 *t+35/17)+11/4 *Sin(29 *t+2/3)
y = -7/5 *Sin(3/4-30 *t)-8/7 *Sin(4/3-27 *t)-Sin(2/3-26 *t)-2/3 *Sin(1/3-25 *t)-15/4 *Sin(3/4-23 *t)-83/7 *Sin(4/3-16 *t)-99/7 *Sin(5/4-13 *t)-19/3 *Sin(6/5-12 *t)-32 *Sin(8/5-9 *t)-298/9 *Sin(2/3-8 *t)-129/5 *Sin(1/3-7 *t)-76/3 *Sin(6/5-6 *t)+11/4 *Sin(29 *t)+873/4 *Sin(t+17/5)+364/11 *Sin(2 *t+26/9)+268/5 *Sin(3 *t+4/5)+167/3 *Sin(4 *t+25/6)+208/3 *Sin(5 *t+21/5)+31/4 *Sin(10 *t+5/4)+21 *Sin(11 *t+11/5)+27/5 *Sin(14 *t+25/6)+81/10 *Sin(15 *t+31/8)+10/3 *Sin(17 *t+9/2)+3/2 *Sin(18 *t+2)+91/9 *Sin(19 *t+39/10)+3/2 *Sin(20 *t+7/5)+25/8 *Sin(21 *t+1)+4 *Sin(22 *t+5/4)+3 *Sin(24 *t+13/3)+Sin(28 *t+26/9)
z = Rnd(0,1)
'Vertex(n).red = Rnd(0,1) : Vertex(n).green = Rnd(0,1) : Vertex(n).blue = Rnd(0,1)
If z = 1 Then
Vertex(n).red = 1 :Vertex(n).green = 0 :Vertex(n).blue = 0
Else
Vertex(n).red = 0 :Vertex(n).green = 1 :Vertex(n).blue = 0.3
End If
Vertex(n).x = x*0.02
Vertex(n).y = y*0.02
Vertex(n).z = z*2
n + 1 : t + 0.001
Wend
End Sub
Attachment 9369
2- Gbuffers method to draw the shark in two parallel sharks, the shark is too tall and parts of it is not displayed so i have find this TBGL function: TBGL_SetDrawDistance 250 , i set it to 250 meters, so we can see the shark in full. you can see the GBuffers example are much shorter if we delete the commented lines
Code:
Uses "TBGL"
Uses "math"
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 - press ESC to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_BackColor 255,255,255
TBGL_SetDrawDistance 250
' -- Create 3D points buffer
Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
'Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_LINES, %TBGL_3D)
Global Nb As DWord = 10053 '30159 '62831
' -- Define data for it
Global VertexA(Nb) As TBGL_TVECTOR3F
Global ColorA(Nb) As TBGL_TRGB
FillArrays ' call the sub to fill VertexA , ColorA arrays
' -- 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
' -- Main loop
While TBGL_IsWindow(hWnd)
'init
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera(0, 0, 150, 0, 0, 0)
' -- Turn triangle
TBGL_Rotate GetTickCount/50, 0, 1, 0
'TBGL_Scale 0.6, 0.6, 0.6
' -- 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
Sub FillArrays()
Dim x, y, z, t As Single
Dim N As DWord = 1
While t <= 2*Pi
x = -18/5 *Sin(3/4-30 *t)-7/2 *Sin(2/3-20 *t)-129/4 *Sin(6/7-6 *t)-285/11 *Sin(1/15-5 *t)+2749/3 *Sin(t+11/6)+556/3 *Sin(2 *t+8/3)+625/12 *Sin(3 *t+6/5)+318/5 *Sin(4 *t+7/2)+257/6 *Sin(7 *t+20/7)+35 *Sin(8 *t+3/5)+118/7 *Sin(9 *t+3/5)+48/5 *Sin(10 *t+1/3)+96/5 *Sin(11 *t+5/4)+77/6 *Sin(12 *t+9/2)+91/4 *Sin(13 *t+7/3)+19/3 *Sin(14 *t+8/5)+56/11 *Sin(15 *t+9/5)+55/7 *Sin(16 *t+13/3)+37/9 *Sin(17 *t+13/5)+13/4 *Sin(18 *t+1/4)+27/5 *Sin(19 *t+7/3)+25/6 *Sin(21 *t+8/5)+5/6 *Sin(22 *t+7/3)+49/5 *Sin(23 *t+12/5)+11/4 *Sin(24 *t+5/3)+18/5 *Sin(25 *t+3)+23/4 *Sin(26 *t+5/2)+8/3 *Sin(27 *t+3/4)+5/3 *Sin(28 *t+35/17)+11/4 *Sin(29 *t+2/3) ' shark
y = -7/5 *Sin(3/4-30 *t)-8/7 *Sin(4/3-27 *t)-Sin(2/3-26 *t)-2/3 *Sin(1/3-25 *t)-15/4 *Sin(3/4-23 *t)-83/7 *Sin(4/3-16 *t)-99/7 *Sin(5/4-13 *t)-19/3 *Sin(6/5-12 *t)-32 *Sin(8/5-9 *t)-298/9 *Sin(2/3-8 *t)-129/5 *Sin(1/3-7 *t)-76/3 *Sin(6/5-6 *t)+11/4 *Sin(29 *t)+873/4 *Sin(t+17/5)+364/11 *Sin(2 *t+26/9)+268/5 *Sin(3 *t+4/5)+167/3 *Sin(4 *t+25/6)+208/3 *Sin(5 *t+21/5)+31/4 *Sin(10 *t+5/4)+21 *Sin(11 *t+11/5)+27/5 *Sin(14 *t+25/6)+81/10 *Sin(15 *t+31/8)+10/3 *Sin(17 *t+9/2)+3/2 *Sin(18 *t+2)+91/9 *Sin(19 *t+39/10)+3/2 *Sin(20 *t+7/5)+25/8 *Sin(21 *t+1)+4 *Sin(22 *t+5/4)+3 *Sin(24 *t+13/3)+Sin(28 *t+26/9)
z = Rnd(0,1)
VertexA(N).x = x*0.05
VertexA(N).y = y*0.05
VertexA(N).z = z*10
'ColorA(N).r = 255 :ColorA(N).g = 0 :ColorA(N).b = 0
If z = 1 Then
ColorA(N).r = 255 :ColorA(N).g = 0 :ColorA(N).b = 0
Else
ColorA(N).r = 0 :ColorA(N).g = 255 :ColorA(N).b = 100
End If
t + 0.001 : N + 1
Wend
End Sub
Function theta(a As Single) As Single
If a<=0 Then
Function = 0
Else
Function = 1
End If
End Function