Announcement

Collapse
No announcement yet.

Stable Background

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

  • Stable Background

    In the following, is there a way to make the gldrawpixels image stable?

    Code:
    #COMPILE EXE "SPINNER"
    DEFLNG I - N
    
    '#include "win32api.inc"
    ' pixel types
    %PFD_TYPE_RGBA       = 0
    %PFD_TYPE_COLORINDEX = 1
    
    ' layer types
    %PFD_MAIN_PLANE      = 0
    %PFD_OVERLAY_PLANE   = 1
    %PFD_UNDERLAY_PLANE  = -1
    
    ' PIXELFORMATDESCRIPTOR flags
    %PFD_DOUBLEBUFFER           = &H00000001
    %PFD_STEREO                 = &H00000002
    %PFD_DRAW_TO_WINDOW         = &H00000004
    %PFD_DRAW_TO_BITMAP         = &H00000008
    %PFD_SUPPORT_GDI            = &H00000010
    %PFD_SUPPORT_OPENGL         = &H00000020
    %PFD_GENERIC_FORMAT         = &H00000040
    %PFD_NEED_PALETTE           = &H00000080
    %PFD_NEED_SYSTEM_PALETTE    = &H00000100
    %PFD_SWAP_EXCHANGE          = &H00000200
    %PFD_SWAP_COPY              = &H00000400
    %PFD_SWAP_LAYER_BUFFERS     = &H00000800
    %PFD_GENERIC_ACCELERATED    = &H00001000
    %PFD_SUPPORT_DIRECTDRAW     = &H00002000
    %PFD_DIRECT3D_ACCELERATED   = &H00004000
    %PFD_SUPPORT_COMPOSITION    = &H00008000
    
    ' PIXELFORMATDESCRIPTOR flags for use in ChoosePixelFormat only
    %PFD_DEPTH_DONTCARE         = &H20000000
    %PFD_DOUBLEBUFFER_DONTCARE  = &H40000000
    %PFD_STEREO_DONTCARE        = &H80000000???
    
    '/* AttribMask */
    %GL_CURRENT_BIT                    = &H00000001???
    %GL_POINT_BIT                      = &H00000002???
    %GL_LINE_BIT                       = &H00000004???
    %GL_POLYGON_BIT                    = &H00000008???
    %GL_POLYGON_STIPPLE_BIT            = &H00000010???
    %GL_PIXEL_MODE_BIT                 = &H00000020???
    %GL_LIGHTING_BIT                   = &H00000040???
    %GL_FOG_BIT                        = &H00000080???
    %GL_DEPTH_BUFFER_BIT               = &H00000100???
    %GL_ACCUM_BUFFER_BIT               = &H00000200???
    %GL_STENCIL_BUFFER_BIT             = &H00000400???
    %GL_VIEWPORT_BIT                   = &H00000800???
    %GL_TRANSFORM_BIT                  = &H00001000???
    %GL_ENABLE_BIT                     = &H00002000???
    %GL_COLOR_BUFFER_BIT               = &H00004000???
    %GL_HINT_BIT                       = &H00008000???
    %GL_EVAL_BIT                       = &H00010000???
    %GL_LIST_BIT                       = &H00020000???
    %GL_TEXTURE_BIT                    = &H00040000???
    %GL_SCISSOR_BIT                    = &H00080000???
    %GL_ALL_ATTRIB_BITS                = &H000fffff???
    
    '/* BeginMode */
    %GL_POINTS                         = &H0000???
    %GL_LINES                          = &H0001???
    %GL_LINE_LOOP                      = &H0002???
    %GL_LINE_STRIP                     = &H0003???
    %GL_TRIANGLES                      = &H0004???
    %GL_TRIANGLE_STRIP                 = &H0005???
    %GL_TRIANGLE_FAN                   = &H0006???
    %GL_QUADS                          = &H0007???
    %GL_QUAD_STRIP                     = &H0008???
    %GL_POLYGON                        = &H0009???
    
    '/* MatrixMode */
    %GL_MODELVIEW                      = &H1700???
    %GL_PROJECTION                     = &H1701???
    %GL_TEXTURE                        = &H1702???
    
    '---- Draw Pixels -------
    %GL_UNPACK_LSB_FIRST               = &H0CF1???
    %GL_UNPACK_ROW_LENGTH              = &H0CF2???
    %GL_UNPACK_SKIP_ROWS               = &H0CF3???
    %GL_UNPACK_SKIP_PIXELS             = &H0CF4???
    %GL_UNPACK_ALIGNMENT               = &H0CF5???
    %GL_PACK_SWAP_BYTES                = &H0D00???
    %GL_PACK_LSB_FIRST                 = &H0D01???
    %GL_PACK_ROW_LENGTH                = &H0D02???
    %GL_PACK_SKIP_ROWS                 = &H0D03???
    %GL_PACK_SKIP_PIXELS               = &H0D04???
    
    '/* PixelFormat */
    %GL_COLOR_INDEX                    = &H1900???
    %GL_STENCIL_INDEX                  = &H1901???
    %GL_DEPTH_COMPONENT                = &H1902???
    %GL_RED                            = &H1903???
    %GL_GREEN                          = &H1904???
    %GL_BLUE                           = &H1905???
    %GL_ALPHA                          = &H1906???
    %GL_RGB                            = &H1907???
    %GL_RGBA                           = &H1908???
    %GL_LUMINANCE                      = &H1909???
    %GL_LUMINANCE_ALPHA                = &H190A???
    
    '/* DataType */
    %GL_BYTE                           = &H1400???
    %GL_UNSIGNED_BYTE                  = &H1401???
    %GL_SHORT                          = &H1402???
    %GL_UNSIGNED_SHORT                 = &H1403???
    %GL_INT                            = &H1404???
    %GL_UNSIGNED_INT                   = &H1405???
    %GL_FLOAT                          = &H1406???
    %GL_2_BYTES                        = &H1407???
    %GL_3_BYTES                        = &H1408???
    %GL_4_BYTES                        = &H1409???
    %GL_DOUBLE                         = &H140A???
    
    %SRCCOPY             = &H00CC0020
    '-------- dib flags ---------
    %BI_RGB                                         = 0
    %BI_BITFIELDS                                   = 3
    
    %OPAQUE                                         = 2
    %TRANSPARENT                                    = 1
    %DIB_RGB_COLORS                                 = 0 ' color table in RGBs
    
    ' Pixel format descriptor
    TYPE PIXELFORMATDESCRIPTOR
        nSize           AS WORD
        nVersion        AS WORD
        dwFlags         AS DWORD
        iPixelType      AS BYTE
        cColorBits      AS BYTE
        cRedBits        AS BYTE
        cRedShift       AS BYTE
        cGreenBits      AS BYTE
        cGreenShift     AS BYTE
        cBlueBits       AS BYTE
        cBlueShift      AS BYTE
        cAlphaBits      AS BYTE
        cAlphaShift     AS BYTE
        cAccumBits      AS BYTE
        cAccumRedBits   AS BYTE
        cAccumGreenBits AS BYTE
        cAccumBlueBits  AS BYTE
        cAccumAlphaBits AS BYTE
        cDepthBits      AS BYTE
        cStencilBits    AS BYTE
        cAuxBuffers     AS BYTE
        iLayerType      AS BYTE
        bReserved       AS BYTE
        dwLayerMask     AS DWORD
        dwVisibleMask   AS DWORD
        dwDamageMask    AS DWORD
    END TYPE
    
    TYPE Bitmap_Struct
        bmType       AS LONG
        bmWidth      AS LONG
        bmHeight     AS LONG
        bmWidthBytes AS LONG
        bmPlanes     AS WORD
        bmBitsPixel  AS WORD
        bmBits       AS DWORD  ' LPVOID
    END TYPE
    
    TYPE RGBQUAD
      rgbBlue AS BYTE
      rgbGreen AS BYTE
      rgbRed AS BYTE
      rgbReserved AS BYTE
    END TYPE
    
    TYPE BITMAPFILEHEADER WORD FILL
        bfType      AS WORD
        bfSize      AS DWORD
        bfReserved1 AS WORD
        bfReserved2 AS WORD
        bfOffBits   AS DWORD
    END TYPE
    
    TYPE BITMAPINFOHEADER
        biSize          AS DWORD
        biWidth         AS LONG
        biHeight        AS LONG
        biPlanes        AS WORD
        biBitCount      AS WORD
        biCompression   AS DWORD
        biSizeImage     AS DWORD
        biXPelsPerMeter AS LONG
        biYPelsPerMeter AS LONG
        biClrUsed       AS DWORD
        biClrImportant  AS DWORD
    END TYPE
    
    TYPE BITMAPINFO
        bmiHeader AS BITMAPINFOHEADER
        bmiColors(0) AS RGBQUAD
    END TYPE
    
    TYPE BitMapFile_Struct
      BitmapFileHeader
      BitmapInfoHeader
    END TYPE
    
    '/========================================================================================/'
    '                       OPENGL FUNCTIONS
    '/========================================================================================/'
    DECLARE FUNCTION glBegin LIB "OPENGL32.DLL" ALIAS "glBegin"(BYVAL GlMode AS DWORD) AS LONG
    DECLARE FUNCTION glEnd LIB "OPENGL32.DLL" ALIAS "glEnd" () AS LONG
    DECLARE FUNCTION glColor3ub LIB "OPENGL32.DLL" ALIAS "glColor3ub" (BYVAL red AS BYTE, _                                  ' GLubyte red
            BYVAL green AS BYTE, BYVAL blue AS BYTE) AS LONG
    DECLARE FUNCTION glVertex3f LIB "OPENGL32.DLL" ALIAS "glVertex3f" (BYVAL x AS SINGLE, _                                  ' GLfloat x
            BYVAL y AS SINGLE, BYVAL z AS SINGLE) AS LONG
    DECLARE FUNCTION glRotatef LIB "OPENGL32.DLL" ALIAS "glRotatef" (BYVAL angle AS SINGLE, _                              ' GLfloat angle
            BYVAL x AS SINGLE, BYVAL y AS SINGLE, BYVAL z AS SINGLE) AS LONG
    DECLARE FUNCTION glPushMatrix LIB "OPENGL32.DLL" ALIAS "glPushMatrix" () AS LONG
    DECLARE FUNCTION glPopMatrix LIB "OPENGL32.DLL" ALIAS "glPopMatrix" () AS LONG
    
    DECLARE FUNCTION wglCopyContext LIB "OPENGL32.DLL" ALIAS "wglCopyContext" _
        (BYVAL hglrcSrc AS DWORD, BYVAL hlglrcDst AS DWORD, BYVAL dMask AS DWORD) _
        AS LONG
    DECLARE FUNCTION wglCreateContext LIB "OPENGL32.DLL" ALIAS "wglCreateContext" _
            (BYVAL hdc AS DWORD) AS DWORD
    DECLARE FUNCTION wglCreateLayerContext LIB "OPENGL32.DLL" ALIAS "wglCreateLayerContext" _
            (BYVAL hdc AS DWORD, BYVAL iLayerPlane AS LONG) AS DWORD
    DECLARE FUNCTION wglDeleteContext LIB "OPENGL32.DLL" ALIAS "wglDeleteContext" _
            (BYVAL hglrc AS DWORD) AS LONG
    DECLARE FUNCTION wglGetCurrentContext LIB "OPENGL32.DLL" _
            ALIAS "wglGetCurrentContext" () AS DWORD
    DECLARE FUNCTION wglGetCurrentDC LIB "OPENGL32.DLL" ALIAS "wglGetCurrentDC" _
            () AS DWORD
    DECLARE FUNCTION wglGetProcAddress LIB "OPENGL32.DLL" _
            ALIAS "wglGetProcAddress" (lpszProc AS ASCIIZ) AS DWORD
    DECLARE FUNCTION wglMakeCurrent LIB "OPENGL32.DLL" ALIAS "wglMakeCurrent" _
            (BYVAL hdc AS DWORD, BYVAL hglrc AS DWORD) AS LONG
    DECLARE FUNCTION wglShareLists LIB "OPENGL32.DLL" ALIAS "wglShareLists" _
            (BYVAL hglrc1 AS DWORD, BYVAL hglrc2 AS DWORD) AS LONG
    DECLARE FUNCTION glViewport LIB "OPENGL32.DLL" ALIAS "glViewport"(BYVAL x AS LONG, _                                    ' GLint x
            BYVAL y AS LONG, BYVAL Width AS LONG, BYVAL height AS LONG) AS LONG
    DECLARE FUNCTION glLoadIdentity LIB "OPENGL32.DLL" ALIAS "glLoadIdentity" () AS LONG
    DECLARE FUNCTION glLoadMatrixd LIB "OPENGL32.DLL" ALIAS "glLoadMatrixd"_
            (BYVAL Matrix AS DOUBLE) AS LONG
    DECLARE FUNCTION glMatrixMode LIB "OPENGL32.DLL" ALIAS "glMatrixMode" _
            (BYVAL Mode AS DWORD) AS LONG                            ' void
    DECLARE FUNCTION glOrtho LIB "OPENGL32.DLL" ALIAS "glOrtho"(BYVAL Xleft AS DOUBLE,  _
            BYVAL XRight AS DOUBLE, BYVAL Bottom AS DOUBLE, BYVAL Top AS DOUBLE, _
            BYVAL zNear AS DOUBLE, BYVAL zFar AS DOUBLE) AS LONG
    DECLARE FUNCTION glClearColor LIB "OPENGL32.DLL" ALIAS "glClearColor"(BYVAL red AS SINGLE, _
            BYVAL green AS SINGLE, BYVAL blue AS SINGLE, BYVAL alpha AS SINGLE) AS LONG
    DECLARE FUNCTION glClear LIB "OPENGL32.DLL" ALIAS "glClear"(BYVAL mask AS DWORD) AS LONG
    DECLARE FUNCTION glPixelStoreI LIB "OPENGL32.DLL" ALIAS "glPixelStorei"(BYVAL pname AS DWORD, _
            BYVAL param AS LONG) AS LONG
    DECLARE FUNCTION glPixelZoom LIB "OPENGL32.DLL" ALIAS "glPixelZoom"(BYVAL xfactor AS SINGLE, _
            BYVAL yfactor AS SINGLE) AS LONG
    DECLARE FUNCTION glRasterPos2i LIB "OPENGL32.DLL" ALIAS "glRasterPos2i"(BYVAL x AS LONG, _
            BYVAL y AS LONG) AS LONG
    DECLARE FUNCTION glDrawPixels LIB "OPENGL32.DLL" ALIAS "glDrawPixels"(BYVAL XWide AS LONG, _
            BYVAL height AS LONG, BYVAL format AS DWORD, BYVAL NumType AS DWORD, _
            BYVAL LpPixels AS DWORD) AS LONG
    DECLARE FUNCTION glFlush LIB "OPENGL32.DLL" ALIAS "glFlush" () AS LONG
    DECLARE FUNCTION glFinish LIB "OPENGL32.DLL" ALIAS "glFinish" () AS LONG
    DECLARE FUNCTION glTranslatef LIB "OPENGL32.DLL" ALIAS "glTranslatef" (BYVAL x AS SINGLE, _                                  ' GLfloat x
             BYVAL y AS SINGLE, BYVAL z AS SINGLE) AS LONG
    DECLARE FUNCTION gluLookAt LIB "GLU32.DLL" ALIAS "gluLookAt" (BYVAL eyex AS DOUBLE, _
            BYVAL eyey AS DOUBLE, BYVAL eyez AS DOUBLE, BYVAL centerx AS DOUBLE, _
            BYVAL centery AS DOUBLE, BYVAL centerz AS DOUBLE, BYVAL upx AS DOUBLE,  _
            BYVAL upy AS DOUBLE, BYVAL upz AS DOUBLE) AS LONG
    
    '/========================================================================================/'
    '                       GDI FUNCTIONS
    '/========================================================================================/'
    #IF %DEF(%UNICODE)
      DECLARE FUNCTION GetObject LIB "GDI32.DLL" ALIAS "GetObjectW" _
              (BYVAL hObject AS DWORD, BYVAL nCount AS LONG, lpObject AS ANY) AS LONG
    #ELSE
      DECLARE FUNCTION GetObject LIB "GDI32.DLL" ALIAS "GetObjectA" _
              (BYVAL hObject AS DWORD, BYVAL nCount AS LONG, lpObject AS ANY) AS LONG
    #ENDIF
    
    DECLARE FUNCTION ChoosePixelFormat LIB "GDI32.DLL" ALIAS "ChoosePixelFormat" _
           (BYVAL hDC AS DWORD, pPixelFormatDescriptor AS PIXELFORMATDESCRIPTOR) AS LONG
    DECLARE FUNCTION SetPixelFormat LIB "GDI32.DLL" ALIAS "SetPixelFormat"(BYVAL hDC AS DWORD, _
            BYVAL n AS LONG, pcPixelFormatDescriptor AS PIXELFORMATDESCRIPTOR) AS LONG
    DECLARE FUNCTION SwapBuffers LIB "GDI32.DLL" ALIAS "SwapBuffers" _
            (BYVAL hDC AS DWORD) AS LONG
    DECLARE FUNCTION CreateDIBSection LIB "GDI32.DLL" ALIAS "CreateDIBSection" _
            (BYVAL hdc AS DWORD, pbmi AS BITMAPINFO, BYVAL dwUsage AS DWORD, _
            BYVAL ppvBits AS DWORD, BYVAL hSection AS DWORD, BYVAL dwOffset AS DWORD) AS DWORD
    DECLARE FUNCTION CreateCompatibleDC LIB "GDI32.DLL" _
            ALIAS "CreateCompatibleDC" (BYVAL hdc AS DWORD) AS DWORD
    DECLARE FUNCTION BitBlt LIB "GDI32.DLL" ALIAS "BitBlt" (BYVAL hDestDC AS DWORD, _
            BYVAL x AS LONG, BYVAL y AS LONG, BYVAL nWidth AS LONG, BYVAL nHeight AS LONG, _
            BYVAL hSrcDC AS DWORD, BYVAL xSrc AS LONG, BYVAL ySrc AS LONG, _
            BYVAL dwRop AS DWORD) AS LONG
    
    '/========================================================================================/'
    '                       USER32 FUNCTIONS
    '/========================================================================================/'
    DECLARE FUNCTION GetDC LIB "User32.dll" ALIAS "GetDC" (OPTIONAL BYVAL hWnd AS DWORD) AS DWORD
    DECLARE FUNCTION ReleaseDC LIB "User32.dll" ALIAS "ReleaseDC"(BYVAL hWnd AS DWORD, _
            BYVAL hDC AS DWORD) AS LONG
    DECLARE FUNCTION GetDesktopWindow LIB "User32.dll" ALIAS "GetDesktopWindow" _
            () AS DWORD
    DECLARE FUNCTION SelectObject LIB "GDI32.DLL" ALIAS "SelectObject" (BYVAL hdc AS DWORD, _
            BYVAL hObject AS DWORD) AS DWORD
    DECLARE FUNCTION DeleteObject LIB "GDI32.DLL" ALIAS "DeleteObject" _
            (BYVAL hObject AS DWORD) AS LONG
    
    '======================================================================================='
    '======================================================================================='
    FUNCTION PBMAIN () AS LONG
    
    LOCAL DlgHndl AS DWORD
    
    GRAPHIC BITMAP NEW 128, 128 TO DlgHndl
    GRAPHIC ATTACH DlgHndl, 0
    GRAPHIC CLEAR %RGB_WHEAT
    GRAPHIC WIDTH 5
    GRAPHIC ELLIPSE (0, 0) - (128, 128), %RGB_PERU
    GRAPHIC SAVE "SomeBmp.Bmp"
    
    DIALOG NEW PIXELS, 0, "OPENGL", , , 640, 480, %WS_OVERLAPPEDWINDOW, TO DlgHndl
    
    CONTROL ADD BUTTON, DlgHndl, 1, "RUN", 10, 10, 55, 20
    
    DIALOG SHOW MODAL Dlghndl, CALL PbMainDLG_CB
    
    KILL "SomeBmp.Bmp"
    
    END FUNCTION
    
    '-------------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------------
    
    CALLBACK FUNCTION PbMainDLG_CB()
    
    SELECT CASE CBMSG
      CASE %WM_INITDIALOG
      CASE %WM_COMMAND
        SELECT CASE CBCTLMSG
          CASE %BN_CLICKED
            IF CBCTL = 1 THEN
              CALL BeginOgl(CBHNDL)
            END IF
        END SELECT
    END SELECT
    
    END FUNCTION
    
    '-------------------------------------------------------
    '-------------------------------------------------------
    
    SUB BeginOgl(BYVAL WinHndl AS DWORD)
    
    LOCAL Xwide, _
          Yhigh           AS INTEGER
    
    LOCAL BmpSize         AS LONG
    
    LOCAL ClearBuffer, _
            ChildHndl, _
              ChildDc, _
                OglRc     AS DWORD
    
    LOCAL Buffer()        AS DWORD
    
    LOCAL tPixFmt         AS PIXELFORMATDESCRIPTOR
    
    LOCAL BkgPtr, _
          OglPtr      AS DWORD POINTER
    
    ClearBuffer = %GL_DEPTH_BUFFER_BIT OR %GL_COLOR_BUFFER_BIT 'OR
               '%GL_ACCUM_BUFFER_BIT 'OR %GL_STENCIL_BUFFER_BIT
    
    CALL LoadBGRColorsFromFile(Xwide, Yhigh, BmpSize, "SomeBmp.Bmp", Buffer())
    
    CALL CreateRGBAColors(Buffer())
    
    DIALOG NEW PIXELS, WinHndl, "test", 100, 100, 64, 64, %WS_CHILD OR %WS_VISIBLE OR _
       %WS_BORDER, %WS_EX_TRANSPARENT TO ChildHndl
    
    DIALOG SHOW MODELESS ChildHndl, CALL PbMainDlg_CB
    
    
    ChildDc = getdc(ChildHndl)
    CALL OglPixFormat(tPixFmt, 0)
    
    OglRc = SetOglPipe(0, ChildDc, tPixFmt)
    
    CALL SetOglWindow(0, 0, 64, 64, 100)
    
    glcolor3ub(128, 128, 255)
    
    FOR I = 1 TO 10000
      CALL RenderDrawImage(64, 64, XWide, Yhigh, Buffer())
      glrotatef(.1, 0, 0, 1)
      glrotatef(-.1, 1, 0, 0)
      glrotatef(.3, 0, 1, 0)
      glbegin(%GL_TRIANGLES)
        glvertex3f(0, -1.75, 0)
        glvertex3f(1.75, 0, 0)
        glvertex3f(-1.75, 1.75, 0)
      glend()
      swapbuffers(ChildDc)
      glClear(ClearBuffer)
    NEXT I
    END SUB
    
    '-------------------------------------------------------
    '-------------------------------------------------------
    
    SUB OglPixFormat(Pfd AS PIXELFORMATDESCRIPTOR, BYVAL Flag AS BYTE)
    
    '==============================================================
    ' This is the 1st step in setting up windows for OpenGl support
    ' %PFD_DOUBLEBUFFER seems to be required for OpenGl to display
    ' graphics
    '==============================================================
    
    LOCAL FlagParam AS DWORD
    
    IF Flag THEN
      FlagParam = %PFD_DRAW_TO_WINDOW OR %PFD_DRAW_TO_BITMAP OR %PFD_SUPPORT_OPENGL _
                  OR %PFD_DOUBLEBUFFER
    ELSE
      FlagParam = %PFD_DRAW_TO_WINDOW OR %PFD_SUPPORT_OPENGL OR %PFD_DOUBLEBUFFER
    END IF
    
    pfd.nsize = SIZEOF(PIXELFORMATDESCRIPTOR)
    pfd.nversion = 1
    pfd.dwflags = FlagParam
    pfd.dwlayermask = %PFD_MAIN_PLANE
    pfd.ipixeltype = %PFD_TYPE_RGBA
    pfd.ccolorbits = 24
    pfd.cdepthbits = 24
    pfd.caccumbits = 0
    pfd.cstencilbits = 0
    
    END SUB
    
    '----------------------------------------------------------------
    '----------------------------------------------------------------
    
    FUNCTION SetOglPipe(BYVAL Hndl AS DWORD, BYVAL GfxDc AS DWORD, _
                        tPixFmt AS PIXELFORMATDESCRIPTOR) AS LONG
    
    '================================================================
    '  This is the second step for OpenGL support
    '  ChoosePixelFormat selects a pixel format that matches that
    '  described in the PIXELFORMATDESCRIPTOR structure.
    '
    '  Each step is the setup is quried because not all windows support
    '  OpenGL rendering
    '================================================================
    
    LOCAL PixFormat AS DWORD
    LOCAL OglRc     AS DWORD
    
    LOCAL T AS LONG
    
    PixFormat = ChoosePixelFormat(GfxDc, tPixFmt)
    
    IF ISFALSE (PixFormat) THEN
      ? "Could not find matching pixel format: error # 1"
      EXIT FUNCTION
    END IF
    
    t = SetPixelFormat(GfxDc, PixFormat, tPixFmt)
    
    IF ISFALSE(t) THEN
      ? "Could not set selected pixel format: error # 2"
      EXIT FUNCTION
    END IF
    
    OglRc = wglCreateContext(GfxDc)
    
    IF ISFALSE(OglRc) THEN
      ? "Could not create rendering context: error # 3"
      EXIT FUNCTION
    END IF
    
    t = wglMakeCurrent(GfxDc, OglRc)
    
    IF ISFALSE(t) THEN
      ? "Could not make rendering context current.  Defined window may not" _
                   + " support it: error # 4"
      EXIT FUNCTION
    END IF
    
    SetOglPipe = OglRc
    END FUNCTION
    
    '----------------------------------------------------------------
    '----------------------------------------------------------------
    
    SUB SetOglWindow(BYVAL BtmXpos AS LONG, BYVAL BtmYpos AS LONG, BYVAL XWide AS LONG, _
                     BYVAL YHigh AS LONG, BYVAL OrthoRange AS LONG)
    
    LOCAL ClearClr AS DWORD
    
    '=================================================================
    ' This defines the graphics environment.
    ' Getting this correct is crucial to using OpenGL.
    '
    ' gluOrtho2D is equivalent to using glOrtho with the FAR
    ' parameter set to -1 and the NEAR parameter set to 1
    '=================================================================
    
    'ClearClr = %GL_COLOR_BUFFER_BIT OR %GL_DEPTH_BUFFER_BIT OR _
    '           %GL_ACCUM_BUFFER_BIT OR %GL_STENCIL_BUFFER_BIT
    
    glViewport(BtmXpos, BtmYpos, Xwide, YHigh)
    glMatrixMode(%GL_PROJECTION)
    glLoadIdentity
    'glOrtho(-Xwide / 2, XWide / 2, -Yhigh / 2, Yhigh / 2, -100, 100)
    glortho(-2, 2, 2, -2, 100, -100)
    glMatrixMode(%GL_MODELVIEW)
    glClearColor(0, 0, 0, 0) '(.6, 0, .2, 0)
    'glClear(ClearClr)
    'glLoadIdentity
    
    END SUB
    
    '------------------------------------------------------------------
    '------------------------------------------------------------------
    
    SUB CloseOpenGL(BYVAL OglRc AS DWORD, BYVAL GfxDc AS DWORD, BYVAL GfxHndl AS DWORD)
    
    wglMakeCurrent(0, 0)
    wglDeleteContext(OglRc)
    ReleaseDc(GfxHndl,GfxDc)
    
    END SUB
    
    '------------------------------------------------------------------
    '------------------------------------------------------------------
    
    SUB RenderDrawImage(BYVAL WinWide AS LONG, BYVAL WinHigh AS LONG, BYVAL ImgWide AS LONG, _
        BYVAL ImgHigh AS LONG, Buffer() AS DWORD)
    
    LOCAL BmpWide  AS LONG
    LOCAL BmpHigh  AS LONG
    
    LOCAL Xscale AS SINGLE
    LOCAL Yscale AS SINGLE
    
    BmpWide = ImgWide
    BmpHigh = ImgHigh
    
    Xscale! = WinWide / BmpWide
    Yscale! = WinHigh / BmpHigh
    
    glPixelZoom(Xscale!, Yscale!)
    glRasterPos2I(-2, 2)
    
    glDrawPixels(BmpWide, BmpHigh, %GL_RGBA, %GL_UNSIGNED_BYTE, VARPTR(Buffer(1)))
    
    glPixelZoom(1, 1)
    
    END SUB
    
    '----------------------------------------------------------------------
    '----------------------------------------------------------------------
    
    FUNCTION LoadBGRColorsFromFile(BmpWIDTH AS INTEGER, BmpHeight AS INTEGER, BmpSize AS LONG, _
             Bmpfile AS STRING, Buffer() AS DWORD) AS LONG
    '/===============================================================/'
    'FUNCTION LoadBGRColorsFromFile() loads bitmap data from the file specified by
    'Bmpfile and places the data into Buffer().
    '
    'The return value is the number of the bitmap bytes read, i. e.,
    'the size of the bitmap including padding.
    '/===============================================================/'
    
    REGISTER I      AS DWORD
    REGISTER J      AS DWORD
    
    LOCAL Colour, _
               K    AS DWORD
    
    LOCAL Bmp       AS STRING
    
    LOCAL tSave     AS BitMapFile_Struct
    
    LOCAL BytPtr    AS BYTE POINTER
    
    LOCAL ByteBuf() AS BYTE
    
    '---- open file to read from -------------------
    OPEN BmpFile$ FOR BINARY AS #1 BASE = 0
    
    GET #1, , tSave
    
    I = VAL("&H" + HEX$(ASC("M")) + HEX$(ASC("B")))
    
    '---- check file type --------------------------
    IF tSave.bftype <> I THEN
      MSGBOX "not a bitmap file " + STR$(I) + STR$(tSave.bftype)
      EXIT FUNCTION
    END IF
    
    '---- get image measurements -----------------------
    BmpWIDTH   = tSave.biWidth
    BmpHeight  = ABS(tSave.biHeight)
    
    BmpSIZE = tSave.bfSize - SIZEOF(BitMapFile_Struct)
    
    DIM ByteBuf(BmpSize)
    
    GET #1, , ByteBuf()
    CLOSE #1
    
    K = (BmpWidth) * (BmpHeight)
    
    REDIM Buffer(1 TO K)
    
    I = 0
    J = 0
    BytPtr = VARPTR(Colour)
    DO
      I = I + 1
      IF J >= BmpSize THEN EXIT DO
      @BytPtr[0] = ByteBuf(J)
      @BytPtr[1] = ByteBuf(J + 1)
      @BytPtr[2] = ByteBuf(J + 2)
      Buffer(I) = Colour
      J = J + 3
    LOOP
    
    LoadBmp = BmpSize
    
    END FUNCTION
    
    '----------------------------------------------------------------------
    '----------------------------------------------------------------------
    
    SUB CreateBmp(BmpHndl AS LONG, BmpDc AS LONG, BYVAL Xwide AS LONG, BYVAL Yhigh AS LONG, _
                     OldObj AS LONG)
    
    LOCAL DispHndl, _
            DispDc, _
             TmpDc, _
           BmpWide, _
           BmpHigh    AS LONG
    
    LOCAL tMap        AS Bitmap_Struct
    LOCAL tMapInfo    AS BITMAPINFO
    
    BmpWide = Xwide
    BmpHigh = Yhigh
    
    DispHndl = getdesktopwindow()
    DispDc   = getdc(DispHndl)
    Tmpdc    = createcompatibledc(DispDc)
    releasedc(DispHndl, DispDc)
    
    tMapInfo.bmiHeader.biSize        = SIZEOF(tMapInfo.bmiHeader)
    tMapInfo.bmiHeader.biWidth       = BmpWide
    tMapInfo.bmiHeader.biHeight      = -BmpHigh
    tMapInfo.bmiHeader.biPlanes      = 1
    tMapInfo.bmiHeader.biBitCount    = 32
    tMapInfo.bmiHeader.biCompression = %BI_RGB
    
    BmpHndl = CreateDIBSection(TmpDC, tMapInfo, %DIB_RGB_COLORS, 0, 0, 0)
    OldObj = selectobject(TmpDc, BmpHndl)
    
    Xwide = BmpWide
    Yhigh = BmpHigh
    BmpDc = TmpDc
    END SUB
    
    '---------------------------------------------------------------------------------
    '---------------------------------------------------------------------------------
    
    SUB SetRgbColors(BYVAL BmpHndl AS DWORD, RgbColors() AS DWORD)
    
    LOCAL BmpWide, _
          BmpHigh, _
             Ubnd, _
             Lbnd      AS DWORD
    
    
    LOCAL BmpClrs()   AS DWORD
    
    LOCAL LocPtr      AS DWORD POINTER
    
    LOCAL tMap        AS Bitmap_Struct
    
    Lbnd = LBOUND(RgbColors)
    Ubnd = UBOUND(RgbColors)
    
    IF Ubnd - Lbnd + 1 = 0 THEN
      MSGBOX "Input array " + $DQ + "RgbColors()" + $DQ +" is not dimensioned"
      EXIT SUB
    END IF
    
    getobject(BmpHndl, SIZEOF(Bitmap_Struct), tMap)
    
    BmpWide = tMap.bmWidth
    BmpHigh = tMap.bmHeight
    LocPtr = tMap.bmBits
    
    DIM BmpClrs(1 TO BmpWide * BmpHigh) AT LocPtr
    MAT BmpClrs() = RgbColors()
    
    END SUB
    
    '----------------------------------------------------------------------------------
    '----------------------------------------------------------------------------------
    
    SUB CreateRGBAColors(Buffer() AS DWORD)
    
    REGISTER      I AS DWORD
    
    LOCAL Colour, _
            Lbnd, _
            Ubnd    AS LONG
    
    LOCAL TmpByte   AS BYTE
    
    LOCAL ClrPtr    AS BYTE POINTER
    
    Lbnd = LBOUND(Buffer)
    Ubnd = UBOUND(Buffer)
    
    IF Ubnd - Lbnd + 1 = 0 THEN
      MSGBOX "Input variable " + $DQ + "Buffer()" + $DQ + " is not dimensioned)
      EXIT SUB
    END IF
    
    ClrPtr = VARPTR(Colour)
    
    FOR I = Lbnd TO Ubnd
      Colour = Buffer(I)
      TmpByte = @ClrPtr[0]
      @ClrPtr[0] = @ClrPtr[2]
      @ClrPtr[2] = TmpByte
      @ClrPtr[3] = 255
      Buffer(I) = Colour
    NEXT I
    
    END SUB
    
    '----------------------------------------------------------------------------------
    '----------------------------------------------------------------------------------
    Walt Decker

  • #2
    Minor change: Add: SLEEP 5 to the loop code in BeginOgl like this:
    Code:
    FOR I = 1 TO 1000
      CALL RenderDrawImage(64, 64, XWide, Yhigh, Buffer())
      glrotatef(.1, 0, 0, 1)
      glrotatef(-.1, 1, 0, 0)
     glrotatef(.3, 0, 1, 0)
      glbegin(%GL_TRIANGLES)
      glvertex3f(0, -1.75, 0)
      glvertex3f(1.75, 0, 0)
      glvertex3f(-1.75, 1.75, 0)
     glend()
     swapbuffers(ChildDc)
     glClear(ClearBuffer)
     SLEEP 5
    NEXT I
    Last edited by Chris Boss; 29 Apr 2017, 12:51 PM.
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      Your problem is that you cycled your code too fast and you are drawing faster than the display can keep up with, so you get image tearing.
      Chris Boss
      Computer Workshop
      Developer of "EZGUI"
      http://cwsof.com
      http://twitter.com/EZGUIProGuy

      Comment


      • #4
        Thanks, Chris, but that is not the problem. The problem is that background via gldrawpixels is moving. I need the background to remain in one place as the triangle rotates.
        Walt Decker

        Comment


        • #5
          I think this is what you're after...

          Code:
          FOR I = 1 TO 10000
          glPushMatrix() : glLoadIdentity()
            CALL RenderDrawImage(64, 64, XWide, Yhigh, Buffer())
          glPopMatrix()
            glrotatef(.1, 0, 0, 1)
            glrotatef(-.1, 1, 0, 0)
            glrotatef(.3, 0, 1, 0)
            glbegin(%GL_TRIANGLES)
              glvertex3f(0, -1.75, 0)
              glvertex3f(1.75, 0, 0)
              glvertex3f(-1.75, 1.75, 0)
            glend()
            swapbuffers(ChildDc)
            glClear(ClearBuffer)
          glflush() 'just precautionary; see also glFinish()
          NEXT I
          "Show me a young Conservative and I'll show you someone with no heart. Show me an old Liberal and I'll show you someone with no brains." ― Winston Churchill
          "Socialism is the philosophy of failure, the creed of ignorance and the gospel of envy." ― Winston Churchill

          Comment


          • #6
            Another way to do this is..
            1. use %PFD_SWAP_COPY in the pixelformatdescriptor flag when setting pixelformat
            When swapping buffers, the back is copied to the front, but not the front to back, leaving the back unchanged.
            2. Render the Bitmap ONCE to the backbuffer.
            3. Swap buffers, and render the triangle to the FRONTBUFFER.
            4. Repeat 3 as necessary.

            Of coarse, it would then be unnecessary to:
            clear the buffer as, since swapping now just copies the back buffer (your bmp) to front overwriting the last triangle.
            (in fact, clearing the buffer is unnecessary in your original code, since the background is entirely your bmp...)
            or re-copy the bmp each time to the back buffer.

            This method can be better, but depends on the complexity of the scene. I'll leave that for you to determine.

            A note: I would have provided a sample, but since you seem determined to not use the provided include files, I wasn't going to sort it all out, since some equates / functions where note defined that would be needed when revising your code. I'd advise for test code on the forum, just stick to using includes, so when someone else might modify your code (to help you), the declarations and equates are more easily available.
            Last edited by Doug Hitchens; 29 Apr 2017, 03:15 PM. Reason: Left out: %PFD_SWAP_COPY
            "Show me a young Conservative and I'll show you someone with no heart. Show me an old Liberal and I'll show you someone with no brains." ― Winston Churchill
            "Socialism is the philosophy of failure, the creed of ignorance and the gospel of envy." ― Winston Churchill

            Comment


            • #7
              Walt, Make sure you do everything in 32 bits. All memory buffers and the opengl buffer should be in 32 bits, otherwise you will have problems in properly calculating bit positions. I have no problem in using glDrawPixels as long as I use 32 bit buffers. The same goes with the GDI, always use 32 bit buffers. 16 bit and 24 bit buffers require offsets and padding bits, while 32 bits can simply move the entire buffer in one pass with no offsets or padding issues. My EZGUI engine has an opengl canvas control which is a hybrid and uses bitmap background buffers which it draws using glDrawPixels. I works fine when using 32 bits.
              Chris Boss
              Computer Workshop
              Developer of "EZGUI"
              http://cwsof.com
              http://twitter.com/EZGUIProGuy

              Comment


              • #8
                Shifting of the image is a likely result of failing to take bit padding into consideration when using 16 or 24 bit buffers.
                Chris Boss
                Computer Workshop
                Developer of "EZGUI"
                http://cwsof.com
                http://twitter.com/EZGUIProGuy

                Comment


                • #9
                  Doug:
                  glPushMatrix() : glLoadIdentity() CALL RenderDrawImage(64, 64, XWide, Yhigh, Buffer()) glPopMatrix()
                  Never tried over-writing a background image. At on time I had push and pop in the code but forgot the loadid.

                  Used both extensively in a demo to manipulate only parts of an object. As far as examples go, pointing in the right direction is usually sufficient; however, pseudo-code can be helpful.

                  Thanks.

                  Chris: All the data is 32-bit. Thanks for the advice.
                  Walt Decker

                  Comment

                  Working...
                  X