cocos2dx 2.x实现闪电效果(非画线版)
在网上搜索到一个直接用opengl画线实现的版本,但放在游戏中效果不太搭,要求用贴图的。我这个版本用的也是画线版的算法。
闪动的时候效果还可以,每段衔接的地方还是不太完美,各位有其他的建议的请联系我 [email protected]
转载请注明出处 http://www.cnblogs.com/mrblue/p/4315091.html
效果图
//头文件
#ifndef __LIGINTNING_H__ #define __LIGINTNING_H__ #include "cocos2d.h" #include <vector> class Lightning : public cocos2d::CCNode { public: Lightning(); ~Lightning(); public: static Lightning* create(const std::string& strTextureFile, float fLength); virtual bool init(const std::string& strTextureFile, float fLength); protected: virtual void draw()/*override*/; virtual void update(float fDelta)/*override*/; public: void setTexture(cocos2d::CCTexture2D *texture); void setLength(float fLength){ m_fLength = fLength; } protected: void calculateVertex(); void addLightningSegment(float x1, float y1, float x2, float y2, float displace); protected: std::vector<cocos2d::CCPoint> m_vecLightningTrackPoint; std::vector<float> m_vecVertics; std::vector<float> m_vecCoordinates; cocos2d::CCTexture2D* m_pTexture; float m_fLength; }; #endif // __HELLOWORLD_SCENE_H__
//cpp
#include "Lightning.h" USING_NS_CC; using namespace std; Lightning::Lightning() :m_pTexture(NULL) { m_vecLightningTrackPoint.reserve(64); m_vecVertics.reserve(256); m_vecCoordinates.reserve(256); } Lightning::~Lightning() { CC_SAFE_RELEASE(m_pTexture); } Lightning* Lightning::create( const std::string& strTextureFile, float fLength ) { Lightning * pRet = new Lightning(); if (pRet && pRet->init(strTextureFile,fLength)) { pRet->autorelease(); } else { CC_SAFE_DELETE(pRet); } return pRet; } bool Lightning::init(const string& strFile, float fLength) { CCNode::init(); setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture)); setLength(fLength); setTexture(CCTextureCache::sharedTextureCache()->addImage(strFile.c_str())); scheduleUpdate(); return true; } void Lightning::draw() { if(m_vecVertics.empty()||m_vecCoordinates.empty()) return; CC_NODE_DRAW_SETUP(); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords ); ccGLBlendFunc( CC_BLEND_SRC,CC_BLEND_DST ); ccGLBindTexture2D( m_pTexture->getName() ); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(&m_vecVertics[0])); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(&m_vecCoordinates[0])); glDrawArrays(GL_TRIANGLE_STRIP, 0, m_vecVertics.size()/2); } void Lightning::addLightningSegment( float x1, float y1, float x2, float y2, float displace ) { if (displace <=m_pTexture->getPixelsWide()/2 ) { m_vecLightningTrackPoint.push_back(ccp(x2,y2)); } else { float mid_x = (x2+x1)/2; float mid_y = (y2+y1)/2; mid_x += (CCRANDOM_0_1() - 0.5) * displace; mid_y += (CCRANDOM_0_1() - 0.5) * displace; addLightningSegment(x1, y1, mid_x, mid_y, displace/2); addLightningSegment(mid_x, mid_y,x2, y2, displace/2); } } void Lightning::update(float delta) { calculateVertex(); } void Lightning::calculateVertex() { m_vecLightningTrackPoint.clear(); m_vecLightningTrackPoint.push_back(CCPointZero); addLightningSegment(0,0,m_fLength,0,m_fLength/3); m_vecVertics.clear(); m_vecCoordinates.clear(); for (size_t i=0; i<m_vecLightningTrackPoint.size(); i++) { const auto& pt = m_vecLightningTrackPoint[i]; m_vecVertics.push_back(pt.x); m_vecVertics.push_back(pt.y-m_pTexture->getPixelsHigh()/2); m_vecVertics.push_back(pt.x); m_vecVertics.push_back(pt.y+m_pTexture->getPixelsHigh()/2); m_vecCoordinates.push_back(i); m_vecCoordinates.push_back(0); m_vecCoordinates.push_back(i); m_vecCoordinates.push_back(1); } } void Lightning::setTexture( cocos2d::CCTexture2D *texture ) { CC_SAFE_RETAIN(texture); CC_SAFE_RELEASE(m_pTexture); m_pTexture = texture;
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT}; m_pTexture->setTexParameters(¶ms);
}
时间: 2024-10-09 20:47:09