1 2 3 4 5 6 | // 创建渲染目标纹理 auto WinSize = Director::getInstance()->getWinSize(); m_pTarget = CCRenderTexture::create(WinSize.width, WinSize.height, kCCTexture2DPixelFormat_RGBA8888); m_pTarget->setClearColor(Color4F(0, 0, 0, 0)); m_pTarget->setVisible(false); addChild(m_pTarget); |
1 2 3 4 5 6 7 8 9 10 11 12 | void VortexLayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) { if( m_pTarget && m_TargetLayer) { m_pTarget->clear(0,0,0,0); m_pTarget->begin(); m_TargetLayer->visit(); m_pTarget->end(); } … } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | auto WinSize = Director::getInstance()->getWinSize(); //行,列数 int cows = 100; int rows = 100; //每一行,列的顶点位置偏移 float vertexStepX = WinSize .width/cows; float vertexStepY = WinSize .height/rows; //第一行,列的纹理坐标偏移 float uStep = 1.0/rows; float vStep = 1.0/cows; //顶点,法线,色,纹理的容器 vector<float> positions; vector<float> normals; vector<float> colors; vector<float> texs; for ( unsigned int x = 0; x <= cows; ++x ) { for ( unsigned int y = 0; y <= rows; ++y ) { unsigned int offset = x + y * ( cows + 1 ); positions.push_back(x*vertexStepX); positions.push_back(y*vertexStepY); positions.push_back(10); texs.push_back(x*uStep); texs.push_back(y*vStep); } } std::vector<unsigned short> tTriangle; for ( unsigned int x = 0; x < cows; ++x ) { for ( unsigned int y = 0; y < rows; ++y ) { unsigned short* ptr = &(m_IndiceArray[(x+y*cows)*6]); tTriangle.push_back((x+0) + (y+0)*(cows+1)); tTriangle.push_back((x+0) + (y+1)*(cows+1)); tTriangle.push_back((x+1) + (y+0)*(cows+1)); tTriangle.push_back((x+0) + (y+1)*(cows+1)); tTriangle.push_back((x+1) + (y+1)*(cows+1)); tTriangle.push_back((x+1) + (y+0)*(cows+1)); } } //创建模型 m_RenderMesh = Mesh::create(positions,normals,texs,tTriangle); m_RenderMesh->retain(); //设置顶点格式 long offset = 0; auto attributeCount = m_RenderMesh->getMeshVertexAttribCount(); for (auto k = 0; k < attributeCount; k++) { auto meshattribute = m_RenderMesh->getMeshVertexAttribute(k); m_ProgramState->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, m_RenderMesh->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } m_meshCommand.genMaterialID(0, m_ProgramState, m_RenderMesh, m_BlendFunc); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // 输入的顶点格式 attribute vec4 a_position; attribute vec2 a_texCoord; //输出给PS的变量 varying vec2 v_texCoord; //用户自定义的变量 uniform float radius ; uniform float angle ; //旋涡的计算函数 vec2 vortex( vec2 uv ) { //先减去贴图中心点的纹理坐标,这样是方便旋转计算 uv -= vec2(0.5, 0.5); //计算当前坐标与中心点的距离。 float dist = length(uv); //计算出旋转的百分比 float percent = (radius - dist) / radius; if ( percent < 1.0 && percent >= 0.0) { //通过sin,cos来计算出旋转后的位置。 float theta = percent * percent * angle * 8.0; float s = sin(theta); float c = cos(theta); uv = vec2(dot(uv, vec2(c, -s)), dot(uv, vec2(s, c))); } //再加上贴图中心点的纹理坐标,这样才正确。 uv += vec2(0.5, 0.5); return uv; } void main() { //计算出顶点的位置 gl_Position = CC_MVPMatrix * a_position; //调用函数计算出纹理坐标 v_texCoord = vortex( a_texCoord ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //计算时间间隔 timeval currtime; gettimeofday( &currtime, nullptr); float dt = (currtime.tv_sec - m_lasttime.tv_sec) + (currtime.tv_usec - m_lasttime.tv_usec) / 1000000.0f; if ( m_Time < m_maxTime ) { setAngle( getAngel()+m_angleSpeed*dt ); setRadius( getRadius()+m_radiusSpeed*dt ); m_Time += dt; } else { m_Time = 0.0; setAngle( 0.0f ); setRadius( 0.0f ); } m_lasttime = currtime; //设置Shader的参数。 GLProgramState* programstate = getGLProgramState(); programstate->setUniformFloat("radius",m_radius); programstate->setUniformFloat("angle",m_angle); programstate->setUniformTexture("u_texture", m_pTarget->getSprite()->getTexture()); |
欢迎光临 让生活充满乐趣 (http://www.ofunny.com/) | Powered by Discuz! 7.0.0 |