本節(jié)主要學(xué)習(xí)如何使用Ogre2.3加載粒子效果。為了學(xué)習(xí)方便,直接將官方粒子模塊Sample_ParticleFX單獨(dú)拿出來(lái)編譯,學(xué)習(xí)如何實(shí)現(xiàn)粒子效果。
一. 前提須知
- 如果參考官方示例建議用最新版的Ogre 2.3.1。否則找不到有粒子效果的示例。不要用官網(wǎng)Ogre2.3 script腳本編譯源碼!
-
Ogre 1.x中關(guān)于實(shí)現(xiàn)粒子效果的官方示例材質(zhì)太老,不支持固管,要寫(xiě)shader,不建議在Ogre 2.3中使用。要使用新的材質(zhì)!
Ogre 1.x中的材質(zhì):
material Examples/Flare
{
technique
{
pass
{
lighting off
scene_blend add
depth_write off
diffuse vertexcolour
texture_unit
{
texture flare.png
}
}
}
}
Ogre 2.3中的材質(zhì):
hlms Examples/Flare2 unlit
{
depth_write off
cull_hardware none
scene_blend add
diffuse_map flaretrail.png
}
二. 新建項(xiàng)目工程的頭文件和庫(kù)文件配置如下:
注意:你需要把Media文件夾和bin/Debug文件夾下的資源拷貝過(guò)來(lái),具體拷貝到哪里大家應(yīng)該很了解。
這里說(shuō)下頭文件和庫(kù)文件的設(shè)置。
頭文件:庫(kù)目錄:
注意:這里改成窗口
三. 粒子效果總結(jié)
先展示下官網(wǎng)給的12個(gè)粒子效果,具體代碼放到最后。
- 煙花效果,粒子Examples/Fireworks,材質(zhì)官方?jīng)]有給出,自己嘗試模仿寫(xiě)了一個(gè),添加到Particles.material文件中。
Particles.material:
hlms Examples/Flare unlit
{
depth_write off
cull_hardware none
scene_blend add
diffuse_map flare.png
}
2. 向上漂浮的綠色顆粒,粒子Examples/GreenyNimbus。材質(zhì)自己為自己添加。
Particles.material:
hlms Examples/FlarePointSprite unlit
{
depth_write off
cull_hardware none
scene_blend add
diffuse_map flare.png
}
3. 閃閃發(fā)光的紫色噴泉。粒子Examples/PurpleFountain。材質(zhì)為官方自帶。
4. 雨,粒子Examples/Rain。材質(zhì)自己實(shí)現(xiàn)。
材質(zhì)Particles.material:
hlms Examples/Droplet unlit
{
depth_write off
cull_hardware none
scene_blend add
diffuse_map basic_droplet.png
}
5. 飛機(jī)噴氣尾焰,粒子Examples/JetEngine1。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
6. 飛機(jī)噴氣尾焰2,粒子Examples/JetEngine2。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
7. 向上漂浮的光環(huán),類似龍卷風(fēng)。粒子 Examples/Aureola。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
8. 四處游動(dòng)的類似火花。粒子Examples/Swarm。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
9.漫天雪花,粒子Examples/Snow。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
10. 類似煙花。粒子Examples/Animated/Test1。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
11. 大雨滴,粒子Examples/Water/Rain。粒子和材質(zhì)代碼詳見(jiàn)官方示例。
12. 煙霧。粒子Examples/Smoke。粒子代碼見(jiàn)官方示例。材質(zhì)代碼自己寫(xiě)。如下:
材質(zhì)Particles.material:
hlms Examples/Smoke unlit
{
depth_write off
cull_hardware none
scene_blend add
diffuse_map Smoke15Frames.png
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-492590.html
四. 代碼
具體代碼參考官方模塊Sample_ParticleFX,這里為了單獨(dú)測(cè)試不同粒子效果,把少數(shù)代碼注釋了。
ParticleFXGameState.cpp文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-492590.html
#include "ParticleFXGameState.h"
#include "CameraController.h"
#include "GraphicsSystem.h"
#include "OgreCamera.h"
#include "OgreHlmsManager.h"
#include "OgreHlmsPbs.h"
#include "OgreHlmsPbsDatablock.h"
#include "OgreHlmsSamplerblock.h"
#include "OgreItem.h"
#include "OgreMesh2.h"
#include "OgreMeshManager.h"
#include "OgreMeshManager2.h"
#include "OgreRoot.h"
#include "OgreSceneManager.h"
#include "OgreTextureFilters.h"
#include "OgreTextureGpuManager.h"
#include "OgreWindow.h"
#include "OgreParticleSystem.h"
//
#include <iostream>
using namespace Demo;
namespace Demo
{
ParticleFXGameState::ParticleFXGameState(const Ogre::String& helpDescription) :
TutorialGameState(helpDescription),
mTime(0.0f)
{
}
//-----------------------------------------------------------------------------------
void ParticleFXGameState::createScene01()
{
Ogre::SceneManager* sceneManager = mGraphicsSystem->getSceneManager();
Ogre::v1::MeshPtr planeMeshV1 = Ogre::v1::MeshManager::getSingleton().createPlane(
"Plane v1", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Ogre::Plane(Ogre::Vector3::UNIT_Y, 1.0f), 50.0f, 50.0f, 1, 1, true, 1, 4.0f, 4.0f,
Ogre::Vector3::UNIT_Z, Ogre::v1::HardwareBuffer::HBU_STATIC,
Ogre::v1::HardwareBuffer::HBU_STATIC);
Ogre::MeshPtr planeMesh = Ogre::MeshManager::getSingleton().createByImportingV1(
"Plane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, planeMeshV1.get(), true,
true, true);
{
Ogre::Item* item = sceneManager->createItem(planeMesh, Ogre::SCENE_DYNAMIC);
item->setDatablock("Marble");
Ogre::SceneNode* sceneNode = sceneManager->getRootSceneNode(Ogre::SCENE_DYNAMIC)
->createChildSceneNode(Ogre::SCENE_DYNAMIC);
sceneNode->setPosition(0, -1, 0);
sceneNode->attachObject(item);
// Change the addressing mode of the roughness map to wrap via code.
// Detail maps default to wrap, but the rest to clamp.
assert(dynamic_cast<Ogre::HlmsPbsDatablock*>(item->getSubItem(0)->getDatablock()));
Ogre::HlmsPbsDatablock* datablock =
static_cast<Ogre::HlmsPbsDatablock*>(item->getSubItem(0)->getDatablock());
// Make a hard copy of the sampler block
Ogre::HlmsSamplerblock samplerblock(*datablock->getSamplerblock(Ogre::PBSM_ROUGHNESS));
samplerblock.mU = Ogre::TAM_WRAP;
samplerblock.mV = Ogre::TAM_WRAP;
samplerblock.mW = Ogre::TAM_WRAP;
// Set the new samplerblock. The Hlms system will
// automatically create the API block if necessary
datablock->setSamplerblock(Ogre::PBSM_ROUGHNESS, samplerblock);
}
Ogre::SceneNode* rootNode = sceneManager->getRootSceneNode();
Ogre::Light* light = sceneManager->createLight();
Ogre::SceneNode* lightNode = rootNode->createChildSceneNode();
lightNode->attachObject(light);
light->setPowerScale(Ogre::Math::PI);
light->setType(Ogre::Light::LT_DIRECTIONAL);
light->setDirection(Ogre::Vector3(-1, -1, -1).normalisedCopy());
sceneManager->setAmbientLight(Ogre::ColourValue(0.3f, 0.5f, 0.7f) * 0.1f * 0.75f,
Ogre::ColourValue(0.6f, 0.45f, 0.3f) * 0.065f * 0.75f,
-light->getDirection() + Ogre::Vector3::UNIT_Y * 0.2f);
/* Ogre::ParticleSystem* pSystem0 = sceneManager->createParticleSystem("Examples/PurpleFountain");
Ogre::ParticleSystem* pSystem1 = sceneManager->createParticleSystem("Examples/Aureola");
Ogre::ParticleSystem* pSystem2 = sceneManager->createParticleSystem("Examples/Animated/Test1");
Ogre::SceneNode* sceneNode = rootNode->createChildSceneNode();
sceneNode->attachObject(pSystem0);
sceneNode = rootNode->createChildSceneNode();
sceneNode->attachObject(pSystem1);
sceneNode = rootNode->createChildSceneNode();
sceneNode->setPosition(Ogre::Vector3(10, 0, 0));
sceneNode->attachObject(pSystem2);*/
Ogre::ParticleSystem* pSystem0 = sceneManager->createParticleSystem("Examples/Smoke");
Ogre::SceneNode* sceneNode = rootNode->createChildSceneNode();
sceneNode->attachObject(pSystem0);
//參數(shù)獲取
unsigned short numAffectors = pSystem0->getNumAffectors();
Ogre::ParticleAffector* particleAffector = pSystem0->getAffector(0);
/* for (int i = 0; i < list.size(); i++) {
Ogre::ParameterDef parameterDef = list[i];
std::cout << parameterDef.description() << std::endl;
}*/
/*size_t s = pSystem0->getParticleQuota();
Ogre::ParticleAffector * particleAffector = pSystem0->getAffector(0);
bool cull = pSystem0->getCullIndividually();
bool castShadows = pSystem0->getCastShadows();
Ogre::Real height = pSystem0->getDefaultHeight();
pSystem0->getDefaultLightMask();
Ogre::Real nonVisibleUpdateTimeout = pSystem0->getDefaultNonVisibleUpdateTimeout();
pSystem0->getDefaultVisibilityFlags();*/
/* Ogre::Item* particleSystem3_item = sceneManager->createItem(planeMesh, Ogre::SCENE_DYNAMIC);
particleSystem3_item->setDatablock("Marble");
mParticleSystem3_RootSceneNode = rootNode->createChildSceneNode();
sceneNode = mParticleSystem3_RootSceneNode->createChildSceneNode();
sceneNode->setScale(Ogre::Vector3(0.2f));
sceneNode->attachObject(particleSystem3_item);
Ogre::ParticleSystem* pSystem3 = sceneManager->createParticleSystem("Examples/Animated/Test1");
mParticleSystem3_RootSceneNode->attachObject(pSystem3);
mParticleSystem3_EmmitterSceneNode = mParticleSystem3_RootSceneNode->createChildSceneNode();
pSystem3->setParticleEmitterRootNode(mParticleSystem3_EmmitterSceneNode);
Ogre::Item* particleSystem3_emmiter_item =
sceneManager->createItem(planeMesh, Ogre::SCENE_DYNAMIC);
particleSystem3_emmiter_item->setDatablock("Marble");
mParticleSystem3_EmmitterSceneNode->setScale(Ogre::Vector3(0.01f));
mParticleSystem3_EmmitterSceneNode->attachObject(particleSystem3_emmiter_item);*/
mGraphicsSystem->getCamera()->setPosition(Ogre::Vector3(0.0f, 40.0f, 120.0f));
mCameraController = new CameraController(mGraphicsSystem, false);
TutorialGameState::createScene01();
}
void ParticleFXGameState::update(float timeSinceLast)
{
mTime += timeSinceLast;
if (mTime > 10.0f)
mTime = 0.0f;
/* mParticleSystem3_RootSceneNode->setPosition(
Ogre::Vector3(-50.0f * mTime / 10.0f + 25.0f, 2, -50));
mParticleSystem3_EmmitterSceneNode->setPosition(
Ogre::Vector3(20.0f * mTime / 10.0f - 10.f, 0.5, 20.0f * mTime / 10.0f - 10));*/
TutorialGameState::update(timeSinceLast);
}
} // namespace Demo
到了這里,關(guān)于第五節(jié) 利用Ogre 2.3實(shí)現(xiàn)雨,雪,爆炸,飛機(jī)噴氣尾焰等粒子效果的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!