创建平面,点光源
class MyApplication :public ExampleApplication{public: void createScene() { // 定义一个平面 // 参数1 平面的单位法线 // 参数2 平面距离原点的距离 Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -200); // 利用定义的平面创建一个平面网格 // 参数1 平面的名字;网格资源的名字 // 参数2 资源组,如同c++中的命名空间 // 参数3 平面的定义 // 参数4,5 平面的大小 // 参数6,7 平面的x,y轴上由多少个三角形组成 // param8 normals If true, normals are created perpendicular to the plane // param9 numTexCoordSets The number of 2D texture coordinate sets created - // by default the corners are created to be the corner of the texture. Ogre::MeshManager::getSingleton().createPlane("plane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 20, 20, true, 1, 5, 5, Vector3::UNIT_Z ); // 参数1 实体的名字 // 参数2 创建实体用的网格的名字 Ogre::Entity* ent = mSceneMgr->createEntity("lightpalneentity", "plane"); mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent); ent->setMaterialName("Examples/BeachStones"); Ogre::SceneNode* node = mSceneMgr->createSceneNode("node1"); mSceneMgr->getRootSceneNode()->addChild(node); // 创建点光源 Ogre::Light* light1 = mSceneMgr->createLight("light1"); light1->setType(Ogre::Light::LT_POINT); light1->setPosition(0, 20, 0); light1->setDiffuseColour(1.0f, 1.0f, 1.0f); // 将创建的实体设置在点光源的位置 Ogre::Entity* lightEnt = mSceneMgr->createEntity("myentity", "sphere.mesh"); Ogre::SceneNode* node3 = node->createChildSceneNode("node3"); node3->setScale(0.1f, 0.1f, 0.1f); node3->setPosition(0, 20, 0); node3->attachObject(lightEnt); // 创建点光源2 Ogre::Light* light2 = mSceneMgr->createLight("light2"); light2->setType(Ogre::Light::LT_POINT); light2->setPosition(20, 20,20); light2->setDiffuseColour(1.0f, 0.0f, 0.0f); // 将创建的实体设置在点光源2的位置 Ogre::Entity* lightEnt2 = mSceneMgr->createEntity("myentity2", "sphere.mesh"); Ogre::SceneNode* node4 = node->createChildSceneNode("node4"); node4->setScale(0.1f, 0.1f, 0.1f); node4->setPosition(20, 20, 20); node4->attachObject(lightEnt2); }private:};
spotlight
class MyApplication :public ExampleApplication{public: void createScene() { Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -200); Ogre::MeshManager::getSingleton().createPlane("plane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 200, 200, true, 1, 5, 5, Vector3::UNIT_Z ); Ogre::Entity* ent = mSceneMgr->createEntity("lightpalneentity", "plane"); mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent); ent->setMaterialName("Examples/BeachStones"); Ogre::SceneNode* node = mSceneMgr->createSceneNode("node1"); mSceneMgr->getRootSceneNode()->addChild(node); Ogre::SceneNode* node2 = node->createChildSceneNode("node2"); node2->setPosition(0, 100, 0); Ogre::Light* light2 = mSceneMgr->createLight("light2"); light2->setType(Ogre::Light::LT_SPOTLIGHT); light2->setDirection(Ogre::Vector3(1, -1, 0)); light2->setSpotlightInnerAngle(Ogre::Degree(5.0f)); light2->setSpotlightOuterAngle(Ogre::Degree(45.0f)); light2->setSpotlightFalloff(0.0f); light2->setDiffuseColour(Ogre::ColourValue(0.0f, 1.0f, 0.0f)); node2->attachObject(light2); Ogre::SceneNode* node3 = node2->createChildSceneNode("node3"); node3->setPosition(0, 100, 0); Ogre::Entity* sphere = mSceneMgr->createEntity("sphereentity", "sphere.mesh"); node3->attachObject(sphere); }private:};
directionallight + shadow
class MyApplication :public ExampleApplication{public: void createScene() { // 先用定义的平面创建一个网格,再用创建的网格构造一个实体 Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -200); Ogre::MeshManager::getSingleton().createPlane("plane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 200, 200, true, 1, 5, 5, Vector3::UNIT_Z ); Ogre::Entity* ent = mSceneMgr->createEntity("lightpalneentity", "plane"); Ogre::SceneNode* node = mSceneMgr->createSceneNode("node"); mSceneMgr->getRootSceneNode()->addChild(node); node->attachObject(ent); ent->setMaterialName("Examples/BeachStones"); // 方向光不用设置位置 Ogre::Light* light = mSceneMgr->createLight("light"); light->setType(Ogre::Light::LT_DIRECTIONAL); light->setDiffuseColour(Ogre::ColourValue(1.0f, 1.0f, 1.0f)); light->setDirection(Ogre::Vector3(1, -1, 0)); Ogre::Entity* ent2 = mSceneMgr->createEntity("sinbad2", "Sinbad.mesh"); Ogre::SceneNode* node2 = mSceneMgr->createSceneNode("scenenode2"); node->addChild(node2); node2->setScale(3.0f, 3.0f, 3.0f); node2->setPosition(Ogre::Vector3(0.0f, 1.0f, 0.0f)); node2->attachObject(ent2); // 设置阴影 mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE); }private:};
创建摄像机,视口
class MyApplication :public ExampleApplication{public: void createCamera() { mCamera = mSceneMgr->createCamera("mycamera"); mCamera->setPosition(0, 100, 200); mCamera->lookAt(0, 0, 0); mCamera->setNearClipDistance(50); //mCamera->setFarClipDistance(2000); // the same effect as press R mCamera->setPolygonMode(Ogre::PM_WIREFRAME); } void createViewports() { // create a viewport Ogre::Viewport* vp = mWindow->addViewport(mCamera); // set viewport's background color vp->setBackgroundColour(Ogre::ColourValue(0.0f, 0.0f, 1.0f)); // set aspect ratio mCamera->setAspectRatio(Real(vp->getActualWidth() / vp->getActualHeight())); } void createScene() { // 先用定义的平面创建一个网格,再用创建的网格构造一个实体 Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -200); Ogre::MeshManager::getSingleton().createPlane("plane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 200, 200, true, 1, 5, 5, Vector3::UNIT_Z ); Ogre::Entity* ent = mSceneMgr->createEntity("lightpalneentity", "plane"); Ogre::SceneNode* node = mSceneMgr->createSceneNode("node"); mSceneMgr->getRootSceneNode()->addChild(node); node->attachObject(ent); ent->setMaterialName("Examples/BeachStones"); // 方向光不用设置位置 Ogre::Light* light = mSceneMgr->createLight("light"); light->setType(Ogre::Light::LT_DIRECTIONAL); light->setDiffuseColour(Ogre::ColourValue(1.0f, 1.0f, 1.0f)); light->setDirection(Ogre::Vector3(1, -1, 0)); Ogre::Entity* ent2 = mSceneMgr->createEntity("sinbad2", "Sinbad.mesh"); Ogre::SceneNode* node2 = mSceneMgr->createSceneNode("scenenode2"); node->addChild(node2); node2->setScale(3.0f, 3.0f, 3.0f); node2->setPosition(Ogre::Vector3(0.0f, 4.0f, 0.0f)); node2->attachObject(ent2); // 设置阴影 mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE); }private:};