No announcement yet.

Combining Graphic and OpenGL Statements in a Graphic Control

  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining Graphic and OpenGL Statements in a Graphic Control

    Ok, I'm back to work on some basic OpenGL testing and immediately run into a problem. I thought I'd seen a discussion on this topic, but cannot find it.

    I want to use OpenGL to draw in a Graphic Control, then add addition graphics content on top of the OpenGL graphics by using PowerBASIC Graphic statements.

    In the simple code below, drawing is under WM_Size. Whenever I resize, the Graphic Statements are drawn but then overridden by the OpenGL statements. Flickering results. I get the same results regardless of which statements are executed first - the OpenGL is always the last to be displayed.

    Can I do what I want? Or is there some fundamental issue that will prevent me from combining OpenGL and PowerBASIC Graphic statements?

    Here's the example code ...
     #Compile Exe
     #Dim All
     #Debug Error On
     #Debug Display On
     #Include ""
     #Include ""
     #Include ""
     %IDC_Graphic = 500
     Global hDlg, hGraphic, hDC, hRC As Dword
     Function PBMain() As Long
       Dialog New Pixels, 0, "OpenGL Example",,, 250,250, %WS_OverlappedWindow To hDlg
       Control Add Graphic, hDlg, %IDC_Graphic, "",25,25,200,200, %WS_Border Or %SS_Notify
       Control Handle hDlg, %IDC_Graphic To hGraphic
       Graphic Attach hDlg, %IDC_Graphic
       hDC = GetDC(hGraphic)
       Dialog Show Modal hdlg Call dlgproc
     End Function
     CallBack Function dlgproc()
        Local w,h As Long
        Select Case Cb.Msg
           Case %WM_InitDialog
           Case %WM_Size
               Dialog Get Client hDlg To w,h
               Control Set Size hDlg, %IDC_Graphic, w-50,h-50
               Graphic Box (10,10)-(50,50),,%Red,%Red
               ResizeScene w-50,h-50
           Case %WM_Close
               wglmakecurrent %null, %null 'unselect rendering context
               wgldeletecontext hRC        'delete the rendering context
        End Select
     End Function
     Sub GetRenderContext
        Local pfd As PIXELFORMATDESCRIPTOR, fmt As Long
        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
        fmt = ChoosePixelFormat(hDC, pfd) 'set device context properties
        SetPixelFormat(hDC, fmt, 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   'color to be used with glClear
        glClearDepth 1         'zvalue to be used with glClear
     End Sub
     Sub ResizeScene (w As Long, h As Long)
        glViewport 0, 0, w, h             'resize viewport
        glMatrixMode %gl_projection       'select projection matrix
        glLoadIdentity                    'reset projection matrix
        gluPerspective 45, w/h, 0.1, 100  'set perspective aspect ratio
        glMatrixMode %gl_modelview        'select modelview matrix
     End Sub
     Sub DrawScene
        glClear %gl_color_buffer_bit Or %gl_depth_buffer_bit
        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
        SwapBuffers hDC              'display the buffer (image)
     End Sub

  • #2


    • #3
      You can ask OpenGL to render into a bitmap but that of course is much slower. And here again better to switch to plain SDK mode, to bypass the DDT graphic limitations, because you will have to do much low level pixel manipulation.

      I have posted several examples on José Roca's forum to render OpenGL on a composited layered window that could lead you to the right direction.

      I have also many demo based on GDImage (version 5.00+), showing the mixing of 2D and 3D altogether, but that is another story

      Note: OpenGL is also able to mix 2D and 3D altogether and this is the way to go to use the full power of the GPU (this is what i am doing now in GDImage version 6.00+).

      Last edited by Patrice Terrier; 18 Mar 2013, 04:40 AM.
      Patrice Terrier
      Addons: GDImage.DLL 32/64-bit (Graphic library), WinLIFT.DLL 32/64-bit (Skin Engine).


      • #4
        I tried it, and it worked just fine! Thanks very much for the info.

        I appreciate your SDK suggestion. I like understanding/clarifying the limitations of the core PowerBASIC statements. Since many users will not go the SDK route, I like my posted code to demonstrate what can be done with PowerBASIC statements (and the as-needed API assists). Were I writing a commercial product such as your GDImage, I might take an entirely different attitude.