LuckyScript封装库sample:改写ogre例子
续 http://www.cppblog.com/zzxhang/archive/2009/04/18/80269.html,为了展示这个封装库的能力,我把ogre的一些核心类跟接口注册给脚本,用luckyScript改写了OGRE的sample中cameraTrack跟terrain的例子,实现了使用luckyScript来控制OGRE的启动和更新,这个demo会作为LuckyScript的一个sample发布。
terrain例子的LuckyScript脚本代码
var
g_Root;
var g_SceneManager;
var g_Camera;
var g_ViewPort;
var g_Window;
var g_RaySceneQuery;
var g_UpdateRay;
var g_NegativeUnitY;
var g_FrameListener;
var g_Str1;
var g_Str2;
var g_CameraPos;
func updateScene( var timeDiv)
{
g_CameraPos = g_Camera.getPosition();
g_UpdateRay.setOrigin(g_CameraPos);
g_UpdateRay.setDirection(g_NegativeUnitY);
g_RaySceneQuery.setRay(g_UpdateRay);
var y = getRayIntersectionHeight(g_RaySceneQuery);
g_CameraPos.y = y;
g_Camera.setPosition(g_CameraPos);
}
func createRenderWindow()
{
g_Str1 = "Ogre Render Window";
g_Str2 = "";
g_Window = g_Root.initialise(1,g_Str1,g_Str2);
}
func choseSceneManager()
{
g_Str1 = "TerrainSceneManager";
g_Str2 = "Terrain";
g_SceneManager = g_Root.createSceneManagerByStrType(g_Str1,g_Str2);
}
func createCamera()
{
g_Str1 = "PlayerCam";
g_Camera = g_SceneManager.createCamera(g_Str1);
var cameraPos = new Vector3(0.0,0.0,500.0);
var lookAtPos = new Vector3(0.0,0.0,-300.0);
g_Camera.setPosition(cameraPos);
g_Camera.lookAt(lookAtPos);
g_Camera.setNearClipDistance(5.0);
g_ViewPort = g_Window.addViewport(g_Camera,0,0.0,0.0,1.0,1.0);
var colorVal = new ColourValue(0.0,0.0,0.0,1.0);
g_ViewPort.setBackgroundColour(colorVal);
var aspect = g_ViewPort.getActualWidth() / g_ViewPort.getActualHeight();
g_Camera.setAspectRatio(aspect);
}
func initResourceGroup()
{
var resourceGroupManager = getResourceGroupManager();
resourceGroupManager.initialiseAllResourceGroups();
}
func createScene()
{
var waterPlane = new Plane();
// Set ambient light
var ambientColor = new ColourValue(0.5, 0.5, 0.5,1.0);
g_SceneManager.setAmbientLight(ambientColor);
// Create a light
g_Str1 = "MainLight";
var l = g_SceneManager.createLight(g_Str1);
l.setPosition(20,80,50);
var fadeColour = new ColourValue(0.93, 0.86, 0.76,1.0);
setSceneFog(g_SceneManager,3,fadeColour,0.001,500,1000);
var viewPort = g_Window.getViewport(0);
viewPort.setBackgroundColour(fadeColour);
g_Str1 = "terrain.cfg";
g_SceneManager.setWorldGeometry(g_Str1);
g_Camera.setFarClipDistance(0);
// Define the required skyplane
var plane = new Plane();
// 5000 world units from the camera
plane.d = 5000;
var unitDown = new Vector3(0,-1,0);
// Above the camera, facing down
plane.normal = unitDown;
var q = new Quaternion(-0.3486, 0.0122, 0.9365, 0.0329);
var newPos = new Vector3(707,2500,528);
g_Camera.setPosition(newPos);
g_Camera.setOrientation(q);
var cameraPos = g_Camera.getPosition();
var ray = new Ray(cameraPos,unitDown);
g_RaySceneQuery = g_SceneManager.createRayQuery(ray,-1);
}
func createFrameListener()
{
g_FrameListener = new FrameListener(g_Window,g_Camera);
g_Root.addFrameListener(g_FrameListener);
}
func Main()
{
g_Root = new Root("plugins_d.cfg","ogre.cfg", "Ogre.log");
setupResources();
if(g_Root.showConfigDialog())
{
g_Str1 = new String("");
g_Str2 = new String("");
var zero = new Vector3(0,0,0);
g_NegativeUnitY = new Vector3(0,-1,0);
g_UpdateRay = new Ray(zero,g_NegativeUnitY);
createRenderWindow();
choseSceneManager();
createCamera();
initResourceGroup();
createScene();
createFrameListener();
updateScene(0);
g_Root.startRendering();
}
}
var g_SceneManager;
var g_Camera;
var g_ViewPort;
var g_Window;
var g_RaySceneQuery;
var g_UpdateRay;
var g_NegativeUnitY;
var g_FrameListener;
var g_Str1;
var g_Str2;
var g_CameraPos;
func updateScene( var timeDiv)
{
g_CameraPos = g_Camera.getPosition();
g_UpdateRay.setOrigin(g_CameraPos);
g_UpdateRay.setDirection(g_NegativeUnitY);
g_RaySceneQuery.setRay(g_UpdateRay);
var y = getRayIntersectionHeight(g_RaySceneQuery);
g_CameraPos.y = y;
g_Camera.setPosition(g_CameraPos);
}
func createRenderWindow()
{
g_Str1 = "Ogre Render Window";
g_Str2 = "";
g_Window = g_Root.initialise(1,g_Str1,g_Str2);
}
func choseSceneManager()
{
g_Str1 = "TerrainSceneManager";
g_Str2 = "Terrain";
g_SceneManager = g_Root.createSceneManagerByStrType(g_Str1,g_Str2);
}
func createCamera()
{
g_Str1 = "PlayerCam";
g_Camera = g_SceneManager.createCamera(g_Str1);
var cameraPos = new Vector3(0.0,0.0,500.0);
var lookAtPos = new Vector3(0.0,0.0,-300.0);
g_Camera.setPosition(cameraPos);
g_Camera.lookAt(lookAtPos);
g_Camera.setNearClipDistance(5.0);
g_ViewPort = g_Window.addViewport(g_Camera,0,0.0,0.0,1.0,1.0);
var colorVal = new ColourValue(0.0,0.0,0.0,1.0);
g_ViewPort.setBackgroundColour(colorVal);
var aspect = g_ViewPort.getActualWidth() / g_ViewPort.getActualHeight();
g_Camera.setAspectRatio(aspect);
}
func initResourceGroup()
{
var resourceGroupManager = getResourceGroupManager();
resourceGroupManager.initialiseAllResourceGroups();
}
func createScene()
{
var waterPlane = new Plane();
// Set ambient light
var ambientColor = new ColourValue(0.5, 0.5, 0.5,1.0);
g_SceneManager.setAmbientLight(ambientColor);
// Create a light
g_Str1 = "MainLight";
var l = g_SceneManager.createLight(g_Str1);
l.setPosition(20,80,50);
var fadeColour = new ColourValue(0.93, 0.86, 0.76,1.0);
setSceneFog(g_SceneManager,3,fadeColour,0.001,500,1000);
var viewPort = g_Window.getViewport(0);
viewPort.setBackgroundColour(fadeColour);
g_Str1 = "terrain.cfg";
g_SceneManager.setWorldGeometry(g_Str1);
g_Camera.setFarClipDistance(0);
// Define the required skyplane
var plane = new Plane();
// 5000 world units from the camera
plane.d = 5000;
var unitDown = new Vector3(0,-1,0);
// Above the camera, facing down
plane.normal = unitDown;
var q = new Quaternion(-0.3486, 0.0122, 0.9365, 0.0329);
var newPos = new Vector3(707,2500,528);
g_Camera.setPosition(newPos);
g_Camera.setOrientation(q);
var cameraPos = g_Camera.getPosition();
var ray = new Ray(cameraPos,unitDown);
g_RaySceneQuery = g_SceneManager.createRayQuery(ray,-1);
}
func createFrameListener()
{
g_FrameListener = new FrameListener(g_Window,g_Camera);
g_Root.addFrameListener(g_FrameListener);
}
func Main()
{
g_Root = new Root("plugins_d.cfg","ogre.cfg", "Ogre.log");
setupResources();
if(g_Root.showConfigDialog())
{
g_Str1 = new String("");
g_Str2 = new String("");
var zero = new Vector3(0,0,0);
g_NegativeUnitY = new Vector3(0,-1,0);
g_UpdateRay = new Ray(zero,g_NegativeUnitY);
createRenderWindow();
choseSceneManager();
createCamera();
initResourceGroup();
createScene();
createFrameListener();
updateScene(0);
g_Root.startRendering();
}
}
运行结果: