#ifndef __INITGL_BI__
#define __INITGL_BI__

#include once "fbgfx.bi"
#include once "GL/gl.bi"

enum KEYCODES
  K_NONE            =   0 
  Ctrl_A            =   1 
  Ctrl_B            =   2 
  Ctrl_C            =   3 
  Ctrl_D            =   4 
  Ctrl_E            =   5 
  Ctrl_F            =   6 
  K_BELL            =   7 
  K_BACKSPACE       =   8 
  K_TAB             =   9 
  Ctrl_J            =  10 
  Ctrl_K            =  11 
  Ctrl_L            =  12 
  K_ENTER           =  13 
  Ctrl_N            =  14 
  Ctrl_O            =  15 
  Ctrl_P            =  16 
  Ctrl_Q            =  17 
  Ctrl_R            =  18 
  Ctrl_S            =  19 
  Ctrl_T            =  20 
  Ctrl_U            =  21 
  Ctrl_V            =  22 
  Ctrl_W            =  23 
  Ctrl_X            =  24 
  Ctrl_Y            =  25 
  Ctrl_Z            =  26 
  K_ESCAPE          =  27 
  Ctrl_5            =  29 
  Ctrl_6            =  30 
  Ctrl_7            =  31 
  K_SPACE           =  32
  K_exclamationmark =  33 ' ! 
  K_quote           =  34
  K_hash            =  35 ' # 
  K_dollar          =  36 ' $ 
  K_percent         =  37 ' % 
  K_ampersand       =  38 ' & 
  K_singlequote     =  39 ' ' 
  K_leftbracket     =  40 ' ( 
  K_rightbracket    =  41 ' ) 
  K_multiply        =  42 ' * 
  K_plus            =  43 ' + 
  K_comma           =  44 ' , 
  K_minus           =  45 ' - 
  K_period          =  46 ' . 
  K_slash           =  47 ' / 

  K_0               =  48 
  K_1               =  49 
  K_2               =  50 
  K_3               =  51 
  K_4               =  52 
  K_5               =  53 
  K_6               =  54 
  K_7               =  55 
  K_8               =  56 
  K_9               =  57 

  K_COLON           =  58 ' : 
  K_SEMICOLON       =  59 ' ; 
  K_LESS            =  60 ' < 
  K_EQUALS          =  61 ' = 
  K_GREATER         =  62 ' > 
  Shift_QUESTIONMARK=  63 ' ? 
  AltGr_Q           =  64 ' @ 

  Shift_A           =  65 
  Shift_B           =  66 
  Shift_C           =  67 
  Shift_D           =  68 
  Shift_E           =  69 
  Shift_F           =  70 
  Shift_G           =  71 
  Shift_H           =  72 
  Shift_I           =  73 
  Shift_J           =  74 
  Shift_K           =  75 
  Shift_L           =  76 
  Shift_M           =  77 
  Shift_N           =  78 
  Shift_O           =  79 
  Shift_P           =  80 
  Shift_Q           =  81 
  Shift_R           =  82 
  Shift_S           =  83 
  Shift_T           =  84 
  Shift_U           =  85 
  Shift_V           =  86 
  Shift_W           =  87 
  Shift_X           =  88 
  Shift_Y           =  89 
  Shift_Z           =  90 

  K_L_SQUARE_BRACKET=  91 ' [ 
  K_Backslash       =  92 ' \ 
  K_R_SQUARE_BRACKET=  93 ' ] 
  K_CARET           =  94 ' ^ 
  K_UNDERSCORE      =  95 ' _ 

  K_A               =  97 
  K_B               =  98 
  K_C               =  99 
  K_D               = 100 
  K_E               = 101 
  K_F               = 102 
  K_G               = 103 
  K_H               = 104 
  K_I               = 105 
  K_J               = 106 
  K_K               = 107 
  K_L               = 108 
  K_M               = 109 
  K_N               = 110 
  K_O               = 111 
  K_P               = 112 
  K_Q               = 113 
  K_R               = 114 
  K_S               = 115 
  K_T               = 116 
  K_U               = 117 
  K_V               = 118 
  K_W               = 119 
  K_X               = 120 
  K_Y               = 121 
  K_Z               = 122 
  K_L_CURLY_BRACKET = 123 ' { 
  K_R_CURLY_BRACKET = 125 ' } 
  K_TILDE           = 126 ' ~ 
  AltGr_C           = 162 ' ¢ 
  AltGr_E           = 164 ' (€) 
  AltGr_Y           = 171 ' « 
  AltGr_6           = 172 
  AltGr_2           = 178 ' ² 
  AltGr_3           = 179 ' ³ 
  AltGr_M           = 181 ' µ 
  AltGr_R           = 182 
  AltGr_dot         = 183 ' · 
  AltGr_1           = 185 ' ¹ 
  AltGr_X           = 187 ' » 
  Shift_AE          = 196 ' Ä 
  Shift_OE          = 214 ' Ö 
  Shift_UE          = 220 ' Ü 
  K_SS              = 223 ' ß 
  K_AE              = 228 ' ä 
  AltGr_A           = 230 ' æ 

  AltGr_D           = 240 ' ð 
  K_OE              = 246 ' ö 
  AltGr_O           = 248 ' ø 
  K_UE              = 252 ' ü 
  AltGr_P           = 254 ' þ 

  K_F1              = 315
  K_F2              = 316
  K_F3              = 317
  K_F4              = 318
  K_F5              = 319
  K_F6              = 320
  K_F7              = 321
  K_F8              = 322
  K_F9              = 323
  K_F10             = 324

  K_HOME            = 327
  K_UP              = 328
  K_PAGEUP          = 329

  K_LEFT            = 331

  K_RIGHT           = 333

  K_END             = 335
  K_DOWN            = 336
  K_PAGEDOWN        = 337
  K_INSERT          = 338
  K_DELETE          = 339

  K_WINDOW_CLOSE    = 363
end enum

function GetKeyCode() As KEYCODES
  dim as string  key=inkey() 
  dim as integer keycode=len(key)
  If keycode Then
    keycode-=1
    keycode=key[keycode]+(keycode shl 8)
  End If
  Return keycode
end function

' glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
' glEnable(GL_BLEND)

' same as gluPerspective
sub Perspective(fov as double, ratio as double, zNear as double, zFar as double)
  dim as double fH = tan( fov / 360.0 * 3.1415926535897932384626433832795 ) * zNear
  dim as double fW = fH * ratio
  glFrustum(-fW,fW,-fH,fH,zNear,zFar)
end sub
' same as gluLockAt
sub LookAt(ex as double, ey as double, ez as double, _
           lx as double, ly as double, lz as double, _
           ux as double, uy as double, uz as double )
  dim as double z(2) = {ex-lx,ey-ly,ez-lz}
  dim as double l=z(0)*z(0) + z(1)*z(1) + z(2)*z(2)
  if (l) then
    l=1.0/sqr(l)
    z(0)*=l : z(1)*=l : z(2)*=l
  end if
  dim as double x(2)={uy*z(2)-uz*z(1), _
                     -ux*z(2)+uz*z(0), _
                      ux*z(1)-uy*z(0)}
  l=x(0)*x(0) + x(1)*x(1) + x(2)*x(2)
  if (l) then
    l=1.0/sqr(l)
    x(0)*=l : x(1)*=l : x(2)*=l
  end if
  dim as double y(2)={z(1)*x(2)-z(2)*x(1), _
                     -z(0)*x(2)+z(2)*x(0), _
                      z(0)*x(1)-z(1)*x(0)}

  l=y(0)*y(0) + y(1)*y(1) + y(2)*y(2)
  if (l) then
    l=1.0/sqr(l)
    y(0)*=l : y(1)*=l : y(2)*=l
  end if
  dim as double m4x4(15)=>{x(0),y(0),z(0),0, _
                           x(1),y(1),z(1),0, _
                           x(2),y(2),z(2),0, _
                             0,    0,   0,1}
  glMultMatrixd @m4x4(0)
  glTranslated -ex,-ey,-ez
end sub

sub InitGL(w as integer=640,h as integer=480,b as integer=24,f as integer=0)
  dim as single lp(3) = { 0.0, 20, 0.0, 1.0 }
  dim as single la(3) = { 0.2, 0.2, 0.2, 1.0 }
  dim as single ld(3) = { 1.0, 1.0, 1.0, 1.0 }
  dim as single ls(3) = { 1.0, 1.0, 1.0, 1.0 }
  dim as integer sw,sh,sb
  ScreenInfo sw,sh,sb
  if f=0 then
    b=sb
    if w<32 then w=32
    if h<32 then h=32
    if w>sw then w=sw
    if h>sh then h=sh
  else
    if w<640 or h<480 then
      w=640:h=480
    end if
  end if
  
  ScreenRes w,h,b,,FB.GFX_OPENGL or iif(f,FB.GFX_FULLSCREEN,0)
  if ScreenPtr()=0 then
    ScreenRes 640,480,sb,,FB.GFX_OPENGL
  end if
  glViewport 0, 0, w, h
  glMatrixMode GL_PROJECTION
  glLoadIdentity
  Perspective 45.0, w/h, 1, 4096
  glMatrixMode GL_MODELVIEW
  glLoadIdentity

  glShadeModel GL_SMOOTH
  glClearColor .4, .4, .8, 1.0
  glClearDepth 1.0
  glEnable GL_DEPTH_TEST
  glDepthFunc GL_LEQUAL
  glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
  
  glCullFace(GL_BACK)
  glFrontFace(GL_CCW)
  glEnable(GL_CULL_FACE)
  
  glLightfv(GL_LIGHT0, GL_POSITION, @lp(0) )
  glLightfv(GL_LIGHT0, GL_AMBIENT , @la(0) )
  glLightfv(GL_LIGHT0, GL_DIFFUSE , @ld(0) )
  glLightfv(GL_LIGHT0, GL_SPECULAR, @ls(0) )
  glEnable(GL_LIGHT0)
  glEnable(GL_LIGHTING)
  
  glEnable(GL_COLOR_MATERIAL)  
  
end sub

#endif ' __INITGL_BI__
