续
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;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func updateScene(var timeDiv)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func createRenderWindow()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
g_Str1 = "Ogre Render Window";
g_Str2 = "";
g_Window = g_Root.initialise(1,g_Str1,g_Str2);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func choseSceneManager()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
g_Str1 = "TerrainSceneManager";
g_Str2 = "Terrain";
g_SceneManager = g_Root.createSceneManagerByStrType(g_Str1,g_Str2);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func createCamera()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func initResourceGroup()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
var resourceGroupManager = getResourceGroupManager();
resourceGroupManager.initialiseAllResourceGroups();
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func createScene()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
var waterPlane = new Plane();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// 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);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
l.setPosition(20,80,50);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
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);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
g_Str1 = "terrain.cfg";
g_SceneManager.setWorldGeometry(g_Str1);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
g_Camera.setFarClipDistance(0);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// 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);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func createFrameListener()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
g_FrameListener = new FrameListener(g_Window,g_Camera);
g_Root.addFrameListener(g_FrameListener);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
func Main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
g_Root = new Root("plugins_d.cfg","ogre.cfg", "Ogre.log");
setupResources();
if(g_Root.showConfigDialog())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
updateScene(0);
g_Root.startRendering();
}
}
运行结果:
posted on 2009-04-18 17:40
清風 阅读(1603)
评论(3) 编辑 收藏 引用 所属分类:
LuckyScript