Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

OpenGL - Minimal Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin OpenGL - Minimal Code

    This code shows that including OpenGL to a PowerBASIC application doesn't necessarily require a lot of code.

    The OpenGL templateI already posted is more complete/usable than this example, but I thought this minimal version would let the OpenGL content stand out better for those looking to understand how to add OpenGL code to their PowerBASIC apps.

    Three very short procedures contain the primary code:

    GetRenderContext gets the app ready for OpenGL by creating the rendering context (the OpenGL canvas). Note that this code contains only Windows API and PowerBASIC statements - no OpenGL statements required.
    required here.

    ResizeScene
    clears and initializes the OpenGL canvas (on startup and whenever the dialog is resized). It's where drawing statements for any of the 2D/3D OpenGL primitives would go (points, lines, triangles, quads, ...).

    DrawScene draws a triangle.

    You'll see a dozen more lines in the template I posted, wheres this minimal version relies on default versions for many of the OpenGL settings.

    Code:
    #Compile Exe
    #Dim All
    #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_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
Working...
X