Date: 02-16-2022
Return to Index
created by gbSnippets
'This snippets draws each of the ten primitives supported by OpenGL.
'GL_POINTS GL_LINE_STRIP
'GL_LINES GL_LINE_LOOP
'GL_POLYGON GL_TRIANGLE_STRIP
'GL_TRIANGLES GL_TRIANGLE_FAN
'GL_QUADS GL_QUAD_STRIP
'Compiler Comments:
'This code was written to compilete in PBWin10. To compile with PBWin9, split pt
'into pt.x and pt.y as arguments wherever the ChildWindowFromPoint() API is used (4 places).
'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 = 1001 : %ID_Label = 1002
Global hDlg, hDC, hRC, hLabel As DWord
Global scalefactor As Single
Function PBMain() As Long
Dialog New Pixels, 0, "OpenGL Example",,, 500, 400,%WS_OverlappedWindow To hDlg
Control Add Label, hdlg, %ID_Label,"",110,10,100,100, %WS_Child Or %WS_Visible Or %SS_Sunken Or %SS_Notify
Control Add Option, hdlg, 501,"Points",10,10,100,20
Control Add Option, hdlg, 502,"Lines" ,10,35,100,20
Control Add Option, hdlg, 503,"Line_Strip",10,60,100,20
Control Add Option, hdlg, 504,"Line_Loop",10,85,100,20
Control Add Option, hdlg, 505,"Triangles",10,110,100,20
Control Add Option, hdlg, 506,"Triangle_Strip",10,135,100,20
Control Add Option, hdlg, 507,"Triangle_Fan",10,160,100,20
Control Add Option, hdlg, 508,"Quads",10,185,100,20
Control Add Option, hdlg, 509,"Quad_Strip",10,210,100,20
Control Add Option, hdlg, 510,"Polygon",10,235,100,20
Control Set Option hDlg, 501, 501, 510
Dialog Show Modal hdlg Call dlgproc
End Function
CallBack Function dlgproc()
Local pt As Point
Local XDelta, YDelta as Single
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_Command : If CB.Ctl > 500 AND CB.Ctl < 511 Then DrawScene 0,0,0
Case %WM_Timer : DrawScene 1,1,0 'redraw with rotation on all 3 axes
Case %WM_Paint : DrawScene 0,0,0 'redraw with no rotation
Case %WM_Size : Control Set Size hDlg, %ID_Label, Lo(Word, CB.lParam)-120, Hi(Word, CB.lParam)-20
ResizeScene Lo(Word, CB.lParam)-120, Hi(Word, CB.lParam)-20
DrawScene 0,0,0 'redraw with no rotation
Case %WM_Close : wglmakecurrent %null, %null 'unselect rendering context
wgldeletecontext hRC 'delete the rendering context
releasedc hDlg, hDC 'release device context
Case %WM_MouseWheel
Select Case Hi(Integer,CB.wParam)
Case > 0 : ScaleFactor = ScaleFactor + 0.1 : DrawScene 0,0,0
Case < 0 : ScaleFactor = ScaleFactor - 0.1 : DrawScene 0,0,0
End Select
Case %WM_SetCursor
GetCursorPos pt 'p.x and p.y are in screen coordinates
ScreenToClient hDlg, pt 'p.x and p.y are now dialog client coordinates
If GetDlgCtrlID(ChildWindowFromPoint( hDlg, pt )) <> %ID_Label Then Exit Function
Select Case Hi(Word, CB.lParam)
Case %WM_LButtonDown
GetCursorPos pt 'pt has xy screen coordinates
ScreenToClient hDlg, pt 'pt now has dialog client coordinates
If pt.y < 0 Then Exit Select
KillTimer CB.Hndl, %ID_Timer
SpinInWork = 1
XLast = Pt.x
YLast = Pt.y
Case %WM_MouseMove
If SpinInWork Then
GetCursorPos pt 'pt has xy screen coordinates
ScreenToClient hDlg, pt 'pt now has dialog client coordinates
If pt.y < 0 Then Exit Select
XDelta = XLast - Pt.x
YDelta = YLast - Pt.y
DrawScene -YDelta, -XDelta, 0
XLast = pt.x
YLast = pt.y
End If
Case %WM_LButtonUp
SpinInWork = 0
SetTimer(hDlg, %ID_Timer, 50, %NULL)
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
Control Handle hdlg, %ID_Label To hLabel
hDC = GetDC(hLabel)
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
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 (dx As Single, dy As Single, dz As Single)
Static anglex, angley, anglez As Single
Local i,j as Long
For i = 1 to 10
Control Get Check hDlg, 500+i to j
If j = %True Then Exit For
Next i
glClear %gl_color_buffer_bit Or %gl_depth_buffer_bit 'clear buffers
glLoadIdentity 'clear the modelview matrix
gluLookAt 0,0,10,0,0,0,0,1,0
glScalef scalefactor, scalefactor, scalefactor
anglex = anglex + dx : glRotatef anglex, 1,0,0
angley = angley + dy : glRotatef angley, 0,1,0
anglez = anglez + dz : glRotatef anglez, 0,0,1
Select Case i
Case 1
glcolor3ub 255,0,0 'set default vertex color
glBegin %gl_points
glvertex3f -1, 0, 0 'vertex1
glvertex3f 1, 0, 0 'vertex1
glvertex3f 0, 1, 0 'vertex3
glvertex3f 0, -1, 0 'vertex4
glvertex3f 0, 0, -1 'vertex4
glvertex3f 0, 0, 1 'vertex4
glEnd
Case 2
glBegin %gl_lines
glcolor3ub 0,0,255 : glvertex3f 2, 2, -2 'vertex1
glcolor3ub 255,0,0 : glvertex3f -1, 1, 2 'vertex2
glvertex3f -2, -1, -2 'vertex1
glvertex3f 2, -3, -1 'vertex2
glEnd
Case 3
glBegin %gl_line_strip
glcolor3ub 0,0,255
glvertex3f -1, -1, -1 'vertex1
glvertex3f -1, -1, 2 'vertex2
glvertex3f -1, 2, -1 'vertex3
glvertex3f 1, -1.5, -1 'vertex4
glEnd
Case 4
glBegin %gl_line_loop
glcolor3ub 255,0,255
glvertex3f 2, 2, -1 'vertex1
glvertex3f -1, 1.5, -1 'vertex2
glvertex3f -2, -1, -2 'vertex3
glvertex3f 2, -3, -1 'vertex4
glEnd
Case 5
glBegin %gl_triangles
glcolor3ub 0,255,0 : glvertex3f 0, 3, -2 'vertex1
glcolor3ub 255,0,0 : glvertex3f -3, 0, 2 'vertex2
glcolor3ub 0,0,255 : glvertex3f 2, -3, -1 'vertex3
glEnd
Case 6
glBegin %gl_triangle_strip
glcolor3ub 0,255,0 : glvertex3f -1, 3, -2 'vertex1
glcolor3ub 255,0,0 : glvertex3f 2, 2, 2 'vertex2
glcolor3ub 0,0,255 : glvertex3f -2, 2, -2 'vertex3
glcolor3ub 255,0,255 : glvertex3f 2, 1, 2 'vertex4
glcolor3ub 0,255,255 : glvertex3f 0, 1, -2 'vertex5
glcolor3ub 255,0,255 : glvertex3f 2, 0, 2 'vertex6
glEnd
Case 7
glBegin %gl_triangle_fan
glcolor3ub 0,255,0 : glvertex3f -2, -2, -1 'vertex1
glcolor3ub 255,0,0 : glvertex3f -1, 3, 1 'vertex2
glcolor3ub 0,0,255 : glvertex3f 3, 1, -2 'vertex3
glcolor3ub 255,0,255 : glvertex3f 2, 1, 2 'vertex3
glcolor3ub 255,255,0 : glvertex3f 1, -2, 1 'vertex3
glEnd
Case 8
glcolor3ub 0,255,0 'set default vertex color
glBegin %gl_quads
glVertex2f 0.0, 0.0
glVertex2f 1.0, 0.0
glVertex2f 1.5, 1.1
glVertex2f 0.5, 1.1
glEnd
Case 9
glBegin %gl_quad_strip
glcolor3ub 0,255,0 : glVertex2f -2,-3
glcolor3ub 0,255,255 : glVertex2f 2, -3
glcolor3ub 255,0,0 : glVertex2f -2, 0
glcolor3ub 0,255,255 : glVertex2f 2, 0
glcolor3ub 255,255,0 : glVertex2f -3, 3
glcolor3ub 0,255,0 : glVertex2f 1, 1
glcolor3ub 255,0,0 : glVertex2f -4, 3
glcolor3ub 0,255,255 : glVertex2f -2, 3
glEnd
Case 10
glBegin %gl_polygon
glVertex2f -0.5, -0.5
glVertex2f -0.5, 0.5
glVertex2f 1.0, 1.0
glVertex2f 1.5, 0.5
glVertex2f 0.5, -0.5
glEnd
End Select
SwapBuffers hDC 'display the buffer (image)
End Sub
'gbs_00586
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm