posts - 28,  comments - 15,  trackbacks - 0

对上次的代码做了一些修改今天传上来,代码中肯定有很多问题需要高人指点一二
1.可以运用00P和GP方法对代码进行压缩
2.对内存的利用更合理,更节省开销
.................
如果谁有时间修改,麻烦修改后回复,多谢

截图:




#pragma once
#include 
"exampleapplication.h"
#include 
<stdlib.h>

class ParticleSysListener : public ExampleFrameListener
{
public:
    ParticleSysListener(RenderWindow
* win,Camera* cam,AnimationState* anistate,AnimationState* anistate1,
        AnimationState
* bbsp_anistate,AnimationState* stateTrail,SceneNode* pNode,SceneNode* Node_chain) : ExampleFrameListener(win,cam)
    
{

        mAnimationSate
=anistate;
        mAnimationSate1
=anistate1;
        mNode
=pNode;
        bbsp_anistate1
=bbsp_anistate;
        mstatetrial
=stateTrail;
        node_chain 
= Node_chain;
    }

protected:
    
bool frameStarted(const FrameEvent& evt)
    
{
        
if (trFlag==0)
        
{
            mNode
->translate(Vector3(-3,-1.5,-3));
        }

        
else 
        
{
            
if(trFlag==3)
            
{
                mNode
->translate(Vector3(0,0,0));
            }


            
if (trFlag==6)
            
{
                mNode
->translate(Vector3(3,1.5,3));
            }


        }

        
//mNode->yaw(Radian(evt.timeSinceLastFrame*0.1));
        mAnimationSate->addTime(evt.timeSinceLastFrame);
        mAnimationSate1
->addTime(evt.timeSinceLastFrame*0.3);
        trFlag
++;
        
if (trFlag==8)
        
{
            trFlag
=0;
        }


        bbsp_anistate1
->addTime(evt.timeSinceLastFrame);
        mstatetrial
->addTime(evt.timeSinceLastFrame);

        node_chain
->roll(Radian(evt.timeSinceLastFrame*100));
        
return ExampleFrameListener::frameStarted(evt);
    }

private:
    AnimationState
* mAnimationSate;
    AnimationState
* mAnimationSate1;
    
//static SceneNode* mNode;
    SceneNode* mNode;
    
static UINT trFlag;
    AnimationState
* bbsp_anistate1;
    AnimationState
* mstatetrial;
    SceneNode
* node_chain;
}
;
//SceneNode* ParticleSysListener::mNode=NULL;
UINT ParticleSysListener::trFlag=FALSE;


class ParticleSysApplication : public ExampleApplication
{
public:
    ParticleSysApplication()
    
{
        mHeadAnimation
=NULL;
        
//mHeadAnimation1=NULL;
        stateTrail=NULL;
    }

    
~ParticleSysApplication()
    
{
        
//如果指针是new得到的,则一定要delete [] p;
    }

protected:
    
void createScene(void)
    
{
        mRootSceneNode 
= mSceneMgr->getRootSceneNode();

        mHeadNode 
= mRootSceneNode->createChildSceneNode("RobotHeadNode");

        mRollHeadNode 
= mRootSceneNode->createChildSceneNode("RollParentNode");

        mRollLeftNode 
= mRollHeadNode->createChildSceneNode("RollLeftNode");

        mRollRightNode 
= mRollHeadNode->createChildSceneNode("RollRightNode");


        mSceneMgr
->setAmbientLight(ColourValue(0.2,0.2,0.2,1.0));//仅影响环境的亮度

        
//机器人以及绑定在机器人上的灯光
            
//实体
        Entity* ent=mSceneMgr->createEntity("Rotbot","robot.mesh");
        
//Entity* ent= static_cast<Entity*> (mSceneMgr->createMovableObject("Robot","Entity"));//这种定义方式无效
        ent->setMaterialName("Examples/Robot");//是否添加材质不影响效果,因为robot.skeleton
        
//mRollHeadNode->setDirection(0,0,1);
        mHeadNode->setOrientation(0.86,0,-0.5,0); //Q = (c,s XA,s YA,s ZA)。其中s = sin(θ/2),c = cos(θ/2)。
        
//mRollHeadNode->setScale(0.5,0.5,0.5);
        mHeadNode->setPosition(0,-50,0);
        mHeadNode
->attachObject(ent);

        
/*
            //灯光,为什么聚光灯失效
        Light* lent=mSceneMgr->createLight("ent_light");
        lent->setType(Light::LT_SPOTLIGHT);
        lent->setPosition(0,100,0);
        lent->setDirection(0,-1,0);
        lent->setDiffuseColour(0.0,1.0,0.0);
        lent->setSpotlightRange(Radian(Degree(10.0)),Radian(Degree(30.0)),2.0);
        mHeadNode->attachObject(lent);
        
*/



        
//整体的灯灯光
        Light* l=mSceneMgr->createLight("SpotLight");
        l
->setType(Light::LT_DIRECTIONAL);  //方向性光源,setPosition无效
        Vector3 dir(-1,-1,0);
        dir.normalise();
//当Vector表示向量时一定要注意记得归一化
        l->setDirection(Vector3(-1,-1,0));   //参数是向量,不是点坐标,所以(0,0,0)无效
        l->setDiffuseColour(0.4,0.4,0.8);
        
        
//骨骼动画
        
//mHeadAnimation=mSceneMgr->createAnimation();
        Animation::setDefaultInterpolationMode(Animation::IM_SPLINE);//帧间样条插值,脚步移动平缓
        mHeadAnistate=ent->getAnimationState("Walk");
        mHeadAnistate1
=ent->getAnimationState("Shoot");
        mHeadAnistate
->setEnabled(TRUE);
        mHeadAnistate1
->setEnabled(TRUE);

        
//粒子系统

        
//Green steam on Robot'head
        ParticleSystem* PsRobotHead=mSceneMgr->createParticleSystem("Nimbus","Examples/GreenyNimbus");
        
//mHeadNode->createChildSceneNode()->attachObject(RobotHead);
        mHeadNode->createChildSceneNode(Vector3(0,40,0))->attachObject(PsRobotHead);

        
//手动创建地面
        Plane p;
        p.normal
=Vector3::UNIT_Y;
        p.d
=50;
        
//MovablePlane p(Vector3(0,1,0),Vector3(0,0,0));
        MeshManager::getSingleton().createPlane(
            
"FloorPlane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
            p, 
2000002000002020true15050, Vector3::UNIT_X);
        Entity
* ent1=mSceneMgr->createEntity("floor","FloorPlane");
        ent1
->setMaterialName("Examples/RustySteel");
        
//mRollHeadNode->
        mRollHeadNode->attachObject(ent1);
        
//天空
        mSceneMgr->setSkyDome(true"Examples/SpaceSkyPlane"58);
        
        
//公告板 星星
        BillboardSet* bbs0=mSceneMgr->createBillboardSet("star",1);//Billboard的位置由SceneNode决定
        bbs0->setMaterialName("Examples/praticlesys_bbs0");
        Billboard
* bb0=bbs0->createBillboard(Vector3(-80,200,-1800));
        
//bb0=bbs0->createBillboard(Vector3(0,200,-1800));
        SceneNode* bbs0Node=mSceneMgr->getRootSceneNode()->createChildSceneNode("bbs0Node");
        bbs0Node
->attachObject(bbs0);

        
//公告板 精灵
        BillboardSet* bbs1=mSceneMgr->createBillboardSet("sprite",3);
        bbs1
->setMaterialName("Examples/FlarePointSprite");

        Billboard
* bb_sp_00=bbs1->createBillboard(0,0,0,ColourValue(1.0,0.0,0.0,1.0));
        Billboard
* bb_sp_01=bbs1->createBillboard(-20,0,0,ColourValue(0.0,1.0,0.0,1.0));
        Billboard
* bb_sp_02=bbs1->createBillboard(-40,0,0,ColourValue(0.0,1.0,0.0,1.0));

        
//bb_sp_00->setColour()
        bb_sp_00->setDimensions(30.0,30.0);
        bb_sp_01
->setDimensions(20.0,20.0);
        bb_sp_02
->setDimensions(10.0,10.0);


        
//每个精灵上都绑定一个Light
        Light* lsp[3];

        lsp[
0]=mSceneMgr->createLight("lsp_0");
        lsp[
1]=mSceneMgr->createLight("lsp_1");
        lsp[
2]=mSceneMgr->createLight("lsp_2");

        lsp[
0]->setType(Light::LT_POINT);
        lsp[
1]->setType(Light::LT_POINT);
        lsp[
2]->setType(Light::LT_POINT);

        
//光照的影响与材质的反光系数有关
        lsp[0]->setSpecularColour(ColourValue(0.0,1.0,0.0));
        lsp[
0]->setDiffuseColour(ColourValue(0.0,1.0,0.0));
        lsp[
1]->setSpecularColour(ColourValue(0.0,1.0,0.0));
        lsp[
1]->setDiffuseColour(ColourValue(0.0,1.0,0.0));
        lsp[
2]->setSpecularColour(ColourValue(0.0,1.0,0.0));
        lsp[
2]->setDiffuseColour(ColourValue(0.0,1.0,0.0));

        lsp[
0]->setPosition(Vector3(0,0,0));
        lsp[
1]->setPosition(Vector3(-80,0,0));
        lsp[
2]->setPosition(Vector3(-160,0,0));


        SceneNode
* bbs_sp_node=mSceneMgr->getRootSceneNode()->createChildSceneNode();

        bbs_sp_node
->attachObject(bbs1);
        bbs_sp_node
->attachObject(lsp[0]);
        bbs_sp_node
->attachObject(lsp[1]);
        bbs_sp_node
->attachObject(lsp[2]);
    

        
/*
        //公告板集合 巴格达星星群
        bbs0=mSceneMgr->createBillboardSet("bagedastar",1);//Billboard的位置由SceneNode决定
        bbs0->setMaterialName("Examples/praticlesys_bageda");
        Billboard* bb_bageda_01=bbs0->createBillboard(Vector3(0,600,-100));
        //bb0=bbs0->createBillboard(Vector3(0,200,-1800));
        //SceneNode* bbs0Node=mSceneMgr->getRootSceneNode()->createChildSceneNode("bbs0Node");
        bbs0Node->attachObject(bbs0);
        
*/


        
//关于精灵的节点动画
        Animation* ami_bbsp=mSceneMgr->createAnimation("anibbsp",32);//总共32秒钟
        Animation::setDefaultInterpolationMode(Animation::IM_SPLINE);
        
//ami_bbsp->setInterpolationMode(Animation::IM_SPLINE);
        NodeAnimationTrack* track=ami_bbsp->createNodeTrack(0,bbs_sp_node);

        TransformKeyFrame
* tk=track->createNodeKeyFrame(0);
        tk
->setTranslate(Vector3(-500,-35,-200));//这里点坐标应该是世界坐标系里的吧?

        tk
=track->createNodeKeyFrame(2);
        
//tk->setTranslate(Vector3(rand(),-35,200));//ogre中怎样随机取值
        tk->setTranslate(Vector3(-300,-35,-100));

        tk
=track->createNodeKeyFrame(4);
        tk
->setTranslate(Vector3(0,-35,0));

        tk
=track->createNodeKeyFrame(6);
        tk
->setTranslate(Vector3(0,-35,0));

        tk
=track->createNodeKeyFrame(8);
        tk
->setTranslate(Vector3(100,-35,100));

        tk
=track->createNodeKeyFrame(10);
        tk
->setTranslate(Vector3(50,-35,150));

        tk
=track->createNodeKeyFrame(12);
        tk
->setTranslate(Vector3(-100,-35,150));

        tk
=track->createNodeKeyFrame(13);
        tk
->setTranslate(Vector3(0,-35,200));

        tk
=track->createNodeKeyFrame(14);
        tk
->setTranslate(Vector3(100,-35,200));

        tk
=track->createNodeKeyFrame(15);
        tk
->setTranslate(Vector3(200,-35,200));

        tk
=track->createNodeKeyFrame(16);
        tk
->setTranslate(Vector3(300,-35,200));

        tk
=track->createNodeKeyFrame(17);
        tk
->setTranslate(Vector3(500,-35,200));

        tk
=track->createNodeKeyFrame(18);
        tk
->setTranslate(Vector3(200,-35,300));

        tk
=track->createNodeKeyFrame(19);
        tk
->setTranslate(Vector3(100,-35,300));

        tk
=track->createNodeKeyFrame(20);
        tk
->setTranslate(Vector3(50,-35,300));

        tk
=track->createNodeKeyFrame(22);
        tk
->setTranslate(Vector3(0,-35,300));

        tk
=track->createNodeKeyFrame(24);
        tk
->setTranslate(Vector3(-50,-35,300));

        tk
=track->createNodeKeyFrame(26);
        tk
->setTranslate(Vector3(-100,-35,300));

        tk
=track->createNodeKeyFrame(28);
        tk
->setTranslate(Vector3(-200,-35,300));

        tk
=track->createNodeKeyFrame(30);
        tk
->setTranslate(Vector3(-500,-35,300));

        tk
=track->createNodeKeyFrame(31);
        tk
->setTranslate(Vector3(-500,-35,100));

        tk
=track->createNodeKeyFrame(32);
        tk
->setTranslate(Vector3(-500,-35,-200));

        bbsp_anistate
=mSceneMgr->createAnimationState("anibbsp");
        bbsp_anistate
->setEnabled(1);

        
/*    Ogre::RibbonTrail轨迹条(带状轨迹),: pubilic Ogre::BillboardChain : public MovableObject
            我们通常通过公告板链来创建诸如闪电、电波、速度线和轨迹这种需要“跟随前面的运动”而不是独立的公告板集合产生的特效。
            在Ogre中我们提供了一个现成的带状轨迹类型,用以实现追赶一个场景节点的公告板链,
            就如同场景节点在空间中划出一道光亮的轨迹一般的效果
        
*/


        
//条状轨迹初始化
        
//RibbonTrail* trial=mSceneMgr->createRibbonTrail()
        NameValuePairList paralist;
        paralist[
"numberOfChains"]="1";        //2个RibbonTrail,默认为1,写成0的时候怎样?第一条轨迹的索引为0
        paralist["maxElements"]="100";        //每一个RibbonTrail含有100个Billboard
        RibbonTrail* trial = static_cast<RibbonTrail*>(
            mSceneMgr
->createMovableObject("RibbonTrail_01","RibbonTrail",&paralist));//创建RibbonTrail的一种方式
        trial->setMaterialName("Examples/LightRibbonTrail/particleSys");//在这个脚本中改变轨迹条的形状
        trial->setTrailLength(300);//轨迹条中每个元素的长度为3(300/"maxElements");
        SceneNode* Node_Trail=mSceneMgr->getRootSceneNode()->createChildSceneNode(Vector3(0,200,0));
        Node_Trail
->attachObject(trial);//挂接的这个Node到底是什么作用,难道这里trail是一个相当于BillboardSet的东西(容器)?

        
//初始化轨迹条
        
//颜色递减式
        trial->setInitialColour(0,ColourValue(1.0,1.0,1.0,1.0));
        trial
->setColourChange(0,ColourValue(0.5,0.2,0.1,0.05));
        
//宽度递减式
        trial->setInitialWidth(0,5);
        trial
->setWidthChange(0,1);

        

        SceneNode
* node_trial_tral=mSceneMgr->getRootSceneNode()->createChildSceneNode();
        Animation
* aniTrail=mSceneMgr->createAnimation("Animation_trail",12);//节点动画的名称和运行的时间
        Animation::setDefaultInterpolationMode(Animation::IM_SPLINE);
        
//或者
        
//aniTrail->setInterpolationMode(Animation::IM_SPLINE);
        
//NodeAnimationTrack* trialtrack=aniTrail->createNodeTrack(0,Node_Trail);
        NodeAnimationTrack* trialtrack=aniTrail->createNodeTrack(0,node_trial_tral);
        TransformKeyFrame
* tf=trialtrack->createNodeKeyFrame(0);
        tf
->setTranslate(Vector3(0,0,0));
        tf
=trialtrack->createNodeKeyFrame(2);
        tf
->setTranslate(Vector3(0,100,0));
        tf
=trialtrack->createNodeKeyFrame(4);
        tf
->setTranslate(Vector3(-100,0,0));
        tf
=trialtrack->createNodeKeyFrame(6);
        tf
->setTranslate(Vector3(0,-100,0));
        tf
=trialtrack->createNodeKeyFrame(8);
        tf
->setTranslate(Vector3(100,0,0));
        tf
=trialtrack->createNodeKeyFrame(10);
        tf
->setTranslate(Vector3(0,100,0));
        tf
=trialtrack->createNodeKeyFrame(12);
        tf
->setTranslate(Vector3(0,0,0));

        
//动画状态
        stateTrail=mSceneMgr->createAnimationState("Animation_trail");
        stateTrail
->setEnabled(TRUE);

        
//trial->addNode(Node_Trail);
        trial->addNode(node_trial_tral);
        
//为轨迹条头部装上"导航点"
        BillboardSet* trialSet=mSceneMgr->createBillboardSet("trialSet",1);
        Billboard
* bbtrial_01=trialSet->createBillboard(Vector3(0,0,0));
        bbtrial_01
->setDimensions(60,60);
        
//trialSet->setMaterialName("Examples/trialSet");//在Billboard中可以放入任何东西,任何形状,但最终都是方形的
        trialSet->setMaterialName("Examples/praticlesys_bbs0");    //添加星星
        node_trial_tral->attachObject(trialSet);

        
//BillboardChain公告板链
        BillboardChain* chain=mSceneMgr->createBillboardChain("1chain");//不含具体的数据如:长度,chain条数,等信息
        
//chain->setMaterialName("Examples/LightRibbonTrail");//材质是闪电
        chain->setNumberOfChains(4);//一个Chain索引号为0
        chain->setTextureCoordDirection(BillboardChain::TCD_U);//从U方向上分割纹理

        
//存储Element的Vector
        BillboardChain::ElementList melementList;
        
for (int i=0;i<20;i++)
        
{
            melementList.push_back(BillboardChain::Element(Vector3(i
*10,i*10,i*10),5,0.05*i,
                                    ColourValue((Real)rand()
/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX)));
        }

        
//解析Vector,并添加Element到chain中,1chain_0
        BillboardChain::ElementList::iterator pelement;
        
for (pelement=melementList.begin();pelement !=melementList.end();++pelement)
        
{
            
//第一个参数为chain的索引号,即第几个chain;第二个参数为元素
            chain->addChainElement(0,*pelement);
        }



        Node_chain
=mSceneMgr->getRootSceneNode()->createChildSceneNode("Node_chain",Vector3(0.0,0.0,-100));
        Node_chain
->attachObject(chain);

        
{
            chain
->updateChainElement(0,10,BillboardChain::Element(Vector3(100,100,100),5,0.5,ColourValue(0.0,0.0,0.0,1.0)));
            chain
->updateChainElement(0,11,BillboardChain::Element(Vector3(110,110,110),5,0.55,ColourValue(0.0,0.0,0.0,1.0)));
        }


        Node_chain
->scale(0.5,0.5,0.5);

        
//1chain_1
        
//存储Element的Vector
        BillboardChain::ElementList melementList1;
        
for (int i=0;i<20;i++)
        
{
            melementList1.push_back(BillboardChain::Element(Vector3(i
*(-10),i*10,i*10),5,0.05*i,
                ColourValue((Real)rand()
/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX)));
        }

        
//解析Vector,并添加Element到chain中,1chain_0
        BillboardChain::ElementList::iterator pelement1;
        
for (pelement1=melementList1.begin();pelement1 !=melementList1.end();++pelement1)
        
{
            
//第一个参数为chain的索引号,即第几个chain;第二个参数为元素
            chain->addChainElement(1,*pelement1);
        }

        
        
//1chain_2
        
//存储Element的Vector
        BillboardChain::ElementList melementList2;
        
for (int i=0;i<20;i++)
        
{
            melementList2.push_back(BillboardChain::Element(Vector3(i
*(-10),i*(-10),i*10),5,0.05*i,
                ColourValue((Real)rand()
/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX)));
        }

        
//解析Vector,并添加Element到chain中,1chain_0
        BillboardChain::ElementList::iterator pelement2;
        
for (pelement2=melementList2.begin();pelement2 !=melementList2.end();++pelement2)
        
{
            
//第一个参数为chain的索引号,即第几个chain;第二个参数为元素
            chain->addChainElement(2,*pelement2);
        }


        
//1chain_3
        
//存储Element的Vector
        BillboardChain::ElementList melementList3;
        
//melementList2.clear();
        for (int i=0;i<20;i++)
        
{
            melementList3.push_back(BillboardChain::Element(Vector3(i
*10,i*(-10),i*10),5,0.05*i,
                ColourValue((Real)rand()
/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX,(Real)rand()/(Real)RAND_MAX)));
        }

        
//解析Vector,并添加Element到chain中,1chain_0
        BillboardChain::ElementList::iterator pelement3;
        
for (pelement3=melementList3.begin();pelement3 !=melementList3.end();++pelement3)
        
{
            
//第一个参数为chain的索引号,即第几个chain;第二个参数为元素
            chain->addChainElement(3,*pelement3);
        }



        
    }


    
void createFrameListener()
    
{
        mFrameListener
=OGRE_NEW ParticleSysListener(mWindow,mCamera,mHeadAnistate,mHeadAnistate1,bbsp_anistate,
                                                    stateTrail,mRollHeadNode,Node_chain);
        
//mFrameListener->showDebugOverlay(true);
        mRoot->addFrameListener(mFrameListener);
    }



private:
    SceneNode
* mRootSceneNode;
    SceneNode
* mHeadNode;
    SceneNode
* mRollHeadNode;
    SceneNode
* mRollLeftNode;
    SceneNode
* mRollRightNode;
    Animation
* mHeadAnimation;
    AnimationState
* mHeadAnistate;
    AnimationState
* mHeadAnistate1;
    
//SceneNode* mNode;
    AnimationState* bbsp_anistate;
    AnimationState
* stateTrail;
    SceneNode
* Node_chain;
}
;
posted on 2009-06-06 13:42 闫军yy 阅读(1360) 评论(2)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理