void HelloWorld::onDrawGradient(float textureWidth, float textureHeight)
{
	setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
	getGLProgram()->use();
	getGLProgram()->setUniformsForBuiltins();

	float gradientAlpha = 0.8f;

	int nVertices = 0;
	std::vector<Vec2> vertices;
	std::vector<Color4F> colors;

	vertices.push_back(Vec2(0, 0));
	colors.push_back(Color4F( 0, 0, 0, 0 ));

	vertices.push_back(Vec2(textureWidth, 0));
	colors.push_back(Color4F( 0.0f, 0.0f, 0.0f, 0.0f ));

	vertices.push_back(Vec2(0, textureHeight));
	colors.push_back(Color4F( 0, 0, 0, gradientAlpha ));

	vertices.push_back(Vec2(textureWidth, textureHeight));
	colors.push_back(Color4F( 0, 0, 0, gradientAlpha ));

	GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices.data());
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors.data());
	glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)vertices.size());
}
bool ShaderNode::initWithWH(float w, float h)
{
    if (!Node::init())
        return false;
    
    _rendertTexture = chaung::RenderTexture::create(w, h);
    //_rendertTexture->setKeepMatrix(true);
    _rendertTexture->retain();
    
    _texture = _rendertTexture->getSprite()->getTexture();
    _texture->retain();
    
    Size size = _texture->getContentSizeInPixels();
    _vertices.push_back(Point::ZERO);
    _vertices.push_back(Point(size.width, 0));
    _vertices.push_back(Point(0, size.height));
    _vertices.push_back(Point(size.width, size.height));
    
    _texCoords.push_back(Point::ZERO);
    _texCoords.push_back(Point(1, 0));
    _texCoords.push_back(Point(0, 1));
    _texCoords.push_back(Point(1, 1));
    
    setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
    
    Node::setContentSize(Size(w, h));
    
    return true;
}
void TMXLayer::parseInternalProperties()
{
    auto vertexz = getProperty("cc_vertexz");
    if (vertexz.isNull()) return;
    
    std::string vertexZStr = vertexz.asString();
    // If "automatic" is on, then parse the "cc_alpha_func" too
    if (vertexZStr == "automatic")
    {
        _useAutomaticVertexZ = true;
        auto alphaFuncVal = getProperty("cc_alpha_func");
        float alphaFuncValue = alphaFuncVal.asFloat();
        setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
        
        GLint alphaValueLocation = glGetUniformLocation(getGLProgram()->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
        
        // NOTE: alpha test shader is hard-coded to use the equivalent of a glAlphaFunc(GL_GREATER) comparison
        
        // use shader program to set uniform
        getGLProgram()->use();
        getGLProgram()->setUniformLocationWith1f(alphaValueLocation, alphaFuncValue);
        CHECK_GL_ERROR_DEBUG();
    }
    else
    {
        _vertexZvalue = vertexz.asInt();
    }
}
void BlurScene::runOwnAct()
{
	auto shader = GLProgram::createWithFilenames("myShader/MVP_Stand.vert", "myShader/SharpFilter.frag");

	auto state = hero->getGLProgramState();

	state->setGLProgram(shader);

	state->setUniformFloat(shader->getUniformLocationForName("u_number"), 0.01);
}
示例#5
0
bool GAFSprite::initWithTexture(cocos2d::Texture2D *pTexture, const cocos2d::Rect& rect, bool rotated)
{
    if (cocos2d::Sprite::initWithTexture(pTexture, rect, rotated))
    {
        setGLProgram(cocos2d::ShaderCache::getInstance()->getGLProgram(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
        return true;
    }
    else
    {
        return false;
    }
}
示例#6
0
// designated initializer
bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect, bool rotated)
{
    bool result;
    if (Entity::init())
    {
        //_batchNode = nullptr;
        
        _recursiveDirty = false;
        setDirty(false);
        
        _opacityModifyRGB = true;
        
        _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
        
        _flippedX = _flippedY = false;
        
        // default transform anchor: center
        setAnchorPoint(Point(0.5f, 0.5f));
        
        // zwoptex default values
        _offsetPosition = Point(0.f, 0.f);

        // add vbo 3+4+2
        _vbo = VBO::create(VERTEX_SIZE,VERTICES_PER_SPRITE);
		struct VBOAttribute *attributes[3] = {
        	new VBOAttribute(GLProgram::ATTRIBUTE_NAME_POSITION,GLProgram::VERTEX_ATTRIB_POSITION,0,3,GL_FLOAT,false),
        	new VBOAttribute(GLProgram::ATTRIBUTE_NAME_COLOR,GLProgram::VERTEX_ATTRIB_COLOR,3*sizeof(float),4,GL_FLOAT,false),
        	new VBOAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD,GLProgram::VERTEX_ATTRIB_TEX_COORD,7*sizeof(float),2,GL_FLOAT,false)
    	};
		_vbo->setAttributes(attributes,3);
        

        GLProgram* program = GLProgram::createWithByteArrays(Shader::PositionTextureColor_vert,Shader::PositionTextureColor_frag);
        // shader state
        setGLProgram(program);

        // update texture (calls updateBlendFunc)
        setTexture(texture);
        setTextureRect(rect, rotated, rect.size);

        // by default use "Self Render".
        // if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render"
        //setBatchNode(nullptr);
        result = true;
    }
    else
    {
        result = false;
    }
    _recursiveDirty = true;
    setDirty(true);
    return result;
}
bool HBlurNode::initWithWH(float w, float h, unsigned int n)
{
    if (!ShaderNode::initWithWH(w, h))
        return false;
    
    GLProgram* program = GLProgram::createWithFilenames("Shaders/ccShader_blur.vert", "Shaders/Blur_horizontal_sampling.frag");
    setGLProgram(program);
    
    _n = n;
    
    return true;
}
// on "init" you need to initialize your instance
bool ShadowLayer::init() {
    if (!LayerColor::initWithColor(Color4B(0, 0, 0, 180))) {
        return false;
    }

    _lightShaderProgram = GLProgram::createWithFilenames("res/shaders/vertexShader.vert", "res/shaders/fragmentShader.frag");
    setGLProgram(_lightShaderProgram);
    
    _lightSize = 0.0;
    _lightPosition = Point(0, 0);

    return true;
}
示例#9
0
bool SpineBatchNode::init()
{
    if (Node::init()) {

        _batch = PolygonBatch::createWithCapacity(2000); // Max number of vertices and triangles per batch.
        _batch->retain();

        setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));

        return true;
    }
    return false;
}
示例#10
0
bool EasyPolygon::initWithPointsandTextureusingTriangulator(Vector2dVector &polygonPoints, Texture2D *fillTexture, PRRatcliffTriangulator* polygonTriangulator) {
    
    triangulator = polygonTriangulator;
    
#ifdef __GLES2
    setGLProgram(CCShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
#endif
    
    setTexture(fillTexture);
    setPoints(polygonPoints);
    
	return true;
}
示例#11
0
    ParticleSystemQuad::ParticleSystemQuad(fzUInt number, Texture2D *texture)
    : ParticleSystem(number, texture)
    {
        // allo cating quads
        p_quads = new fzC4_T2_V2_Quad[getTotalParticles()];
        
        // initialize only once the texCoords and the indices
        initTexCoordsWithRect(fzRect(FZPointZero, p_texture->getContentSize()));
        initIndices();
        
#if FZ_GL_SHADERS
        setGLProgram(kFZShader_mat_aC4_TEX);
#endif  
    }
示例#12
0
Hills::Hills() {
    Sprite* groundSprite = Sprite::create("DemoAssets/Ground_Demo.png");
    groundSprite->retain();
    groundTexture = groundSprite->getTexture();
    groundTexture->setTexParameters( {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT} );
    
    Sprite* grassSprite = Sprite::create("DemoAssets/grass_Demo.png");
    grassSprite->retain();
    grassTexture = grassSprite->getTexture();
    grassTexture->setTexParameters( {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT} );
    
    GLProgram *shaderProgram = ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
    setGLProgram(shaderProgram);
    scheduleUpdate();
}
示例#13
0
    Node::~Node()
    {
#if FZ_GL_SHADERS
        setFilter(NULL);
        setGLProgram(nullptr);
#endif
        if(p_camera)
            delete p_camera;
        
        Node *child;
        FZ_LIST_FOREACH_MUTABLE(m_children, child)
        {
            child->setParent(NULL);
            child->release();
        }
void HelloWorld::onDrawStripes(Color4F c2, float textureWidth, float textureHeight, int nStripes)
{
	std::vector<Vec2> vertices;
	std::vector<Color4F> colors;

	float x1 = -textureHeight;
	float x2;
	float y1 = textureHeight;
	float y2 = 0;
	float dx = textureWidth / nStripes * 2;
	float stripeWidth = dx / 2;
	for (int i = 0; i < nStripes; i++) {
		x2 = x1 + textureHeight;

		vertices.push_back(Vec2(x1, y1));
		colors.push_back(Color4F( c2.r, c2.g, c2.b, c2.a ));

		vertices.push_back(Vec2(x1 + stripeWidth, y1));
		colors.push_back(Color4F( c2.r, c2.g, c2.b, c2.a ));

		vertices.push_back(Vec2(x2, y2));
		colors.push_back(Color4F( c2.r, c2.g, c2.b, c2.a ));

		vertices.push_back(vertices[vertices.size() - 2]);
		colors.push_back(Color4F( c2.r, c2.g, c2.b, c2.a ));

		vertices.push_back(vertices[vertices.size() - 2]);
		colors.push_back(Color4F( c2.r, c2.g, c2.b, c2.a ));

		vertices.push_back(Vec2(x2 + stripeWidth, y2));
		colors.push_back(Color4F( c2.r, c2.g, c2.b, c2.a ));
		x1 += dx;
	}

	setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
	getGLProgram()->use();
	getGLProgram()->setUniformsForBuiltins();

	GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices.data());
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_TRUE, 0, colors.data());
	GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());
}
示例#15
0
Sprite::Sprite(const Vec3& position, Texture* texture, const Color4& color /* = Color4(1.0f,1.0f,1.0f,1.0f) */) :
	m_texture(texture)
{
	int width = m_texture->getWidth();
	int height = m_texture->getHeight();

	m_size = Size(width, height);
	m_position = position;
	m_color = color;

	m_vertexes[0] = PositionTextureColor( Vec3(-width / 2 + position.x,  height / 2 + position.y, position.z), Vec2(0.0f, 0.0f), color);
	m_vertexes[1] = PositionTextureColor( Vec3(-width / 2 + position.x, -height / 2 + position.y, position.z), Vec2(0.0f, 1.0f), color);
	m_vertexes[2] = PositionTextureColor( Vec3( width / 2 + position.x,  height / 2 + position.y, position.z), Vec2(1.0f, 0.0f), color);
	m_vertexes[3] = PositionTextureColor( Vec3( width / 2 + position.x, -height / 2 + position.y, position.z), Vec2(1.0f, 1.0f), color);

	glGenBuffers(1, &m_vbo);

	setGLProgram(PositionTextureColorVert, PositionTextureColorFrag);
}
bool TMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
{    

    if( tilesetInfo )
    {
        _texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage);
        _texture->retain();
    }

    // layerInfo
    _layerName = layerInfo->_name;
    _layerSize = layerInfo->_layerSize;
    _tiles = layerInfo->_tiles;
    _quadsDirty = true;
    setOpacity( layerInfo->_opacity );
    setProperties(layerInfo->getProperties());

    // tilesetInfo
    _tileSet = tilesetInfo;
    CC_SAFE_RETAIN(_tileSet);

    // mapInfo
    _mapTileSize = mapInfo->getTileSize();
    _layerOrientation = mapInfo->getOrientation();

    // offset (after layer orientation is set);
    Vec2 offset = this->calculateLayerOffset(layerInfo->_offset);
    this->setPosition(CC_POINT_PIXELS_TO_POINTS(offset));

    this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(_layerSize.width * _mapTileSize.width, _layerSize.height * _mapTileSize.height)));
    
    this->tileToNodeTransform();

    // shader, and other stuff
    setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
    
    _useAutomaticVertexZ = false;
    _vertexZvalue = 0;

    return true;
}
示例#17
0
bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
{
    _blurRadius = 0;
    if (Sprite::initWithTexture(texture, rect))
    {
#if CC_ENABLE_CACHE_TEXTURE_DATA
        auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event) {
            setGLProgram(nullptr);
            initGLProgram();
        });

        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif

        initGLProgram();

        return true;
    }

    return false;
}
示例#18
0
//------------------------------------------------------------------
//
// Atlas1
//
//------------------------------------------------------------------
Atlas1::Atlas1()
{
    setGLProgram(GLProgramCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
    _textureAtlas = TextureAtlas::create(s_AtlasTest, 3); _textureAtlas->retain();
    
    auto s = Director::getInstance()->getWinSize();

    //
    // Notice: u,v tex coordinates are inverted
    //
    V3F_C4B_T2F_Quad quads[] = 
    {
        {
            {Vec3(0,0,0),Color4B(0,0,255,255),Tex2F(0.0f,1.0f),},                // bottom left
            {Vec3(s.width,0,0),Color4B(0,0,255,0),Tex2F(1.0f,1.0f),},            // bottom right
            {Vec3(0,s.height,0),Color4B(0,0,255,0),Tex2F(0.0f,0.0f),},            // top left
            {Vec3(s.width,s.height,0),Color4B(0,0,255,255),Tex2F(1.0f,0.0f),},    // top right
        },        
        {
            {Vec3(40,40,0),Color4B(255,255,255,255),Tex2F(0.0f,0.2f),},            // bottom left
            {Vec3(120,80,0),Color4B(255,0,0,255),Tex2F(0.5f,0.2f),},            // bottom right
            {Vec3(40,160,0),Color4B(255,255,255,255),Tex2F(0.0f,0.0f),},        // top left
            {Vec3(160,160,0),Color4B(0,255,0,255),Tex2F(0.5f,0.0f),},            // top right
        },

        {
            {Vec3(s.width/2,40,0),Color4B(255,0,0,255),Tex2F(0.0f,1.0f),},        // bottom left
            {Vec3(s.width,40,0),Color4B(0,255,0,255),Tex2F(1.0f,1.0f),},        // bottom right
            {Vec3(s.width/2-50,200,0),Color4B(0,0,255,255),Tex2F(0.0f,0.0f),},        // top left
            {Vec3(s.width,100,0),Color4B(255,255,0,255),Tex2F(1.0f,0.0f),},        // top right
        },
        
    };
    
    
    for( int i=0;i<3;i++) 
    {
        _textureAtlas->updateQuad(&quads[i], i);
    }
}
示例#19
0
    Sprite::Sprite()
    : m_mode            (kFZSprite_Uninitialized)
    , m_textureRect     (FZRectZero)
    , m_offset          (FZPointZero)
    , m_unflippedOffset (FZPointZero)
    , m_color           (fzWHITE)
    , m_alpha           (255)
    , m_rectRotated     (false)
    , m_flipX           (false)
    , m_flipY           (false)
    , mode              ()
    {
        useSelfRender();

        setIsRelativeAnchorPoint(true);
        setAnchorPoint(0.5f, 0.5f);
        
#if FZ_GL_SHADERS
        // shader program
        setGLProgram(kFZShader_mat_uC4_TEX);
#endif
    }
示例#20
0
bool DHSkeleton::init() {
    if (!m_data || !Node::init()) {
        return false;
    }
    
    m_bones = m_data->newBones();
    
    m_slots = m_data->newSlots(this);
    
    m_drawOrder = new DHSlot*[m_data->getSlotCount()];
    memcpy(m_drawOrder, m_slots, sizeof(DHSlot*) * m_data->getSlotCount());
    
    m_ikConstraints = m_data->newIkConstraints(this);
    
    updateCache();
    
    updateWorldTransform();
    
    setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
    
    return true;
}
void LayerShaderStudy05::initData()
{
    GLProgram* pProgram = GLProgram::createWithFilenames("res/shader_study/pos_col_uniform.vert", "res/shader_study/pos_col_uniform.frag");
    setGLProgram(pProgram);
    
    Size winSize = Director::getInstance()->getWinSize();
    GLfloat vertexlist[] = {
        0.0f,0.0f,
        winSize.width,0.0f,
        winSize.width*0.5f,winSize.height};
    
    // gen bind vao
    glGenVertexArrays(1, &m_vao);
    glBindVertexArray(m_vao);
    // gen bind vbo
    glGenBuffers(1, &m_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexlist), vertexlist, GL_STREAM_DRAW);
    
    // input params
    GLuint attributePositionID = glGetAttribLocation(pProgram->getProgram(), "a_position");
    // vertex
    glEnableVertexAttribArray(attributePositionID);
    //    glVertexAttribPointer(attributePositionID, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
    // 注意:stride参数是取了 2-GL_FLOAT 的内容后的偏移值,所以此处为0 可以理解为第一个参数和第二个参数之间的内存间隔,上面注释内容是从Demo3和Demo4中取得的代码,可以和此处进行对比,则理解更为透彻
    // 而最后一个参数,是在数组中需要访问数据的起始地址,是相对于数组的地址,而不是内存的地址
    glVertexAttribPointer(attributePositionID, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
    
    // uniform
    GLuint uniformColorID = glGetUniformLocation(pProgram->getProgram(), "u_color");
    glUniform4f(uniformColorID, 0, 0, 1, 1);
    
    // clear
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    
    CHECK_GL_ERROR_DEBUG();
}
示例#22
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
}
示例#23
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;
}
示例#24
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;


}
void FilteredSpriteWithOne::clearFilter()
{
    _pFilters.clear();
    //CCLOG("FilteredSpriteWithOne::clearFilter");
    setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
}