序:
当初看到dnf 中有些 拖影的效果,感觉很炫,所以到后面自己做游戏的时候自然的就想到了 那种炫 的拖尾效果~, 于是 我就想 要将效果 的实现写下来,以后 自己做游戏 也可以 用的了啊~~~~, 希望 对大家 有 的用处 ,哈哈~
正文:
先来一张效果图~。
就是 蓝色 的 影子了,直观的看就是几张尾巴的图片坐标不同,透明度不一样,可能还有旋转的角度,是否翻转了。
一般的做法,可能会想到多建立几个精灵对象,然后跟着目标走,每一个精灵逐渐改变透明度等等,没错这是直接的 实现,虽然可以 达到目的,但是仔细想想 会很麻烦,而且不利于通用到每一个 游戏中。
下面说说实现的另一种方法~~
gl贴图,用4个点表示然后映射纹理坐标,想要几个尾巴就 对应的多建立几个顶点 了。每一组顶点要对应的改变属性,透明度这里用透明通道改变,翻转就是交换左右或者上下顶点的位置,旋转只要跟随目标的 旋转度就行了,基本上计算就不复杂了。
下面就直接贴出完整代码.(3.x版本,2.x需要适当做些修改)
//SmariShadow.h
#ifndef __SMRITISHADOW_H__
#define __SMRITISHADOW_H__
#include "cocos2d.h"
#include "global.h"
USING_NS_CC;
class SmritiShadow:public Node
{
public:
~SmritiShadow();
voID draw(Renderer *renderer,const Mat4 &transform,uint32_t flags);
CREATE_FUNC(SmritiShadow)
bool init();
voID onDraw();
//设置跟随目标,spr就是跟随的对象,tex尾巴的纹理,如果为null,那么就给出第三个参数精灵帧。
voID setTarget(Sprite* spr,Texture2D* tex,SpriteFrame *sf=0);
//因为用到了自定义shader,所以这里写个重置shader程序的函数,以便在程序切换前台的时候恢复。
static voID reset();
static GLProgram* pg;
Sprite* target;
Texture2D *texture2d;
Size srcsz;
CustomCommand _customCommand;
Vertex *Vertices;
long pretime;
int arrcount;
gluint vertexBuffer;
gluint indexBuffer;
};
#endif
//SmariShadow.cpp
#include "SmritiShadow.h"
GLProgram* SmritiShadow::pg=0;
voID SmritiShadow::draw(Renderer *renderer,uint32_t flags)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(SmritiShadow::onDraw,this);
renderer->addCommand(&_customCommand);
}
voID SmritiShadow::onDraw()
{
struct timeval tv;
gettimeofday(&tv,NulL);
long ct=tv.tv_sec*1000000+tv.tv_usec;
//如果想改变尾巴的长度,请改写下面的50000~
if(ct-pretime>50000)
{
pretime=ct;
arrcount=0;
for(int i=0;i<20;i+=4)
{
Vertices[i].position[0]=Vertices[i+4].position[0];
Vertices[i].position[1]=Vertices[i+4].position[1];
Vertices[i+1].position[0]=Vertices[i+5].position[0];
Vertices[i+1].position[1]=Vertices[i+5].position[1];
Vertices[i+2].position[0]=Vertices[i+6].position[0];
Vertices[i+2].position[1]=Vertices[i+6].position[1];
Vertices[i+3].position[0]=Vertices[i+7].position[0];
Vertices[i+3].position[1]=Vertices[i+7].position[1];
}
Size sz=srcsz;
Vec2 pos=target->getposition();
Vec2 mIDp=pos;
Size sz1=target->getContentSize();
bool bfx=target->isFlippedX();
bool bfy=target->isFlippedY();
Vec2 ap=target->getAnchorPoint();
float rad=CC_degrees_TO_radians(target->getRotation());
pos.x-=sz1.wIDth*ap.x;
pos.y-=sz1.height*ap.y;
float s=sin(-rad);
float c=cos(-rad);
Vertices[20].position[0]=c*pos.x-s*pos.y+(1-c)*mIDp.x+s*mIDp.y;
Vertices[20].position[1]=s*pos.x+c*pos.y-s*mIDp.x+(1-c)*mIDp.y;
Vertices[21].position[0]=c*(pos.x+sz.wIDth)-s*pos.y+(1-c)*mIDp.x+s*mIDp.y;
Vertices[21].position[1]=s*(pos.x+sz.wIDth)+c*pos.y-s*mIDp.x+(1-c)*mIDp.y;
Vertices[22].position[0]=c*pos.x-s*(pos.y+sz.height)+(1-c)*mIDp.x+s*mIDp.y;
Vertices[22].position[1]=s*pos.x+c*(pos.y+sz.height)-s*mIDp.x+(1-c)*mIDp.y;
Vertices[23].position[0]=c*(pos.x+sz.wIDth)-s*(pos.y+sz.height)+(1-c)*mIDp.x+s*mIDp.y;
Vertices[23].position[1]=s*(pos.x+sz.wIDth)+c*(pos.y+sz.height)-s*mIDp.x+(1-c)*mIDp.y;
float tempx=0,tempy=0;
if(bfx)
{
tempx=Vertices[20].position[0];
tempy=Vertices[20].position[1];
Vertices[20].position[0]=Vertices[21].position[0];
Vertices[20].position[1]=Vertices[21].position[1];
Vertices[21].position[0]=tempx;
Vertices[21].position[1]=tempy;
tempx=Vertices[22].position[0];
tempy=Vertices[22].position[1];
Vertices[22].position[0]=Vertices[23].position[0];
Vertices[22].position[1]=Vertices[23].position[1];
Vertices[23].position[0]=tempx;
Vertices[23].position[1]=tempy;
}
if(bfy)
{
tempx=Vertices[20].position[0];
tempy=Vertices[20].position[1];
Vertices[20].position[0]=Vertices[22].position[0];
Vertices[20].position[1]=Vertices[22].position[1];
Vertices[22].position[0]=tempx;
Vertices[22].position[1]=tempy;
tempx=Vertices[21].position[0];
tempy=Vertices[21].position[1];
Vertices[21].position[0]=Vertices[23].position[0];
Vertices[21].position[1]=Vertices[23].position[1];
Vertices[23].position[0]=tempx;
Vertices[23].position[1]=tempy;
}
for(int i=16;i>=0;i-=4)
{
if(Vertices[i].position[0]==Vertices[20].position[0])
{
arrcount+=1;
}
}
}
glEnable(GL_BLEND);
BlendFunc bf=BlendFunc::Alpha_NON_PREMulTIPLIED;
glBlendFunc(GL_ONE,GL_ONE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
pg->use();
pg->setUniformsForBuiltins();
glushort iarr[]={0,1,2,3,
0+4,1+4,2+4,3+4,
0+8,1+8,2+8,3+8,
0+12,1+12,2+12,3+12,
0+16,1+16,2+16,3+16};
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(Vertex)*20,Vertices,GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(iarr),iarr,GL_STATIC_DRAW);
Glint _positionLocation = glGetAttribLocation(pg->getProgram(),"a_position");
Glint _colorLocation = glGetAttribLocation(pg->getProgram(),"a_color");
Glint _textureLocation = glGetAttribLocation(pg->getProgram(),"a_texCoord");
Glint _textureUniform = glGetUniformlocation(pg->getProgram(),"CC_Texture0");
glEnabLevertexAttribarray(_positionLocation);
glEnabLevertexAttribarray(_colorLocation);
glEnabLevertexAttribarray(_textureLocation);
glVertexAttribPointer(_positionLocation,GL_float,GL_FALSE,sizeof(Vertex),(GLvoID*)offsetof(Vertex,position));
glVertexAttribPointer(_colorLocation,4,color));
glVertexAttribPointer(_textureLocation,
(GLvoID*)offsetof(Vertex,TexCoord));
GL::bindTexture2DN(0,texture2d->getname());
gluniform1i(_textureUniform,0);
glDrawElements(GL_TRIANGLES,sizeof(iarr)/sizeof(glushort)-arrcount*6,GL_UNSIGNED_SHORT,0);
gldisable(GL_DEPTH_TEST);
glBlendFunc(bf.src,bf.dst);
glEnable(GL_DEPTH_TEST);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
SmritiShadow::~SmritiShadow()
{
glDeleteBuffers(1,&vertexBuffer);
glDeleteBuffers(1,&indexBuffer);
delete[] Vertices;
}
voID SmritiShadow::setTarget(Sprite* spr,SpriteFrame *sf)
{
target=spr;
Rect texrc;
if(sf!=0)
{
texture2d=sf->getTexture();
Rect rc=sf->getRectInPixels();
Size dd=texture2d->getContentSize();
texrc=Rect(rc.origin.x/dd.wIDth,rc.origin.y/dd.height,
(rc.origin.x+rc.size.wIDth)/dd.wIDth,(rc.origin.y+rc.size.height)/dd.height);
srcsz=sf->getRectInPixels().size;
}
else
{
texture2d=tex;
texrc=Rect(0,1);
srcsz=texture2d->getContentSize();
}
Size sz=srcsz;
Size sz1=spr->getContentSize();
Vec2 pos=spr->getposition();
CCPoint ap=target->getAnchorPoint();
pos.x-=sz1.wIDth*ap.x;
pos.y-=sz1.height*ap.y;
for(int i=0;i<24;i+=4)
{
Vertices[i].position[0]=pos.x;
Vertices[i].position[1]=pos.y;
Vertices[i].position[2]=0;
Vertices[i].TexCoord[0]=texrc.origin.x;
Vertices[i].TexCoord[1]=texrc.size.height;
Vertices[i+1].position[0]=pos.x+sz.wIDth;
Vertices[i+1].position[1]=pos.y;
Vertices[i+1].position[2]=0;
Vertices[i+1].TexCoord[0]=texrc.size.wIDth;
Vertices[i+1].TexCoord[1]=texrc.size.height;
Vertices[i+2].position[0]=pos.x;
Vertices[i+2].position[1]=pos.y+sz.height;
Vertices[i+2].position[2]=0;
Vertices[i+2].TexCoord[0]=texrc.origin.x;
Vertices[i+2].TexCoord[1]=texrc.origin.y;
Vertices[i+3].position[0]=pos.x+sz.wIDth;
Vertices[i+3].position[1]=pos.y+sz.height;
Vertices[i+3].position[2]=0;
Vertices[i+3].TexCoord[0]=texrc.size.wIDth;
Vertices[i+3].TexCoord[1]=texrc.origin.y;
}
}
voID SmritiShadow::reset()
{
if(pg==0)
{
pg=new CCGLProgram();
}
pg->reset();
pg->initWithVertexShaderByteArray(vertshader,fragshader);
pg->link();
pg->updateUniforms();
}
bool SmritiShadow::init()
{
struct timeval tv;
gettimeofday(&tv,NulL);
long ct=tv.tv_sec*1000000+tv.tv_usec;
pretime=ct;
arrcount=0;
Vertices=new Vertex[4*6];
for(int i=0;i<24;i+=4)
{
Vertices[i].color[3]=0.2*(i/4+1);
Vertices[i].TexCoord[0]=0;
Vertices[i].TexCoord[1]=1;
Vertices[i+1].color[3]=0.2*(i/4+1);
Vertices[i+1].TexCoord[0]=1;
Vertices[i+1].TexCoord[1]=1;
Vertices[i+2].color[3]=0.2*(i/4+1);
Vertices[i+2].TexCoord[0]=0;
Vertices[i+2].TexCoord[1]=0;
Vertices[i+3].color[3]=0.2*(i/4+1);
Vertices[i+3].TexCoord[0]=1;
Vertices[i+3].TexCoord[1]=0;
}
glGenBuffers( 1,&vertexBuffer );
glGenBuffers( 1,&indexBuffer );
texture2d=0;
target=0;
if(pg==0)
{
pg=new GLProgram();
pg->initWithVertexShaderByteArray(vertshader,fragshader);
pg->link();
pg->updateUniforms();
}
return true;
}
用法:
SmritiShadow* temp=SmritiShadow::create();
temp->setTarget(obj,tex);//obj跟随对象,tex设置的尾巴纹理。
如果 有 什么 不懂的或者 有啥问题 可以 互相 交流交流~~,end。
总结以上是内存溢出为你收集整理的cocos2dx 关于拖影(类似dnf传承武器的拖尾效果)的快速实现全部内容,希望文章能够帮你解决cocos2dx 关于拖影(类似dnf传承武器的拖尾效果)的快速实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)