Esempio n. 1
0
GLProgram make_program(const char * _vshader_path,const char * _fshader_path){
  std::string vshader_path = std::string(_vshader_path);
  std::string fshader_path = std::string(_fshader_path);
  GLProgram sprogram;
	
  sprogram.vertex_shader=make_shader(vshader_path,GL_VERTEX_SHADER);
  sprogram.fragment_shader=make_shader(fshader_path,GL_FRAGMENT_SHADER);

  sprogram.gl_ref = glCreateProgram();
  
  glAttachShader(sprogram.gl_ref, sprogram.vertex_shader);
  glAttachShader(sprogram.gl_ref, sprogram.fragment_shader);

  glLinkProgram(sprogram.gl_ref);
  GLint status;
  glGetProgramiv (sprogram.gl_ref, GL_LINK_STATUS, &status);
  if (status == GL_FALSE)
    {
      GLint infoLogLength;
      glGetProgramiv(sprogram.gl_ref, GL_INFO_LOG_LENGTH, &infoLogLength);
      
      GLchar *strInfoLog = new GLchar[infoLogLength + 1];
      glGetProgramInfoLog(sprogram.gl_ref, infoLogLength, NULL, strInfoLog);
      fprintf(stderr, "Linker failure: %s\n", strInfoLog);
      delete[] strInfoLog;
    }		
  sprogram.link();
  return sprogram;
}
inline GLProgram buildProgram(std::initializer_list<GLShader> shaders) {
    GLProgram program;
    for (const auto& shader : shaders) {
        program.attachShader(shader);
    }

    if (!program.link()) {
        std::cerr << program.getInfoLog() << std::endl;
        throw std::runtime_error(program.getInfoLog());
    }

    return program;
}
Esempio n. 3
0
void Utils::addGray(Sprite* sp)
{
    //return;
    GLProgram * p = new GLProgram();
    //GLProgram* p = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
    p->initWithFilenames("gray.vsh", "gray.fsh");
    p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
    p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
    p->link();
    p->updateUniforms();
    sp->setGLProgram(p);
}
Esempio n. 4
0
void ToolFunc::graySprite(cocos2d::ui::ImageView* sprite)
{
	if(sprite)  
	{  
		GLProgram * p = new GLProgram();  
		p->initWithFilenames("Shader/gray.vsh", "Shader/gray.fsh");  
		p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);  
		p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);  
		p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);  
		p->link();  
		p->updateUniforms();  
		sprite->setShaderProgram(p);  
	} 
}
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{
    GLProgram *shader = new GLProgram();
    shader->initWithVertexShaderFilename(vert, frag);

    shader->addAttribute("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
    shader->link();

    shader->updateUniforms();

    _uniformCenter = glGetUniformLocation(shader->getProgram(), "center");
    _uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution");
    _uniformTime = glGetUniformLocation(shader->getProgram(), "time");

    this->setShaderProgram(shader);

    shader->release();
}
Esempio n. 6
0
void init()
{
    glfwWindowHint(GLFW_SAMPLES, 15);
    glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
    glClearColor(0.1f, 0.2f, 0.3f, 1.0f);

    cube = new Cube(false, 1);

    GLShader vert(GLShader::GLShaderType::VERTEX);
    vert.readShader("fog.vert");
    vert.compileShader();
    GLShader frag(GLShader::GLShaderType::FRAGMENT);
    frag.readShader("fog.frag");
    frag.compileShader();

    program.setShaders({ vert.getId(), frag.getId() });
    program.link();

    model = glm::mat4(1.0f);

    view = glm::lookAt(eye, target, up);

    projection = glm::perspective(45.0f, (float)WIN_WIDTH / WIN_HEIGHT, 0.1f, 100.0f);

    program.use();
    /*program.setUniform("Light[0].Position", view * worldLight);
    program.setUniform("Light[0].Intensity", 0.8f, 0.5f, 0.5f);
    worldLight = glm::vec4(7.0f, 5.0f, 5.0f, 1.0f);
    program.setUniform("Light[1].Position", view * worldLight);
    program.setUniform("Light[1].Intensity", -0.8f, 0.5f, 0.5f);
    */

    program.setUniform("sli.Position", eye);
    program.setUniform("sli.intensity", 0.8f, 0.5f, 0.5f);
    program.setUniform("sli.direction", target);
    program.setUniform("sli.exponent", 3);
    program.setUniform("sli.cutoff", 0.1f);

}
Esempio n. 7
0
void FSprite::InitShader(const std::string& alpha_file)
{
#ifdef ETC1
    //Shader
    GLProgram* glp = new GLProgram();
    glp->autorelease();
    
    glp->initWithFilenames("testv.vsh", "test.fsh");
    glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
    glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD);
    glp->link();
    glp->updateUniforms();
    
    opacity_location_ = glGetUniformLocation(glp->getProgram(), "u_opacity");
    
    current_alpha_file_ = alpha_file;
    
//  #pragma message WARN("getTextureForKey or someth else, texture should be added on loading scene, do not add (cocos should check that anyway but still dont do it)  all the time!!")
    Texture2D* tex = Director::getInstance()->getTextureCache()->addImage(alpha_file);
	setBlendFunc(BlendFunc({ GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }));
    //setBlendFunc((BlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA });
    
    GLProgramState* glprogramstate = GLProgramState::getOrCreateWithGLProgram(glp);
    
    setGLProgramState(glprogramstate);
    
    glprogramstate->setUniformFloat(opacity_location_, (float)getOpacity()/255.0);
    
    glprogramstate->setUniformTexture("u_texture1", tex);
    

    
    setGLProgram(glp);
#endif
}
Esempio n. 8
0
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto label = LabelTTF::create("Hello World", "Arial", 24);
    
    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label, 1);

    // add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    auto _gridNodeTarget = NodeGrid::create();
    _gridNodeTarget->addChild(sprite);
    this->addChild(_gridNodeTarget);
    // add the sprite as a child to this layer
//    this->addChild(sprite, 0);
    
    GLProgram* shader = new GLProgram();
    sprite->setGLProgram(shader);
    shader->initWithFilenames("sepia.vsh", "sepia.fsh");
    shader->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    shader->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD);
    shader->link();
    shader->updateUniforms();
    
    shader->setUniformLocationWith1i(shader->getUniformLocationForName("u_enableSepia"), true);
    shader->setUniformLocationWith1i(shader->getUniformLocationForName("u_enableGray"), true);

    auto action = Waves3D::create(10, Size(15,10), 5, 40);;
    _gridNodeTarget->runAction(RepeatForever::create(action));
//	CCSprite *sprite = [CCSprite spriteWithImageNamed:@"Rocks.png"];
//	sprite.shader = [CCShader shaderNamed:self.shaderName];
//	
//	sprite.shaderUniforms[@"u_CausticTexture"] = [CCTexture textureWithFile:@"Caustic.png"];
//	
//	CCTexture *noise = [CCTexture textureWithFile:@"BisectionNoise.png"];
//	noise.texParameters = &(ccTexParams){GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
//	sprite.shaderUniforms[@"u_NoiseTexture"] = noise;
    
//    auto texture0 = Director::getInstance()->getTextureCache()->addImage("Images/grossini_dance_atlas.png");
//    texture0->generateMipmap();
//    Texture2D::TexParams texParams = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
//    texture0->setTexParameters(texParams);
    
    return true;
}
Esempio n. 9
0
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();



    auto center = Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);

    auto bg = Sprite::create("bg.png");
    bg->setPosition(center);
    auto bgsize = bg->getContentSize();
    float scaleX = visibleSize.width / bgsize.width;
    float scaleY = visibleSize.height / bgsize.height;
    bg->setScaleX(scaleX);
    bg->setScaleY(scaleY);
    addChild(bg);

    auto grass = Sprite::create("grass.png");
    grass->setAnchorPoint(ccp(0.5, 0.5));
    grass->setPosition(center);
    addChild(grass);

    Texture2D::TexParams p;
    p.minFilter = GL_LINEAR;
    p.magFilter = GL_LINEAR;
    p.wrapS = GL_REPEAT;
    p.wrapT = GL_REPEAT;

    glActiveTexture(GL_TEXTURE0);

    auto textureCache = Director::getInstance()->getTextureCache();
    auto tex1 = textureCache->addImage("grass.png");
    //tex1->setTexParameters(p);

    GLProgram* prog = new GLProgram();
    prog->initWithFilenames("perlin_wind.vert", "perlin_wind.frag");
    prog->retain();
    prog->link();
    prog->use();
    prog->updateUniforms();


    float gtime = Director::getInstance()->getDeltaTime();
    float ctime = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();

    //CCLog("Wind is %f", pnoise);

    ShaderCache::getInstance()->addGLProgram(prog, "perlin_wind");

    grass->setGLProgram(prog);

    GLProgramState* state = GLProgramState::getOrCreateWithGLProgram(prog);

    //state->setUniformFloat("u_gtime", 1.0f);
    //state->setUniformFloat("u_ctime",0.5f);
    //state->setUniformFloat("u_color", m_cloudAmount);
    state->setUniformFloat("u_wind", 0);
    schedule(schedule_selector(HelloWorld::setWind));
    prog->release();

    schedule(schedule_selector(HelloWorld::setWind));
    return true;


}
Esempio n. 10
0
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);


    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);
    auto playSp = Sprite::create("general_right.png");
    playSp->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(playSp);
    /*auto backgroundButton = Scale9Sprite::create("general_right.png");
    auto titleButton = LabelTTF::create("", "Marker Felt", 30);
    ControlButton *button = ControlButton::create(titleButton, backgroundButton);
    button->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    button->setAdjustBackgroundImage(false); 
    this->addChild(button);*/
    GLchar * pszFragSource =
    "#ifdef GL_ES \n \
    precision mediump float; \n \
    #endif \n \
    uniform sampler2D u_texture; \n \
    varying vec2 v_texCoord; \n \
    varying vec4 v_fragmentColor; \n \
    void main(void) \n \
    { \n \
    // Convert to greyscale using NTSC weightings \n \
    float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114)); \n \
    gl_FragColor = vec4(grey, grey, grey, 1.0); \n \
    }";
    GLProgram* greyShader = new GLProgram();
    greyShader->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
    greyShader->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    greyShader->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
    greyShader->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
    greyShader->link();
    greyShader->updateUniforms();
    //backgroundButton->setShaderProgram(greyShader);
    sprite->setShaderProgram(greyShader);
    playSp->setShaderProgram(greyShader);
    /*Array * children = button->getChildren();
    for(int i = 0;i < children->count();i ++){
        ((Node *)children->getObjectAtIndex(i))->setShaderProgram(greyShader);
    }*/
    
    greyShader->release();
    
    return true;
}
Esempio n. 11
0
void Skin::setSkinEffectShaderProgram()
{
	switch (_effectType)
	{
	case cocostudio::SkinEffect_None: setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));break;
	case cocostudio::SkinEffect_Grey:
		{
			const GLchar* fragSource = SkinEffectShader_Grey;
			GLProgram* program = new GLProgram();
			program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
			setShaderProgram(program);
			program->release();

			CHECK_GL_ERROR_DEBUG();

			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);

			CHECK_GL_ERROR_DEBUG();

			program->link();

			CHECK_GL_ERROR_DEBUG();

			program->updateUniforms();

			CHECK_GL_ERROR_DEBUG();
		}
		break;
	case cocostudio::SkinEffect_Elite:
		{
			const GLchar* fragSource = SkinEffectShader_Elite;
			GLProgram* program = new GLProgram();
			program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
			setShaderProgram(program);
			program->release();

			CHECK_GL_ERROR_DEBUG();

			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);

			CHECK_GL_ERROR_DEBUG();

			program->link();

			CHECK_GL_ERROR_DEBUG();

			program->updateUniforms();

			CHECK_GL_ERROR_DEBUG();
		}
		break;
	case cocostudio::SkinEffect_HighLight:
		{
			const GLchar* fragSource = SkinEffectShader_HighLight;
			GLProgram* program = new GLProgram();
			program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
			setShaderProgram(program);
			program->release();

			CHECK_GL_ERROR_DEBUG();

			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
			program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);

			CHECK_GL_ERROR_DEBUG();

			program->link();

			CHECK_GL_ERROR_DEBUG();

			program->updateUniforms();

			CHECK_GL_ERROR_DEBUG();
		}
		break;
	default:
		break;
	}
}