Uses "tbgl"
#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
Dim Nb As DWord =60 ' number of vertexes at each Horzontal line and Vertical line
Dim Vertex(Nb,Nb) As Point3D
Dim indices(2 * Nb * Nb * 2) As DWord ' Can be also specified as UInt32
hwnd = TBGL_CreateWindowEx("glDrawElements example with Grid - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
TBGL_ResetKeyState()
glPointSize( 3.5 )
init
While TBGL_IsWindow(hwnd)
TBGL_ClearFrame
display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
Sub init()
Dim a,b As DWord
Dim xMin, yMin, zMin, xMax, yMax, zMax, range, step1, x, y, z As Single
xMin = -1 : yMin = -1: zMin = -1: xMax = 1: yMax = 1: zMax = 1
'xMin = -0.5 : zMin = -0.5 : xMax = 0.5: zMax = 0.5
range = xMax - xMin
step1 = range / Nb
x = xMin: z = zMin : y = yMin
For b=1 To Nb
For a=1 To Nb
y = Sin(10*(x^2+z^2))/10
'y = 0
Vertex(a,b).x = x
Vertex(a,b).y = y
Vertex(a,b).z = z
If y>=0 Then
Vertex(a,b).red = 1.0 :Vertex(a,b).green = 0.0 :Vertex(a,b).blue = 0
Else
Vertex(a,b).red = 0.0 :Vertex(a,b).green = 0.5 :Vertex(a,b).blue = 0.2
EndIf
x = x + step1
Next a
x = xMin
z = z + step1
Next b
'=================================================================================
Dim i As DWord = 1
Dim yy,xx As DWord
'Grid Horizontal lines
For yy = 1 To Nb
For xx = 1 To Nb-1
indices(i) = (yy-1) * Nb + xx -1
i+1
indices(i) = (yy-1) * Nb + xx
i+1
Next
Next
'Grid vertical lines
For xx = 1 To Nb
For yy = 1 To Nb-1
indices(i) = (yy-1) * Nb + xx -1
i=i+1
indices(i) = (yy) * Nb + xx -1
i+1
Next
Next
glScalef(2,2,2) ' amplify the graphics Size by 2
'position camera at 0,1.5,1 __ object position 0,1,0___0,1,0 direction
gluLookAt( 0, 1.5, 0,
0, 0, 0,
0, 1, 0 )
End Sub
'--------------------------------------------------------
'--------------------------------------------------------
Sub display()
glMatrixMode(%GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 800/600, 1.0, 60.0)
glMatrixMode(%GL_MODELVIEW)
glTranslatef(0, 1, -2)
glShadeModel(%GL_SMOOTH)
glEnable(%GL_DEPTH_TEST)
gluLookAt( 0, 2.0, 2.3,
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
glScalef(2,2,2) ' amplify the graphics Size by 2
glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)
glEnableClientState(%GL_VERTEX_ARRAY )
glEnableClientState(%GL_COLOR_ARRAY)
glVertexPointer(3, %GL_FLOAT,SizeOf(Point3D),VarPtr(Vertex(1,1)))
glColorPointer(3, %GL_FLOAT, SizeOf(Point3D), VarPtr(Vertex(1,1).red))
'glDrawElements(%GL_POINTS, CountOf(indices), %GL_UNSIGNED_INT, VarPtr(indices(1)))
glDrawElements(%GL_LINES , CountOf(indices), %GL_UNSIGNED_INT, VarPtr(indices(1)))
glDisableClientState(%GL_VERTEX_ARRAY)
glDisableClientState(%GL_COLOR_ARRAY)
End Sub
Bookmarks