zoukankan      html  css  js  c++  java
  • OGRE 入门 三、Basic Tutorial 2 : Cameras, Lights, and Shadows

    1. 编写代码:

    在上次的文件夹中新建BasicTutorial2.h和BasicTutorial2.cpp文件:

    BasicTutorial2.h:

     1 #ifndef BASICTUTORIAL2_H
     2 #define BASICTUTORIAL2_H
     3 
     4 #include "BaseApplication.h"
     5 
     6 class BasicTutorial2:public BaseApplication
     7 {
     8 public:
     9     BasicTutorial2(void);
    10     virtual ~BasicTutorial2(void);
    11 
    12 protected:
    13     virtual void createScene(void);
    14     virtual void createCamera(void);
    15     virtual void createViewports(void);
    16 };
    17 
    18 #endif

    BasicTutorial2.cpp:

      1 #include "BasicTutorial2.h"
      2 
      3 BasicTutorial2::BasicTutorial2(void)
      4 {}
      5 
      6 BasicTutorial2::~BasicTutorial2(void)
      7 {}
      8 
      9 void BasicTutorial2::createViewports(void)
     10 {
     11     Ogre::Viewport* vp=mWindow->addViewport(mCamera);
     12 
     13     vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
     14     mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth())/
     15                             Ogre::Real(vp->getActualHeight()));
     16     
     17 }
     18 
     19 void BasicTutorial2::createScene(void)
     20 {
     21     mSceneMgr->setAmbientLight(Ogre::ColourValue(0,0,0));
     22     mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
     23 
     24     Ogre::Entity* entNinja=mSceneMgr->createEntity("Ninja","ninja.mesh");
     25     entNinja->setCastShadows(true);
     26     mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(entNinja);
     27 
     28     Ogre::Plane plane(Ogre::Vector3::UNIT_Y,0);
     29 
     30     Ogre::MeshManager::getSingleton().createPlane("ground",
     31                                                   Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
     32                                                   plane,
     33                                                   1500,
     34                                                   1500,
     35                                                   20,
     36                                                   20,
     37                                                   true,
     38                                                   1,5,5,
     39                                                   Ogre::Vector3::UNIT_Z);
     40     Ogre::Entity* entGround=mSceneMgr->createEntity("GroundEntity","ground");
     41     mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(entGround);
     42 
     43     entGround->setMaterialName("Examples/Rockwall");
     44     entGround->setCastShadows(false);
     45     /* lights */
     46     Ogre::Light* pointLight=mSceneMgr->createLight("pointLight");
     47     pointLight->setType(Ogre::Light::LT_POINT);
     48     pointLight->setPosition(Ogre::Vector3(0,150,250));
     49 
     50     pointLight->setDiffuseColour(1.0,0,0);
     51     pointLight->setSpecularColour(1.0,0,0);
     52 
     53     Ogre::Light* directionalLight=mSceneMgr->createLight("directionalLight");
     54     directionalLight->setType(Ogre::Light::LT_DIRECTIONAL);
     55     directionalLight->setDiffuseColour(Ogre::ColourValue(.25,.25,0));
     56     directionalLight->setSpecularColour(Ogre::ColourValue(.25,.25,0));
     57 
     58     directionalLight->setDirection(Ogre::Vector3(0,-1,1));
     59 
     60     Ogre::Light* spotLight=mSceneMgr->createLight("spotLight");
     61     spotLight->setType(Ogre::Light::LT_SPOTLIGHT);
     62     spotLight->setDiffuseColour(0,0,1.0);
     63     spotLight->setSpecularColour(0,0,1.0);
     64 
     65     spotLight->setDirection(-1,-1,0);
     66     spotLight->setPosition(Ogre::Vector3(300,300,0));
     67 
     68     spotLight->setSpotlightRange(Ogre::Degree(35),Ogre::Degree(35));
     69     
     70                                        
     71 }
     72 
     73 void BasicTutorial2::createCamera(void)
     74 {
     75     mCamera=mSceneMgr->createCamera("PlayerCam");
     76 
     77     mCamera->setPosition(Ogre::Vector3(0,10,500));
     78     mCamera->lookAt(Ogre::Vector3(0,0,0));
     79     mCamera->setNearClipDistance(5);
     80 
     81     mCameraMan=new OgreBites::SdkCameraMan(mCamera);
     82     
     83 }
     84 
     85 
     86 int main(int argc,char* argv[])
     87 {
     88     BasicTutorial2 app;
     89 
     90     try
     91     {
     92         app.go();
     93     } catch (Ogre::Exception& e)
     94     {
     95         std::cerr<<"An exception has occured:"<<
     96                 e.getFullDescription().c_str()<<std::endl;
     97         
     98     }
     99 
    100     return 0;
    101 }

    修改CMakeLists.txt中以下几行,注释教程1中的头文件及代码,并加入新建的头文件和代码


    set(HDRS
    ./BaseApplication.h
    # ./TutorialApplication.h
    ./BasicTutorial2.h
    )



    set(SRCS
    ./BaseApplication.cpp
    ./BasicTutorial2.cpp
    # ./TutorialApplication.cpp
    )

    复制nanjia.mesh文件到当前文件夹的dist/media/models中,nanjia.mesh 在ogre的源代码包里,在第一次下载的代码包里搜索一下就有了。

    同样的,将dist/media中的子文件夹以源代码包中的Sample/Media/materials中的同名文件夹覆盖,其实就是一些资源文件,启动demo的时候会加载。

    2. 编译,安装:

    cmake CMakeLists.txt
    make install

    3. 运行:

    效果如图:

  • 相关阅读:
    C# 操作配置文件
    C# Excel操作类
    没有找到 mspdb100.dll 的解决办法
    工厂方法模式
    .Net互操作2
    The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决
    手机抓包xcode自带命令行工具配合wireshark实现
    expecting SSH2_MSG_KEX_ECDH_REPLY ssh_dispatch_run_fatal问题解决
    使用ssh-keygen设置ssh无密码登录
    远程复制文件到服务器
  • 原文地址:https://www.cnblogs.com/invisible/p/2803954.html
Copyright © 2011-2022 走看看