Date: 02-16-2022
Return to Index
created by gbSnippets
'I always wonder how few lines of code it takes to get something to work.
'I don't recommend this particular set of "minimal" code, but it is interested
'to see that only about 15 lines of code can put an OpenGL image on the screen!
'In this example, using defaults whereever possible is what let the number of
'code lines be so small.
'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
Case %WM_Size : ResizeScene Lo(Word, CB.lParam), Hi(Word, CB.lParam)
DrawScene
End Select
End Function
Sub GetRenderContext
Local pfd As PIXELFORMATDESCRIPTOR
pfd.dwFlags = %PFD_DRAW_TO_WINDOW Or %PFD_SUPPORT_OPENGL Or %PFD_DOUBLEBUFFER
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 ResizeScene (w As Long, h As Long)
glViewport 0, 0, w, h 'resize viewport to match window size
glLoadIdentity 'reset the projection matrix
gluPerspective 45, w/h, 0.1, 100 'calculate the aspect ratio of the Window
End Sub
Sub DrawScene
glBegin %gl_points
glvertex3f 0, 0, -1 'vertex1
glEnd
SwapBuffers hDC 'display the buffer (image)
End Sub
'gbs_00582
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm