DECLARE SUB CalcMD2Normals
DECLARE SUB FormCreate
DECLARE SUB FormClick
DECLARE SUB FormKeyDown (Key AS Word, KeyShift AS INTEGER)
DECLARE SUB FormSize
DECLARE SUB Form_MouseMove (X as integer, Y as integer, Shift as integer)
DECLARE SUB Instructions
DECLARE SUB Render

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'Sample  code to view and rotate MD2 (Quake level 2) 3D models 
'   Code adapted from many sources,  by Tom Milgrew, FreeBasic community 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

$TYPECHECK ON
$INCLUDE <RapidQ2.inc>          'need this for textures 
$INCLUDE <GL\gl.inc>
$INCLUDE <GL\glu.inc>
$INCLUDE <GL\QGL.inc>
DIM GL          AS QGL              'open GL wrapped up 
DIM MD2         AS QMD2model        'defined in QGLobject 

' working vars 
DIM QuitIt      AS INTEGER
DIM xang        AS SINGLE
DIM yang        AS SINGLE
DIM zoom        AS SINGLE


'-- user interface here 
create Form as QFORMEx
  caption       = "MD2 example"
  center
  width         = 256
  height        = 256
  onpaint       = Render
  onmousemove   = Form_MouseMove
  onshow        = FormCreate
  onkeydown     = FormKeyDown
  onresize      = FormSize
  onClick       = FormClick
end create
Form.showmodal


SUB FormKeyDown (Key AS Word, KeyShift AS INTEGER) 
  if Key = 27 then GL.Close: QuitIt = True: Form.close: EXIT SUB
    SELECT CASE Key
        CASE 37 'VK_LEFT 
            yang = yang - 5.0 
        CASE 39 'VK_RIGHT 
            yang = yang + 5.0
        CASE 38 'VK_UP 
            xang = xang - 5.0
        CASE 40 'VK_DOWN 
            xang = xang + 5.0
        CASE 34
            zoom = zoom - 5.0
        CASE 33'pg dwn 
            zoom = zoom + 5.0
    END SELECT
END SUB



SUB FormSize
  GL.Resize(form.clientWidth, form.clientHeight)
END SUB

SUB Form_MouseMove (X as integer, Y as integer, Shift as integer) 
    IF X > Form.ClientWidth\2 THEN xang = xang + 5.0
    IF X < Form.ClientWidth\2 THEN xang = xang - 5.0
    IF Y > Form.ClientHeight\2 THEN yang = yang + 5.0
    IF X < Form.ClientHeight\2 THEN yang = yang - 5.0
END SUB



SUB FormClick
    QuitIt = True           'turn off rendering 
    MD2.Clear               'clear out vars & arrays 
    FormCreate              'just reload it all 
    GL.Resize(form.clientWidth, form.clientHeight)      'for resetting projection 
    QuitIt = False          'turn on rendering 
END SUB

SUB FormCreate
    GL.Init(Form)
    MD2.Clear
    MD2.LoadMD2File("")                  'no file name signals open dialog 
	glEnable GL_TEXTURE_2D               '' Enable Texture Mapping ( NEW ) 
	glClearColor 0.3, 0.5, 0.2, 1.0      '' grey Background 
 	glClearDepth 1.0                     '' Depth Buffer Setup 
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)
    glEnable (GL_CULL_FACE)
    glCullFace (GL_FRONT)

    yang            = 30
    zoom            = 10
    MD2.SetScale(0.18, 0.18, 0.18)      'set entire scale before drawing 
    ' Animate all frames until the end, then start over, last arg is step increment res. 
    MD2.CreateAnimationSet(MD2.KeyFrame(1), MD2.KeyFrame(2)-1, 0.15)
    MD2.hDC = GL.hDC                    ' tell the model where to draw for fast auto drawing 
    MD2.SetScale(1.0, 1.0, 1.0)         'reset scale, don't need it anymore 
END SUB


SUB Render          ' Draw model 
    MD2.FPS = 30    'set update speed 
    DO              'take this out if using QTimer ! 
        glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
        glLoadIdentity 
        MD2.SetPosition (0, 0, -zoom)
        MD2.SetRotation (xang, yang, 0)
        MD2.Animate                         'draws next frame of animation 
'        SwapBuffers GL.hDC                  ' flip the screen or use this:    GL.Flip 
        Instructions
        DOEVENTS            'take this out if using QTimer! 
    LOOP UNTIL QuitIt   'take this out if using QTimer! 
END SUB


SUB Instructions
     Form.TextOut( 8, 6, "Arrow keys rotate", &HFFFFFF, -1)
     Form.TextOut( 8, 20, "PgUp/PgDn zooms", &HFFFFFF, -1)
     Form.TextOut( 8, 34, "Frame " + STR$(MD2.CurrentFrame), &HFFFFFF, -1)
END SUB