Skip to content

Instantly share code, notes, and snippets.

@Yonaba
Created June 29, 2011 14:19
Show Gist options
  • Save Yonaba/1053923 to your computer and use it in GitHub Desktop.
Save Yonaba/1053923 to your computer and use it in GitHub Desktop.
Draw MD2 Model using irrLua
-- Quick Sheet on How to load and draw MD2 model using IrrLua
-- IrrLua can be found at http://irrlua.sourceforge.net/
require "irr"
local MyEventReceiver = {}
function MyEventReceiver:OnEvent(Event)
if Event.EventType == irr.EET_KEY_INPUT_EVENT then
return self:OnKeyEvent(Event.KeyInput.Key, Event.KeyInput.PressedDown, Event.KeyInput.Control, Event.KeyInput.Shift)
end
return false
end
function MyEventReceiver:OnKeyEvent(Key, PressedDown, Shift, Control)
if node then
local v = node:getPosition()
if Key == irr.KEY_KEY_Z then
v.Y = v.Y + 2
node:setPosition(v)
return true
end
if Key == irr.KEY_KEY_S then
v.Y = v.Y - 2
node:setPosition(v)
return true
end
if Key == irr.KEY_KEY_Q then
v.X = v.X - 2
node:setPosition(v)
return true
end
if Key == irr.KEY_KEY_D then
v.X = v.X + 2
node:setPosition(v)
return true
end
if Key == irr.KEY_KEY_W then
v.Z = v.Z - 2
node:setPosition(v)
return true
end
if Key == irr.KEY_KEY_X then
v.X = v.X + 2
node:setPosition(v)
return true
end
return false
end
end
function drawModel(windowWidth, windowHeight,windowCaption,modelname,frameLoopMax)
local receiver = irr.createIEventReceiver(MyEventReceiver)
--We Inits the Video Device, and set the size of the window
local IrrDevice = irr.createDevice(irr.video.EDT_DIRECT3D9, irr.core.dimension2d(windowWidth,windowHeight), 16, false, false, false,receiver)
--We set the window Caption
IrrDevice:setWindowCaption(windowCaption)
--We get the Driver and the Scene Manager Objects
local Driver = IrrDevice:getVideoDriver()
local SceManager = IrrDevice:getSceneManager()
--We add a model in the scene manager, and its texture too.
local current_Md2 = SceManager:getMesh(modelname..'.md2')
node = SceManager:addAnimatedMeshSceneNode( current_Md2 )
node:setMaterialFlag(irr.video.EMF_LIGHTING , false)
node:setFrameLoop(0, frameLoopMax)
node:setMaterialTexture( 0, Driver:getTexture(modelname..'.jpg'))
--We set first the camera position and the camera lookAt point of view coordinates
SceManager:addCameraSceneNode(nil, irr.core.vector3d(20,50,50), irr.core.vector3d(0,0,0), 0 )
--We draw all the stuff set before in a loop
while IrrDevice:run() do
Driver:beginScene(true, true, irr.video.SColor(255,100,101,140))
SceManager:drawAll()
Driver:endScene()
end
--We destroys the video device when out of the main loop
IrrDevice:drop()
end
-- Example: Assuming we have 2 files :
-- 'dragon.md2' (The MD2 Model) and 'dragon.jpg' (Its Texture)
drawModel(480,272,'Md2 Model Viewer - Roland Yonaba(SeanPaul223)','dragon',310)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment