bool GameLayerMapBorder::init() { GameLayerBase::init(); setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); _mapGrid = nullptr; _quads = nullptr; GameLayerMapBorder::initQuadMap(); // if(!_itemBorderLine) = new ItemModel(); static ItemModel itemline; itemline.setType(TypeBorderLine); _itemBorderLine =&itemline; // _itemBorderLine->setType(111); return true; }
void BlurSprite::initGLProgram() { GLchar * fragSource = (GLchar*)String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString(); auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource); auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program); setGLProgramState(glProgramState); auto size = getTexture()->getContentSizeInPixels(); getGLProgramState()->setUniformVec2("resolution", size); getGLProgramState()->setUniformFloat("blurRadius", 8.00f); getGLProgramState()->setUniformFloat("sampleNum", 7.0f); }
// designated initializer bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect, bool rotated) { bool result = false; if (Node::init()) { _batchNode = nullptr; _recursiveDirty = false; setDirty(false); _opacityModifyRGB = true; _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; _flippedX = _flippedY = false; // default transform anchor: center setAnchorPoint(Vec2(0.5f, 0.5f)); // zwoptex default values _offsetPosition.setZero(); // clean the Quad memset(&_quad, 0, sizeof(_quad)); // Atlas: Color _quad.bl.colors = Color4B::WHITE; _quad.br.colors = Color4B::WHITE; _quad.tl.colors = Color4B::WHITE; _quad.tr.colors = Color4B::WHITE; // shader state setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)); // 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; } _recursiveDirty = true; setDirty(true); return result; }
void DrawNode3D::loadShaderVertex(const std::string &vert, const std::string &frag) { auto fileUtiles = FileUtils::getInstance(); // frag auto fragmentFilePath = fileUtiles->fullPathForFilename(frag); auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath); // vert auto vertexFilePath = fileUtiles->fullPathForFilename(vert); auto vertSource = fileUtiles->getStringFromFile(vertexFilePath); auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); setGLProgramState(glprogramstate); }
bool DrawNode3D::init() { _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR)); ensureCapacity(512); if (Configuration::getInstance()->supportsShareableVAO()) { glGenVertexArrays(1, &_vao); GL::bindVAO(_vao); } glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B)* _bufferCapacity, _buffer, GL_STREAM_DRAW); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, vertices)); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, colors)); glBindBuffer(GL_ARRAY_BUFFER, 0); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } CHECK_GL_ERROR_DEBUG(); _dirty = true; #if CC_ENABLE_CACHE_TEXTURE_DATA // Need to listen the event only when not use batchnode, because it will use VBO auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){ /** listen the event that coming to foreground on Android */ this->init(); }); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); #endif return true; }
bool ProgressTimer::initWithSprite(Sprite* sp) { setPercentage(0.0f); _vertexData = nullptr; _vertexDataCount = 0; setAnchorPoint(Vector2(0.5f,0.5f)); _type = Type::RADIAL; _reverseDirection = false; setMidpoint(Vector2(0.5f, 0.5f)); setBarChangeRate(Vector2(1,1)); setSprite(sp); // shader state setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); return true; }
bool BatchNodeBase::init(float density) { if (!Node::init()) { return false; } if (density == 0.0f) { density = stappler::screen::density(); } _density = density; _blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(getProgramStateA8()); setColor(cocos2d::Color3B(255, 255, 255)); setCascadeOpacityEnabled(true); return true; }
void Sprite3D::genGLProgramState() { auto programstate = GLProgramState::getOrCreateWithGLProgram(getDefaultGLProgram(_mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_TEX_COORD))); long offset = 0; auto attributeCount = _mesh->getMeshVertexAttribCount(); for (auto k = 0; k < attributeCount; k++) { auto meshattribute = _mesh->getMeshVertexAttribute(k); programstate->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, _mesh->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } setGLProgramState(programstate); }
bool FilteredSpriteWithOne::updateFilters() { CCASSERT(_pFilters.size() == 1, "Invalid Filter!"); do { unsigned int __count = _pFilters.size(); CC_BREAK_IF(__count == 0); Filter* __filter = static_cast<Filter*>(_pFilters.at(0)); __filter->initSprite(this); if (__filter->getGLProgramState()) { setGLProgramState(__filter->getGLProgramState()); } CHECK_GL_ERROR_DEBUG(); return true; } while (0); return false; }
void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &frag) { auto fileUtiles = FileUtils::getInstance(); // frag auto fragmentFilePath = fileUtiles->fullPathForFilename(frag); auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath); // vert std::string vertSource; if (vert.empty()) { vertSource = ccPositionTextureColor_vert; } else { std::string vertexFilePath = fileUtiles->fullPathForFilename(vert); vertSource = fileUtiles->getStringFromFile(vertexFilePath); } auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); setGLProgramState(glprogramstate); }
void Scale9Sprite::setState(cocos2d::ui::Scale9Sprite::State state) { GLProgramState *glState = nullptr; switch (state) { case State::NORMAL: { glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP); } break; case State::GRAY: { glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_GRAYSCALE); } default: break; } setGLProgramState(glState); _brightState = state; }
void SpriteBlur::initGLProgram() { GLchar * fragSource = nullptr; #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) fragSource = (GLchar*) String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString(); #else fragSource = (GLchar*)String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur_winrt.fsh").c_str())->getCString(); #endif auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource); auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program); setGLProgramState(glProgramState); auto size = getTexture()->getContentSizeInPixels(); getGLProgramState()->setUniformVec2("resolution", size); #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) getGLProgramState()->setUniformFloat("blurRadius", _blurRadius); getGLProgramState()->setUniformFloat("sampleNum", 7.0f); #endif }
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture) { Node::setPosition(Vec2::ZERO); setAnchorPoint(Vec2::ZERO); setIgnoreAnchorPointForPosition(true); _startingPositionInitialized = false; _positionR.setZero(); _fastMode = true; _minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg; _minSeg *= _minSeg; _stroke = stroke; _fadeDelta = 1.0f/fade; double fps = 1/Director::getInstance()->getAnimationInterval(); _maxPoints = (int)(fade*fps)+2; _nuPoints = 0; _pointState = (float *)malloc(sizeof(float) * _maxPoints); _pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints); _vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2); _texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2); _colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4); // Set blend mode _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED; // shader state setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texture)); setTexture(texture); setColor(color); scheduleUpdate(); return true; }
bool PointsOrLines::init() { if (!CustomDrawNode::init()) { return false; } _blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_COLOR)); if (cocos2d::Configuration::getInstance()->supportsShareableVAO()) { glGenVertexArrays(1, &_vao); cocos2d::GL::bindVAO(_vao); } glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid *)0); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, colors)); glBindBuffer(GL_ARRAY_BUFFER, 0); if (cocos2d::Configuration::getInstance()->supportsShareableVAO()) { cocos2d::GL::bindVAO(0); } CHECK_GL_ERROR_DEBUG(); _dirty = true; return true; }
// on "init" you need to initialize your instance bool Lession25::init() { if ( !Layer::init() ) { return false; } auto winSize = Director::getInstance()->getWinSize(); FileUtils::getInstance()->addSearchPath(FileUtils::getInstance()->getSearchPaths().at(0) + "Lession25/"); ////µÃµ½ÆÁÄ»³ß´ç auto s = Director::getInstance()->getWinSize(); //´´½¨3DÉãÏñ»ú auto _camera = cocos2d::Camera::createPerspective(60, (GLfloat)s.width / s.height, 1, 1000); //ÉèÖÃÉãÏñ»úÑÛ¾¦Î»Öà _camera->setPosition3D(Vec3(0, 200, 200)); //ÉèÖÃÉãÏñ»ú¹Û²ìµãλÖà _camera->lookAt(Vec3(0, 0, 0)); _camera->setCameraFlag(CameraFlag::USER1); addChild(_camera); //´´½¨¾²Ì¬Ä£ÐÍ auto spriteStatic3D = Sprite3D::create("water.c3t"); _sprite3D = spriteStatic3D; spriteStatic3D->setCameraMask(2); spriteStatic3D->setScale(2); spriteStatic3D->setPosition3D(Vec3(0, 0, 0)); spriteStatic3D->setRotation3D(Vec3(-90, 0, 0)); addChild(spriteStatic3D); GLProgramState* state = GLProgramState::create(GLProgram::createWithFilenames("Lession25.vert", "Lession25.frag")); state->applyUniforms(); spriteStatic3D->setGLProgramState(state); return true; }
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 }
void Sprite3D::setGLProgram(GLProgram* glprogram) { auto glProgramState = GLProgramState::create(glprogram); setGLProgramState(glProgramState); }
void Sprite3D::setGLProgram(GLProgram *glprogram) { Node::setGLProgram(glprogram); setGLProgramState(getGLProgramState()); }
bool DrawNode::init() { _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR)); ensureCapacity(512); ensureCapacityGLPoint(64); ensureCapacityGLLine(256); if (Configuration::getInstance()->supportsShareableVAO()) { glGenVertexArrays(1, &_vao); GL::bindVAO(_vao); glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW); // vertex glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices)); // color glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); // texcood glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); glGenVertexArrays(1, &_vaoGLLine); GL::bindVAO(_vaoGLLine); glGenBuffers(1, &_vboGLLine); glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW); // vertex glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices)); // color glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); // texcood glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); glGenVertexArrays(1, &_vaoGLPoint); GL::bindVAO(_vaoGLPoint); glGenBuffers(1, &_vboGLPoint); glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW); // vertex glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices)); // color glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); // Texture coord as pointsize glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); GL::bindVAO(0); glBindBuffer(GL_ARRAY_BUFFER, 0); } else { glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW); glGenBuffers(1, &_vboGLLine); glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW); glGenBuffers(1, &_vboGLPoint); glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } CHECK_GL_ERROR_DEBUG(); _dirty = true; _dirtyGLLine = true; _dirtyGLPoint = true; #if CC_ENABLE_CACHE_TEXTURE_DATA // Need to listen the event only when not use batchnode, because it will use VBO auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){ /** listen the event that renderer was recreated on Android/WP8 */ this->init(); }); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); #endif return true; }
void Self::initShader() { customState_ = createHsvProgramState(); setGLProgramState(customState_); }
bool MainLayer::initUI() { _rootNode = CSLoader::createNode("MainScene.csb"); _hideNode = _rootNode->getChildByName<Node*>("Node_Main"); this->addChild(_rootNode); for (auto& child : LsTools::seekNodeByName(_rootNode, "Node_btn")->getChildren()) { auto btn = dynamic_cast<Button*>(child); btn->addTouchEventListener(CC_CALLBACK_2(MainLayer::clickMainBtn, this)); } _pageView = static_cast<PageView*>(LsTools::seekNodeByName(_rootNode, "PageView")); CCASSERT(_pageView, "page view"); _pageView->setCustomScrollThreshold(MY_SCREEN.width / 8); _pageView->setUsingCustomScrollThreshold(true); _pageView->addEventListener(CC_CALLBACK_2(MainLayer::pageViewEvent, this)); _pageView->setClippingType(Layout::ClippingType::SCISSOR); _mapFocusZOrder.clear(); for (ssize_t i = 0; i < PAGE_COUNT; i++) { auto page = _pageView->getPage(i); if (i == MAIN_PAGE) { for (auto& child : page->getChildren()) { //排除倒影 std::string::size_type idx = child->getName().find("dy_"); if (idx != std::string::npos) continue; _mapFocusZOrder.insert(std::pair<int, int>(child->getTag(), child->getLocalZOrder())); auto view = dynamic_cast<ImageView*>(child); view->addTouchEventListener(CC_CALLBACK_2(MainLayer::clickMainPageItem, this)); if (view->getTag() == 22){ _recommend = view; CC_SAFE_RETAIN(_recommend); } } } else if (i == MAIN_PAGE_PLUS){ for (auto& child : page->getChildren()) { //排除倒影 std::string::size_type idx = child->getName().find("dy_"); if (idx != std::string::npos) continue; _mapFocusZOrder.insert(std::pair<int, int>(child->getTag(), child->getLocalZOrder())); auto view = dynamic_cast<ImageView*>(child); view->addTouchEventListener(CC_CALLBACK_2(MainLayer::clickMainPlusPageItem, this)); } } else if (i == CATEGORY_PAGE) { for (auto& child : page->getChildren()) { std::string::size_type idx = child->getName().find("dy_"); if (idx != std::string::npos) continue; _mapFocusZOrder.insert(std::pair<int, int>(child->getTag(), child->getLocalZOrder())); auto view = dynamic_cast<ImageView*>(child); view->addTouchEventListener(CC_CALLBACK_2(MainLayer::clickCategoryPageItem, this)); } } else if (i == MANAGE_PAGE) { for (auto& child : page->getChildren()) { std::string::size_type idx = child->getName().find("dy_"); if (idx != std::string::npos) continue; _mapFocusZOrder.insert(std::pair<int, int>(child->getTag(), child->getLocalZOrder())); auto view = dynamic_cast<ImageView*>(child); if (view->getName() == "3") //Remote QR { CQR_Encode code; float size = 5.5f; std::string ip = PH::getPlatformIP(); std::string str = ZM->getServerAddress() + "?" + formatStr("commend=%d&xLinkIP=%s", CommendEnum::DOWNLOAD_REMOTE, ip.c_str()); LS_LOG("CQR_Encode:%s", str.c_str()); auto drawNode = LsTools::createQRAndDraw(code, size, LsTools::str2charp(str)); drawNode->setPosition(cocos2d::Vec2((view->getContentSize().width - size * code.m_nSymbleSize) / 2 - 6, view->getContentSize().height - (view->getContentSize().height - size * code.m_nSymbleSize) / 2 + 45)); view->addChild(drawNode); } else view->addTouchEventListener(CC_CALLBACK_2(MainLayer::clickManagerPageItem, this)); } } } _tabNode = _pageView->getPage(_pageView->getCurPageIndex()); _navi_Scelect = dynamic_cast<Sprite*>(LsTools::seekNodeByName(_rootNode, "Navi_Scelect")); _bg = _rootNode->getChildByName<Sprite*>("background"); //倒影 if (_isUseSpec) { initSpec(); for (int i = 0; i < PAGE_COUNT; i++) { auto sprite = dynamic_cast<Sprite*>(LsTools::seekNodeByName(_rootNode, formatStr("dy_%d", i))); sprite->setVisible(true); GLchar * fragSource = (GLchar*)String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename("blend.fsh").c_str())->getCString(); auto glprogram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource); auto program = GLProgramState::getOrCreateWithGLProgram(glprogram); V3F_C4B_T2F_Quad quad = sprite->getQuad(); float minTex = (quad.tl.texCoords.v > quad.bl.texCoords.v ? quad.bl.texCoords.v : quad.tl.texCoords.v); float maxTex = (quad.tl.texCoords.v < quad.bl.texCoords.v ? quad.bl.texCoords.v : quad.tl.texCoords.v); program->setUniformFloat("minTex", minTex); program->setUniformFloat("maxTex", maxTex); sprite->setGLProgramState(program); } } return true; }
bool RoadNode::init() { assert(Node::init()); _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_3D_POSITION)); ensureCapacity(512); ensureCapacityGLLine(256); if (Configuration::getInstance()->supportsShareableVAO()) { glGenVertexArrays(1, &_vao); GL::bindVAO(_vao); glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW); // vertex glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid *)offsetof(V3F_C4B_T2F, vertices)); // color glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid *)offsetof(V3F_C4B_T2F, colors)); // texcood glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid *)offsetof(V3F_C4B_T2F, texCoords)); glGenVertexArrays(1, &_vaoGLLine); GL::bindVAO(_vaoGLLine); glGenBuffers(1, &_vboGLLine); glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine); glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW); // vertex glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_T2F), (GLvoid *)offsetof(V3F_T2F, vertices)); // texcood glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_T2F), (GLvoid *)offsetof(V3F_T2F, texCoords)); GL::bindVAO(0); glBindBuffer(GL_ARRAY_BUFFER, 0); } else { glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW); glGenBuffers(1, &_vboGLLine); glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine); glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } CHECK_GL_ERROR_DEBUG(); _dirty = true; _dirtyGLLine = true; return true; }
Sprite *HelloWorld::spriteWithColor1(Color4F color1, Color4F color2, float texWidth, float texHeight, int nStripes) { RenderTexture *rt = RenderTexture::create(texWidth, texHeight); rt->beginWithClear(color1.r, color1.g, color1.b, color1.a); // 3: Draw into the texture // You'll add this later setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)); _customCommand.init(_globalZOrder); _customCommand.func = [texWidth, texHeight, nStripes, color2, this]() { Vec2 *vertices = new Vec2[6*nStripes]; Color4F *colors = new Color4F[6*nStripes]; int nVertices = 0; float x1 = -texHeight; float x2; float y1 = texHeight; float y2 = 0; float dx = texWidth / nStripes * 2; float stripeWidth = dx/2; for (int i=0; i<nStripes; i++) { x2 = x1 + texHeight; vertices[nVertices] = Vec2 {x1, y1}; colors[nVertices++] = Color4F{color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = Vec2 {x1+stripeWidth, y1}; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = Vec2 {x2, y2}; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = vertices[nVertices-2]; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = vertices[nVertices-2]; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = Vec2 {x2+stripeWidth, y2}; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; x1 += dx; } getGLProgram()->use(); getGLProgram()->setUniformsForBuiltins(); cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR); // glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_TRUE, 0, colors); glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices); float gradientAlpha = 0.7f; nVertices = 0; vertices[nVertices] = Vec2 {0, 0}; colors[nVertices++] = Color4F {0, 0, 0, 0 }; vertices[nVertices] = Vec2 {texWidth, 0}; colors[nVertices++] = Color4F {0, 0, 0, 0}; vertices[nVertices] = Vec2 {0, texHeight}; colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha}; vertices[nVertices] = Vec2 {texWidth, texHeight}; colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha}; glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors); // glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); cocos2d::GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST); glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices); // layer 3: top highlight float borderHeight = texHeight/16; float borderAlpha = 0.3f; nVertices = 0; vertices[nVertices] = Vec2 {0, 0}; colors[nVertices++] = Color4F {1, 1, 1, borderAlpha}; vertices[nVertices] = Vec2 {texWidth, 0}; colors[nVertices++] = Color4F {1, 1, 1, borderAlpha}; vertices[nVertices] = Vec2 {0, borderHeight}; colors[nVertices++] = Color4F {0, 0, 0, 0}; vertices[nVertices] = Vec2 {texWidth, borderHeight}; colors[nVertices++] = Color4F {0, 0, 0, 0}; glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors); glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices); CC_SAFE_DELETE_ARRAY(vertices); CC_SAFE_DELETE_ARRAY(colors); }; auto renderer = Director::getInstance()->getRenderer(); renderer->addCommand(&_customCommand); Sprite *noise = Sprite::create("Noise-iphone5hd.png"); noise->setBlendFunc(BlendFunc {GL_DST_COLOR, GL_ZERO}); noise->setPosition(texWidth/2, texHeight/2); noise->visit(); rt->end(); return Sprite::createWithTexture(rt->getSprite()->getTexture()); }
bool BillboardParticleSystem::initWithTotalParticles(int numberOfParticles) { _totalParticles = numberOfParticles; CC_SAFE_FREE(_particles); _particles = (sBillboardParticle*)calloc(_totalParticles, sizeof(sBillboardParticle)); if( ! _particles ) { CCLOG("Particle system: not enough memory"); this->release(); return false; } _allocatedParticles = numberOfParticles; // default, active _isActive = true; // default blend function _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; // default movement type; _positionType = PositionType::FREE; // by default be in mode A: _emitterMode = Mode::GRAVITY; // default: modulate // XXX: not used // colorModulate = YES; _isAutoRemoveOnFinish = false; // Optimization: compile updateParticle method //updateParticleSel = @selector(updateQuadWithParticle:newPosition:); //updateParticleImp = (CC_UPDATE_PARTICLE_IMP) [self methodForSelector:updateParticleSel]; //for batchNode _transformSystemDirty = false; // allocating data space if( ! this->allocMemory() ) { this->release(); return false; } initIndices(); if (Configuration::getInstance()->supportsShareableVAO()) { setupVBOandVAO(); } else { setupVBO(); } setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)); #if CC_ENABLE_CACHE_TEXTURE_DATA // Need to listen the event only when not use batchnode, because it will use VBO auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(ParticleSystemQuad::listenRendererRecreated, this)); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); #endif return true; }