Example #1
0
void CCSprite::setDisplayFrame(CCSpriteFrame *pNewFrame)
{
	m_obUnflippedOffsetPositionFromCenter = pNewFrame->getOffsetInPixels();

	CCTexture2D *pNewTexture = pNewFrame->getTexture();
	// update texture before updating texture rect
	if (!m_pobTexture ||
		pNewTexture->getName() != m_pobTexture->getName())
	{
        setTexture(pNewTexture);
	}

	// update rect
	m_bRectRotated = pNewFrame->isRotated();
	setTextureRectInPixels(pNewFrame->getRectInPixels(), pNewFrame->isRotated(), pNewFrame->getOriginalSizeInPixels());
}
//--------------------------------------------------------
bool CShaderNode::initWithVertex( const char* image,const char *vert, const char *frag)
{
	initShader(vert,frag);
	CCTexture2D* pTex = CCTextureCache::sharedTextureCache()->addImage( image );
	if( pTex == NULL )
	{
		return false;
	}
	m_pTexture = pTex->getName();

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	setContentSize( winSize );//pTex->getContentSize() );
	setPosition(ccp(winSize.width / 2 - getContentSize().width / 2,
		winSize.height / 2 - getContentSize().height / 2 ));

	m_fTime = 0;

	// 开启自更新
	scheduleUpdate();
	return true;
}
void SpriteMask::draw()
{
	if (!_isSetMask)   //沒有設定就直接用預設方法
	{
		CCSprite::draw();
		return;
	}

	CC_PROFILER_START_CATEGORY(kProfilerCategorySprite, "CCSprite - draw");

	CCASSERT(!_batchNode, "If Sprite is being rendered by SpriteBatchNode, Sprite#draw SHOULD NOT be called");

	CC_NODE_DRAW_SETUP();

	GL::blendFunc(_blendFunc.src, _blendFunc.dst);

	GL::bindTexture2D(_texture->getName());
	GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);

	if (_texture != NULL) {
		ccGLBindTexture2D(_texture->getName());
	} else {
		ccGLBindTexture2D(0);
	}
	
	//mask 設定 設定使用mask貼圖
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, _maskTexture->getName());
	glUniform1i(_maskLocation, 1);
	glUniform2f(_offsetLocation, offsetWithPosition.x, offsetWithPosition.y);

#define kQuadSize sizeof(_quad.bl)
#ifdef EMSCRIPTEN
	long offset = 0;
	setGLBufferData(&_quad, 4 * kQuadSize, 0);
#else
	long offset = (long)&_quad;
#endif // EMSCRIPTEN

	// vertex
	int diff = offsetof(V3F_C4B_T2F, vertices);
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));

	// texCoods
	diff = offsetof(V3F_C4B_T2F, texCoords);
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));

	// color
	diff = offsetof(V3F_C4B_T2F, colors);
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));


	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

	CHECK_GL_ERROR_DEBUG();


#if CC_SPRITE_DEBUG_DRAW == 1
	// draw bounding box
	Point vertices[4] = {
		Point(_quad.tl.vertices.x, _quad.tl.vertices.y),
		Point(_quad.bl.vertices.x, _quad.bl.vertices.y),
		Point(_quad.br.vertices.x, _quad.br.vertices.y),
		Point(_quad.tr.vertices.x, _quad.tr.vertices.y),
	};
	ccDrawPoly(vertices, 4, true);
#elif CC_SPRITE_DEBUG_DRAW == 2
	// draw texture box
	Size s = this->getTextureRect().size;
	Point offsetPix = this->getOffsetPosition();
	Point vertices[4] = {
		Point(offsetPix.x, offsetPix.y), Point(offsetPix.x + s.width, offsetPix.y),
		Point(offsetPix.x + s.width, offsetPix.y + s.height), Point(offsetPix.x, offsetPix.y + s.height)
	};
	ccDrawPoly(vertices, 4, true);
#endif // CC_SPRITE_DEBUG_DRAW

	CC_INCREMENT_GL_DRAWS(1);

	glActiveTexture(GL_TEXTURE0);  //must set to texture0

	CC_PROFILER_STOP_CATEGORY(kProfilerCategorySprite, "CCSprite - draw");
}
void LHBezierNode::draw(void)
{
    
    float scale = CCDirector::sharedDirector()->getContentScaleFactor();
    
	if(0.0f != LHSettings::sharedInstance()->customAlpha())
	{
		glColor4f(color.origin.x, 
				  color.origin.y, 
				  color.size.width, 
				  color.size.height*LHSettings::sharedInstance()->customAlpha());
		glPushMatrix();
		
        glDisableClientState(GL_COLOR_ARRAY);
        
        if(NULL != texture)
        {
            glEnable(GL_TEXTURE_2D);		            
            glBindTexture(GL_TEXTURE_2D, texture->getName());
		
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            
            int size = (int)trianglesHolder.size();
            CCPoint* verts = new CCPoint[size* 3];
            CCPoint* texcoords = new CCPoint[size* 3];
            
            for(int k = 0; k < (int)trianglesHolder.size();++k)
            {
                std::vector<CCPoint> fix = trianglesHolder[k];
                
                for(int j = 0; j < 3; ++j)
                {
                    CCPoint pt = fix[j];
                
                    pt.x *= scale;
                    pt.y *= scale;
                    
                    verts[k*3 + j] = pt;
                    texcoords[k*3 + j].x = pt.x/imageSize.width;
                    texcoords[k*3 + j].y = (winSize.height - pt.y)/imageSize.height;
                }
            }
                                
            glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
            glVertexPointer(2, GL_FLOAT, 0, verts);
            glDrawArrays(GL_TRIANGLES, 0, 3*size);
            
            bool wasBlend = glIsEnabled(GL_BLEND);
            glEnable(GL_BLEND);            
            int oldBlendDST = 0;
            glGetIntegerv(GL_BLEND_DST, &oldBlendDST);
            int oldBlendSRC = 0;
            glGetIntegerv(GL_BLEND_SRC, &oldBlendSRC);

            for(int i = 0; i < blendingTextures.size(); ++i)
            {
                CCTexture2D* tex = blendingTextures[i].texture;
                if(NULL != tex)
                {
                    glBlendFunc(blendingTextures[i].blendSource, blendingTextures[i].blendDestination);
                    glBindTexture(GL_TEXTURE_2D, tex->getName());
                    
                    if(blendingTextures[i].tile){
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
                    }

                    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
                    glVertexPointer(2, GL_FLOAT, 0, verts);
                    glDrawArrays(GL_TRIANGLES, 0, 3*size);                
                }
            }
            
            glBlendFunc(oldBlendSRC, oldBlendDST);
            if(!wasBlend)
                glDisable(GL_BLEND);
            
            delete [] verts;
            delete [] texcoords;
		}
        
		float oldLineWidth = 1.0f;
		glGetFloatv(GL_LINE_WIDTH, &oldLineWidth); 
		
		glLineWidth(lineWidth);
		
		glDisable(GL_TEXTURE_2D);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glColor4f(lineColor.origin.x, 
				  lineColor.origin.y, 
				  lineColor.size.width, 
				  lineColor.size.height*LHSettings::sharedInstance()->customAlpha());
		
        CCPoint* line_verts = new CCPoint[linesHolder.size()];
        for(int i = 0; i < (int)linesHolder.size(); i+=2)
		{
			CCPoint pt1 = linesHolder[i];
			CCPoint pt2 = linesHolder[i+1];
            line_verts[i] = pt1;
            line_verts[i+1] = pt2;            
		}
        
        glVertexPointer(2, GL_FLOAT, 0, line_verts);
        glDrawArrays(GL_LINES, 0, linesHolder.size());
        delete[] line_verts;
        
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
		glLineWidth(oldLineWidth);
		glEnable(GL_TEXTURE_2D);	
		glPopMatrix();
	}	
}
void BlurSprite::draw()
{

    CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
        
	CCAssert(!m_pobBatchNode, "If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");
        
	//CC_NODE_DRAW_SETUP();
	ccGLEnable( m_eGLServerState );

	ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );

	// 水平方向blur 
	m_frameTexture->clear(0.f, 0.f, 0.f, 0.f);
	m_frameTexture->begin();
	{

		m_shader->use();
		m_shader->setUniformsForBuiltins();

		glUniform2f(m_location_pix_size, 1.f/m_tex_size.width, 0);

		if (m_pobTexture != NULL)
		{
			ccGLBindTexture2D(m_pobTexture->getName());
		}
		else
		{
			ccGLBindTexture2D(0);
		}


		//
		// Attributes
		//
		ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

#define kQuadSize sizeof(m_sQuad.bl)
		long offset = (long)&m_sQuad;

		// vertex
		int diff = offsetof( ccV3F_C4B_T2F, vertices);
		glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));

		// texCoods
		diff = offsetof( ccV3F_C4B_T2F, texCoords);
		glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));

		// color
		diff = offsetof( ccV3F_C4B_T2F, colors);
		glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));


		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

		CHECK_GL_ERROR_DEBUG();

	}
	m_frameTexture->end();

	// 垂直方向blur
	CCTexture2D* rt = m_frameTexture->getSprite()->getTexture();
#if CC_DEBUG
	m_frameTexture2->clear(0.f, 0.f, 0.f, 0.f);
	m_frameTexture2->begin();
#endif
	{
		m_shader->use();
		m_shader->setUniformsForBuiltins();

		glUniform2f(m_location_pix_size, 0, 1.f/m_tex_size.height);

		if ( rt != NULL)
		{
			ccGLBindTexture2D(rt->getName());
		}
		else
		{
			ccGLBindTexture2D(0);
		}


		//
		// Attributes
		//
		ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

#define kQuadSize sizeof(m_sQuad.bl)
		long offset = (long)&m_sQuad;

		// vertex
		int diff = offsetof( ccV3F_C4B_T2F, vertices);
		glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));

		// texCoods
		diff = offsetof( ccV3F_C4B_T2F, texCoords);
		glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));

		// color
		diff = offsetof( ccV3F_C4B_T2F, colors);
		glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));


		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

		CHECK_GL_ERROR_DEBUG();
	}
#if CC_DEBUG
	m_frameTexture2->end();
	m_frameTexture2->saveToFile("blur_complete.png");
#endif

	CC_INCREMENT_GL_DRAWS(1);
        
	CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
}