예제 #1
0
void ShaderManager::loadShader(const char* key, const GLchar* vertexShader, const GLchar* fragShader)
{
	CCGLProgram * ccProgram = CCShaderCache::sharedShaderCache()->programForKey(key);
	if (ccProgram == NULL)
	{
		ccProgram = new CCGLProgram();
		ccProgram->autorelease();
		CCShaderCache::sharedShaderCache()->addProgram(ccProgram, key);
	}
	else
	{
		ccProgram->reset();
	}
	ccProgram->initWithVertexShaderByteArray(vertexShader, fragShader);
	CHECK_GL_ERROR_DEBUG();

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

	// 自定义着色器链接  
	ccProgram->link();  
	CHECK_GL_ERROR_DEBUG();  

	// 设置移动、缩放、旋转矩阵  
	ccProgram->updateUniforms();
	CHECK_GL_ERROR_DEBUG();
}
예제 #2
0
void CCShaders::loadCustomShader(const string& key) {
    if(!CCShaderCache::sharedShaderCache()->programForKey(key.c_str())) {
		// load shader
        CCGLProgram* p = new CCGLProgram();
		p->autorelease();
		LOAD_PROGRAM_IF(flash);
		LOAD_PROGRAM_IF(blur);
		
		// add attribute
		if(false) {
			// non-default situation
		} else {
			p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
			p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
			p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		}

		// link it
        p->link();
		
		// add custom uniform
		if(key == kCCShader_flash) {
			ADD_UNIFORM(flashColor);
			ADD_UNIFORM(flashTime);
		} else if(key == kCCShader_blur) {
			ADD_UNIFORM(blurSize);
			ADD_UNIFORM(blurSubtract);
		}
		
		// add standard uniforms
        p->updateUniforms();
		
		// add program
        CCShaderCache::sharedShaderCache()->addProgram(p, key.c_str());
    }
}