DECLARE SUB CalcMD2Normals
DECLARE SUB FormCreate
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
DIM MD2 AS QMD2model 'defined in QGLobject
' Use this to Render by timer (slower but with better event handling)
' create tmx as QTimer
' interval = 1
' enabled = 0
' onTimer = Render
' end create
' working vars
DIM QuitIt AS INTEGER
DIM xang AS SINGLE
DIM yang AS SINGLE
DIM animSpeed AS SINGLE
DIM zoom AS SINGLE
DIM anim$ AS STRING
DIM TheFrameNum AS INTEGER 'each model has a position frame
DIM TheFrameNum2 AS INTEGER 'the next position frame
DIM TheSkin AS INTEGER 'texture number
DIM TheFrameFactor AS SINGLE 'linear interpolate between frames
'-- user interface here
create Form as QFORM
caption = "MD2 example"
center
width = 256
height = 256
onpaint = Render
onmousemove = Form_MouseMove
onshow = FormCreate
onkeydown = FormKeyDown
onresize = FormSize
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 - 15.0
CASE 39 'VK_RIGHT
yang = yang + 15.0
CASE 38 'VK_UP
xang = xang - 15.0
CASE 40 'VK_DOWN
xang = xang + 15.0
CASE 34
zoom = zoom - 15.0
CASE 33'pg dwn
zoom = zoom + 15.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 + 15.0
IF X < Form.ClientWidth\2 THEN xang = xang - 15.0
IF Y > Form.ClientHeight\2 THEN yang = yang + 15.0
IF X < Form.ClientHeight\2 THEN yang = yang - 15.0
END SUB
SUB FormCreate
GL.Init(Form)
MD2.LoadMD2File("")', TheModel)
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
theFrameNum = 0
theFrameNum2 = 0
theFrameFactor = 0
theSkin = 0
animSpeed = 0.5 '0.33
zoom = 10
'tmx.Enabled = True 'use this for start rendering with timer
END SUB
SUB Render ' Draw model
DO 'take this out if using Timer !
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity
glScalef(0.12, 0.12, 0.12)
glTranslatef (0, 0, -zoom)
glRotatef (yang, 0, 1, 0)
glRotatef (xang, 1, 0, 0)
MD2.DrawMD2 (theFrameNum, theFrameNum2, theFrameFactor, theSkin)
GL.Flip
Instructions
' Animate all frames until the end, then start over
theFrameFactor = theFrameFactor + animSpeed
if theFrameFactor >= 1.0 then
theFrameFactor = theFrameFactor - 1.0
theFrameNum = theFrameNum2
theFrameNum2++
end if
if theFrameNum2 >= MD2.MaxFrame THEN
theFrameNum2 = 1
theFrameNum = 0
end if
DOEVENTS 'take this out if using Timer!
LOOP UNTIL QuitIt 'take this out if using Timer!
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$(TheFrameNum), &HFFFFFF, -1)
Form.TextOut( 8, 48, MD2.Frame_Name(theFrameNum), &HFFFFFF, -1)
END SUB