Date: 02-16-2022
Return to Index
created by gbSnippets
'Walkthrough is typically associated with changing the position/direction
'of the viewpoint from which a scene is viewed. And using arrow keys is a
'common method for allowing the user to perform a walkthrough.
'Primary Code:
'A basic approach to using arrow keys for performing a walkthrough is to check
'the state of the arrow keys (up/down) at the start of DrawScene procedure.
'If an arrow key is pressed, perform rotation/translation as necessary.
'While events exist to capture keystrokes
If getAsyncKeystate(%vk_left) Then
rotVx = rotVx - 0.1
ElseIf getAsyncKeystate(%vk_right) Then
rotVx = rotVx + 0.1
End If
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "gl.inc"
#Include "glu.inc"
%ID_Timer = 1000
Global hDlg, hDC, hRC As DWord
Global Scalefactor as Single
Function PBMain() As Long
Dialog New Pixels, 0, "OpenGL Example",,, 320, 240,%WS_OverlappedWindow To hDlg
Dialog Show Modal hdlg Call dlgproc
End Function
CallBack Function dlgproc()
Local pt As Point
Static SpinInWork,XLast,YLast As Long
Select Case CB.Msg
Case %WM_InitDialog : GetRenderContext
InitializeScene
SetTimer(hDlg, %ID_Timer, 50, %NULL)
ScaleFactor = 1
Case %WM_Timer : DrawScene
Case %WM_Paint : DrawScene
Case %WM_Size : ResizeScene Lo(Word, CB.lParam), Hi(Word, CB.lParam)
DrawScene
Case %WM_Close : wglmakecurrent %null, %null 'unselect rendering context
wgldeletecontext hRC 'delete the rendering context
releasedc hDlg, hDC 'release device context Case %WM_MouseWheel
Case %WM_MouseWheel
Select Case Hi(Integer,CB.wParam)
Case > 0 : ScaleFactor = ScaleFactor + 0.1 : DrawScene
Case < 0 : ScaleFactor = ScaleFactor - 0.1 : DrawScene
End Select
End Select
End Function
Sub GetRenderContext
Local pfd As PIXELFORMATDESCRIPTOR 'pixel format properties for device context
pfd.nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd.nVersion = 1
pfd.dwFlags = %pfd_draw_to_window Or %pfd_support_opengl Or %pfd_doublebuffer
pfd.dwlayermask = %pfd_main_plane
pfd.iPixelType = %pfd_type_rgba
pfd.ccolorbits = 24
pfd.cdepthbits = 24
hDC = GetDC(hDlg) 'DC for dialog
SetPixelFormat(hDC, ChoosePixelFormat(hDC, pfd), pfd) 'set properties of device context
hRC = wglCreateContext (hDC) 'get rendering context
wglMakeCurrent hDC, hRC 'make the RC current
End Sub
Sub InitializeScene
glClearColor 0,0,0,0 'sets color to be used with glClear
glClearDepth 1 'sets zvalue to be used with glClear
glDepthFunc %gl_less 'specify how depth-buffer comparisons are made
glEnable %gl_depth_test 'enable depth testing
glShadeModel %gl_smooth 'smooth shading
glHint %gl_perspective_correction_hint, %gl_nicest 'best quality rendering
BuildCubeDisplayList(1)
End Sub
Sub ResizeScene (w As Long, h As Long)
glViewport 0, 0, w, h 'resize viewport to match window size
glMatrixMode %gl_projection 'select the projection matrix
glLoadIdentity 'reset the projection matrix
gluPerspective 45, w/h, 0.1, 100 'calculate the aspect ratio of the Window
glMatrixMode %gl_modelview 'select the modelview matrix
End Sub
Sub DrawScene
Static rotVx, rotVy As Single
glClear %gl_color_buffer_bit Or %gl_depth_buffer_bit 'clear buffers
glLoadIdentity 'clear the modelview matrix
If getAsyncKeystate(%vk_left) Then
rotVx = rotVx - 0.1
ElseIf getAsyncKeystate(%vk_right) Then
rotVx = rotVx + 0.1
End If
gluLookAt Cos(rotVx)*8, 0, Sin(rotVx)*8, 0,0,0,0,1,0
glScalef scalefactor, scalefactor, scalefactor
glpushMatrix
glTranslatef -2,0,0
glCallList 1
glpopMatrix
glTranslatef +2,0,0
glCallList 1
SwapBuffers hDC 'display the buffer (image)
End Sub
Sub BuildCubeDisplayList(iCube As Long)
glNewList iCube, %gl_compile
glBegin %GL_QUADS
glColor3f 0.0, 1.0, 0.0 ' Set the color to green
glVertex3f 1.0, 1.0, -1.0 ' Top right of the quad (Top)
glVertex3f -1.0, 1.0, -1.0 ' Top left of the quad (Top)
glVertex3f -1.0, 1.0, 1.0 ' Bottom left of the quad (Top)
glVertex3f 1.0, 1.0, 1.0 ' Bottom right of the quad (Top)
glColor3f 1.0, 0.5, 0.0 ' Set the color to orange
glVertex3f 1.0, -1.0, 1.0 ' Top right of the quad (Bottom)
glVertex3f -1.0, -1.0, 1.0 ' Top left of the quad (Bottom)
glVertex3f -1.0, -1.0, -1.0 ' Bottom left of the quad (Bottom)
glVertex3f 1.0, -1.0, -1.0 ' Bottom right of the quad (Bottom)
glColor3f 1.0, 0.0, 0.0 ' Set the color to red
glVertex3f 1.0, 1.0, 1.0 ' Top right of the quad (Front)
glVertex3f -1.0, 1.0, 1.0 ' Top left of the quad (Front)
glVertex3f -1.0, -1.0, 1.0 ' Bottom left of the quad (Front)
glVertex3f 1.0, -1.0, 1.0 ' Bottom right of the quad (Front)
glColor3f 1.0, 1.0, 0.0 ' Set the color to yellow
glVertex3f 1.0, -1.0, -1.0 ' Top right of the quad (Back)
glVertex3f -1.0, -1.0, -1.0 ' Top left of the quad (Back)
glVertex3f -1.0, 1.0, -1.0 ' Bottom left of the quad (Back)
glVertex3f 1.0, 1.0, -1.0 ' Bottom right of the quad (Back)
glColor3f 0.0, 0.0, 1.0 ' Set the color to blue
glVertex3f -1.0, 1.0, 1.0 ' Top right of the quad (Left)
glVertex3f -1.0, 1.0, -1.0 ' Top left of the quad (Left)
glVertex3f -1.0, -1.0, -1.0 ' Bottom left of the quad (Left)
glVertex3f -1.0, -1.0, 1.0 ' Bottom right of the quad (Left)
glColor3f 1.0, 0.0, 1.0 ' Set the color to violet
glVertex3f 1.0, 1.0, -1.0 ' Top right of the quad (Right)
glVertex3f 1.0, 1.0, 1.0 ' Top left of the quad (Right)
glVertex3f 1.0, -1.0, 1.0 ' Bottom left of the quad (Right)
glVertex3f 1.0, -1.0, -1.0 ' Bottom right of the quad (Right)
glEnd
glEndList
End Sub
'gbs_00592
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm