Announcement

Collapse
No announcement yet.

Problems with OpenGL code in PBWin7.02

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

  • Problems with OpenGL code in PBWin7.02

    This isn't intended to be a "my code is broken, please fix it for
    me" type of post, but rather a request to see if anyone else sees
    a spinning red triangle when compiled in 7.02.

    Using the latest WinAPI for both, this code behaves as intended in
    7.0, but I'm getting nothing but a blank, black window if I use 7.02.

    If there's something glaring at you, or if you have any suggestions
    or ideas as to why this may be happening, PLEASE let me know-- I have
    a TON of OpenGL code I'd like to post, but won't if there are problems
    with it when using the latest version of PBWin.

    One note: Normally the OpenGL sub/function declarations, macros etc.
    are in a separate include file. This is simply a condensed
    bare-bones example that still incurs the problem.

    #COMPILE EXE
    OPTION EXPLICIT

    #INCLUDE "WIN32API.INC"
    DECLARE SUB glBegin LIB "opengl32.dll" ALIAS "glBegin" (BYVAL mode AS DWORD)
    DECLARE SUB glClear LIB "opengl32.dll" ALIAS "glClear" (BYVAL mask AS DWORD)
    DECLARE SUB glColor3f LIB "opengl32.dll" ALIAS "glColor3f" (BYVAL red AS SINGLE,_
    BYVAL green AS SINGLE, BYVAL blue AS SINGLE)
    DECLARE SUB glEnd LIB "opengl32.dll" ALIAS "glEnd"
    DECLARE SUB glFlush LIB "opengl32.dll" ALIAS "glFlush"
    DECLARE SUB glFrustum LIB "opengl32.dll" ALIAS "glFrustum" (BYVAL nleft AS DOUBLE,_
    BYVAL nright AS DOUBLE, BYVAL bottom AS DOUBLE, BYVAL top AS DOUBLE,_
    BYVAL zNear AS DOUBLE, BYVAL zFar AS DOUBLE)
    DECLARE SUB glLoadIdentity LIB "opengl32.dll" ALIAS "glLoadIdentity"
    DECLARE SUB glMatrixMode LIB "opengl32.dll" ALIAS "glMatrixMode" (BYVAL mode AS DWORD)
    DECLARE SUB glRotatef LIB "opengl32.dll" ALIAS "glRotatef" (BYVAL angle AS SINGLE,_
    BYVAL x AS SINGLE, BYVAL y AS SINGLE, BYVAL z AS SINGLE)
    DECLARE SUB glTranslatef LIB "opengl32.dll" ALIAS "glTranslatef" (BYVAL x AS SINGLE,_
    BYVAL y AS SINGLE, BYVAL z AS SINGLE)
    DECLARE SUB glViewport LIB "opengl32.dll" ALIAS "glViewport" (BYVAL x AS LONG,_
    BYVAL y AS LONG, BYVAL nWIDTH AS LONG, BYVAL height AS LONG)
    DECLARE SUB glVertex3f LIB "opengl32.dll" ALIAS "glVertex3f" (BYVAL x AS SINGLE,_
    BYVAL y AS SINGLE, BYVAL z AS SINGLE)
    ' gluPerspective
    MACRO gluPerspective(fovy, nAspect, zNear, zFar)
    MACROTEMP xmin, xmax, ymin, ymax
    DIM xmin AS DOUBLE : DIM xmax AS DOUBLE
    DIM ymin AS DOUBLE : DIM ymax AS DOUBLE
    ymax = zNear * TAN((fovy * 3.14159265358979#)/360#)
    ymin = -ymax
    xmin = ymin * nAspect
    xmax = ymax * nAspect
    glFrustum xmin, xmax, ymin, ymax, zNear, zFar
    END MACRO

    'GLOBAL VARIABLES
    GLOBAL angle AS SINGLE
    GLOBAL g_HDC AS DWORD

    FUNCTION WINMAIN (BYVAL hInstance AS DWORD, _
    BYVAL hPrevInstance AS DWORD, _
    BYVAL lpCmdLine AS ASCIIZ PTR, _
    BYVAL iCmdShow AS LONG) AS LONG

    LOCAL msg AS tagMsg
    LOCAL wce AS WndClassEx
    LOCAL ClassName AS ASCIIZ * 80
    LOCAL hWnd AS DWORD
    LOCAL done AS DWORD

    ClassName = "PowerBasic OpenGL"

    wce.cbSize = SIZEOF(wce)
    wce.style = %CS_HREDRAW OR %CS_VREDRAW

    ' CODEPTR is used to pass the address of the function that will
    ' receive all messages sent to any window created with this class
    wce.lpfnWndProc = CODEPTR(WndProc)

    wce.cbClsExtra = 0
    wce.cbWndExtra = 0
    wce.hInstance = hInstance
    wce.hIcon = LoadIcon(hInstance, "PBGL_EXAMPLE")
    wce.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
    wce.hbrBackground = GetStockObject(%BLACK_BRUSH)
    wce.lpszMenuName = %NULL
    wce.lpszClassName = VARPTR(ClassName)
    wce.hIconSm = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)

    RegisterClassEx wce

    ' Create a window using the registered class
    hWnd = CreateWindowEx(%NULL,_ 'extended style
    ClassName,_ ' window class name
    "PowerBasic OpenGL-- Example 1",_ ' window caption
    %WS_OVERLAPPEDWINDOW OR _ ' window style
    %WS_VISIBLE OR %WS_SYSMENU,_ '
    100, 100,_ ' initial x, y position
    400, 400,_ ' initial x, y size
    %NULL, _ ' parent window handle
    %NULL, _ ' window menu handle
    hInstance, _ ' program instance handle
    BYVAL %NULL) ' creation parameters

    IF hWnd = 0 THEN
    MSGBOX STR$(hWnd)'"Unable to create window"
    EXIT FUNCTION
    END IF

    ' Display the window on the screen
    ShowWindow hWnd, iCmdShow
    UpdateWindow hWnd

    ' Main message loop:
    ' Messages sent to PBGL_EXAMPLE while it has the focus are received by
    ' PeekMessage(). This loop translates each message and dispatches it
    ' to the appropriate handler. When PostQuitMessage() is called, the
    ' loop terminates which ends the application.
    DO
    IF PeekMessage(msg, hWnd, 0, 0, %PM_REMOVE) THEN
    IF msg.message = %WM_QUIT THEN EXIT LOOP
    END IF
    glClear &H00004000 OR &H00000100
    glLoadIdentity

    angle = angle + 1
    IF (angle >= 360!) THEN angle = 0
    glTranslatef 0, 0, -5
    glRotatef angle, 0, 0, 1

    glColor3f 1, 0, 0
    glBegin &H0004 ' "GL_TRIANGLES" = &H0004
    glVertex3f 0, 0, 0
    glVertex3f 1, 0, 0
    glVertex3f 1, 1, 0
    glEnd

    SwapBuffers g_HDC

    TranslateMessage msg
    DispatchMessage msg
    'end if
    LOOP

    FUNCTION = msg.wParam

    END FUNCTION

    'Subroutine to set the pixel format for the device context
    SUB SetupPixelFormat(hDC AS DWORD)
    LOCAL nPixelFormat AS LONG

    STATIC pfd AS PIXELFORMATDESCRIPTOR
    pfd.nSize = SIZEOF(PIXELFORMATDESCRIPTOR) 'Size of UDT structure
    pfd.nVersion = 1 'Version. Always set to 1.
    pfd.dwFlags = %PFD_DRAW_TO_WINDOW OR _ 'Support Window
    %PFD_SUPPORT_OPENGL OR _ 'Support OpenGL
    %PFD_DOUBLEBUFFER 'Support Double Buffering
    pfd.iPixelType = %PFD_TYPE_RGBA 'Red, Green, Blue, & Alpha Mode
    pfd.cColorBits = 32 '32-Bit Color Mode
    pfd.cRedBits = %NULL 'Ignore Color and Shift Bits...
    pfd.cRedShift = %NULL '...
    pfd.cGreenBits = %NULL '...
    pfd.cGreenShift = %NULL '...
    pfd.cBlueBits = %NULL '...
    pfd.cBlueShift = %NULL '...
    pfd.cAlphaBits = %NULL 'No Alpha Buffer
    pfd.cAlphaShift = %NULL 'Ignore Shift Bit.
    pfd.cAccumBits = %NULL 'No Accumulation Buffer
    pfd.cAccumRedBits = %NULL 'Ignore Accumulation Bits...
    pfd.cAccumGreenBits = %NULL '...
    pfd.cAccumBlueBits = %NULL '...
    pfd.cAccumAlphaBits = %NULL '... Good Cereal!
    pfd.cDepthBits = 16 '16-bit z-buffer depth
    pfd.cStencilBits = %NULL 'No Stencil Buffer
    pfd.cAuxBuffers = %NULL 'No Auxiliary Buffer
    pfd.iLayerType = %PFD_MAIN_PLANE 'Main Drawing Plane
    pfd.bReserved = %NULL 'Reserved
    pfd.dwLayerMask = %NULL 'Ignore Layer Masks...
    pfd.dwVisibleMask = %NULL '...
    pfd.dwDamageMask = %NULL '...

    'Choose best matching pixel format, return index.
    nPixelFormat = ChoosePixelFormat(hDC, pfd)

    'Set pixel format to device context.
    hDC = SetPixelFormat(hDC, nPixelFormat, pfd)
    END SUB

    ' WndProc is the message handler for all windows creating using the "PBGL_EXAMPLE"
    ' class name. A single WndProc procedure can handle multiple windows by
    ' testing the hWnd variable passed to it.

    FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, _
    BYVAL wParam AS DWORD, BYVAL lParam AS LONG) EXPORT AS LONG

    STATIC hDC AS DWORD
    STATIC hRC AS DWORD
    LOCAL gHeight AS LONG
    LOCAL gWidth AS LONG
    LOCAL gAspect AS SINGLE

    ' The SELECT CASE is used to catch only those messages which the message
    ' handler needs to process. All other messages are passed through the
    ' tests to the default handler.
    SELECT CASE wMsg

    CASE %WM_CREATE
    hDC = GetDC(hWnd)
    g_hDC = hDC
    SetupPixelFormat(hDC)
    hRC = wglCreateContext(hDC)
    wglMakeCurrent hDC, hRC
    FUNCTION = 0
    EXIT FUNCTION

    CASE %WM_CLOSE
    wglMakeCurrent hDC, %NULL
    wglDeleteContext hDC
    PostQuitMessage 0
    FUNCTION = 0
    EXIT FUNCTION

    CASE %WM_SIZE
    gHeight = HIWRD(lParam)
    gWidth = LOWRD(lParam)
    IF gHeight < 1 THEN gHeight = 1 'To prevent divide by zero error.

    'Reset the viewport to new dimensions
    glViewport 0, 0, gWidth, gHeight
    glMatrixMode &H1701 '"GL_PROJECTION" = &H1701
    glLoadIdentity
    gAspect = gWidth/gHeight
    gluPerspective(45!, gAspect, 1!, 1000!)
    glMatrixMode &H1700 '"GL_MODELVIEW" = &H1700
    glLoadIdentity
    FUNCTION = 0
    EXIT FUNCTION

    CASE ELSE
    FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)

    END SELECT

    ' Any message which is not handled in the above SELECT CASE reaches this
    ' point and is processed by the Windows default message handler.


    END FUNCTION


    Thanks!

    ------------------
    Scott Martindale
    [email protected]

    [This message has been edited by Scott J. Martindale (edited March 28, 2003).]
    Scott Martindale
    [email protected]

  • #2
    Scott,

    Dont' have PB 7.0, so I converted the one macro to a sub and
    compiled using PB 6.1 and it works fine !

    There was one error in the win32api.inc file, where swapbuffers
    was declared wrong (the alias was missing the wgl prefix).



    ------------------
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      Thanks for checking, Chris.

      Nice to know that macro aside, it is backwards-compatible.
      The thing that has me scratching my head is, using the same API
      include, 7.00 compiles the code as intended, while 7.02 doesn't.
      My point being is that the only variable in the (in)equation that
      I can see, are internal differences between the compilers.

      If this is "driver-error" (something I'm doing wrong), it's something
      that I've been doing "wrong" for a while...

      Did I miss the thread telling us where we can get the info on
      what the new do's and don'ts for the latest version?

      ------------------
      Scott Martindale
      [email protected]
      Scott Martindale
      [email protected]

      Comment


      • #4
        hDC = SetPixelFormat(hDC, nPixelFormat, pfd)
        SetPixelFormat returns a boolean, you are setting a boolean to
        a handle to a device context, (as you are passing hdc byref) ... BOOM!

        Regards,

        RValois.


        ------------------
        http://www.rvalois.com.br/downloads/free/

        Comment


        • #5
          DOH!

          Better it be head-slapper, than something more "serious".

          Replacing
          Code:
           hDC = SetPixelFormat(hDC, nPixelFormat, pfd)
          with
          Code:
           SetPixelFormat(hDC, nPixelFormat, pfd)
          does the trick...

          Thanks!


          ------------------
          Scott Martindale
          [email protected]



          [This message has been edited by Scott J. Martindale (edited March 28, 2003).]
          Scott Martindale
          [email protected]

          Comment


          • #6
            There was one error in the win32api.inc file, where swapbuffers
            was declared wrong (the alias was missing the wgl prefix).
            Err, no. SwapBuffers is a GDI32.DLL function. wglSwapBuffers is an
            OPENGL32.DLL function. I am not seeing any docs on the latter in MSDN,
            so it may not be intended for public use...

            ------------------
            Tom Hanlin
            PowerBASIC Staff

            Comment


            • #7
              Tom;

              I have PB 6.1 in the win32api.inc file this the declaration:

              Code:
              DECLARE FUNCTION SwapBuffers LIB "OPENGL32.DLL" ALIAS "SwapBuffers" (BYVAL hDC AS DWORD) AS LONG
              (should have used alias of wglSwapBuffers if calling opengl32.dll library)

              In the latest PB win32api.inc file downloaded from web site it is now:
              (also same in PB 6.0 win32api.inc file!)

              Code:
              DECLARE FUNCTION SwapBuffers LIB "GDI32.DLL" ALIAS "SwapBuffers" (BYVAL hDC AS DWORD) AS LONG
              My post was correct when using PB 6.1 inc file (if you haven't
              downloaded most recent).

              PB 6.0 has it right.
              PB 6.1 had it wrong.
              PB 7.0+ has it right.


              ------------------


              [This message has been edited by Chris Boss (edited March 31, 2003).]
              Chris Boss
              Computer Workshop
              Developer of "EZGUI"
              http://cwsof.com
              http://twitter.com/EZGUIProGuy

              Comment

              Working...
              X