Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

OpenGL Background Image

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

  • OpenGL Background Image

    In OpenGL there's apparently no easy mechanism for drawing a simple background image.

    You can create a quad and apply an image as texture to it, but that seems like a long way around what must be a very common requirement for graphic programs.

    Yes, I'm surprised again - my expectations of what OpenGL could do is doused with water one more time!

    I would have thought, that if I had a bitmap in memory, that there would be somewhere in the OpenGL pipeline where I could simply BitBlt the image to the rendering context - or to the corresponding DC - and have OpenGL do it's drawing over the image.

    But as best my tests show, it's not possible. I can BitBlt to the DC but OpenGL draws over the bitmap no matter where in the pipeline I attempt the BitBlt operation. the image does not appear - at least, not persistently as the image will flash in view as the scene is re-rendered (such as when resizing).

    Has anyone had success with that approach for creating a background image in an OpenGL scene?

    Sample code ... resize the dialog or drag the mouse inside the dialog and you'll see the image "chess.bmp" (300x300) flash in and out of view ...

    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    #Include "win32api.inc"
    #Include "gl.inc"
    #Include "glu.inc"
    #Include "afxglut.inc"
    
    %IDC_Label = 500
    Global hDlg, hDC, hRC, hLabel, SpinInWork As Dword, ptLast As Point, ScaleFactor As Single
    Global hBMP,hGraphicDC As Dword
    
    Function PBMain() As Long
       Dialog New Pixels, 0, "OpenGL Example",,, 320, 240, %WS_OverlappedWindow To hDlg
       Control Add Label, hDlg, %IDC_Label,"",0,0,320,240
       Control Handle hDlg, %IDC_Label To hLabel
       Dialog Show Modal hdlg Call dlgproc
    End Function
    
    CallBack Function dlgproc()
       Local pt As Point, XDelta, YDelta,w,h As Long
       Select Case Cb.Msg
          Case %WM_InitDialog
             Graphic Bitmap Load "chess.bmp", 0, 0 To hBMP
             Graphic Attach hBMP, 0
             Graphic Get DC To hGraphicDC
             GetRenderContext
             InitializeScene
             ScaleFactor = 1
             Dialog Post hDlg, %WM_User+500, 0, 0
    
          Case %WM_User+500 : ResizeDialog
          Case %WM_Size     : ResizeDialog
    
          Case %WM_Close
             wglmakecurrent %null, %null 'unselect rendering context
             wgldeletecontext hRC        'delete the rendering context
          Case %WM_MouseWheel
             Select Case Hi(Integer,Cb.WParam)    'note the use of Integer
               Case > 0
                  ScaleFactor *= 1.1
                  DrawScene 0,0
               Case < 0
                  ScaleFactor *= 0.9
                  DrawScene 0,0
             End Select
          Case %WM_LButtonDown
             GetCursorPos pt              'pt has xy screen coordinates
             ScreenToClient hDlg, pt      'pt now has dialog client coordinates
             If ChildWindowFromPoint(hDlg,pt) = hLabel Then
                SetCapture hDlg
                SpinInWork = 1
             End If
          Case %WM_MouseMove
             If SpinInWork Then
                GetCursorPos pt           'pt has xy screen coordinates
                ScreenToClient hDC, pt    'pt now has dialog client coordinates
                XDelta = ptLast.x - pt.x
                YDelta = ptLast.y - pt.y
                DrawScene -YDelta, -XDelta
                ptLast.x = pt.x
                ptLast.y = pt.y
             End If
          Case %WM_LButtonUp
             SpinInWork = 0
             ReleaseCapture
       End Select
    End Function
    
    Sub ResizeDialog
       Local w,h As Long
       Dialog Get Client hDlg To w,h
       Control Set Size hDlg, %IDC_Label, w,h
       ResizeScene w,h
       DrawScene 0,0
    End Sub
    
    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
    
       hDC = GetDC(hLabel)                'DC for dialog
       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
       glDepthFunc %gl_less     'values used for depth-buffer comparisons
       glEnable %gl_depth_test  'enable depth testing
       glShadeModel %gl_smooth  'smooth shading
    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(ddx As Single, ddy As Single)
       Local i,j As Long
       Static angleX, angleY As Long
    
       glClear %gl_color_buffer_bit Or %gl_depth_buffer_bit
       glLoadIdentity
    
       BitBlt hDC, 0,0,300,300, hGraphicDC, 0,0, %SRCCopy
    
       gluLookAt 0,0,60,0,0,0,0,1,0
       glScalef scalefactor, scalefactor, scalefactor
       anglex = anglex + ddx : glRotatef anglex, 1,0,0
       angley = angley + ddy : glRotatef angley, 0,1,0
    
       afxglutWireSphere 10,20,20
       SwapBuffers hDC              'display the buffer (image)
    End Sub
    Last edited by Gary Beene; 29 Oct 2015, 11:23 PM.

  • #2
    I would recommend NEHE to learn the basic of OpenGL.

    Using a Quad is the easiest solution, and nothing realy complex, except that you must keep the quad wallpaper in the background, like this.

    Code:
        glEnable(%GL_TEXTURE_2D)
        glColor4ub(255,255,255,255)
        GL_BindTexture(%GL_TEXTURE_2D, gP.mt.texture)
    
        '//////////////////////////////////////////////
        glMatrixMode(%GL_PROJECTION)
        glPushMatrix()
            glLoadIdentity()
    
            glOrtho(-gP.glRatio, gP.glRatio, -1, 1, -1, 1)
    
            glDisable(%GL_DEPTH_TEST)
            glBegin(%GL_QUADS)
            glTexCoord2i(0, 0): glVertex2i(-1,-1)
            glTexCoord2i(1, 0): glVertex2i( 1,-1)
            glTexCoord2i(1, 1): glVertex2i( 1, 1)
            glTexCoord2i(0, 1): glVertex2i(-1, 1)
            glEnd()
    
            glBindTexture(%GL_TEXTURE_2D, 0)
            glEnable(%GL_DEPTH_TEST)
        glPopMatrix()
        glMatrixMode(%GL_MODELVIEW)
        '//////////////////////////////////////////////
    Using GL_PROJECTION for the background is like working in 2D.

    And get rid of GLUT that will lead you sooner or later into a dead end.


    ...
    Last edited by Patrice Terrier; 30 Oct 2015, 04:13 AM.
    Patrice Terrier
    www.zapsolution.com
    www.objreader.com
    Addons: GDImage.DLL 32/64-bit (Graphic library), WinLIFT.DLL 32/64-bit (Skin Engine).

    Comment


    • #3
      Hi Patrice!

      I would recommend NEHE to learn the basic of OpenGL...
      Yes, and for those watching, there's a PowerBASIC conversion of the NEHE tutorials on Jose's web site.

      And see this forum's FAQ for NEHE (and other) links.
      Last edited by Gary Beene; 30 Oct 2015, 02:02 PM.

      Comment


      • #4
        Depending on what you want to do you can have ogl draw a bmp to the display and then using one or more of the ogl primitives draw over the top of the bmp.

        Do a search for opengl or ogl with my name as the poster. You'll find a bunch of opengl demos, and I think one of them is in pbcc that demonstrates how to draw a bmp to the display.

        You can rescale the bmp, read pixels from the screen, and do a number of other operations on the bmp.
        Last edited by Walt Decker; 30 Oct 2015, 12:32 PM. Reason: add comments
        Walt Decker

        Comment


        • #5
          Originally posted by Walt Decker View Post
          Do a search for opengl or ogl with my name as the poster.
          I did some stuff with OpenGL a while ago and they were very useful. Thanks Walt!

          Comment


          • #6
            You are welcome, Chris. I'm happy that they were found useful.

            I'm currently working on a sky-box routine in both ogl and using my own transform mechanisms. When it's finished I'll post it. Might be a while though.
            Walt Decker

            Comment

            Working...
            X