Author Topic: Why wont GLU work with this framework?  (Read 10939 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Why wont GLU work with this framework?
« on: June 27, 2009 »
As soon as I try to include GLU with this nice framework it will not compile any more, it finds the following error..

Quote
C:\FreeBasic\inc\gl/glu.bi(192) : error 6: Expected '(', found: 'max'
declare function gluBuild1DMipmapLevels alias "gluBuild1DMipmapLevels" (byval target as GLenum, byval internalFormat as GLint, byval width as GLsizei, byval format as GLenum, byval type as GLenum, byval level as GLint, byval base as GLint, byval max as GLint, byval data as any ptr) as GLint

The framework..

Code: [Select]
'---------------------------------------------------------------------------------
'
'   FreeBasic OpenGL Window Framework by rbraz
'
'   12 Apr 2008
'
'---------------------------------------------------------------------------------


#include once "windows.bi"
#include once "gl/gl.bi"
#include once "gl/glu.bi"
#include once "gl/glext.bi"
#include once "crt.bi"


'DEVMODE bug, found the cure here --> http://www.freebasic.net/forum/viewtopic.php?t=5659&highlight=devmode
'Will be corrected by Victor in the next freebasic update
#undef DEVMODE
Type DEVMODE
    dmDeviceName As Zstring * CCHDEVICENAME
    dmSpecVersion As WORD
    dmDriverVersion As WORD
    dmSize As WORD
    dmDriverExtra As WORD
    dmFields As DWORD
    dmOrientation As Short
    dmPaperSize As Short
    dmPaperLength As Short
    dmPaperWidth As Short
    dmScale As Short
    dmCopies As Short
    dmDefaultSource As Short
    dmPrintQuality As Short
    dmColor As Short
    dmDuplex As Short
    dmYResolution As Short
    dmTTOption As Short
    dmCollate As Short
    dmFormName As Zstring * CCHFORMNAME
    dmLogPixels As WORD
    dmBitsPerPel As DWORD
    dmPelsWidth As DWORD
    dmPelsHeight As DWORD
    dmDisplayFlags As DWORD
    dmDisplayFrequency As DWORD
    dmICMMethod As DWORD
    dmICMIntent As DWORD
    dmMediaType As DWORD
    dmDitherType As DWORD
    dmReserved1 As DWORD
    dmReserved2 As DWORD
    dmPanningWidth As DWORD
    dmPanningHeight As DWORD
End Type



  'Win Main                
  Declare Function WinMain ( byval hInstance as HINSTANCE, _
                           byval hPrevInstance as HINSTANCE, _
                           szCmdLine as string, _
                           byval iCmdShow as integer ) as integer
    '
    ' Entry point    
    '
end WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )  
    
  'app window procedure
  Declare Function WndProc( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT    
                          
  'window creation helper
  Declare Function MakeWindow(byval hInstance as HINSTANCE) as integer
                              
  Declare Sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)


  'OpenGl Draw sub
  Declare Sub glDraw()
  

  'Application name
  Const szAppName = "The Leading Force"
  
  'app window handle
  Dim Shared hWndWin as HWND
  
  'app hdc
  Dim Shared hdcWin as HDC
  
  'window mode
  Dim Shared winmode as integer
  
  'Window resolution
  const XRES = 800
  const YRES = 600




' ----------------------------------
' name: WndProc
' desc: Processes windows messages
'
' ----------------------------------
function WndProc ( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT
        
    '
    ' Process messages
    '
    select case( message )    
'
' Key pressed
'
case WM_KEYDOWN
if( lobyte( wParam ) = 27 ) then
PostMessage( hWnd, WM_CLOSE, 0, 0 )
end if

        '
        ' Window was closed
        '
     case WM_DESTROY
            PostQuitMessage( 0 )
            exit function
            
    end select
    
    '
    ' Message doesn't concern us, send it to the default handler
    ' and get result
    '
    function = DefWindowProc( hWnd, message, wParam, lParam )    
    
end function

' ---------------------------------------
' name: WinMain
' desc: A win2 gui program entry point
'
' ---------------------------------------
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   szCmdLine as string, _
                   byval iCmdShow as integer ) as integer    
    
    dim wMsg as MSG
        
    ' get module instance
    hInstance = GetModuleHandle(NULL)
    
    'Create our skinned window    
    if MakeWindow(hInstance) = 0 then
        MessageBox(NULL, "Failed to create window", szAppName, MB_ICONERROR )
        return -1
    end if
    
    ShowWindow( hWndWin, iCmdShow )
    UpdateWindow( hWndWin )
      
    'Get window device context
    hdcWin = GetDC(hWndWin)
    
    'Initialize OGL window
    dim pfd as PIXELFORMATDESCRIPTOR
    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 = 32
    pfd.cDepthBits = 32
    SetPixelFormat ( hdcWin, ChoosePixelFormat ( hdcWin, @pfd) , @pfd )
    wglMakeCurrent ( hdcWin, wglCreateContext(hdcWin) )
      
    '
    ' Process windows messages
    '
    while(wMsg.message <> WM_QUIT)
        
        if (PeekMessage (@wMsg,NULL,0,0,PM_REMOVE) <> 0) then
            DispatchMessage(@wMsg)
        else
            glDraw()
            SwapBuffers (hdcWin)
        end if
    
    wend
    
    if winmode = 0 then
        ChangeDisplaySettings(NULL,0)
   ShowCursor (true)
    end if
    
    ' Ends a process and all its threads.
    ExitProcess(0)    

end function

'Change The Screen Resolution
sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)
dim dmScreenSettings as DEVMODE
ZeroMemory(@dmScreenSettings, sizeof (DEVMODE))
dmScreenSettings.dmSize = sizeof(DEVMODE)
dmScreenSettings.dmPelsWidth = wwidth
dmScreenSettings.dmPelsHeight = hheight
dmScreenSettings.dmBitsPerPel = bitsPerPixel
dmScreenSettings.dmFields = DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT

    if (ChangeDisplaySettings (cast(LPDEVMODE,@dmScreenSettings), CDS_FULLSCREEN) <> DISP_CHANGE_SUCCESSFUL) then
        MessageBox(NULL,"Change Display Error!", szAppName,MB_OK or MB_ICONEXCLAMATION)
end -1
end if
    
end sub
' ------------------------------------------------------------------------
' Window creation function.
' ------------------------------------------------------------------------
Function MakeWindow(byval hInstance as HINSTANCE) as integer
  
  function = 0
  
  ' our window class
  dim wndWc as WNDCLASS
  
  dim windowStyle as DWORD
  dim windowExtendedStyle as DWORD
  dim rc as RECT

  ' ---------------------------------------------------------
  ' fill window class members
  ' ---------------------------------------------------------
  wndWc.style         = CS_OWNDC
  wndWc.lpfnWndProc   = @WndProc
  wndWc.cbClsExtra    = 0
  wndWc.cbWndExtra    = 0
  wndWc.hInstance     = GetModuleHandle(NULL)
  wndWc.hIcon         = NULL
  wndWc.hCursor       = LoadCursor(0, IDC_ARROW)
  wndWc.hIcon         = LoadIcon(hInstance, IDI_APPLICATION) ' Load our normal icon
  wndWc.hbrBackground = GetStockObject(BLACK_BRUSH)
  wndWc.lpszMenuName  = NULL
  wndWc.lpszClassName = strptr( szAppName )

  ' register class
  if( RegisterClass( @wndWc ) = FALSE ) then
      MessageBox(NULL, "Failed to register Window Class!", szAppName, MB_ICONERROR )
      exit function
  end if
  
  rc.left   = 0
  rc.top    = 0
  rc.right  = XRES
  rc.bottom = YRES
  
  'Setup video mode window - Windowed / Fullscreen
  if( MessageBox(NULL,"Full Screen ?", "Skid Row",MB_YESNO or MB_ICONQUESTION) = IDYES ) then
    winmode = 0
  else
    winmode = 1
  end if

  if (winmode = 0) then
    windowStyle = WS_POPUP or WS_VISIBLE
    windowExtendedStyle = WS_EX_TOPMOST
    ChangeScreenResolution (XRES, YRES, 32)
  else
    windowStyle =  WS_SYSMENU or WS_POPUP or WS_VISIBLE or WS_MINIMIZEBOX
    windowExtendedStyle = WS_EX_APPWINDOW
    'Center window
    rc.left = (GetSystemMetrics(SM_CXSCREEN) - XRES) / 2
    rc.top =  (GetSystemMetrics(SM_CYSCREEN) - YRES) / 2
  end if
 
  ' create the window
  hWndWin = CreateWindowEx(windowExtendedStyle,   _
wndWc.lpszClassName, _
                    szAppName,           _
windowStyle,         _
                             rc.left,             _
rc.top,              _
                             rc.right,            _
        rc.bottom,          _
                             HWND_DESKTOP, 0,     _
hInstance, 0)
  
  ShowCursor(false)
  
  ' return result
  if (hWndWin) then
      return 1
  else
      return 0
  end if

end function


sub glDraw()
    glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
    
    GLLOADIDENTITY
    
    GLBEGIN GL_TRIANGLES

    GLCOLOR3F 1,0,0
    GLVERTEX3F 0,-1,0
    GLCOLOR3F 0,1,0
    GLVERTEX3F 1, 1,0
    GLCOLOR3F 0,0,1
    GLVERTEX3F -1, 1,0

    GLEND

end sub

I was hoping to be able to use some of the glu functions like gluperspective and possibly some quadrics.
I can use glu fine with the tiny (and pretty incompatible) framework.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2756
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Why wont GLU work with this framework?
« Reply #1 on: June 27, 2009 »
It seems that you are using an ancient version of freebasic (0.15 perhaps)  :D

Works fine on fb 0.20 without any modifications.

If you don't want to update your FB, try to change your "include" sequence like this:
Quote
#include once "gl/gl.bi"
#include once "gl/glu.bi"
#include once "gl/glext.bi"
#include once "crt.bi"
#include once "windows.bi"

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #2 on: June 27, 2009 »
Cheers rbz, it is fb 1.5... Maybe time I upgraded... I had a similar problem with minifmod but finally fixed it.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #3 on: June 27, 2009 »
yep definatly upgrade dude!! :D
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #4 on: June 28, 2009 »
The root of the problem is that max is a keyword.  You'd need to change the .bi file not to use max as a parameter name in that function (you can change it to mymax and it should just work).

Jim
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #5 on: June 28, 2009 »
Cheers Jim :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #6 on: July 01, 2009 »
Just a little correction, max is not an FB keyword. The reason you were getting the error was because FB was trying to expand a macro that's defined in windef.bi (which is automatically included by windows.bi):

#ifndef NOMINMAX
#ifndef max
#define max(a,b) iif((a)>(b), a, b)
#endif
#ifndef min
#define min(a,b) iif((a)<(b), a, b)
#endif
#endif


Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #7 on: July 01, 2009 »
Thanks for explaining why it failed Merick and welcome back, haven't seen you for a while :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #8 on: July 03, 2009 »
I noticed it because these min/max macros seem to be quite common, I've seen them several times before.

Haven't been around much because (except for another attempt to translate the mappy maploader) I haven't done much programming. Most of the time I just end up staring at the screen with no ideas.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #9 on: July 03, 2009 »
Happens to the best of us im afraid dude.
If I havent greeted you before, then a warm welcome and nice to meet you dude.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #10 on: July 11, 2009 »
The tips worked and now I am able to draw quadrics.

What I find confusing with this framework at the moment is where to define my screen settings.

GLUPERSPECTIVE 105.0, W_XW / W_YH, .1, 100.0

Seems to have no effect wherever I place it and the display is limited to a very small depth, the layout of this more stable framework is so totally different to the hacked one which I was used to using.

Also why do you clear the screen like this;

glClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT)

Instead of;

glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)

?

The biggie is this though...

A typical setup for one of my windows may look something like this;

Code: [Select]
    GLVIEWPORT 0, 0, w_xw, w_yh                         
GLMATRIXMODE GL_PROJECTION                         
GLLOADIDENTITY
   
    GLUPERSPECTIVE 105.0, W_XW / W_YH, .1, 100.0     
   
GLMATRIXMODE GL_MODELVIEW                         
GLLOADIDENTITY                                     

GLSHADEMODEL GL_SMOOTH                       
GLCLEARCOLOR 0.0, 0.0, 0.0,0
GLCLEARDEPTH 1.0                                   
GLENABLE GL_DEPTH_TEST                             
GLDEPTHFUNC GL_LEQUAL                               
GLHINT GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST   


    GLENABLE (GL_CULL_FACE)
    GLCULLFACE(GL_BACK)
   
    GLENABLE GL_BLEND
    GLBLENDFUNC(GL_ONE_MINUS_CONSTANT_ALPHA,GL_SRC_ALPHA)   


    GLHINT(GL_FOG_HINT,GL_NICEST)
    GLFOGF(GL_FOG_DENSITY,0.045)
    GLFOGF(GL_FOG_START,    6.0)
    GLFOGF(GL_FOG_END,     100.0)
    GLFOGI(GL_FOG_MODE,GL_EXP)   
    GLENABLE (GL_FOG)
    GLLINEWIDTH 1
    GLENABLE GL_SMOOTH


    GLLOADIDENTITY       
    glLightfv( GL_LIGHT1, GL_AMBIENT, @LightAmbient(0))        '' Load Light-Parameters Into GL_LIGHT1
glLightfv( GL_LIGHT1, GL_DIFFUSE, @LightDiffuse(0))

    glLightfv( GL_LIGHT1, GL_POSITION, @LightPosition(0))


    glLightfv( GL_LIGHT2, GL_AMBIENT, @LightAmbient2(0))        '' Load Light-Parameters Into GL_LIGHT1
    glLightfv( GL_LIGHT2, GL_POSITION, @LightPosition2(0))


    glLightf ( GL_LIGHT2, GL_LINEAR_ATTENUATION,0.00001)
    glLightf ( GL_LIGHT2, GL_QUADRATIC_ATTENUATION,0.6)
   
    glEnable(GL_LIGHTING)
glEnable(GL_LIGHT1)
    glEnable(GL_LIGHT2)

So where would I put that stuff so that I know it will work?

Thanks.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #11 on: July 12, 2009 »
It's in the right place at the moment.  Your z values will be between .1 and 100.  That's a smallish range, might want to set far plane to 32000 or something.

bitwise-OR and + are the same when you're working with powers of two.
ie.
001b=1
010b=2
100b=4
1+2+4 = 7

001 OR 010 OR 100 = 111b = 7

The parameters for glClear are bitflags with powers of 2, each one indicating a thing to do.

Jim
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #12 on: July 12, 2009 »
It's in the right place at the moment.  Your z values will be between .1 and 100.  That's a smallish range, might want to set far plane to 32000 or something.

It's not anywhere at the moment mate.
Wherever I place it it seems not to make a difference.

bitwise-OR and + are the same when you're working with powers of two.

Is it faster to do the OR or the AND ?


Thanks.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #13 on: July 12, 2009 »
I'm not sure about the other things, but if it were me then when doing the flags I would stick to using OR. Most of the C code that I've seen for gl uses the | operator. Not all of the gl flags are power of 2, so if you fall into the habit of using + instead of OR it might cause some problems with some of the other functions.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #14 on: July 12, 2009 »
Quote
Quote
It's in the right place at the moment
It's not anywhere at the moment mate.
I mean, where you put it in your code snippet was the right place.

Quote
Quote
bitwise-OR and + are the same when you're working with powers of two.
Is it faster to do the OR or the AND ?
Exactly the same on every kind of cpu I've ever used.  But in this case if the FB compiler is any good it will have worked out the fact the expression is constant and turned it into a number.

Jim
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #15 on: July 12, 2009 »
Quote
Quote
It's in the right place at the moment
It's not anywhere at the moment mate.
I mean, where you put it in your code snippet was the right place.

:(

Sorry, where would the Code Snippet fit into the framework that was posted above (1st post), I know the code snippet works, I've used it lots of times, my question is, where does it fit into RBZ's framework???? I don't get it.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2756
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Why wont GLU work with this framework?
« Reply #16 on: July 12, 2009 »
Sorry, I forgot to to add a glInit function, it should looks like this:

Code: [Select]
'---------------------------------------------------------------------------------
'
'   FreeBasic Window Framework by rbraz
'
'   12 Apr 2008
'
'---------------------------------------------------------------------------------


#include once "windows.bi"
#include once "gl/gl.bi"
#include once "gl/glext.bi"
#include once "crt.bi"


'DEVMODE bug, found the cure here --> http://www.freebasic.net/forum/viewtopic.php?t=5659&highlight=devmode
'Will be corrected by Victor in the next freebasic update
#undef DEVMODE
Type DEVMODE
    dmDeviceName As Zstring * CCHDEVICENAME
    dmSpecVersion As WORD
    dmDriverVersion As WORD
    dmSize As WORD
    dmDriverExtra As WORD
    dmFields As DWORD
    dmOrientation As Short
    dmPaperSize As Short
    dmPaperLength As Short
    dmPaperWidth As Short
    dmScale As Short
    dmCopies As Short
    dmDefaultSource As Short
    dmPrintQuality As Short
    dmColor As Short
    dmDuplex As Short
    dmYResolution As Short
    dmTTOption As Short
    dmCollate As Short
    dmFormName As Zstring * CCHFORMNAME
    dmLogPixels As WORD
    dmBitsPerPel As DWORD
    dmPelsWidth As DWORD
    dmPelsHeight As DWORD
    dmDisplayFlags As DWORD
    dmDisplayFrequency As DWORD
    dmICMMethod As DWORD
    dmICMIntent As DWORD
    dmMediaType As DWORD
    dmDitherType As DWORD
    dmReserved1 As DWORD
    dmReserved2 As DWORD
    dmPanningWidth As DWORD
    dmPanningHeight As DWORD
End Type



  'Win Main               
  Declare Function WinMain ( byval hInstance as HINSTANCE, _
                           byval hPrevInstance as HINSTANCE, _
                           szCmdLine as string, _
                           byval iCmdShow as integer ) as integer
    '
    ' Entry point   
    '
end WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )   
   
'app window procedure
  Declare Function WndProc( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT   
                           
  'window creation helper
  Declare Function MakeWindow(byval iWidth as integer, _
                              byval iHeight as integer, _
                              byval hInstance as HINSTANCE) as integer
                             
  Declare Sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)


  'OpenGL Draw sub
  Declare Sub glDraw()
 
  'OpenGL Init
  Declare Sub glInit()

  'Application name
  Const szAppName = "FreeBasic - OpenGL Framework"
 
  'app window handle
  Dim Shared hWndWin as HWND
 
  'app hdc
  Dim Shared hdcWin as HDC
 
  'Window resolution
  const XRES = 800
  const YRES = 600




' ----------------------------------
' name: WndProc
' desc: Processes windows messages
'
' ----------------------------------
function WndProc ( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT
         
    '
    ' Process messages
    '
    select case( message )   
'
' Key pressed
'
case WM_KEYDOWN
if( lobyte( wParam ) = 27 ) then
PostMessage( hWnd, WM_CLOSE, 0, 0 )
end if

        '
        ' Window was closed
        '
    case WM_DESTROY
            PostQuitMessage( 0 )
            exit function
           
    end select
   
    '
    ' Message doesn't concern us, send it to the default handler
    ' and get result
    '
    function = DefWindowProc( hWnd, message, wParam, lParam )   
   
end function

' ---------------------------------------
' name: WinMain
' desc: A win2 gui program entry point
'
' ---------------------------------------
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   szCmdLine as string, _
                   byval iCmdShow as integer ) as integer   
     
    dim wMsg as MSG
       
    ' get module instance
    hInstance = GetModuleHandle(NULL)
   
    'Create our skinned window   
    if MakeWindow(XRES,YRES,hInstance) = 0 then
        MessageBox(NULL, "Failed to create window", szAppName, MB_ICONERROR )
        return -1
    end if
   
    ShowWindow( hWndWin, iCmdShow )
    UpdateWindow( hWndWin )
       
    'Get window device context
    hdcWin = GetDC(hWndWin)
   
    'Initialize OGL window
    dim pfd as PIXELFORMATDESCRIPTOR
    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 = 32
    pfd.cDepthBits = 32
    SetPixelFormat ( hdcWin, ChoosePixelFormat ( hdcWin, @pfd) , @pfd )
    wglMakeCurrent ( hdcWin, wglCreateContext(hdcWin) )
   
    'Call opengl init
    glInit()
   
    '
    ' Process windows messages
    '
    while(wMsg.message <> WM_QUIT)
       
        if (PeekMessage (@wMsg,NULL,0,0,PM_REMOVE) <> 0) then
            DispatchMessage(@wMsg)
        else
            glDraw()
            SwapBuffers (hdcWin)
        end if
   
    wend
   
    ' Ends a process and all its threads.
    ExitProcess(0)

end function

'Change The Screen Resolution
sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)
dim dmScreenSettings as DEVMODE
ZeroMemory(@dmScreenSettings, sizeof (DEVMODE))
dmScreenSettings.dmSize = sizeof(DEVMODE)
dmScreenSettings.dmPelsWidth = wwidth
dmScreenSettings.dmPelsHeight = hheight
dmScreenSettings.dmBitsPerPel = bitsPerPixel
dmScreenSettings.dmFields = DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT

    if (ChangeDisplaySettings (cast(LPDEVMODE,@dmScreenSettings), CDS_FULLSCREEN) <> DISP_CHANGE_SUCCESSFUL) then
        MessageBox(NULL,"Change Display Error!", szAppName,MB_OK or MB_ICONEXCLAMATION)
end -1
end if
   
end sub
' ------------------------------------------------------------------------
' Window creation function.
' ------------------------------------------------------------------------
Function MakeWindow(byval iWidth as integer, _
                              byval iHeight as integer, _
                              byval hInstance as HINSTANCE) as integer
 
  function = 0
 
  ' our window class
  dim wndWc as WNDCLASS
 
  dim windowStyle as DWORD
  dim windowExtendedStyle as DWORD
  dim rc as RECT

  ' ---------------------------------------------------------
  ' fill window class members
  ' ---------------------------------------------------------
  wndWc.style         = CS_OWNDC
  wndWc.lpfnWndProc   = @WndProc
  wndWc.cbClsExtra    = 0
  wndWc.cbWndExtra    = 0
  wndWc.hInstance     = GetModuleHandle(NULL)
  wndWc.hIcon         = NULL
  wndWc.hCursor       = LoadCursor(0, IDC_ARROW)
  wndWc.hIcon         = LoadIcon(hInstance, IDI_APPLICATION) ' Load our normal icon
  wndWc.hbrBackground = GetStockObject(BLACK_BRUSH)
  wndWc.lpszMenuName  = NULL
  wndWc.lpszClassName = strptr( szAppName )

  ' register class
  if( RegisterClass( @wndWc ) = FALSE ) then
      MessageBox(NULL, "Failed to register Window Class!", szAppName, MB_ICONERROR )
      exit function
  end if
   
  rc.left   = 0
  rc.top    = 0
  rc.right  = XRES
  rc.bottom = YRES
 
  'Setup video mode window - Windowed / Fullscreen
  dim winmode as integer
  if( MessageBox(NULL,"Full Screen ?", "Screen Mode",MB_YESNO or MB_ICONQUESTION) = IDYES ) then
    winmode = 0
  else
    winmode = 1
  end if

  if (winmode = 0) then
    windowStyle = WS_POPUP or WS_VISIBLE
    windowExtendedStyle = WS_EX_TOPMOST
    ChangeScreenResolution (XRES, YRES, 32)
  else
    windowStyle =  WS_SYSMENU or WS_POPUP or WS_VISIBLE or WS_MINIMIZEBOX
    windowExtendedStyle = WS_EX_APPWINDOW
    'Center window
    rc.left = (GetSystemMetrics(SM_CXSCREEN) - XRES) / 2
    rc.top =  (GetSystemMetrics(SM_CYSCREEN) - YRES) / 2
  end if
 
  ' create the window
  hWndWin = CreateWindowEx(windowExtendedStyle,   _
wndWc.lpszClassName, _
                     szAppName,           _
windowStyle,         _
                             rc.left,             _
rc.top,              _
                             rc.right,            _
         rc.bottom,           _
                             HWND_DESKTOP, 0,     _
GetModuleHandle(NULL), 0)
 
  ShowCursor(false)
 
  ' return result
  if (hWndWin) then
      return 1
  else
      return 0
  end if

end function


sub glInit()
    glClearColor(0.2f, 0.3f, 0.5f, 1.0f)
end sub

sub glDraw()
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
end sub

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #17 on: July 12, 2009 »
Maybe I will finally make something now that won't crash on everyones machines!

Thanks Rbz :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #18 on: July 12, 2009 »
Seems to work fine now, thanks mate.

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #19 on: July 12, 2009 »
As a reference point for people who are in the same position I was before, here is a working basic routine .

Code: [Select]

#include once "gl/gl.bi"
#include once "gl/glu.bi"
#include once "gl/glext.bi"
#include once "crt.bi"
#include once "windows.bi"

'DEVMODE bug, found the cure here --> http://www.freebasic.net/forum/viewtopic.php?t=5659&highlight=devmode
'Will be corrected by Victor in the next freebasic update
#undef DEVMODE
Type DEVMODE
    dmDeviceName As Zstring * CCHDEVICENAME
    dmSpecVersion As WORD
    dmDriverVersion As WORD
    dmSize As WORD
    dmDriverExtra As WORD
    dmFields As DWORD
    dmOrientation As Short
    dmPaperSize As Short
    dmPaperLength As Short
    dmPaperWidth As Short
    dmScale As Short
    dmCopies As Short
    dmDefaultSource As Short
    dmPrintQuality As Short
    dmColor As Short
    dmDuplex As Short
    dmYResolution As Short
    dmTTOption As Short
    dmCollate As Short
    dmFormName As Zstring * CCHFORMNAME
    dmLogPixels As WORD
    dmBitsPerPel As DWORD
    dmPelsWidth As DWORD
    dmPelsHeight As DWORD
    dmDisplayFlags As DWORD
    dmDisplayFrequency As DWORD
    dmICMMethod As DWORD
    dmICMIntent As DWORD
    dmMediaType As DWORD
    dmDitherType As DWORD
    dmReserved1 As DWORD
    dmReserved2 As DWORD
    dmPanningWidth As DWORD
    dmPanningHeight As DWORD
End Type



  'Win Main               
  Declare Function WinMain ( byval hInstance as HINSTANCE, _
                           byval hPrevInstance as HINSTANCE, _
                           szCmdLine as string, _
                           byval iCmdShow as integer ) as integer
    '
    ' Entry point   
    '
end WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )   
   
'app window procedure
  Declare Function WndProc( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT   
                           
  'window creation helper
  Declare Function MakeWindow(byval iWidth as integer, _
                              byval iHeight as integer, _
                              byval hInstance as HINSTANCE) as integer
                             
  Declare Sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)


  'OpenGL Draw sub
  Declare Sub glDraw()
 
  'OpenGL Init
  Declare Sub glInit()

  'Application name
  Const szAppName = "FreeBasic - OpenGL Framework"
 
  'app window handle
  Dim Shared hWndWin as HWND
 
  'app hdc
  Dim Shared hdcWin as HDC
 
  'Window resolution
  const XRES = 800
  const YRES = 600

  dim SHARED LightAmbient(0 to 3) as single => {0.9, 0.9, 0.9,0.15}   '' Ambient Light is 20% white
  dim SHARED LightDiffuse(0 to 3) as single => {0.4, 0.4, 0.4,0.6}   '' Diffuse Light is white
  dim SHARED LightPosition(0 to 2) as single =>{0.0, 2.0,    10.   }  '' Position is somewhat in front of screen
  DIM SHARED Material(0 to 3) AS SINGLE => {0.5, 0.5, 0.8,0.8}  :' WHITE     
  DIM SHARED MaterialRED(0 to 3) AS SINGLE => {1.0, 0.0, 0.0,0.8}  :' WHITE     
  DIM SHARED MaterialBLU(0 to 3) AS SINGLE => {0.0, 0.0, 1.0,0.8}  :' WHITE     
  DIM SHARED AS INTEGER QOBJ
  DIM SHARED AS INTEGER LP

DECLARE SUB DRAWVERTBAR(BYVAL PSX AS DOUBLE, BYVAL PSY AS DOUBLE, BYVAL PSZ AS DOUBLE)
' ----------------------------------
' name: WndProc
' desc: Processes windows messages
'
' ----------------------------------
function WndProc ( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT
         
    '
    ' Process messages
    '
    select case( message )   
'
' Key pressed
'
case WM_KEYDOWN
if( lobyte( wParam ) = 27 ) then
PostMessage( hWnd, WM_CLOSE, 0, 0 )
end if

        '
        ' Window was closed
        '
    case WM_DESTROY
            PostQuitMessage( 0 )
            exit function
           
    end select
   
    '
    ' Message doesn't concern us, send it to the default handler
    ' and get result
    '
    function = DefWindowProc( hWnd, message, wParam, lParam )   
   
end function

' ---------------------------------------
' name: WinMain
' desc: A win2 gui program entry point
'
' ---------------------------------------
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   szCmdLine as string, _
                   byval iCmdShow as integer ) as integer   
     
    dim wMsg as MSG
       
    ' get module instance
    hInstance = GetModuleHandle(NULL)
   
    'Create our skinned window   
    if MakeWindow(XRES,YRES,hInstance) = 0 then
        MessageBox(NULL, "Failed to create window", szAppName, MB_ICONERROR )
        return -1
    end if
   
    ShowWindow( hWndWin, iCmdShow )
    UpdateWindow( hWndWin )
       
    'Get window device context
    hdcWin = GetDC(hWndWin)
   
    'Initialize OGL window
    dim pfd as PIXELFORMATDESCRIPTOR
    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 = 32
    pfd.cDepthBits = 32
    SetPixelFormat ( hdcWin, ChoosePixelFormat ( hdcWin, @pfd) , @pfd )
    wglMakeCurrent ( hdcWin, wglCreateContext(hdcWin) )
   
    'Call opengl init
    glInit()
   
    '
    ' Process windows messages
    '
    while(wMsg.message <> WM_QUIT)
       
        if (PeekMessage (@wMsg,NULL,0,0,PM_REMOVE) <> 0) then
            DispatchMessage(@wMsg)
        else
            glDraw()
            SwapBuffers (hdcWin)
        end if
   
    wend
   
    ' Ends a process and all its threads.
    ExitProcess(0)

end function

'Change The Screen Resolution
sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)
dim dmScreenSettings as DEVMODE
ZeroMemory(@dmScreenSettings, sizeof (DEVMODE))
dmScreenSettings.dmSize = sizeof(DEVMODE)
dmScreenSettings.dmPelsWidth = wwidth
dmScreenSettings.dmPelsHeight = hheight
dmScreenSettings.dmBitsPerPel = bitsPerPixel
dmScreenSettings.dmFields = DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT

    if (ChangeDisplaySettings (cast(LPDEVMODE,@dmScreenSettings), CDS_FULLSCREEN) <> DISP_CHANGE_SUCCESSFUL) then
        MessageBox(NULL,"Change Display Error!", szAppName,MB_OK or MB_ICONEXCLAMATION)
end -1
end if
   
end sub
' ------------------------------------------------------------------------
' Window creation function.
' ------------------------------------------------------------------------
Function MakeWindow(byval iWidth as integer, _
                              byval iHeight as integer, _
                              byval hInstance as HINSTANCE) as integer
 
  function = 0
 
  ' our window class
  dim wndWc as WNDCLASS
 
  dim windowStyle as DWORD
  dim windowExtendedStyle as DWORD
  dim rc as RECT

  ' ---------------------------------------------------------
  ' fill window class members
  ' ---------------------------------------------------------
  wndWc.style         = CS_OWNDC
  wndWc.lpfnWndProc   = @WndProc
  wndWc.cbClsExtra    = 0
  wndWc.cbWndExtra    = 0
  wndWc.hInstance     = GetModuleHandle(NULL)
  wndWc.hIcon         = NULL
  wndWc.hCursor       = LoadCursor(0, IDC_ARROW)
  wndWc.hIcon         = LoadIcon(hInstance, IDI_APPLICATION) ' Load our normal icon
  wndWc.hbrBackground = GetStockObject(BLACK_BRUSH)
  wndWc.lpszMenuName  = NULL
  wndWc.lpszClassName = strptr( szAppName )

  ' register class
  if( RegisterClass( @wndWc ) = FALSE ) then
      MessageBox(NULL, "Failed to register Window Class!", szAppName, MB_ICONERROR )
      exit function
  end if
   
  rc.left   = 0
  rc.top    = 0
  rc.right  = XRES
  rc.bottom = YRES
 
  'Setup video mode window - Windowed / Fullscreen
  dim winmode as integer
  if( MessageBox(NULL,"Full Screen ?", "Screen Mode",MB_YESNO or MB_ICONQUESTION) = IDYES ) then
    winmode = 0
  else
    winmode = 1
  end if

  if (winmode = 0) then
    windowStyle = WS_POPUP or WS_VISIBLE
    windowExtendedStyle = WS_EX_TOPMOST
    ChangeScreenResolution (XRES, YRES, 32)
  else
    windowStyle =  WS_SYSMENU or WS_POPUP or WS_VISIBLE or WS_MINIMIZEBOX
    windowExtendedStyle = WS_EX_APPWINDOW
    'Center window
    rc.left = (GetSystemMetrics(SM_CXSCREEN) - XRES) / 2
    rc.top =  (GetSystemMetrics(SM_CYSCREEN) - YRES) / 2
  end if
 
  ' create the window
  hWndWin = CreateWindowEx(windowExtendedStyle,   _
wndWc.lpszClassName, _
                     szAppName,           _
windowStyle,         _
                             rc.left,             _
rc.top,              _
                             rc.right,            _
         rc.bottom,           _
                             HWND_DESKTOP, 0,     _
GetModuleHandle(NULL), 0)
 
  ShowCursor(false)
 
  ' return result
  if (hWndWin) then
      return 1
  else
      return 0
  end if

end function


sub glInit()

    QOBJ=GLUNEWQUADRIC
    GLUQUADRICNORMALS  (QOBJ,GLU_SMOOTH)
   
    GLVIEWPORT 0, 50, XRES, YRES-80                          '' Reset The Current Viewport
GLMATRIXMODE GL_PROJECTION                          '' Select The Projection Matrix
GLLOADIDENTITY
   
    GLUPERSPECTIVE 45.0, XRES / YRES, .1, 100.0       '' Calculate The Aspect Ratio Of The Window
   
GLMATRIXMODE GL_MODELVIEW                           '' Select The Modelview Matrix
GLLOADIDENTITY                                      '' Reset The Modelview Matrix

GLSHADEMODEL GL_SMOOTH                              '' Enable Smooth Shading
GLCLEARCOLOR 0.0, 0.00, 0.00,0.0
GLCLEARDEPTH 1.0                                    '' Depth Buffer Setup
GLENABLE GL_DEPTH_TEST                              '' Enables Depth Testing
GLDEPTHFUNC GL_LEQUAL                               '' The Type Of Depth Testing To Do
GLHINT GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations


    GLENABLE (GL_CULL_FACE)
    GLCULLFACE(GL_BACK)
   
    GLENABLE GL_BLEND
    GLBLENDFUNC(GL_ONE_MINUS_CONSTANT_ALPHA,GL_SRC_ALPHA)   


    GLLINEWIDTH 1
    GLENABLE GL_SMOOTH

    glLightfv( GL_LIGHT1, GL_AMBIENT, @LightAmbient(0))        '' Load Light-Parameters Into GL_LIGHT1
glLightfv( GL_LIGHT1, GL_DIFFUSE, @LightDiffuse(0))

    glLightfv( GL_LIGHT1, GL_POSITION, @LightPosition(0))
    glEnable(GL_LIGHTING)
glEnable(GL_LIGHT1)

   
    GLFOGF(GL_FOG_DENSITY,0.5)
    GLFOGF(GL_FOG_START,    1.0)
    GLFOGF(GL_FOG_END,     40.0)
    GLENABLE (GL_FOG)
    GLLINEWIDTH 1
    GLLOADIDENTITY       


   
end sub

sub glDraw()
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)

    GLLOADIDENTITY
    GLDISABLE GL_BLEND
    FOR LP=-50 TO -3 STEP 1
        DRAWVERTBAR((LP*.05)*SIN(((TIMER*20)+LP)*.1)+(LP*.01)*COS(((TIMER*50)+LP)*.2),-.15,LP/5)
    NEXT
   
    GLDISABLE GL_DEPTHTEST   
        GLBEGIN GL_LINES
        GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@MaterialRED(0))
        GLVERTEX3F -.3,-.2065,-.5
        GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@MaterialBLU(0))
        GLVERTEX3F .3,-.2065,-.5

        GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@MaterialBLU(0))
        GLVERTEX3F -.3,.165,-.5
        GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@MaterialRED(0))       
        GLVERTEX3F .3,.165,-.5

        GLEND   
    GLENABLE GL_DEPTHTEST
    GLENABLE GL_BLEND
   
end sub



SUB DRAWVERTBAR(BYVAL PSX AS DOUBLE, BYVAL PSY AS DOUBLE, BYVAL PSZ AS DOUBLE)
GLPUSHMATRIX
    GLLOADIDENTITY
    glTranslatef PSX, PSY, PSZ
    glRotatef(  90, 1.0, 0.0, 0.0)
    glRotatef(  0,  0.0, 1.0, 0.0)
    glRotatef(  TIMER*100,  0.0, 0.0, 1.0)     
   
    GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@Material(0))
    GLUCYLINDER(QOBJ,.1,.1,10,20,1)   
    glRotatef(  0, 1.0, 0.0, 0.0)
    glRotatef(  180,  0.0, 1.0, 0.0)
    glRotatef(  0,  0.0, 0.0, 1.0)     
   
    GLUDISK(QOBJ,0,.1,20,1)   
GLPOPMATRIX
END SUB


Shockwave ^ Codigos
Challenge Trophies Won: