Date: 02-16-2022
Return to Index
created by gbSnippets
'This example takes the "minimal code" example and puts a little more content
'in it - particularly to clarify properties instead of using default values.
'This example uses a dialog as the OpenGL canvas, but a control can also be used.
'See the
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "gl.inc"
#Include "glu.inc"
Global hDlg, hDC, hRC As DWord
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()
Select Case CB.Msg
Case %WM_InitDialog : GetRenderContext
InitializeScene
Case %WM_Size : ResizeScene Lo(Word, CB.lParam), Hi(Word, CB.lParam)
DrawScene
Case %WM_Paint : DrawScene
Case %WM_Close : wglmakecurrent %null, %null 'unselect rendering context
wgldeletecontext hRC 'delete the rendering context
releasedc hDlg, hDC 'release device context
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
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
glClear %gl_color_buffer_bit Or %gl_depth_buffer_bit 'clear buffers
glLoadIdentity 'clear the modelview matrix
glBegin %gl_triangles 'select triangles as primitive
glcolor3ub 255,0,0 'set default vertex color
glvertex3f 0, 1, -4 'vertex1
glvertex3f -1, 0, -4 'vertex2
glvertex3f 1, -1, -4 'vertex3
glEnd
SwapBuffers hDC 'display the buffer (image)
End Sub
'gbs_00583
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm