示例#1
0
// CCAtlasNode - draw
void CCAtlasNode::draw(void)
{
    CC_NODE_DRAW_SETUP(this);

    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    GLfloat colors[4] = {_displayedColor.r / 255.0f, _displayedColor.g / 255.0f, _displayedColor.b / 255.0f, _displayedOpacity / 255.0f};
    getShaderProgram()->setUniformLocationWith4fv(m_nUniformColor, colors, 1);

    m_pTextureAtlas->drawNumberOfQuads(m_uQuadsToDraw, 0);
}
// Cocos2dxAtlasNode - draw
void Cocos2dxAtlasNode::draw()
{
    CC_NODE_DRAW_SETUP();

    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    GLfloat colors[4] = {_displayedColor.r / 255.0f, _displayedColor.g / 255.0f, _displayedColor.b / 255.0f, _displayedOpacity / 255.0f};
    getShaderProgram()->setUniformLocationWith4fv(m_nUniformColor, colors, 1);

    m_pTextureAtlas->drawNumberOfQuads(1, m_nQuadIndex);
	m_pTextureAtlas->getQuads();
}
	void Cocos2dRenderManager::doRender(IVertexBuffer* _buffer, ITexture* _texture, size_t _count)
	{
		Cocos2dVertexBuffer* buffer = static_cast<Cocos2dVertexBuffer*>(_buffer);
		unsigned int buffer_id = buffer->getBufferID();

		unsigned int texture_id = 0;
		Cocos2dTexture* texture = static_cast<Cocos2dTexture*>(_texture);
		texture_id = texture->getGLTexture();

		//MYGUI_PLATFORM_ASSERT(buffer_id, "Texture is not created");
		//MYGUI_PLATFORM_ASSERT(texture_id, "Texture is not created");
		if (!buffer_id) return;
		if (!texture_id) return;

		ccGLEnable(m_eGLServerState);

		CCGLProgram* shaderProgram = NULL;
		cocos2d::ccBlendFunc blendFunc;

		shaderProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor_MyGUI);
		// 对于
		if (texture->getCocos2dTexture()->hasPremultipliedAlpha())
		{
			blendFunc.src = CC_BLEND_SRC;
			blendFunc.dst = CC_BLEND_DST;
		}
		else
		{
			blendFunc.src = GL_SRC_ALPHA;
			blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
		}

		shaderProgram->use();
		shaderProgram->setUniformsForBuiltins();
		ccGLBlendFunc(blendFunc.src, blendFunc.dst);

		ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

		ccGLBindTexture2D(texture_id);
		glBindBuffer(GL_ARRAY_BUFFER, buffer_id);

		glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x));
		glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, colour));
		glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, u));

		glDrawArrays(GL_TRIANGLES, 0, _count);

		vbo_count++;
		vertex_count += _count;

		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}
示例#4
0
/**
 * 绘制
 */
void MapCoordLine::draw(void)
{
	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();

	ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );

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

	//
	// Attributes
	//

	ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

	#define kQuadSize sizeof(m_sQuad.bl)
	if (points.size())
	{
		long offset = (long)&points[0];

		// 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_LINES, 0, points.size());
	}
	CHECK_GL_ERROR_DEBUG();

	CC_INCREMENT_GL_DRAWS(1);

	CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
}
void CCSkeleton::draw () {
	CC_NODE_DRAW_SETUP();

	ccGLBlendFunc(blendFunc.src, blendFunc.dst);
	ccColor3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;

	quadCount = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++)
		if (skeleton->slots[i]->attachment) Attachment_draw(skeleton->slots[i]->attachment, skeleton->slots[i]);
	if (atlas) atlas->drawNumberOfQuads(quadCount);

	if (debugSlots) {
		// Slots.
		ccDrawColor4B(0, 0, 255, 255);
		glLineWidth(1);
		CCPoint points[4];
		for (int i = 0, n = skeleton->slotCount; i < n; i++) {
			if (!skeleton->slots[i]->attachment) continue;
			ccV3F_C4B_T2F_Quad* quad = &((Cocos2dxRegionAttachment*)skeleton->slots[i]->attachment)->quad;
			points[0] = ccp(quad->bl.vertices.x, quad->bl.vertices.y);
			points[1] = ccp(quad->br.vertices.x, quad->br.vertices.y);
			points[2] = ccp(quad->tr.vertices.x, quad->tr.vertices.y);
			points[3] = ccp(quad->tl.vertices.x, quad->tl.vertices.y);
			ccDrawPoly(points, 4, true);
		}
	}
	if (debugBones) {
		// Bone lengths.
		glLineWidth(2);
		ccDrawColor4B(255, 0, 0, 255);
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			float x = bone->data->length * bone->m00 + bone->worldX;
			float y = bone->data->length * bone->m10 + bone->worldY;
			ccDrawLine(ccp(bone->worldX, bone->worldY), ccp(x, y));
		}
		// Bone origins.
		ccPointSize(4);
		ccDrawColor4B(0, 0, 255, 255); // Root bone is blue.
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			ccDrawPoint(ccp(bone->worldX, bone->worldY));
			if (i == 0) ccDrawColor4B(0, 255, 0, 255);
		}
	}
}
示例#6
0
void CCMeshImage::draw()
{
	CC_NODE_DRAW_SETUP();
	
	ccGLBlendFunc( CC_BLEND_SRC, CC_BLEND_DST );

	//GLfloat colors[4] = {_displayedColor.r / 255.0f, _displayedColor.g / 255.0f, _displayedColor.b / 255.0f, _displayedOpacity / 255.0f};
	//getShaderProgram()->setUniformLocationWith4fv(m_nUniformColor, colors, 1);

	for (CCTextureAtlasMap::iterator it = m_atlas_map.begin();
		it != m_atlas_map.end(); it++)
	{
		it->second->drawQuads();
	}
}
void CCCatmullRomSprite::draw() {
    // at least we need two points
    if(m_controlPoints.getCount() < 2)
        return;
    
	// build atlas
	if(m_dirty) {
		updateAtlas();
		m_dirty = false;
	}
	
	// profile start
	CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCCatmullRomSprite - draw");
	
	// setup
    CC_NODE_DRAW_SETUP();
	
	// blend func
	ccBlendFunc bf = m_sprite->getBlendFunc();
    ccGLBlendFunc(bf.src, bf.dst);
	
    // draw
	if(m_atlas) {
        if(m_allVisible) {
            m_atlas->drawQuadsEx();
        } else {
            int startIndex = 0;
            int sc = getSegmentCount();
            for(int i = 0; i < sc; i++) {
                bool visible = isSegmentVisible(i);
                if(!visible) {
                    int endIndex = m_segmentQuadIndices[i];
                    m_atlas->drawNumberOfQuadsEx(endIndex - startIndex, startIndex);
                    startIndex = m_segmentQuadIndices[i + 1];
                }
            }
            
            // last
            if(m_atlas->getTotalQuads() > startIndex) {
                m_atlas->drawNumberOfQuadsEx(m_atlas->getTotalQuads() - startIndex, startIndex);
            }
        }
    }
	
	// profile end
    CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCCatmullRomSprite - draw");
}
void CCParticleBatchNode::draw(void)
{
    CC_PROFILER_START("CCParticleBatchNode - draw");

    if( m_pTextureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    CC_NODE_DRAW_SETUP();

    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    m_pTextureAtlas->drawQuads();

    CC_PROFILER_STOP("CCParticleBatchNode - draw");
}
void ShaderSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");

    // CCAssert(, "If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");

    CC_NODE_DRAW_SETUP();

    //
    // 启用attributes变量输入,顶点坐标,纹理坐标,颜色
    //
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
    ccGLBlendFunc(this->getBlendFunc().src, this->getBlendFunc().dst);

    this->getShaderProgram()->use();
    this->getShaderProgram()->setUniformsForBuiltins();

    // 绑定纹理到纹理槽0
    ccGLBindTexture2D(this->getTexture()->getName());



#define kQuadSize sizeof(this->_quad.bl)
    long offset = (long)&this->_quad;

    // 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();

    CC_INCREMENT_GL_DRAWS(1);
    CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
}
void Cc3dSubMesh::onDraw(const Mat4& modelMat, uint32_t flags){
    assert(m_subMeshData&&m_texture&&m_indexVBO
           &&m_program);
    
    if(m_indexVBO->getIndexArrayLen()==0)return;
    
	//enable array attribute
	bool isPostionAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_position();
	bool isTexCoordAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_texCoord();
	bool isNormalAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_normal();
	bool isColorAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_color();
    Cc3dIndexVBO3d::enableAttribArray_position(true);
	Cc3dIndexVBO3d::enableAttribArray_texCoord(true);
	Cc3dIndexVBO3d::enableAttribArray_normal(true);
	Cc3dIndexVBO3d::enableAttribArray_color(true);
    //apply state
    //for performance sake, we only apply state, not restore
    {
        //set depthTest
        CCDirector::sharedDirector()->setDepthTest(m_isDoDepthTest);
        //set blend function
        ccGLBlendFunc(m_blendFunc.src, m_blendFunc.dst);
    }
    
    //enable server state (i don't know what this means :( )
    ////ccGLEnable(m_eGLServerState);
    //pass values for cocos2d-x build-in uniforms
    Cc3dProgram*program=(Cc3dProgram*)getShaderProgram();
    program->use();
    program->mySetUniformsForBuiltins(Cc3dMatrix4(modelMat.m),
                                      Cc3dDirector::sharedDirector()->getCamera()->calculateViewMat(),
                                      Cc3dDirector::sharedDirector()->getCamera()->calculateProjectionMat());
    //pass values for my own uniforms
    m_passUnifoCallback(this, program,Cc3dMatrix4(modelMat.m));//m_program
    //attach texture to texture attach point
    ccGLBindTexture2DN(0, this->m_texture->getID());
    //draw
    m_indexVBO->setPointers();
    m_indexVBO->draw(GL_TRIANGLES);
	//recover array attribute state
	Cc3dIndexVBO3d::enableAttribArray_position(isPostionAttribEnabled);
	Cc3dIndexVBO3d::enableAttribArray_texCoord(isTexCoordAttribEnabled);
	Cc3dIndexVBO3d::enableAttribArray_normal(isNormalAttribEnabled);
	Cc3dIndexVBO3d::enableAttribArray_color(isColorAttribEnabled);
}
示例#11
0
void CCLayerColor::draw()
{
    CC_NODE_DRAW_SETUP();

    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );

    //
    // Attributes
    //
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, m_pSquareVertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, m_pSquareColors);

    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    CC_INCREMENT_GL_DRAWS(1);
}
void CtinyWingsTerrainSprite::draw(){
    
    //----change shader
    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
    ccGLEnable(m_eGLServerState);
    //pass values for cocos2d-x build-in uniforms
    this->setShaderProgram(m_program);
    CGLProgramWithUnifos*program=(CGLProgramWithUnifos*)this->getShaderProgram();
    program->use();
    program->setUniformsForBuiltins();
    //pass values for my own uniforms
    float colorList_tmp[4*3]={m_colorList[0].r,m_colorList[0].g,m_colorList[0].b,m_colorList[0].a,
        m_colorList[1].r,m_colorList[1].g,m_colorList[1].b,m_colorList[1].a,
        m_colorList[2].r,m_colorList[2].g,m_colorList[2].b,m_colorList[2].a};
    program->passUnifoValue1f("u_texNonlinearFactor", m_texNonlinearFactor);
    program->passUnifoValueNfvN("u_colors", colorList_tmp, 4, 3);
    program->passUnifoValue1f("u_cosA", cosf(m_ribbonRotation*M_PI/180));
    program->passUnifoValue1f("u_sinA", sinf(m_ribbonRotation*M_PI/180));
    program->passUnifoValue1f("u_ribbonRepeat", m_ribbonRepeat);
    //enable attributes
    bool isAttribPositionOn=CindexVBO::isEnabledAttribArray_position();
    bool isAttribColorOn=CindexVBO::isEnabledAttribArray_color();
    bool isAttribTexCoordOn=CindexVBO::isEnabledAttribArray_texCoord();
    CindexVBO::enableAttribArray_position(true);
    CindexVBO::enableAttribArray_color(true);
    CindexVBO::enableAttribArray_texCoord(true);
    //bind texture
    ccGLBindTexture2D( this->getTexture()->getName());
    //draw m_indexVBO
    m_indexVBO->setPointer_position();
    m_indexVBO->setPointer_texCoord();
    m_indexVBO->setPointer_color();
    m_indexVBO->draw(GL_TRIANGLES);
    //unbind texture
    ccGLBindTexture2D(0);
    //disable attributes
    CindexVBO::enableAttribArray_position(isAttribPositionOn);
    CindexVBO::enableAttribArray_color(isAttribColorOn);
    CindexVBO::enableAttribArray_texCoord(isAttribTexCoordOn);
    //draw wire
    if(m_isDrawDebug){
        drawWire();
    }
}
示例#13
0
void CABatchView::draw(void)
{
    CC_PROFILER_START("CABatchView - draw");

    CC_RETURN_IF(!m_pobImageAtlas);
    CC_RETURN_IF(m_pobImageAtlas->getTotalQuads() == 0);
    
    CC_NODE_DRAW_SETUP();

    CAVector<CAView*>::const_iterator itr;
    for (itr=m_obSubviews.begin(); itr!=m_obSubviews.end(); itr++)
        (*itr)->updateTransform();

    ccGLBlendFunc( m_blendFunc.src, m_blendFunc.dst );

    m_pobImageAtlas->drawQuads();

    CC_PROFILER_STOP("CABatchView - draw");
}
示例#14
0
void CCControlSwitchSprite::draw()
{
    CC_NODE_DRAW_SETUP();

    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
    ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    getShaderProgram()->setUniformsForBuiltins();

    glActiveTexture(GL_TEXTURE0);
    glBindTexture( GL_TEXTURE_2D, getTexture()->getName());
    glUniform1i(m_uTextureLocation, 0);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture( GL_TEXTURE_2D, m_pMaskTexture->getName() );
    glUniform1i(m_uMaskLocation, 1);

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

    // 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));

    m_pShaderProgram->setUniform1f(kCCUniformGray, _gray ? 1.0f : 0.0f);
    m_pShaderProgram->setUniform1f(kCCUniformBrightness, _brightness);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glActiveTexture(GL_TEXTURE0);
}
示例#15
0
void CCMotionStreak::draw()
{
    if(m_uNuPoints <= 1)
        return;

    CC_NODE_DRAW_SETUP();

    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    ccGLBindTexture2D( m_pTexture->getName() );

    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, m_pVertices);
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, m_pTexCoords);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, m_pColorPointer);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)m_uNuPoints*2);

    CC_INCREMENT_GL_DRAWS(1);
}
void SpriteBlur::initProgram()
{
	GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
		CCFileUtils::sharedFileUtils()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
	CCGLProgram* pProgram = new CCGLProgram();
	pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
	setShaderProgram(pProgram);
	pProgram->release();

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
	getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
	getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->link();

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->updateUniforms();

	CHECK_GL_ERROR_DEBUG();

	subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract");
	blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize");

	CHECK_GL_ERROR_DEBUG();
    
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
	ccBlendFunc blend = getBlendFunc();
	ccGLBlendFunc(blend.src, blend.dst);
    
	getShaderProgram()->use();
	getShaderProgram()->setUniformsForBuiltins();
	getShaderProgram()->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y);
	getShaderProgram()->setUniformLocationWith4fv(subLocation, sub_, 1);
    
	ccGLBindTexture2D( getTexture()->getName());    
}
void Cc3dSubMesh::draw(){
    assert(m_subMeshData&&m_texture&&m_indexVBO
           &&m_program&&m_lightSource&&m_camera);
	//enable array attribute
	bool isPostionAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_position();
	bool isTexCoordAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_texCoord();
	bool isNormalAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_normal();
	bool isColorAttribEnabled=Cc3dIndexVBO3d::isEnabledAttribArray_color();
    Cc3dIndexVBO3d::enableAttribArray_position(true);
	Cc3dIndexVBO3d::enableAttribArray_texCoord(true);
	Cc3dIndexVBO3d::enableAttribArray_normal(true);
	Cc3dIndexVBO3d::enableAttribArray_color(true);
    //apply state
    //for performance sake, we only apply state, not restore
    {
        //set depthTest
        CCDirector::sharedDirector()->setDepthTest(m_isDoDepthTest);
        //set blend function
        ccGLBlendFunc(m_blendFunc.src, m_blendFunc.dst);
    }
    
    //enable server state (i don't know what this means :( )
    ccGLEnable(m_eGLServerState);
    //pass values for cocos2d-x build-in uniforms
    m_pShaderProgram->use();
    m_pShaderProgram->setUniformsForBuiltins();
    //pass values for my own uniforms
    m_passUnifoCallback(this, m_program);
    //attach texture to texture attach point
    Cc3dIndexVBO3d::bindTexture(0, m_texture->getName());
    //draw
    m_indexVBO->setPointers();
    m_indexVBO->draw(GL_TRIANGLES);
    Cc3dIndexVBO3d::bindTexture(0, 0);
	//recover array attribute state
	Cc3dIndexVBO3d::enableAttribArray_position(isPostionAttribEnabled);
	Cc3dIndexVBO3d::enableAttribArray_texCoord(isTexCoordAttribEnabled);
	Cc3dIndexVBO3d::enableAttribArray_normal(isNormalAttribEnabled);
	Cc3dIndexVBO3d::enableAttribArray_color(isColorAttribEnabled);
}
/// CCAtlasNode - draw
KDvoid CCAtlasNode::draw ( KDvoid )
{
    CC_NODE_DRAW_SETUP ( );

    ccGLBlendFunc ( m_tBlendFunc.src, m_tBlendFunc.dst );

    GLfloat  aColors [ 4 ] = 
	{
		m_tDisplayedColor.r / 255.0f,
		m_tDisplayedColor.g / 255.0f,
		m_tDisplayedColor.b / 255.0f,
		m_cDisplayedOpacity / 255.0f
	};

#if defined ( USE_OPEN_GLES2 )
    getShaderProgram ( )->setUniformLocationWith4fv ( m_nUniformColor, aColors, 1 );
#else
	glColor4f ( aColors [ 0 ], aColors [ 1 ], aColors [ 2 ], aColors [ 3 ] );
#endif

    m_pTextureAtlas->drawNumberOfQuads ( m_uQuadsToDraw, 0 );
}
示例#19
0
void CAFlashView::draw()
{
    CAView::draw();
    ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
    kmMat4 matrixP;
    kmMat4 matrixMV;
    kmMat4 matrixMVP;
    kmGLGetMatrix(KM_GL_PROJECTION, &matrixP );
    kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV );
    matrixMV.mat[13] = this->getFrame().size.height + matrixMV.mat[13];
    kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV);
#if CC_ENABLE_GL_STATE_CACHE
    ccGLUseProgram(0);//valid program is NON_ZERO unsigned int
#endif
    CCSize size = this->getFrame().size;
    float localScaleX = size.width/ m_pFlash->getWidth();
    float localScaleY = -(size.height / m_pFlash->getHeight());
    float localScale = localScaleX > -localScaleY ? -localScaleY : localScaleX;
    kmMat4 matrixs;
    kmMat4Scaling(&matrixs, localScale, -localScale, getZOrder());
    kmMat4Multiply(&matrixMVP, &matrixMVP, &matrixs);
    m_pFlash->display(&matrixMVP);
    CHECK_GL_ERROR_DEBUG();
}
示例#20
0
void SpriteBlur::draw()
{
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
    ccBlendFunc blend = getBlendFunc();
    ccGLBlendFunc(blend.src, blend.dst);

    getShaderProgram()->use();
    getShaderProgram()->setUniformsForBuiltins();
    getShaderProgram()->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y);
    getShaderProgram()->setUniformLocationWith4fv(subLocation, sub_, 1);

    ccGLBiScutTexture2D( getTexture()->getName());

    //
    // Attributes
    //
#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);

    CC_INCREMENT_GL_DRAWS(1);
}
示例#21
0
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");
}
示例#22
0
void GAFSprite::draw(void)
{
	if (_isLocator)
	{
		return;
	}
    CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "GAFSprite - draw");
	
    CCAssert(!m_pobBatchNode, "If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");
	
    CC_NODE_DRAW_SETUP();
	
    if (_useSeparateBlendFunc)
    {
        glBlendFuncSeparate(_blendFuncSeparate.src,      _blendFuncSeparate.dst,
                            _blendFuncSeparate.srcAlpha, _blendFuncSeparate.dstAlpha);
    }
    else
    {
        ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
    }
    
    if (_blendEquation != -1)
    {
        glBlendEquation(_blendEquation);
    }
	
    if (m_pobTexture != NULL)
    {
        ccGLBindTexture2D( m_pobTexture->getName() );
    }
    else
    {
        ccGLBindTexture2D(0);
    }
    
    //
    // Attributes
    //
	
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
	setUniformsForFragmentShader();
	CHECK_GL_ERROR_DEBUG();
	
#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();	
	
    CC_INCREMENT_GL_DRAWS(1);
	
    CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "GAFSprite - draw");
}
示例#23
0
void CCSkeleton::draw () {
	CC_NODE_DRAW_SETUP();

	ccGLBlendFunc(blendFunc.src, blendFunc.dst);
	ccColor3B color = getColor();
	skeleton->r = color.r / (float)255;
	skeleton->g = color.g / (float)255;
	skeleton->b = color.b / (float)255;
	skeleton->a = getOpacity() / (float)255;

	CCTextureAtlas* textureAtlas = 0;
	ccV3F_C4B_T2F_Quad quad;
	quad.tl.vertices.z = 0;
	quad.tr.vertices.z = 0;
	quad.bl.vertices.z = 0;
	quad.br.vertices.z = 0;
	for (int i = 0, n = skeleton->slotCount; i < n; i++) {
		Slot* slot = skeleton->slots[i];
		if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
		RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
		CCTextureAtlas* regionTextureAtlas = (CCTextureAtlas*)((AtlasRegion*)attachment->rendererObject)->page->rendererObject;
		if (regionTextureAtlas != textureAtlas) {
			if (textureAtlas) {
				textureAtlas->drawQuads();
				textureAtlas->removeAllQuads();
			}
		}
		textureAtlas = regionTextureAtlas;
		if (textureAtlas->getCapacity() == textureAtlas->getTotalQuads() &&
			!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
		RegionAttachment_updateQuad(attachment, slot, &quad);
		textureAtlas->updateQuad(&quad, textureAtlas->getTotalQuads());
	}
	if (textureAtlas) {
		textureAtlas->drawQuads();
		textureAtlas->removeAllQuads();
	}

	if (debugSlots) {
		// Slots.
		ccDrawColor4B(0, 0, 255, 255);
		glLineWidth(1);
		CCPoint points[4];
		ccV3F_C4B_T2F_Quad quad;
		for (int i = 0, n = skeleton->slotCount; i < n; i++) {
			Slot* slot = skeleton->slots[i];
			if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
			RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
			RegionAttachment_updateQuad(attachment, slot, &quad);
			points[0] = ccp(quad.bl.vertices.x, quad.bl.vertices.y);
			points[1] = ccp(quad.br.vertices.x, quad.br.vertices.y);
			points[2] = ccp(quad.tr.vertices.x, quad.tr.vertices.y);
			points[3] = ccp(quad.tl.vertices.x, quad.tl.vertices.y);
			ccDrawPoly(points, 4, true);
		}
	}
	if (debugBones) {
		// Bone lengths.
		glLineWidth(2);
		ccDrawColor4B(255, 0, 0, 255);
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			float x = bone->data->length * bone->m00 + bone->worldX;
			float y = bone->data->length * bone->m10 + bone->worldY;
			ccDrawLine(ccp(bone->worldX, bone->worldY), ccp(x, y));
		}
		// Bone origins.
		ccPointSize(4);
		ccDrawColor4B(0, 0, 255, 255); // Root bone is blue.
		for (int i = 0, n = skeleton->boneCount; i < n; i++) {
			Bone *bone = skeleton->bones[i];
			ccDrawPoint(ccp(bone->worldX, bone->worldY));
			if (i == 0) ccDrawColor4B(0, 255, 0, 255);
		}
	}
}
// overriding draw method
void CCParticleSystemQuad::draw()
{    

	if (NULL == m_pTexture)
	{
		return;
	}
	
    CCAssert(!m_pBatchNode,"draw should not be called when added to a particleBatchNode");

    CC_NODE_DRAW_SETUP();

    ccGLBindTexture2D( m_pTexture->getName() );
    
#ifdef AND_ETC_SEP
    ccGLBindTexture2DN(1, m_pTexture->getNameAlpha());
#endif
    
    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    CCAssert( m_uParticleIdx == m_uParticleCount, "Abnormal error in particle quad");

#if CC_TEXTURE_ATLAS_USE_VAO
    //
    // Using VBO and VAO
    //
    ccGLBindVAO(m_uVAOname);

#if CC_REBIND_INDICES_BUFFER
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
#endif

    glDrawElements(GL_TRIANGLES, (GLsizei) m_uParticleIdx*6, GL_UNSIGNED_SHORT, 0);

#if CC_REBIND_INDICES_BUFFER
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif

#else
    //
    // Using VBO without VAO
    //

    #define kQuadSize sizeof(m_pQuads[0].bl)

    ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

    glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
    // vertices
    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, vertices));
    // colors
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, colors));
    // tex coords
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, texCoords));
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);

    glDrawElements(GL_TRIANGLES, (GLsizei) m_uParticleIdx*6, GL_UNSIGNED_SHORT, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

#endif

    CC_INCREMENT_GL_DRAWS(1);
    CHECK_GL_ERROR_DEBUG();
}
示例#25
0
void YUVSprite::draw()
{
    CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "YUVSprite - draw");
    
    CCAssert(!m_pobBatchNode, "If YUVSprite is being rendered by CCSpriteBatchNode, YUVSprite#draw SHOULD NOT be called");
   // CC_NODE_DRAW_SETUP();
    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );

    if(_prog){
        _prog->use();
        _prog->setUniformsForBuiltins();
    }
    if(textureDone){
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, id_y);
        glUniform1i(textureUniformY, 0);
        
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, id_u);
        glUniform1i(textureUniformU, 1);
        
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, id_v);
        glUniform1i(textureUniformV, 2);
        
        glUniform1f(borderUniformY,_border[0]);
        glUniform1f(borderUniformU,_border[1]);
        glUniform1f(borderUniformV,_border[2]);
    }
    //ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
    
#define kQuadSize sizeof(m_sQuad.bl)
#ifdef EMSCRIPTEN
    long offset = 0;
    setGLBufferData(&m_sQuad, 4 * kQuadSize, 0);
#else
    long offset = (long)&m_sQuad;
#endif // EMSCRIPTEN
    CHECK_GL_ERROR_DEBUG();
    
    GLfloat square[8];
    CCSize s = this->getTextureRect().size;
    CCPoint offsetPix = this->getOffsetPosition();
    
    square[0] = offsetPix.x;
    square[1] = offsetPix.y;
    
    square[2] = offsetPix.x+s.width;
    square[3] = offsetPix.y;
    
    square[6] = offsetPix.x+s.width;
    square[7] = offsetPix.y+s.height;
    
    square[4] = offsetPix.x;
    square[5] = offsetPix.y+s.height;

    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, square);
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    
    glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, 0, 0, coordVertices);
    glEnableVertexAttribArray(ATTRIB_TEXTURE);

    /*
    // 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_SPRITE_DEBUG_DRAW == 1
    // draw bounding box
    CCPoint vertices[4]={
        ccp(m_sQuad.tl.vertices.x,m_sQuad.tl.vertices.y),
        ccp(m_sQuad.bl.vertices.x,m_sQuad.bl.vertices.y),
        ccp(m_sQuad.br.vertices.x,m_sQuad.br.vertices.y),
        ccp(m_sQuad.tr.vertices.x,m_sQuad.tr.vertices.y),
    };
    ccDrawPoly(vertices, 4, true);
#elif CC_SPRITE_DEBUG_DRAW == 2
    // draw texture box
    CCSize s = this->getTextureRect().size;
    CCPoint offsetPix = this->getOffsetPosition();
    CCPoint vertices[4] = {
        ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y),
        ccp(offsetPix.x+s.width,offsetPix.y+s.height), ccp(offsetPix.x,offsetPix.y+s.height)
    };
    ccDrawPoly(vertices, 4, true);
#endif // CC_SPRITE_DEBUG_DRAW
    CC_INCREMENT_GL_DRAWS(1);
    
    CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "YUVSprite - draw");
}
示例#26
0
void Live2dXSprite::draw()
{
    if(!m_pobTexture)
    {
        return;
    }
    if(m_isAnimation)
    {
        cc_timeval nowTime;
        CCTime::gettimeofdayCocos2d(&nowTime, NULL);
        float deltime = (float)(nowTime.tv_sec - m_nowTime.tv_sec) + (float)(nowTime.tv_usec - m_nowTime.tv_usec)/1000000.0f;
        m_nowTime = nowTime;
        animationUpdate(deltime);
//        animationUpdate(1/30.0f);
    }
#define Live2dX_Vertex_Size sizeof(Live2dX_Vertex)
#define Live2dX_UV_Size sizeof(Live2dX_UV)
#define Live2dX_Color_Size sizeof(Live2dX_Color)
    
#ifndef __QT__
    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();
    
    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
    
    ccGLBindTexture2D( m_pobTexture->getName() );
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

    
    // vertex
    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, Live2dX_Vertex_Size, (void*)m_nowVertex.data());
    
    // texCoods
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, Live2dX_UV_Size, (void*)m_orginUV.data());
    
    // color
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, Live2dX_Color_Size, (void*)m_orginColor.data());
    
    
    glDrawArrays(GL_TRIANGLES, 0, (int)m_nowVertex.size());
    
    CHECK_GL_ERROR_DEBUG();
    
    CC_INCREMENT_GL_DRAWS(1);
    
    CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
#else
    
    glTranslatef(-0.5f,0,0);
    glEnable(GL_BLEND);
    
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D,m_pobTexture->m_name);
    
    for(int i = 0; i < m_nowVertex.size();i+=3)
    {
        float a,b;
        glBegin(GL_TRIANGLES);
        glColor4f(1,1,1,1);
        glTexCoord2f(m_orginUV[i].u,1-m_orginUV[i].v);
        a = m_nowVertex[i].x * 2 / m_baseSize.width - 1;
        b = m_nowVertex[i].y * 2 / m_baseSize.height-1;
        glVertex3f(a,b,0);
        glColor4f(1,1,1,1);
        glTexCoord2f(m_orginUV[i+1].u,1-m_orginUV[i+1].v);
        a = m_nowVertex[i+1].x * 2 / m_baseSize.width - 1;
        b = m_nowVertex[i+1].y * 2 / m_baseSize.height-1;
        glVertex3f(a,b,0);
        glColor4f(1,1,1,1);
        glTexCoord2f(m_orginUV[i+2].u,1-m_orginUV[i+2].v);
        a = m_nowVertex[i+2].x * 2 / m_baseSize.width - 1;
        b = m_nowVertex[i+2].y * 2 / m_baseSize.height-1;
        glVertex3f(a,b,0);
        glColor4f(1,1,1,1);
        glEnd();
    }
#endif
}
示例#27
0
void CCSuperAnim::draw()
{
	if (mAnimState == kAnimStateInvalid ||
		mAnimState == kAnimStateInitialized)
	{
		return;
	}
	
	if (!mAnimHandler.IsValid()) {
		return;
	}
	
	#define MAX_VERTEX_CNT 4096
	static ccVertex3F sVertexBuffer[MAX_VERTEX_CNT];
	static ccTex2F sTexCoorBuffer[MAX_VERTEX_CNT];
	static ccColor4B sColorBuffer[MAX_VERTEX_CNT];
	int anIndex = 0;
	
	static SuperAnimObjDrawInfo sAnimObjDrawnInfo;
	//float aPixelToPointScale = 1.0f / CC_CONTENT_SCALE_FACTOR();
    float anAnimContentHeightInPixel = getContentSize().height * CC_CONTENT_SCALE_FACTOR();
	BeginIterateAnimObjDrawInfo();
	while (IterateAnimObjDrawInfo(mAnimHandler, sAnimObjDrawnInfo)) {
		if (sAnimObjDrawnInfo.mSpriteId == InvalidSuperAnimSpriteId) {
			CCAssert(false, "Missing a sprite.");
			continue;
		}
		
		// check whether this sprite has been replaced
		SuperAnimSpriteId aCurSpriteId = sAnimObjDrawnInfo.mSpriteId;
		SuperSpriteIdToSuperSpriteIdMap::const_iterator anIter = mReplacedSpriteMap.find(aCurSpriteId);
		if (anIter != mReplacedSpriteMap.end()) {
			aCurSpriteId = anIter->second;
		}
			
		//SuperAnimSprite *aSprite = SuperAnimSpriteMgr::GetInstance()->GetSpriteById(aCurSpriteId);
		SuperAnimSprite *aSprite = (SuperAnimSprite*)aCurSpriteId;
		if (aSprite == NULL){
			CCAssert(false, "Missing a sprite.");
			continue;
		}
		
		// safe check!!
		if (mUseSpriteSheet) {
			CCAssert(mSpriteSheet == aSprite->mTexture, "must in the same texture!!");
		}
		
		// cocos2d the origin is located at left bottom, but is in left top in flash
		sAnimObjDrawnInfo.mTransform.mMatrix.m12 = anAnimContentHeightInPixel - sAnimObjDrawnInfo.mTransform.mMatrix.m12;
		// convert to point
		//sAnimObjDrawnInfo.mTransform.Scale(aPixelToPointScale, aPixelToPointScale);
		
		//sAnimObjDrawnInfo.mTransform.mMatrix.m12 *= -1;
				
		// Be sure that you call this macro every draw
		CC_NODE_DRAW_SETUP(this);
		
		ccV3F_C4B_T2F_Quad aOriginQuad = aSprite->mQuad;
		aSprite->mQuad = sAnimObjDrawnInfo.mTransform.mMatrix * aSprite->mQuad;
		ccColor4B aColor = ccc4(sAnimObjDrawnInfo.mColor.mRed, sAnimObjDrawnInfo.mColor.mGreen, sAnimObjDrawnInfo.mColor.mBlue, sAnimObjDrawnInfo.mColor.mAlpha);		
		aSprite->mQuad.bl.colors = aColor;
		aSprite->mQuad.br.colors = aColor;
		aSprite->mQuad.tl.colors = aColor;
		aSprite->mQuad.tr.colors = aColor;
		
		if (mIsFlipX) {
			float aWidthinPixel = getContentSize().width * CC_CONTENT_SCALE_FACTOR();
			aSprite->mQuad.bl.vertices.x = aWidthinPixel - aSprite->mQuad.bl.vertices.x;
			aSprite->mQuad.br.vertices.x = aWidthinPixel - aSprite->mQuad.br.vertices.x;
			aSprite->mQuad.tl.vertices.x = aWidthinPixel - aSprite->mQuad.tl.vertices.x;
			aSprite->mQuad.tr.vertices.x = aWidthinPixel - aSprite->mQuad.tr.vertices.x;
		}
		
		if (mIsFlipY) {
			float aHeightinPixel = getContentSize().height * CC_CONTENT_SCALE_FACTOR();
			aSprite->mQuad.bl.vertices.y = aHeightinPixel - aSprite->mQuad.bl.vertices.y;
			aSprite->mQuad.br.vertices.y = aHeightinPixel - aSprite->mQuad.br.vertices.y;
			aSprite->mQuad.tl.vertices.y = aHeightinPixel - aSprite->mQuad.tl.vertices.y;
			aSprite->mQuad.tr.vertices.y = aHeightinPixel - aSprite->mQuad.tr.vertices.y;
		}
		
		// draw
		if (!mUseSpriteSheet)
		{
			ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
			ccGLBindTexture2D(aSprite->mTexture->getName());
			//
			// Attributes
			ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
			
			#define kQuadSize sizeof(aSprite->mQuad.bl)
			long offset = (long)&aSprite->mQuad;
			
			// 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);
		} else {
			// 0
			sVertexBuffer[anIndex] = aSprite->mQuad.bl.vertices;
			sTexCoorBuffer[anIndex] = aSprite->mQuad.bl.texCoords;
			sColorBuffer[anIndex++] = aSprite->mQuad.bl.colors;
			// 1
			sVertexBuffer[anIndex] = aSprite->mQuad.tl.vertices;
			sTexCoorBuffer[anIndex] = aSprite->mQuad.tl.texCoords;
			sColorBuffer[anIndex++] = aSprite->mQuad.tl.colors;
			// 2
			sVertexBuffer[anIndex] = aSprite->mQuad.br.vertices;
			sTexCoorBuffer[anIndex] = aSprite->mQuad.br.texCoords;
			sColorBuffer[anIndex++] = aSprite->mQuad.br.colors;
			// 3
			sVertexBuffer[anIndex] = aSprite->mQuad.tl.vertices;
			sTexCoorBuffer[anIndex] = aSprite->mQuad.tl.texCoords;
			sColorBuffer[anIndex++] = aSprite->mQuad.tl.colors;
			// 4
			sVertexBuffer[anIndex] = aSprite->mQuad.tr.vertices;
			sTexCoorBuffer[anIndex] = aSprite->mQuad.tr.texCoords;
			sColorBuffer[anIndex++] = aSprite->mQuad.tr.colors;
			// 5
			sVertexBuffer[anIndex] = aSprite->mQuad.br.vertices;
			sTexCoorBuffer[anIndex] = aSprite->mQuad.br.texCoords;
			sColorBuffer[anIndex++] = aSprite->mQuad.br.colors;
			
			CCAssert(anIndex < MAX_VERTEX_CNT, "buffer is not enough");
		}
		
		aSprite->mQuad = aOriginQuad;
	}
	
	if (mUseSpriteSheet) {
		// Be sure that you call this macro every draw
		CC_NODE_DRAW_SETUP(this);
		
		ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
		ccGLBindTexture2D(mSpriteSheet->getName());
		//
		// Attributes
		ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
		
		// vertex
		glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, (void*) (sVertexBuffer));
		
		// texCoods
		glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sTexCoorBuffer));
		
		// color
		glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void*)(sColorBuffer));
		
		glDrawArrays(GL_TRIANGLES, 0, anIndex);
	}
}
void CBArmature::draw()
{
    if(m_preDrawFunction)
        m_preDrawFunction->execute();
    
    if (m_pParentBone == NULL && m_pBatchNode == NULL)
    {
        CC_NODE_DRAW_SETUP();
        ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
    }
    
    CCObject *object = NULL;
    CCARRAY_FOREACH(m_pChildren, object)
    {
        if (CCBone *bone = dynamic_cast<CCBone *>(object))
        {
            CCNode *node = bone->getDisplayRenderNode();
            
            if (NULL == node)
                continue;
            
            switch (bone->getDisplayRenderNodeType())
            {
                case CS_DISPLAY_SPRITE:
                {
                    CCSkin *skin = (CCSkin *)node;
                    
                    CCTextureAtlas *textureAtlas = skin->getTextureAtlas();
                    bool blendDirty = bone->isBlendDirty();
                    if(m_pAtlas != textureAtlas || blendDirty)
                    {
                        if (m_pAtlas)
                        {
                            if(CBTextureAtlas* pAtlas = dynamic_cast<CBTextureAtlas *>(m_pAtlas)) {
                                pAtlas->drawQuadsEx();
                            } else {
                                m_pAtlas->drawQuads();
                            }
                            m_pAtlas->removeAllQuads();
                        }
                    }
                    
                    m_pAtlas = textureAtlas;
                    if (m_pAtlas->getCapacity() == m_pAtlas->getTotalQuads() && !m_pAtlas->resizeCapacity(m_pAtlas->getCapacity() * 2))
                        return;
                    
                    skin->updateTransform();
                    
                    if (blendDirty)
                    {
                        ccBlendFunc func = bone->getBlendFunc();
                        ccGLBlendFunc(func.src, func.dst);
                        
                        if(CBTextureAtlas* pAtlas = dynamic_cast<CBTextureAtlas *>(m_pAtlas)) {
                            pAtlas->drawQuadsEx();
                        } else {
                            m_pAtlas->drawQuads();
                        }
                        m_pAtlas->removeAllQuads();
                        
                        ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
                        bone->setBlendDirty(false);
                    }
                }
                    break;
                case CS_DISPLAY_ARMATURE:
                {
                    CCArmature *armature = (CCArmature *)(node);
                    
                    CCTextureAtlas *textureAtlas = armature->getTextureAtlas();
                    if(m_pAtlas != textureAtlas)
                    {
                        if (m_pAtlas)
                        {
                            if(CBTextureAtlas* pAtlas = dynamic_cast<CBTextureAtlas *>(m_pAtlas)) {
                                pAtlas->drawQuadsEx();
                            } else {
                                m_pAtlas->drawQuads();
                            }
                            m_pAtlas->removeAllQuads();
                        }
                    }
                    armature->draw();
                    
                    m_pAtlas = armature->getTextureAtlas();
                }
                    break;
                default:
                {
                    if (m_pAtlas)
                    {
                        if(CBTextureAtlas* pAtlas = dynamic_cast<CBTextureAtlas *>(m_pAtlas)) {
                            pAtlas->drawQuadsEx();
                        } else {
                            m_pAtlas->drawQuads();
                        }
                        m_pAtlas->removeAllQuads();
                    }
                    node->visit();
                    
                    CC_NODE_DRAW_SETUP();
                    ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
                }
                    break;
            }
        }
        else if(CCNode *node = dynamic_cast<CCNode *>(object))
        {
            if (m_pAtlas)
            {
                if(CBTextureAtlas* pAtlas = dynamic_cast<CBTextureAtlas *>(m_pAtlas)) {
                    pAtlas->drawQuadsEx();
                } else {
                    m_pAtlas->drawQuads();
                }
                m_pAtlas->removeAllQuads();
            }
            node->visit();
            
            CC_NODE_DRAW_SETUP();
            ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
        }
    }
    
    if(m_pAtlas && !m_pBatchNode && m_pParentBone == NULL)
    {
        if(CBTextureAtlas* pAtlas = dynamic_cast<CBTextureAtlas *>(m_pAtlas)) {
            pAtlas->drawQuadsEx();
        } else {
            m_pAtlas->drawQuads();
        }
        m_pAtlas->removeAllQuads();
    }
}
示例#29
0
void CCSquirmAnimNode::draw()
{
	CCAssert(m_pobTexture != 0 && m_maskTexture != 0, "CCSquirmAnimNode::draw(), textures mustn't be null!");
	CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");

	// 如果顶点数据需要刷新,就刷新之
	if(m_vertices_dirty)
	{
		rebuildVertices();
	}

	CC_NODE_DRAW_SETUP();

	ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );

	ccGLBindTexture2DN(0, m_pobTexture->getName());
	ccGLBindTexture2DN(1, m_maskTexture->getName());

	//
	// Attributes
	//

	ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTexTex );

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

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

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

	diff = offsetof(ccV3F_C4B_T2Fx2, texCoords1);
	glVertexAttribPointer(kCCVertexAttrib_TexCoords1, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));

	// color
	diff = offsetof( ccV3F_C4B_T2Fx2, 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_SPRITE_DEBUG_DRAW == 1
	// draw bounding box
	CCPoint vertices[4]={
		ccp(m_sQuad.tl.vertices.x,m_sQuad.tl.vertices.y),
		ccp(m_sQuad.bl.vertices.x,m_sQuad.bl.vertices.y),
		ccp(m_sQuad.br.vertices.x,m_sQuad.br.vertices.y),
		ccp(m_sQuad.tr.vertices.x,m_sQuad.tr.vertices.y),
	};
	ccDrawPoly(vertices, 4, true);
#elif CC_SPRITE_DEBUG_DRAW == 2
	// draw texture box
	CCSize s = this->getTextureRect().size;
	CCPoint offsetPix = this->getOffsetPosition();
	CCPoint vertices[4] = {
		ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y),
		ccp(offsetPix.x+s.width,offsetPix.y+s.height), ccp(offsetPix.x,offsetPix.y+s.height)
	};
	ccDrawPoly(vertices, 4, true);
#endif // CC_SPRITE_DEBUG_DRAW

	CC_INCREMENT_GL_DRAWS(1);

	CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSquirmAnimNode - draw");
}
示例#30
0
void LaserSprite::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 );
        
	m_shader->use();
    m_shader->setUniformsForBuiltins();


	if (m_pobTexture != NULL)
	{
		//ccGLBindTexture2D( m_texure1->getName());
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, m_pobTexture->getName());
	}
	else
	{
		ccGLBindTexture2D(0);
	}

	// texture normalmap
	glActiveTexture(GL_TEXTURE1);
	glBindTexture( GL_TEXTURE_2D,  m_texMask1->getName());
	glUniform1i(m_loc_texMask1, 1);

	glActiveTexture(GL_TEXTURE2);
	glBindTexture( GL_TEXTURE_2D,  m_texNoise1->getName());
	glUniform1i(m_loc_texNoise1, 2);

	glActiveTexture(GL_TEXTURE3);
	glBindTexture( GL_TEXTURE_2D,  m_texNoise2->getName());
	glUniform1i(m_loc_texNoise2, 3);


	m_shader->setUniformLocationWith1f(m_loc_time, m_time);

	//
	// 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();
        
	CC_INCREMENT_GL_DRAWS(1);
        
	CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
}