用ogre做图像引擎,用MITA做音频引擎做游戏的源代码3(附完整工程代码资源下载地址)

关键的处理代码都写在mitademo.cpp里面了。里面有注释,大家有什么问题给我留言。

下载地址:http://download.youkuaiyun.com/source/3444236

/*
-----------------------------------------------------------------------------
Filename: mitademo.cpp
-----------------------------------------------------------------------------
*/

#include "mitademo.h"

//-------------------------------------------------------------------------------------
mitademo::mitademo(void)
: mAngle(0.0f)
{
}
//-------------------------------------------------------------------------------------
mitademo::~mitademo(void)
{
if(mTrayMgr)
{
MITA_CloseHandle(hListener);

MITA_Engine_RemoveSource(hEngine, speakerSound);
MITA_Engine_RemoveSource(hEngine, f18Sound);
MITA_Engine_RemoveSource(hEngine, carSound);
MITA_Engine_RemoveSource(hEngine, apacheSound);

MITA_Engine_RemoveSource(hEngine, hOutput);

MITA_CloseHandle(speakerSound);
MITA_CloseHandle(f18Sound);
MITA_CloseHandle(carSound);
MITA_CloseHandle(apacheSound);

MITA_CloseHandle(hOutput);
MITA_CloseHandle(hEngine); //Release Engine Object
MITA_CloseHandle(gInstance); //Release global instance handle.
}
}

//-------------------------------------------------------------------------------------
void mitademo::createScene(void)
{
mBodyEnt = mSceneMgr->createEntity("mm", "mitamm.mesh");

mBodyNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("mmNode", Vector3::UNIT_Y * CHAR_HEIGHT);
mBodyNode->attachObject(mBodyEnt);
charaOrientation = Vector3(0,0,1);

Ogre::Entity* f18ent = mSceneMgr->createEntity("f18","f18.mesh");
Ogre::SceneNode* f18node = mSceneMgr->getRootSceneNode()->createChildSceneNode("f18Node",Vector3(100, 20, -150 ));

Animation* anim;
NodeAnimationTrack* track;

// create a 14 second animation with spline interpolation
anim = mSceneMgr->createAnimation("f18Path", 14);
anim->setInterpolationMode(Animation::IM_SPLINE);

track = anim->createNodeTrack(1, f18node); // create a node track for our animation

// enter keyframes for our track to define a path for the light to follow
track->createNodeKeyFrame(0)->setTranslate(Vector3(100, 20, -350 ));
track->createNodeKeyFrame(2)->setTranslate(Vector3(100, 30, -130 ));
track->createNodeKeyFrame(4)->setTranslate(Vector3(100, 100, -50 ));
track->createNodeKeyFrame(6)->setTranslate(Vector3(100, 120, -20 ));
track->createNodeKeyFrame(8)->setTranslate(Vector3(100, 120, 20 ));
track->createNodeKeyFrame(10)->setTranslate(Vector3(100,100, 50 ));
track->createNodeKeyFrame(12)->setTranslate(Vector3(100, 30, 130 ));
track->createNodeKeyFrame(14)->setTranslate(Vector3(100, 20, 350 ));

// create an animation state from the animation and enable it
f18AnimState = mSceneMgr->createAnimationState("f18Path");
f18AnimState->setEnabled(true);
f18node->attachObject(f18ent);

Ogre::Entity* apacheent = mSceneMgr->createEntity("apache","apache.mesh");
Ogre::SceneNode* apachenode = mSceneMgr->getRootSceneNode()->createChildSceneNode("apacheNode",Vector3(-200,80,-200));
apachenode->attachObject(apacheent);


Ogre::Entity* carent = mSceneMgr->createEntity("car","carbody.mesh");
Ogre::SceneNode* carnode = mSceneMgr->getRootSceneNode()->createChildSceneNode("carNode",Vector3(-100, 0, -150 ));
carnode->attachObject(carent);
Animation* anim2;
NodeAnimationTrack* track2;
anim2 = mSceneMgr->createAnimation("carPath", 14);
anim2->setInterpolationMode(Animation::IM_SPLINE);

track2 = anim2->createNodeTrack(1, carnode); // create a node track for our animation

// enter keyframes for our track to define a path for the light to follow
track2->createNodeKeyFrame(0)->setTranslate(Vector3(-100, 0, -150 ));
track2->createNodeKeyFrame(14)->setTranslate(Vector3(-100, 0, 150 ));

// create an animation state from the animation and enable it
carAnimState = mSceneMgr->createAnimationState("carPath");
carAnimState->setEnabled(true);

Ogre::Entity* yxent = mSceneMgr->createEntity("yx","yx.mesh");
Ogre::SceneNode* yxnode = mSceneMgr->getRootSceneNode()->createChildSceneNode("yxNode",Vector3(20,0,0));
yxnode->attachObject(yxent);

// Set ambient light
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.8, 0.8, 0.8));

// Create a light
Ogre::Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20,80,50);

//天空
mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8, 5000, false);
//建立一个地面,面向y轴,位于0处
Ogre::Plane plane(Ogre::Vector3::UNIT_Y, 0);
Ogre::MeshManager::getSingleton().createPlane("ground", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,plane, 1000, 1000, 20, 20, true, 1, 5, 5, Ogre::Vector3::UNIT_Z);
Ogre::Entity* entGround = mSceneMgr->createEntity("GroundEntity", "ground");
entGround->setMaterialName("Examples/Rockwall");
entGround->setCastShadows(false);
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(entGround);

mKeyDirection = Vector3::ZERO;
mVerticalVelocity = 0;
mCameraMan->setStyle(CS_MANUAL);
setupCamera();
setupAnimations();

/
MITA
//Let's start to init the devices to play sound:
//初始化音频设备
initMitaDevice();
//Get to play!
//播放音乐
playSpMusic();
playF18Sound();
playCarSound();
playApacheSound();
/
}

void mitademo::initMitaDevice()
{
//
//1. Create a global instance handle.
//1. 创建一个全局实例处理
gInstance = MITA_Initialize(MITA_NULL);
//
//3. Load all existed plugins.
//3. 加载所有的插件
MITA_Ins_LoadPluginFromDir(gInstance, (MITA_LPCWSTR)L"../../plugins", &PluginCount);
//
//4. Create Engine Object
//4. 创建引擎对象
MITA_Engine_Create(gInstance, MITA_ENGINEFLAG_CHECKPREPARE|MITA_ENGINEFLAG_CHECKRENDER, &hEngine);
//
//5. Create Output Source
//5. 创建一个默认的输出
MITA_GuessChannelMode(2, &ChannelMask);
//MITA_GuessChannelMode(6, &ChannelMask);
hOutput = MITA_CreateDefaultOutput(gInstance, ChannelMask);
//
//6. Add source to engine.
//6. 将输出添加到引擎中
MITA_Engine_AddSource(hEngine, hOutput);
MITA_Output_Startup(hOutput);
// Set Output Volume
// 音量
//MITA_Output_SetVolume(hOutput,0.2);
//
//7. Init a Listener
//7. 初始化一个听者
MITA_Listener_Create(gInstance, &hListener);
//8. 将听者放到角色的初始位置
//8. Put the listener to the position where our character stand
MITA_Listener_SetPosition(hListener, mBodyNode->getPosition().x, (MITA_FLOAT)mBodyNode->getPosition().y, (MITA_FLOAT)mBodyNode->getPosition().z);
// And set the same Orientation with our character
// 并且设置听者的方向与角色方向一致
MITA_Listener_SetDirection(hListener, (MITA_FLOAT)(mBodyNode->getPosition().x+charaOrientation.x),charaOrientation.y,(MITA_FLOAT)(mBodyNode->getPosition().z+charaOrientation.z),(MITA_FLOAT)0, (MITA_FLOAT)1, (MITA_FLOAT)0);
}

void mitademo::playSpMusic()
{
//
//Load input source
//加载音源文件
MITA_Input_LoadByPath(gInstance,(MITA_LPCWSTR)L"../../media/frozen-mainzik-1p-full.ogg",&speakerSound);
MITA_Engine_AddSource(hEngine, speakerSound);
//Play loop
//循环播放
MITA_BYTE Loop = 0xFF;
//Use [MITA_Input_Play] to play a non-3D sound,such as background music.
//非3d播放,可以使用它作为背景音乐的播放语句
//MITA_Input_Play(speakerSound, &Loop);
MITA_FLOAT yxPosx = mSceneMgr->getSceneNode("yxNode")->getPosition().x;
MITA_FLOAT yxPosy = mSceneMgr->getSceneNode("yxNode")->getPosition().y;
MITA_FLOAT yxPosz = mSceneMgr->getSceneNode("yxNode")->getPosition().z;
MITA_FLOAT str = 21.0f;
MITA_Input_Play3D(speakerSound,&Loop,&yxPosx,&yxPosy,&yxPosz,&str);
//Bind the Sound with Listener
//将音源与听者关联
MITA_Input_BindListener3D(speakerSound, hListener);
//Set up the min and max distence of change of the sound
//设置音源的衰减变化范围
MITA_FLOAT minDis = 0.0f, maxDis = 1000.0f;
MITA_Input_SetDistance3D(speakerSound, &minDis, &maxDis);
}

void mitademo::playF18Sound()
{
//
//Load input source
MITA_Input_LoadByPath(gInstance,(MITA_LPCWSTR)L"../../media/f18.ogg",&f18Sound);
MITA_Engine_AddSource(hEngine, f18Sound);

//循环播放
MITA_BYTE Loop = 0xFF;
MITA_FLOAT yxPosx = mSceneMgr->getSceneNode("f18Node")->getPosition().x;
MITA_FLOAT yxPosy = mSceneMgr->getSceneNode("f18Node")->getPosition().y;
MITA_FLOAT yxPosz = mSceneMgr->getSceneNode("f18Node")->getPosition().z;
MITA_FLOAT str = 10.0f;
MITA_Input_Play3D(f18Sound,&Loop,&yxPosx,&yxPosy,&yxPosz,&str);
//将音源与听者关联
MITA_Input_BindListener3D(f18Sound, hListener);
//设置音源的衰减变化范围
MITA_FLOAT minDis = 0.0f, maxDis = 1000.0f;
MITA_Input_SetDistance3D(f18Sound, &minDis, &maxDis);
//MITA_Source_SetPriority(f18Sound, MITA_PRIORITY_HIGHEST);
//MITA_Input_SetVolume(f18Sound, 10.0f);
}

void mitademo::playCarSound()
{
//
// Load input source
MITA_Input_LoadByPath(gInstance,(MITA_LPCWSTR)L"../../media/car.ogg",&carSound);
MITA_Engine_AddSource(hEngine, carSound);

//循环播放
MITA_BYTE Loop = 0xFF;
MITA_FLOAT yxPosx = mSceneMgr->getSceneNode("carNode")->getPosition().x;
MITA_FLOAT yxPosy = mSceneMgr->getSceneNode("carNode")->getPosition().y;
MITA_FLOAT yxPosz = mSceneMgr->getSceneNode("carNode")->getPosition().z;
MITA_FLOAT str = 5.0f;
MITA_Input_Play3D(carSound,&Loop,&yxPosx,&yxPosy,&yxPosz,&str);
//将音源与听者关联
MITA_Input_BindListener3D(carSound, hListener);
//设置音源的衰减变化范围
MITA_FLOAT minDis = 0.0f, maxDis = 500.0f;
MITA_Input_SetDistance3D(carSound, &minDis, &maxDis);
//MITA_Input_SetVolume(carSound, 0.5f);
}

void mitademo::playApacheSound()
{
//
//Load input source
MITA_Input_LoadByPath(gInstance,(MITA_LPCWSTR)L"../../media/helicopter.ogg",&apacheSound);
MITA_Engine_AddSource(hEngine, apacheSound);

//循环播放
MITA_BYTE Loop = 0xFF;
MITA_FLOAT yxPosx = mSceneMgr->getSceneNode("apacheNode")->getPosition().x;
MITA_FLOAT yxPosy = mSceneMgr->getSceneNode("apacheNode")->getPosition().y;
MITA_FLOAT yxPosz = mSceneMgr->getSceneNode("apacheNode")->getPosition().z;
MITA_FLOAT str = 21.0f;
MITA_Input_Play3D(apacheSound,&Loop,&yxPosx,&yxPosy,&yxPosz,&str);
//将音源与听者关联
MITA_Input_BindListener3D(apacheSound, hListener);
//设置音源的衰减变化范围
MITA_FLOAT minDis = 0.0f, maxDis = 500.0f;
MITA_Input_SetDistance3D(apacheSound, &minDis, &maxDis);
}

//
//ChannelMask must has channel number in high 8 bits.
// | 00000000 | MMMMMMMM MMMMMMMM MMMMMMMM |
// Nums ChannelMask
//How to set number?
// MITA_CHNFMT_SetNum(Mask, 2)
//Also you can use MITA_GuessChannelMode.
//
// The channel number must equal with channel mask.
// ChannelMask = MITA_CP_FRONT_LEFT|MITA_CP_FRONT_RIGHT; //Use two speaker
// MITA_CHNFMT_SetNum(ChannelMask, 2); //Set 2
//

MITA_INLINE
MITA_HOUTPUT
mitademo::MITA_CreateDefaultOutput(MITA_HINSTANCE hInstance, MITA_DWORD ChannelMask)
{
MITA_HOUTPUT hOutput;
MITA_HDEVICE hDevice;
MITA_DEVICEPARAM Param;
MITA_SIZE i, n;
MITA_DEVICEINFO DevInfo;
//MITA_DWORD Ids[10];
MITA_BYTE MaxIds = 0;

MITA_Ins_CreateObject(hInstance, &IID_DevWinMME, (MITA_HOBJECT*)&hDevice);

MITA_Device_GetDeviceNums(hDevice, &n);

MITA_Device_GetDevice(hDevice, 0, &DevInfo);

Param.Callback = MITA_NULL;
Param.CallbackHandle = MITA_NULL;
Param.Customer = MITA_NULL; /**< @brief for future use */
Param.ChannelFmt.mask = ChannelMask; /**< @brief The mask of channels. */
Param.ChannelFmt.sfmt = MITA_SDT_F32; /**< @brief Float32 Sample */
Param.ChannelFmt.sps = 44100.0f; /**< @brief 44.1KHz */
Param.Latency = 0.1f; /**< @brief 100 ms latency */
Param.Type = MITA_CT_OUTPUT; /**< @brief For output. */
Param.DevId = DevInfo.DevId; /**< @brief The id of device */

MITA_Output_CreateByDevice(hInstance, hDevice, &Param, MITA_FALSE, &hOutput);

return hOutput;
}

void mitademo::setupCamera()
{
// 创建一个旋转轴心
mCameraPivot = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCameraGoal = mCameraPivot->createChildSceneNode(Vector3(0, 0, 5));
//摄像机节点
mCameraNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCameraNode->setPosition(mCameraPivot->getPosition() + mCameraGoal->getPosition());

mCameraPivot->setFixedYawAxis(true);
mCameraGoal->setFixedYawAxis(true);
mCameraNode->setFixedYawAxis(true);

mCamera->setNearClipDistance(1.5);
//mCamera->setFarClipDistance(1000);
mCameraNode->attachObject(mCamera);

mPivotPitch = 0;
}
void mitademo::setupAnimations()
{
// this is very important due to the nature of the exported animations
mBodyEnt->getSkeleton()->setBlendMode(ANIMBLEND_CUMULATIVE);

String animNames[] ={"idlebase", "idletop", "runbase", "runtop", "dance", "jumpstart", "jumploop","jumpend"};

// populate our animation list
for (int i = 0; i < NUM_ANIMS; i++)
{
mAnims[i] = mBodyEnt->getAnimationState(animNames[i]);
mAnims[i]->setLoop(true);
mFadingIn[i] = false;
mFadingOut[i] = false;
}

// start off in the idle state (top and bottom together)
setBaseAnimation(ANIM_IDLE_BASE,false);
setTopAnimation(ANIM_IDLE_TOP,false);
}
void mitademo::updateCamera(Real deltaTime)
{
//放置摄像机旋转的轴心位置
Ogre::Vector3 tmpCamPos = mBodyNode->getPosition();
tmpCamPos.y = 0;
mCameraPivot->setPosition(tmpCamPos + Vector3::UNIT_Y * CAM_HEIGHT);
//平滑移动摄像机
Vector3 goalOffset = mCameraGoal->_getDerivedPosition() - mCameraNode->getPosition();
mCameraNode->translate(goalOffset * deltaTime * 9.0f);
总是指向轴心位置
tmpCamPos = mCameraPivot->_getDerivedPosition();
tmpCamPos.y = CAM_HEIGHT;
mCameraNode->lookAt(tmpCamPos, Node::TS_WORLD);
}
void mitademo::updateBody(Real deltaTime)
{
mGoalDirection = Vector3::ZERO; // 我们将会在下面计算它

if (mKeyDirection != Vector3::ZERO && mBaseAnimID != ANIM_DANCE)
{
//计算在世界坐标中实际的目标方向,基于角色的关键方向
mGoalDirection += mKeyDirection.z * mCameraNode->getOrientation().zAxis();
mGoalDirection += mKeyDirection.x * mCameraNode->getOrientation().xAxis();
mGoalDirection.y = 0;
mGoalDirection.normalise();//单位向量

Quaternion toGoal = mBodyNode->getOrientation().zAxis().getRotationTo(mGoalDirection);

//计算角色面对到目标方向旋转的角度
Real yawToGoal = toGoal.getYaw().valueDegrees();
//每帧旋转的角度,旋转速度
Real yawAtSpeed = yawToGoal / Math::Abs(yawToGoal) * deltaTime * TURN_SPEED;
//如果在半空就减少旋转
if (mBaseAnimID == ANIM_JUMP_LOOP) yawAtSpeed *= 0.2f;

// 转过需要的角度
if (yawToGoal < 0) yawToGoal = std::min<Real>(0, std::max<Real>(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, yawAtSpeed, 0);
else if (yawToGoal > 0) yawToGoal = std::max<Real>(0, std::min<Real>(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, 0, yawAtSpeed);

mBodyNode->yaw(Degree(yawToGoal));

mAngle += yawToGoal;
if (mAngle >= 360.0f)
mAngle -= 360.0f;
else if (mAngle <= 0.0f)
mAngle += 360.0f;

charaOrientation.x = sin(Degree(mAngle).valueRadians());
charaOrientation.z = cos(Degree(mAngle).valueRadians());

// 在现在的身体方向移动(不是目标方向)
mBodyNode->translate(0, 0, deltaTime * RUN_SPEED * mAnims[mBaseAnimID]->getWeight(),
Node::TS_LOCAL);

//Let the Listener move to the character's position.
//让听者跟着角色移动
MITA_Listener_Move(hListener, mBodyNode->getPosition().x, (MITA_FLOAT)mBodyNode->getPosition().y, (MITA_FLOAT)mBodyNode->getPosition().z);
//And update the Orientation.We can use MITA_Listener_SetDirection or MITA_Listener_SetDirectionAngle.As we already know the Angle,MITA_Listener_SetDirectionAngle is more simple and efficiently.
//并且设置听者的方向与角色方向一致.MITA_Listener_SetDirectionAngle与MITA_Listener_SetDirection功能相同,不过对于知道了旋转角度后,使用MITA_Listener_SetDirectionAngle更简单高效.
//MITA_Listener_SetDirection(hListener, (MITA_FLOAT)(charaOrientation.x),charaOrientation.y,(MITA_FLOAT)(charaOrientation.z),(MITA_FLOAT)0, (MITA_FLOAT)1, (MITA_FLOAT)0);
MITA_Listener_SetDirectionAngle(hListener,mAngle);
}

if (mBaseAnimID == ANIM_JUMP_LOOP)
{
// if we're jumping, add a vertical offset too, and apply gravity
mBodyNode->translate(0, mVerticalVelocity * deltaTime, 0, Node::TS_LOCAL);
mVerticalVelocity -= GRAVITY * deltaTime;

Vector3 pos = mBodyNode->getPosition();
if (pos.y <= CHAR_HEIGHT)
{
// if we've hit the ground, change to landing state
pos.y = CHAR_HEIGHT;
mBodyNode->setPosition(pos);
setBaseAnimation(ANIM_JUMP_END, true);
mTimer = 0;
}
}
}
void mitademo::updateAnimations(Real deltaTime)
{
Real baseAnimSpeed = 1;
Real topAnimSpeed = 1;

mTimer += deltaTime;

if (mBaseAnimID == ANIM_JUMP_START)
{
if (mTimer >= mAnims[mBaseAnimID]->getLength())
{
// takeoff animation finished, so time to leave the ground!
setBaseAnimation(ANIM_JUMP_LOOP, true);
// apply a jump acceleration to the character
mVerticalVelocity = JUMP_ACCEL;
}
}
else if (mBaseAnimID == ANIM_JUMP_END)
{
if (mTimer >= mAnims[mBaseAnimID]->getLength())
{
// safely landed, so go back to running or idling
if (mKeyDirection == Vector3::ZERO)
{
setBaseAnimation(ANIM_IDLE_BASE,false);
setTopAnimation(ANIM_IDLE_TOP,false);
}
else
{
setBaseAnimation(ANIM_RUN_BASE, true);
setTopAnimation(ANIM_RUN_TOP, true);
}
}
}

// increment the current base and top animation times
if (mBaseAnimID != ANIM_NONE) mAnims[mBaseAnimID]->addTime(deltaTime * baseAnimSpeed);
if (mTopAnimID != ANIM_NONE) mAnims[mTopAnimID]->addTime(deltaTime * topAnimSpeed);

// apply smooth transitioning between our animations
fadeAnimations(deltaTime);
}
void mitademo::setBaseAnimation(AnimID id, bool reset)
{
if (mBaseAnimID >= 0 && mBaseAnimID < NUM_ANIMS)
{
// if we have an old animation, fade it out
mFadingIn[mBaseAnimID] = false;
mFadingOut[mBaseAnimID] = true;
}

mBaseAnimID = id;

if (id != ANIM_NONE)
{
// if we have a new animation, enable it and fade it in
mAnims[id]->setEnabled(true);
mAnims[id]->setWeight(0);
mFadingOut[id] = false;
mFadingIn[id] = true;
if (reset) mAnims[id]->setTimePosition(0);
}
}
void mitademo::setTopAnimation(AnimID id, bool reset)
{
if (mTopAnimID >= 0 && mTopAnimID < NUM_ANIMS)
{
// if we have an old animation, fade it out
mFadingIn[mTopAnimID] = false;
mFadingOut[mTopAnimID] = true;
}

mTopAnimID = id;

if (id != ANIM_NONE)
{
// if we have a new animation, enable it and fade it in
mAnims[id]->setEnabled(true);
mAnims[id]->setWeight(0);
mFadingOut[id] = false;
mFadingIn[id] = true;
if (reset) mAnims[id]->setTimePosition(0);
}
}
void mitademo::updateCameraGoal(Real deltaYaw, Real deltaPitch, Real deltaZoom)
{
mCameraPivot->yaw(Degree(deltaYaw), Node::TS_WORLD);

// 角度边界
if (!(mPivotPitch + deltaPitch > 45 && deltaPitch > 0) &&
!(mPivotPitch + deltaPitch < -60 && deltaPitch < 0))
{
mCameraPivot->pitch(Degree(deltaPitch), Node::TS_LOCAL);
mPivotPitch += deltaPitch;
}

Real dist = mCameraGoal->_getDerivedPosition().distance(mCameraPivot->_getDerivedPosition());
Real distChange = deltaZoom * dist;

// 缩放边界
if (!(dist + distChange < 2 && distChange < 0) &&
!(dist + distChange > 25 && distChange > 0))
{
mCameraGoal->translate(0, 0, distChange, Node::TS_LOCAL);
}
}
void mitademo::fadeAnimations(Real deltaTime)
{
for (int i = 0; i < NUM_ANIMS; i++)
{
if (mFadingIn[i])
{
// slowly fade this animation in until it has full weight
Real newWeight = mAnims[i]->getWeight() + deltaTime * ANIM_FADE_SPEED;
mAnims[i]->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
if (newWeight >= 1) mFadingIn[i] = false;
}
else if (mFadingOut[i])
{
// slowly fade this animation out until it has no weight, and then disable it
Real newWeight = mAnims[i]->getWeight() - deltaTime * ANIM_FADE_SPEED;
mAnims[i]->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
if (newWeight <= 0)
{
mAnims[i]->setEnabled(false);
mFadingOut[i] = false;
}
}
}
}
bool mitademo::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
bool ret = BaseApplication::frameRenderingQueued(evt);
//动画状态时间监听
mmaddTime(evt.timeSinceLastFrame);
f18addTime(evt.timeSinceLastFrame);
caraddTime(evt.timeSinceLastFrame);
///下面是用来在面板中显示所有物体位置方向的信息
if (!mTrayMgr->isDialogVisible())
{
mCameraMan->frameRenderingQueued(evt); // if dialog isn't up, then update the camera
if (mDetailsPanel->isVisible()) // if details panel is visible, then update its contents
{
Posx = mSceneMgr->getSceneNode("mmNode")->getPosition().x;
Posy = mSceneMgr->getSceneNode("mmNode")->getPosition().y;
Posz = mSceneMgr->getSceneNode("mmNode")->getPosition().z;
mDetailsPanel->setParamValue(0,"X:"+Ogre::StringConverter::toString((int)Posx)+" Y:"+Ogre::StringConverter::toString((int)Posy)+" Z:"+Ogre::StringConverter::toString((int)Posz));

MITA_Listener_GetPosition(hListener,&Posx,&Posy,&Posz);
mDetailsPanel->setParamValue(1, "X:"+Ogre::StringConverter::toString((int)Posx)+" Y:"+Ogre::StringConverter::toString((int)Posy)+" Z:"+Ogre::StringConverter::toString((int)Posz));

Posx = charaOrientation.x;
Posy = charaOrientation.y;
Posz = charaOrientation.z;
mDetailsPanel->setParamValue(2, "toX:"+Ogre::StringConverter::toString(Posx)+" toY:"+Ogre::StringConverter::toString(Posy)+" toZ:"+Ogre::StringConverter::toString(Posz));

MITA_Listener_GetDirection(hListener,&Posx,&Posy,&Posz,&Directionx,&Directiony,&Directionz);
mDetailsPanel->setParamValue(3, "toX:"+Ogre::StringConverter::toString(Posx)+" toY:"+Ogre::StringConverter::toString(Posy)+" toZ:"+Ogre::StringConverter::toString(Posz));
mDetailsPanel->setParamValue(4, "upX:"+Ogre::StringConverter::toString((int)Directionx)+" upY:"+Ogre::StringConverter::toString((int)Directiony)+" upZ:"+Ogre::StringConverter::toString((int)Directionz));

MITA_Input_GetPosition3D(speakerSound,&Posx,&Posy,&Posz);
mDetailsPanel->setParamValue(5, "X:"+Ogre::StringConverter::toString((int)Posx)+" Y:"+Ogre::StringConverter::toString((int)Posy)+" Z:"+Ogre::StringConverter::toString((int)Posz));

MITA_Input_GetPosition3D(carSound,&Posx,&Posy,&Posz);
mDetailsPanel->setParamValue(6, "X:"+Ogre::StringConverter::toString((int)Posx)+" Y:"+Ogre::StringConverter::toString((int)Posy)+" Z:"+Ogre::StringConverter::toString((int)Posz));

MITA_Input_GetPosition3D(apacheSound,&Posx,&Posy,&Posz);
mDetailsPanel->setParamValue(7, "X:"+Ogre::StringConverter::toString((int)Posx)+" Y:"+Ogre::StringConverter::toString((int)Posy)+" Z:"+Ogre::StringConverter::toString((int)Posz));

MITA_Input_GetPosition3D(f18Sound,&Posx,&Posy,&Posz);
mDetailsPanel->setParamValue(8, "X:"+Ogre::StringConverter::toString((int)Posx)+" Y:"+Ogre::StringConverter::toString((int)Posy)+" Z:"+Ogre::StringConverter::toString((int)Posz));

mDetailsPanel->setParamValue(9, "Angle:"+Ogre::StringConverter::toString(mAngle));

}
}

return ret;
}
void mitademo::mmaddTime(Real deltaTime)
{
updateBody(deltaTime);
updateAnimations(deltaTime);
updateCamera(deltaTime);
}
void mitademo::f18addTime(Real deltaTime)
{
f18AnimState->addTime(deltaTime);
MITA_Input_Move3D(f18Sound,mSceneMgr->getSceneNode("f18Node")->getPosition().x,mSceneMgr->getSceneNode("f18Node")->getPosition().y,mSceneMgr->getSceneNode("f18Node")->getPosition().z);

}
void mitademo::caraddTime(Real deltaTime)
{
carAnimState->addTime(deltaTime);
MITA_Input_Move3D(carSound,mSceneMgr->getSceneNode("carNode")->getPosition().x,mSceneMgr->getSceneNode("carNode")->getPosition().y,mSceneMgr->getSceneNode("carNode")->getPosition().z);

}
void mitademo::createFrameListener(void)
{
BaseApplication::createFrameListener();

mDirection = Ogre::Vector3::ZERO;//初始化
// Populate the camera container
mCamNode = mCamera->getParentSceneNode();

// set the rotation and move speed
mRotate = 0.13;
mMove = 250;

}

bool mitademo::keyPressed( const OIS::KeyEvent& evt )
{
if (evt.key == OIS::KC_E )
{
if (mTopAnimID == ANIM_IDLE_TOP || mTopAnimID == ANIM_RUN_TOP)
{
// start dancing
setBaseAnimation(ANIM_DANCE,true);

}
else if (mBaseAnimID == ANIM_DANCE)
{
// stop dancing
setBaseAnimation(ANIM_IDLE_BASE);
setTopAnimation(ANIM_IDLE_TOP);

}
}

// keep track of the player's intended direction
else if (evt.key == OIS::KC_W) mKeyDirection.z = -1;
else if (evt.key == OIS::KC_A) mKeyDirection.x = -1;
else if (evt.key == OIS::KC_S) mKeyDirection.z = 1;
else if (evt.key == OIS::KC_D) mKeyDirection.x = 1;

else if (evt.key == OIS::KC_SPACE && (mTopAnimID == ANIM_IDLE_TOP || mTopAnimID == ANIM_RUN_TOP))
{
// jump if on ground
setBaseAnimation(ANIM_JUMP_START, true);
setTopAnimation(ANIM_NONE);
mTimer = 0;
}else if (evt.key == OIS::KC_ESCAPE)
{
mShutDown = true;
}

if (!mKeyDirection.isZeroLength() && mBaseAnimID == ANIM_IDLE_BASE)
{
// start running if not already moving and the player wants to move
setBaseAnimation(ANIM_RUN_BASE, true);
if (mTopAnimID == ANIM_IDLE_TOP) setTopAnimation(ANIM_RUN_TOP, true);
}
return true;
}
bool mitademo::keyReleased( const OIS::KeyEvent& evt )
{
// keep track of the player's intended direction
if (evt.key == OIS::KC_W && mKeyDirection.z == -1) mKeyDirection.z = 0;
else if (evt.key == OIS::KC_A && mKeyDirection.x == -1) mKeyDirection.x = 0;
else if (evt.key == OIS::KC_S && mKeyDirection.z == 1) mKeyDirection.z = 0;
else if (evt.key == OIS::KC_D && mKeyDirection.x == 1) mKeyDirection.x = 0;

if (mKeyDirection.isZeroLength() && mBaseAnimID == ANIM_RUN_BASE)
{
// stop running if already moving and the player doesn't want to move
setBaseAnimation(ANIM_IDLE_BASE);
if (mTopAnimID == ANIM_RUN_TOP) setTopAnimation(ANIM_IDLE_TOP);
}
return true;
}
// OIS::MouseListener
bool mitademo::mouseMoved( const OIS::MouseEvent& evt )
{
// update camera goal based on mouse movement
updateCameraGoal(-0.05f * evt.state.X.rel, -0.05f * evt.state.Y.rel, -0.0005f * evt.state.Z.rel);
return true;
}
bool mitademo::mousePressed( const OIS::MouseEvent& evt, OIS::MouseButtonID id )
{

return true;
}
bool mitademo::mouseReleased( const OIS::MouseEvent& evt, OIS::MouseButtonID id )
{
return true;
}


#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
// Create application object
mitademo app;

try {
app.go();
} catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occured: " <<
e.getFullDescription().c_str() << std::endl;
#endif
}

return 0;
}

#ifdef __cplusplus
}
#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值