同基础教程一:
配置项目属性(这点很重要,而且注意Debug和Release别弄错了,
刚开始很容易编译选Debug,而属性里配置Release)
添加代码:(注意我这里的代码是最终代码,大家最好看教程一步一步来,我这里只是用来给大家验证的)
#include "ExampleApplication.h"
class TutorialApplication : public ExampleApplication
{
protected:
public:
TutorialApplication()
{
}
~TutorialApplication()
{
}
protected:
virtual void createCamera(void)
{
// 创建摄像机
mCamera = mSceneMgr->createCamera("PlayerCam");
// 设置摄像机位置和方向
mCamera->setPosition(Vector3(400,500,0));
mCamera->lookAt(Vector3(0,0,0));
mCamera->setNearClipDistance(5);
}
virtual void createViewports(void)
{
// 建立视口
Viewport* vp = mWindow->addViewport(mCamera);
// 背景色
vp->setBackgroundColour(ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
// 默认的纵宽比
mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}
void createScene(void)
{
Entity *ent;
Light *light;
//设置环境光全变亮(暗),然后设置阴影类型
mSceneMgr->setAmbientLight(ColourValue(0, 0, 0));
mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);
//创建一个实体
ent = mSceneMgr->createEntity("Ninja", "ninja.mesh");
ent->setCastShadows(true);
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
//Y轴做法向量
Plane plane(Vector3::UNIT_Y, 0);
//创建平面
MeshManager::getSingleton().createPlane("ground",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
plane,1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z);
ent = mSceneMgr->createEntity("GroundEntity", "ground");
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
ent->setMaterialName("Examples/Rockwall");
ent->setCastShadows(false);
//创建光源设置她的类型和她的位置
light = mSceneMgr->createLight("Light1");
light->setType(Light::LT_POINT);
light->setPosition(Vector3(0, 150, 250));
//设置她的漫射色和镜面色
light->setDiffuseColour(1.0, 0.0, 0.0);
light->setSpecularColour(1.0, 0.0, 0.0);
//添加一些黄色的有向光射向她的前面
light = mSceneMgr->createLight("Light3");
light->setType(Light::LT_DIRECTIONAL);
light->setDiffuseColour(ColourValue(.25, .25, 0));
light->setSpecularColour(ColourValue(.25, .25, 0));
//有向光
light->setDirection(Vector3( 0, -1, 1 ));
//创建一个蓝聚光灯
light = mSceneMgr->createLight("Light2");
light->setType(Light::LT_SPOTLIGHT);
light->setDiffuseColour(0, 0, 1.0);
light->setSpecularColour(0, 0, 1.0);
//聚光源盘旋在Ninja的右侧,然后直射下
light->setDirection(-1, -1, 0);
light->setPosition(Vector3(300, 300, 0));
//指定光束的宽度
light->setSpotlightRange(Degree(35), Degree(50));
}
};
#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
// Create application object
TutorialApplication app;
try
{
app.go();
}
catch( Exception& e )
{
#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK |MB_ICONERROR | MB_TASKMODAL);
#else
fprintf(stderr, "An exception has occurred: %s\n",e.getFullDescription().c_str());
#endif
}
return 0;
}
运行效果: