void RawStencilBufferTest::onBeforeDrawClip(int planeIndex, const Vec2& pt) { this->setupStencilForClippingOnPlane(planeIndex); CHECK_GL_ERROR_DEBUG(); DrawPrimitives::drawSolidRect(Vec2::ZERO, pt, Color4F(1, 1, 1, 1)); }
void GLProgram::addAttribute(const char* attributeName, GLuint index) { glBindAttribLocation(m_program, index, attributeName); CHECK_GL_ERROR_DEBUG(); }
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; }
void Renderer::setupVBOAndVAO() { //generate vbo and vao for trianglesCommand glGenVertexArrays(1, &_buffersVAO); GL::bindVAO(_buffersVAO); glGenBuffers(2, &_buffersVBO[0]); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(_verts[0]) * VBO_SIZE, _verts, GL_DYNAMIC_DRAW); // vertices 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)); // colors 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)); // tex coords 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)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * INDEX_VBO_SIZE, _indices, GL_STATIC_DRAW); // Must unbind the VAO before changing the element buffer. GL::bindVAO(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); //generate vbo and vao for quadCommand glGenVertexArrays(1, &_quadVAO); GL::bindVAO(_quadVAO); glGenBuffers(2, &_quadbuffersVBO[0]); glBindBuffer(GL_ARRAY_BUFFER, _quadbuffersVBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(_quadVerts[0]) * VBO_SIZE, _quadVerts, GL_DYNAMIC_DRAW); // vertices 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)); // colors 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)); // tex coords 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)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _quadbuffersVBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_quadIndices[0]) * INDEX_VBO_SIZE, _quadIndices, GL_STATIC_DRAW); // Must unbind the VAO before changing the element buffer. GL::bindVAO(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERROR_DEBUG(); }
void TextureAtlas::drawNumberOfQuads(int numberOfQuads, int start) { CCASSERT(numberOfQuads>=0 && start>=0, "numberOfQuads and start must be >= 0"); if(!numberOfQuads) return; ccGLBindTexture2D(_texture->getName()); #if CC_TEXTURE_ATLAS_USE_VAO // // Using VBO and VAO // // XXX: update is done in draw... perhaps it should be done in a timer if (_dirty) { glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); // option 1: subdata //glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * n , &_quads[start] ); // option 2: data // glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW); // option 3: orphaning + glMapBuffer glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * (numberOfQuads-start), NULL, GL_DYNAMIC_DRAW); void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(buf, _quads, sizeof(_quads[0])* (numberOfQuads-start)); glUnmapBuffer(GL_ARRAY_BUFFER); glBindBuffer(GL_ARRAY_BUFFER, 0); _dirty = false; } ccGLBindVAO(_VAOname); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); #endif #if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP glDrawElements(GL_TRIANGLE_STRIP, (GLsizei) numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])) ); #else glDrawElements(GL_TRIANGLES, (GLsizei) numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])) ); #endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // glBindVertexArray(0); #else // ! CC_TEXTURE_ATLAS_USE_VAO // // Using VBO without VAO // #define kQuadSize sizeof(_quads[0].bl) glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); // XXX: update is done in draw... perhaps it should be done in a timer if (_dirty) { glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * numberOfQuads , &_quads[start] ); _dirty = false; } ccGLEnableVertexAttribs(kVertexAttribFlag_PosColorTex); // vertices glVertexAttribPointer(kVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices)); // colors glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); // tex coords glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); #if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP glDrawElements(GL_TRIANGLE_STRIP, (GLsizei)numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0]))); #else glDrawElements(GL_TRIANGLES, (GLsizei)numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0]))); #endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // CC_TEXTURE_ATLAS_USE_VAO CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); }
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; } }
void ShaderCache::loadDefaultShader(GLProgram *p, int type) { switch (type) { case kShaderType_PositionTextureColor: p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColor_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameColor, kVertexAttrib_Color); p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords); break; case kShaderType_PositionTextureColorAlphaTest: p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameColor, kVertexAttrib_Color); p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords); break; case kShaderType_PositionColor: p->initWithVertexShaderByteArray(ccPositionColor_vert ,ccPositionColor_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameColor, kVertexAttrib_Color); break; case kShaderType_PositionTexture: p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords); break; case kShaderType_PositionTexture_uColor: p->initWithVertexShaderByteArray(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords); break; case kShaderType_PositionTextureA8Color: p->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameColor, kVertexAttrib_Color); p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords); break; case kShaderType_Position_uColor: p->initWithVertexShaderByteArray(ccPosition_uColor_vert, ccPosition_uColor_frag); p->addAttribute("aVertex", kVertexAttrib_Position); break; case kShaderType_PositionLengthTexureColor: p->initWithVertexShaderByteArray(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position); p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords); p->addAttribute(kAttributeNameColor, kVertexAttrib_Color); break; default: CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__); return; } p->link(); p->updateUniforms(); CHECK_GL_ERROR_DEBUG(); }
void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type) { switch (type) { case kShaderType_PositionTextureColor: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag); break; case kShaderType_PositionTextureColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); break; case kShaderType_PositionTextureColorAlphaTest: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); break; case kShaderType_PositionTextureColorAlphaTestNoMV: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag); break; case kShaderType_PositionColor: p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag); break; case kShaderType_PositionColorTextureAsPointsize: p->initWithByteArrays(ccPositionColorTextureAsPointsize_vert ,ccPositionColor_frag); break; case kShaderType_PositionColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); break; case kShaderType_PositionTexture: p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag); break; case kShaderType_PositionTexture_uColor: p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); break; case kShaderType_PositionTextureA8Color: p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); break; case kShaderType_Position_uColor: p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag); p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); break; case kShaderType_PositionLengthTexureColor: p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); break; case kShaderType_LabelDistanceFieldNormal: p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag); break; case kShaderType_LabelDistanceFieldGlow: p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag); break; case kShaderType_UIGrayScale: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTexture_GrayScale_frag); break; case kShaderType_LabelNormal: p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag); break; case kShaderType_LabelOutline: p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag); break; case kShaderType_3DPosition: p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_Color_frag); break; case kShaderType_3DPositionTex: p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_ColorTex_frag); break; case kShaderType_3DSkinPositionTex: p->initWithByteArrays(cc3D_SkinPositionTex_vert, cc3D_ColorTex_frag); break; case kShaderType_3DPositionNormal: { std::string def = getShaderMacrosForLight(); p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormal_frag)).c_str()); } break; case kShaderType_3DPositionNormalTex: { std::string def = getShaderMacrosForLight(); p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str()); } break; case kShaderType_3DSkinPositionNormalTex: { std::string def = getShaderMacrosForLight(); p->initWithByteArrays((def + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str()); } break; case kShaderType_3DPositionBumpedNormalTex: { std::string def = getShaderMacrosForLight(); std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n"; p->initWithByteArrays((def + normalMapDef + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + normalMapDef + std::string(cc3D_ColorNormalTex_frag)).c_str()); } break; case kShaderType_3DSkinPositionBumpedNormalTex: { std::string def = getShaderMacrosForLight(); std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n"; p->initWithByteArrays((def + normalMapDef + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + normalMapDef + std::string(cc3D_ColorNormalTex_frag)).c_str()); } break; case kShaderType_3DParticleTex: { p->initWithByteArrays(cc3D_Particle_vert, cc3D_Particle_tex_frag); } break; case kShaderType_3DParticleColor: p->initWithByteArrays(cc3D_Particle_vert, cc3D_Particle_color_frag); break; case kShaderType_3DSkyBox: p->initWithByteArrays(cc3D_Skybox_vert, cc3D_Skybox_frag); break; case kShaderType_3DTerrain: p->initWithByteArrays(cc3D_Terrain_vert, cc3D_Terrain_frag); break; case kShaderType_CameraClear: p->initWithByteArrays(ccCameraClearVert, ccCameraClearFrag); break; /// x-studio365 spec, ETC1 ALPHA supports. case kShaderType_ETC1AAPositionTextureColor: p->initWithByteArrays(ccETC1AAPositionTextureColor_vert, ccETC1AAPositionTextureColor_frag); break; case kShaderType_ETC1AAPositionTextureColor_noMVP: p->initWithByteArrays(ccETC1AAPositionTextureColor_noMVP_vert, ccETC1AAPositionTextureColor_frag); break; case kShaderType_ETC1ASPositionTextureColor: p->initWithByteArrays(ccPositionTextureColor_vert, ccETC1ASPositionTextureColor_frag); break; case kShaderType_ETC1ASPositionTextureColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccETC1ASPositionTextureColor_frag); break; /// x-studio365 spec, ETC1 GRAY supports. case kShaderType_ETC1AAPositionTextureGray: p->initWithByteArrays(ccETC1AAPositionTextureColor_vert, ccETC1AAPositionTextureGray_frag); break; case kShaderType_ETC1AAPositionTextureGray_noMVP: p->initWithByteArrays(ccETC1AAPositionTextureColor_noMVP_vert, ccETC1AAPositionTextureGray_frag); break; case kShaderType_ETC1ASPositionTextureGray: p->initWithByteArrays(ccPositionTextureColor_vert, ccETC1ASPositionTextureGray_frag); break; case kShaderType_ETC1ASPositionTextureGray_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccETC1ASPositionTextureGray_frag); break; default: CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__); return; } p->link(); p->updateUniforms(); CHECK_GL_ERROR_DEBUG(); }
void CCTextureAtlas::drawNumberOfQuads(unsigned int n, unsigned int start) { if (0 == n) { return; } ccGLBindTexture2D(m_pTexture->getName()); #if CC_TEXTURE_ATLAS_USE_VAO // // Using VBO and VAO // // XXX: update is done in draw... perhaps it should be done in a timer if (m_bDirty) { glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]); glBufferSubData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0])*start, sizeof(m_pQuads[0]) * n , &m_pQuads[start] ); glBindBuffer(GL_ARRAY_BUFFER, 0); m_bDirty = false; } glBindVertexArray(m_uVAOname); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]); #endif #if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP glDrawElements(GL_TRIANGLE_STRIP, (GLsizei) n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0])) ); #else glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0])) ); #endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif glBindVertexArray(0); #else // ! CC_TEXTURE_ATLAS_USE_VAO // // Using VBO without VAO // #define kQuadSize sizeof(m_pQuads[0].bl) glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]); // XXX: update is done in draw... perhaps it should be done in a timer if (m_bDirty) { glBufferSubData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0])*start, sizeof(m_pQuads[0]) * n , &m_pQuads[start] ); m_bDirty = false; } ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); // 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]); #if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP glDrawElements(GL_TRIANGLE_STRIP, (GLsizei)n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0]))); #else glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0]))); #endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // CC_TEXTURE_ATLAS_USE_VAO CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); }
void Layout::stencilClippingVisit() { if (!_clippingStencil || !_clippingStencil->isVisible()) { CCNode::visit(); return; } if (g_sStencilBits < 1) { CCNode::visit(); return; } static GLint layer = -1; if (layer + 1 == g_sStencilBits) { static bool once = true; if (once) { char warning[200] = {0}; snprintf(warning, sizeof(warning), "Nesting more than %d stencils is not supported. Everything will be drawn without stencil for this node and its childs.", g_sStencilBits); CCLOG("%s", warning); once = false; } CCNode::visit(); return; } layer++; GLint mask_layer = 0x1 << layer; GLint mask_layer_l = mask_layer - 1; GLint mask_layer_le = mask_layer | mask_layer_l; GLboolean currentStencilEnabled = GL_FALSE; GLuint currentStencilWriteMask = ~0; GLenum currentStencilFunc = GL_ALWAYS; GLint currentStencilRef = 0; GLuint currentStencilValueMask = ~0; GLenum currentStencilFail = GL_KEEP; GLenum currentStencilPassDepthFail = GL_KEEP; GLenum currentStencilPassDepthPass = GL_KEEP; currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST); glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)¤tStencilWriteMask); glGetIntegerv(GL_STENCIL_FUNC, (GLint *)¤tStencilFunc); glGetIntegerv(GL_STENCIL_REF, ¤tStencilRef); glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)¤tStencilValueMask); glGetIntegerv(GL_STENCIL_FAIL, (GLint *)¤tStencilFail); glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)¤tStencilPassDepthFail); glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)¤tStencilPassDepthPass); glEnable(GL_STENCIL_TEST); CHECK_GL_ERROR_DEBUG(); glStencilMask(mask_layer); GLboolean currentDepthWriteMask = GL_TRUE; glGetBooleanv(GL_DEPTH_WRITEMASK, ¤tDepthWriteMask); glDepthMask(GL_FALSE); glStencilFunc(GL_NEVER, mask_layer, mask_layer); glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLPushMatrix(); kmGLLoadIdentity(); kmGLMatrixMode(KM_GL_PROJECTION); kmGLPushMatrix(); kmGLLoadIdentity(); ccDrawSolidRect(CCPoint(-1,-1), CCPoint(1,1), ccc4f(1, 1, 1, 1)); kmGLMatrixMode(KM_GL_PROJECTION); kmGLPopMatrix(); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLPopMatrix(); glStencilFunc(GL_NEVER, mask_layer, mask_layer); glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); kmGLPushMatrix(); transform(); _clippingStencil->visit(); kmGLPopMatrix(); glDepthMask(currentDepthWriteMask); glStencilFunc(GL_EQUAL, mask_layer_le, mask_layer_le); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); CCNode::visit(); glStencilFunc(currentStencilFunc, currentStencilRef, currentStencilValueMask); glStencilOp(currentStencilFail, currentStencilPassDepthFail, currentStencilPassDepthPass); glStencilMask(currentStencilWriteMask); if (!currentStencilEnabled) { glDisable(GL_STENCIL_TEST); } layer--; }
void ScutCxListItem::draw(void) { //mark es2.0 //判断是否超出范围,如果超出,那么不进行绘画 if (this->getParent()) { if (this->getPosition().y + this->getParent()->getPosition().y > this->getParent()->getContentSize().height || this->getPosition().y + this->getParent()->getPosition().y + this->getContentSize().height < 0) { return; } } updateColor(); CCSize size = getContentSize(); if (selected_) { if(draw_selected) { CC_NODE_DRAW_SETUP(); ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst ); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color); // vertex glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, (void*)m_pSquareVertices); // color glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, (void*)m_pSquareColors); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CHECK_GL_ERROR_DEBUG(); CC_INCREMENT_GL_DRAWS(1); } } else { CCLayerColor::draw(); } //glDisable(GL_LINE_SMOOTH); if (draw_top_line_) { glLineWidth(1.0f); cocos2d::ccDrawColor4F(line_color_.r, line_color_.g, line_color_.b, 0xFF); ccDrawLine(CCPointMake(0, size.height-0.5f), CCPointMake(size.width, size.height-0.5f)); } if (draw_bottom_line_) { glLineWidth(1.0f); cocos2d::ccDrawColor4F(line_color_.r, line_color_.g, line_color_.b, 0xFF); ccDrawLine(CCPointMake(0, 0.5f), CCPointMake(size.width, 0.5f)); } }
void MySprite::onDraw() { Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); Director::getInstance()->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Director::getInstance()->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Mat4 modelViewMatrix; Mat4::createLookAt(Vec3(0, 0, 1), Vec3(0, 0, 0), Vec3(0, 1, 0), &modelViewMatrix); modelViewMatrix.translate(0, 0, -1); //static float move = 0; //modelViewMatrix.translate(sinf(move*0.05)*2, 2, -5); //move++; //static float rotation = 0; //modelViewMatrix.rotate(Vec3(1,1,1),CC_DEGREES_TO_RADIANS(rotation)); //rotation++; //if (rotation > 360) { // rotation = 0; //} Mat4 projectionMatrix; Mat4::createPerspective(60, 480 / 320, 1.0, 42, &projectionMatrix); Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, projectionMatrix); Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, modelViewMatrix); typedef struct { float Position[3]; float TexCoord[2]; } Vertex; #define TEX_COORD_MAX 1 Vertex Vertices[] = { { { -0.5, 1.0, 0 }, { 0, 0 } }, { { -0.5, -1.0, 0 }, { 0, TEX_COORD_MAX } }, { { 0.5, -1.0, 0 }, { TEX_COORD_MAX, TEX_COORD_MAX } }, { { 0.5, 1.0, 0 }, { TEX_COORD_MAX, 0 } }, }; int vertexCount = sizeof(Vertices) / sizeof(Vertices[0]); GLubyte Indices[] = { 0, 1, 2, 2, 3, 0, }; glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); _positionLoc = glGetAttribLocation(mShaderProgram->getProgram(), "a_position"); _texCoordLoc = glGetAttribLocation(mShaderProgram->getProgram(), "a_texCoord"); //_baseMapLoc = glGetUniformLocation(mShaderProgram->getProgram(), "s_baseMap"); //_lightMapLoc = glGetUniformLocation(mShaderProgram->getProgram(), "s_lightMap"); _baseMapLoc = glGetUniformLocation(mShaderProgram->getProgram(), "CC_Texture0"); _lightMapLoc = glGetUniformLocation(mShaderProgram->getProgram(), "CC_Texture1"); glEnableVertexAttribArray(_positionLoc); glEnableVertexAttribArray(_texCoordLoc); glVertexAttribPointer(_positionLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Position)); glVertexAttribPointer(_texCoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, TexCoord)); mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); glEnable(GL_DEPTH_TEST); glBindVertexArray(VAO); GL::bindTexture2DN(0, _lightMapTexId); glUniform1i(_lightMapLoc, 0); // unnecc in practice GL::bindTexture2DN(1, _baseMapTexId); glUniform1i(_baseMapLoc, 1); // unnecc in practice //if (touching) { tmpCount += 0.005; CCLOG("-------- touching %f", tmpCount); if (tmpCount > 1.0f) { tmpCount = 0.0f; } } /* ---------------------- test3, 为float[]数组中的两个元素赋值 */ GLuint uColorLocation3 = glGetUniformLocation(mShaderProgram->getProgram(), "myColor"); glUniform1f(uColorLocation3, tmpCount); GLuint uResoLoc = glGetUniformLocation(mShaderProgram->getProgram(), "iResolution"); float resoTmp[2] = { mResolution.width, mResolution.height }; glUniform2fv(uResoLoc, 1, resoTmp); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 6); glBindVertexArray(0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 6); CHECK_GL_ERROR_DEBUG(); glDisable(GL_DEPTH_TEST); Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); }
void RenderTexture::onBegin() { // Director *director = Director::getInstance(); _oldProjMatrix = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _projectionMatrix); _oldTransMatrix = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _transformMatrix); if(!_keepMatrix) { director->setProjection(director->getProjection()); #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 Mat4 modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection; director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,modifiedProjection); #endif const Size& texSize = _texture->getContentSizeInPixels(); // Calculate the adjustment ratios based on the old and new projections Size size = director->getWinSizeInPixels(); float widthRatio = size.width / texSize.width; float heightRatio = size.height / texSize.height; Mat4 orthoMatrix; Mat4::createOrthographicOffCenter((float)-1.0 / widthRatio, (float)1.0 / widthRatio, (float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1, 1, &orthoMatrix); director->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix); } else { #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 Mat4 modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection; director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, modifiedProjection); #endif } //calculate viewport { Rect viewport; viewport.size.width = _fullviewPort.size.width; viewport.size.height = _fullviewPort.size.height; float viewPortRectWidthRatio = float(viewport.size.width)/_fullRect.size.width; float viewPortRectHeightRatio = float(viewport.size.height)/_fullRect.size.height; viewport.origin.x = (_fullRect.origin.x - _rtTextureRect.origin.x) * viewPortRectWidthRatio; viewport.origin.y = (_fullRect.origin.y - _rtTextureRect.origin.y) * viewPortRectHeightRatio; //glViewport(_fullviewPort.origin.x, _fullviewPort.origin.y, (GLsizei)_fullviewPort.size.width, (GLsizei)_fullviewPort.size.height); glViewport(viewport.origin.x, viewport.origin.y, (GLsizei)viewport.size.width, (GLsizei)viewport.size.height); } // Adjust the orthographic projection and viewport glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO); glBindFramebuffer(GL_FRAMEBUFFER, _FBO); //TODO move this to configration, so we don't check it every time /* Certain Qualcomm Andreno gpu's will retain data in memory after a frame buffer switch which corrupts the render to the texture. The solution is to clear the frame buffer before rendering to the texture. However, calling glClear has the unintended result of clearing the current texture. Create a temporary texture to overcome this. At the end of RenderTexture::begin(), switch the attached texture to the second one, call glClear, and then switch back to the original texture. This solution is unnecessary for other devices as they don't have the same issue with switching frame buffers. */ if (Configuration::getInstance()->checkForGLExtension("GL_QCOM")) { // -- bind a temporary texture so we can clear the render buffer without losing our texture glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _textureCopy->getName(), 0); CHECK_GL_ERROR_DEBUG(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture->getName(), 0); } }
namespace_ens_begin void CtinyWingsTerrainSprite::init(float width,float height,int insertControlPointCount){ this->CCSprite::init(); m_width=width; m_height=height; m_pointMat.resize(m_nRow); vector<CCPoint>&pointList=m_pointMat[0]; int nseg=ceilf(m_width/m_dx); m_dx=m_width/nseg;//revise dx int nPoint=nseg+1; for(int i=0;i<nPoint;i++){ float x=i*m_dx; CCPoint point(x,m_height); pointList.push_back(point); int nRow=(int)m_pointMat.size(); for(int j=1;j<nRow;j++){ CCPoint _point; _point.x=point.x; _point.y=point.y*(1-(float)j/(nRow-1));//must convert j to float !!! m_pointMat[j].push_back(_point); } }//got m_pointMat; //----create mesh m_mesh=new Cmesh(); m_mesh->autorelease(); m_mesh->retain(); //----create indexVBO m_indexVBO=new CindexVBO(); m_indexVBO->autorelease(); m_indexVBO->retain(); //----colorList m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); //----reShape reGenerateShape(insertControlPointCount); //----update mesh updateMesh(); //----submitMesh submit(); //----create and set shader program { GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("shaders/tinyWingsTerrain_nonlinearTexCoord.fsh").c_str())->getCString(); CGLProgramWithUnifos* program = new CGLProgramWithUnifos(); program->autorelease(); program->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); //bind attribute program->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); program->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color); program->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); //link (must after bindAttribute) program->link(); //get cocos2d-x build-in uniforms program->updateUniforms(); //get my own uniforms program->attachUniform("u_texNonlinearFactor"); program->attachUniform("u_colors"); program->attachUniform("u_cosA"); program->attachUniform("u_sinA"); program->attachUniform("u_ribbonRepeat"); //set program m_program=program; m_program->retain(); //check gl error CHECK_GL_ERROR_DEBUG(); } }
void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start) { CCASSERT(numberOfQuads>=0 && start>=0, "numberOfQuads and start must be >= 0"); if(!numberOfQuads) return; GL::bindTexture2D(_texture->getName()); if (Configuration::getInstance()->supportsShareableVAO()) { // // Using VBO and VAO // // XXX: update is done in draw... perhaps it should be done in a timer if (_dirty) { glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); // option 1: subdata // glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * n , &_quads[start] ); // option 2: data // glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW); // option 3: orphaning + glMapBuffer glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * (numberOfQuads-start), nullptr, GL_DYNAMIC_DRAW); void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(buf, _quads, sizeof(_quads[0])* (numberOfQuads-start)); glUnmapBuffer(GL_ARRAY_BUFFER); glBindBuffer(GL_ARRAY_BUFFER, 0); _dirty = false; } GL::bindVAO(_VAOname); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); #endif glDrawElements(GL_TRIANGLES, (GLsizei) numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])) ); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // glBindVertexArray(0); } else { // // Using VBO without VAO // #define kQuadSize sizeof(_quads[0].bl) glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); // XXX: update is done in draw... perhaps it should be done in a timer if (_dirty) { glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * numberOfQuads , &_quads[start] ); _dirty = false; } GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); // vertices glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices)); // colors glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); // tex coords glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glDrawElements(GL_TRIANGLES, (GLsizei)numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0]))); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,numberOfQuads*6); CHECK_GL_ERROR_DEBUG(); }
void Skybox::onDraw(const Mat4& transform, uint32_t /*flags*/) { auto camera = Camera::getVisitingCamera(); Mat4 cameraModelMat = camera->getNodeToWorldTransform(); Mat4 projectionMat = camera->getProjectionMatrix(); // Ignore the translation cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0; // prescale the matrix to account for the camera fov cameraModelMat.scale(1 / projectionMat.m[0], 1 / projectionMat.m[5], 1.0); auto state = getGLProgramState(); state->apply(transform); Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f); state->setUniformVec4("u_color", color); state->setUniformMat4("u_cameraRot", cameraModelMat); glEnable(GL_DEPTH_TEST); RenderState::StateBlock::_defaultState->setDepthTest(true); glDepthFunc(GL_LEQUAL); RenderState::StateBlock::_defaultState->setDepthFunction(RenderState::DEPTH_LEQUAL); glEnable(GL_CULL_FACE); RenderState::StateBlock::_defaultState->setCullFace(true); glCullFace(GL_BACK); RenderState::StateBlock::_defaultState->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK); glDisable(GL_BLEND); RenderState::StateBlock::_defaultState->setBlend(false); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(_vao); } else { GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); } glDrawElements(GL_TRIANGLES, (GLsizei)6, GL_UNSIGNED_BYTE, nullptr); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } else { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4); CHECK_GL_ERROR_DEBUG(); }
void ClippingNode::onBeforeVisit() { /////////////////////////////////// // INIT // increment the current layer s_layer++; // mask of the current layer (ie: for layer 3: 00000100) GLint mask_layer = 0x1 << s_layer; // mask of all layers less than the current (ie: for layer 3: 00000011) GLint mask_layer_l = mask_layer - 1; // mask of all layers less than or equal to the current (ie: for layer 3: 00000111) _mask_layer_le = mask_layer | mask_layer_l; // manually save the stencil state _currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST); glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)&_currentStencilWriteMask); glGetIntegerv(GL_STENCIL_FUNC, (GLint *)&_currentStencilFunc); glGetIntegerv(GL_STENCIL_REF, &_currentStencilRef); glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&_currentStencilValueMask); glGetIntegerv(GL_STENCIL_FAIL, (GLint *)&_currentStencilFail); glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)&_currentStencilPassDepthFail); glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)&_currentStencilPassDepthPass); // enable stencil use glEnable(GL_STENCIL_TEST); // check for OpenGL error while enabling stencil test CHECK_GL_ERROR_DEBUG(); // all bits on the stencil buffer are readonly, except the current layer bit, // this means that operation like glClear or glStencilOp will be masked with this value glStencilMask(mask_layer); // manually save the depth test state glGetBooleanv(GL_DEPTH_WRITEMASK, &_currentDepthWriteMask); // disable depth test while drawing the stencil //glDisable(GL_DEPTH_TEST); // disable update to the depth buffer while drawing the stencil, // as the stencil is not meant to be rendered in the real scene, // it should never prevent something else to be drawn, // only disabling depth buffer update should do glDepthMask(GL_FALSE); /////////////////////////////////// // CLEAR STENCIL BUFFER // manually clear the stencil buffer by drawing a fullscreen rectangle on it // setup the stencil test func like this: // for each pixel in the fullscreen rectangle // never draw it into the frame buffer // if not in inverted mode: set the current layer value to 0 in the stencil buffer // if in inverted mode: set the current layer value to 1 in the stencil buffer glStencilFunc(GL_NEVER, mask_layer, mask_layer); glStencilOp(!_inverted ? GL_ZERO : GL_REPLACE, GL_KEEP, GL_KEEP); // draw a fullscreen solid rectangle to clear the stencil buffer //ccDrawSolidRect(Point::ZERO, ccpFromSize([[Director sharedDirector] winSize]), Color4F(1, 1, 1, 1)); drawFullScreenQuadClearStencil(); /////////////////////////////////// // DRAW CLIPPING STENCIL // setup the stencil test func like this: // for each pixel in the stencil node // never draw it into the frame buffer // if not in inverted mode: set the current layer value to 1 in the stencil buffer // if in inverted mode: set the current layer value to 0 in the stencil buffer glStencilFunc(GL_NEVER, mask_layer, mask_layer); glStencilOp(!_inverted ? GL_REPLACE : GL_ZERO, GL_KEEP, GL_KEEP); // enable alpha test only if the alpha threshold < 1, // indeed if alpha threshold == 1, every pixel will be drawn anyways if (_alphaThreshold < 1) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) // manually save the alpha test state _currentAlphaTestEnabled = glIsEnabled(GL_ALPHA_TEST); glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint *)&_currentAlphaTestFunc); glGetFloatv(GL_ALPHA_TEST_REF, &_currentAlphaTestRef); // enable alpha testing glEnable(GL_ALPHA_TEST); // check for OpenGL error while enabling alpha test CHECK_GL_ERROR_DEBUG(); // pixel will be drawn only if greater than an alpha threshold glAlphaFunc(GL_GREATER, _alphaThreshold); #else #endif } //Draw _stencil }
/* get buffer as Image */ Image* RenderTexture::newImage(bool fliimage) { CCASSERT(_pixelFormat == Texture2D::PixelFormat::RGBA8888, "only RGBA8888 can be saved as image"); if (nullptr == _texture) { return nullptr; } const Size& s = _texture->getContentSizeInPixels(); // to get the image size to save // if the saving image domain exceeds the buffer texture domain, // it should be cut int savedBufferWidth = (int)s.width; int savedBufferHeight = (int)s.height; GLubyte *buffer = nullptr; GLubyte *tempData = nullptr; Image *image = new (std::nothrow) Image(); do { CC_BREAK_IF(! (buffer = new (std::nothrow) GLubyte[savedBufferWidth * savedBufferHeight * 4])); if(! (tempData = new (std::nothrow) GLubyte[savedBufferWidth * savedBufferHeight * 4])) { delete[] buffer; buffer = nullptr; break; } glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO); glBindFramebuffer(GL_FRAMEBUFFER, _FBO); // TODO: move this to configration, so we don't check it every time /* Certain Qualcomm Andreno gpu's will retain data in memory after a frame buffer switch which corrupts the render to the texture. The solution is to clear the frame buffer before rendering to the texture. However, calling glClear has the unintended result of clearing the current texture. Create a temporary texture to overcome this. At the end of RenderTexture::begin(), switch the attached texture to the second one, call glClear, and then switch back to the original texture. This solution is unnecessary for other devices as they don't have the same issue with switching frame buffers. */ if (Configuration::getInstance()->checkForGLExtension("GL_QCOM")) { // -- bind a temporary texture so we can clear the render buffer without losing our texture glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _textureCopy->getName(), 0); CHECK_GL_ERROR_DEBUG(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture->getName(), 0); } glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0,0,savedBufferWidth, savedBufferHeight,GL_RGBA,GL_UNSIGNED_BYTE, tempData); glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO); if ( fliimage ) // -- flip is only required when saving image to file { // to get the actual texture data // #640 the image read from rendertexture is dirty for (int i = 0; i < savedBufferHeight; ++i) { memcpy(&buffer[i * savedBufferWidth * 4], &tempData[(savedBufferHeight - i - 1) * savedBufferWidth * 4], savedBufferWidth * 4); } image->initWithRawData(buffer, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8); } else { image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8); } } while (0); CC_SAFE_DELETE_ARRAY(buffer); CC_SAFE_DELETE_ARRAY(tempData); return image; }
void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type) { switch (type) { case kShaderType_PositionTextureColor: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag); break; case kShaderType_PositionTextureColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); break; case kShaderType_PositionTextureColorAlphaTest: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); break; case kShaderType_PositionTextureColorAlphaTestNoMV: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag); break; case kShaderType_PositionColor: p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag); break; case kShaderType_PositionColorTextureAsPointsize: p->initWithByteArrays(ccPositionColorTextureAsPointsize_vert ,ccPositionColor_frag); break; case kShaderType_PositionColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); break; case kShaderType_PositionTexture: p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag); break; case kShaderType_PositionTexture_uColor: p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); break; case kShaderType_PositionTextureA8Color: p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); break; case kShaderType_Position_uColor: p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag); p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); break; case kShaderType_PositionLengthTexureColor: p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); break; #if CC_TARGET_PLATFORM != CC_PLATFORM_WP8 case kShaderType_LabelDistanceFieldNormal: p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag); break; case kShaderType_LabelDistanceFieldGlow: p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag); break; #endif case kShaderType_LabelNormal: p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag); break; case kShaderType_LabelOutline: p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag); break; case kShaderType_3DPosition: p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_Color_frag); break; case kShaderType_3DPositionTex: p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_ColorTex_frag); break; case kShaderType_3DSkinPositionTex: p->initWithByteArrays(cc3D_SkinPositionTex_vert, cc3D_ColorTex_frag); break; case kShaderType_3DPositionNormal: { std::string def = getShaderMacrosForLight(); p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormal_frag)).c_str()); } break; case kShaderType_3DPositionNormalTex: { std::string def = getShaderMacrosForLight(); p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str()); } break; case kShaderType_3DSkinPositionNormalTex: { std::string def = getShaderMacrosForLight(); p->initWithByteArrays((def + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str()); } break; #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || defined(WP8_SHADER_COMPILER) case kShaderType_PositionColor_noMVP_GrayScale: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccUIGrayScale_frag); break; #endif default: CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__); return; } p->link(); p->updateUniforms(); CHECK_GL_ERROR_DEBUG(); }
void CameraBackgroundSkyBoxBrush::drawBackground(Camera* camera) { if (!_actived) return; Mat4 cameraModelMat = camera->getNodeToWorldTransform(); Vec4 color(1.f, 1.f, 1.f, 1.f); _glProgramState->setUniformVec4("u_color", color); cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0; _glProgramState->setUniformMat4("u_cameraRot", cameraModelMat); _glProgramState->apply(Mat4::IDENTITY); glEnable(GL_DEPTH_TEST); RenderState::StateBlock::_defaultState->setDepthTest(true); glDepthMask(GL_TRUE); RenderState::StateBlock::_defaultState->setDepthWrite(true); glDepthFunc(GL_ALWAYS); RenderState::StateBlock::_defaultState->setDepthFunction(RenderState::DEPTH_ALWAYS); glEnable(GL_CULL_FACE); RenderState::StateBlock::_defaultState->setCullFace(true); glCullFace(GL_BACK); RenderState::StateBlock::_defaultState->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK); glDisable(GL_BLEND); RenderState::StateBlock::_defaultState->setBlend(false); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(_vao); } else { GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); } glDrawElements(GL_TRIANGLES, (GLsizei)36, GL_UNSIGNED_BYTE, nullptr); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } else { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8); CHECK_GL_ERROR_DEBUG(); }
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"); }
void ShadowCover::onDraw(const cocos2d::Mat4 &transform, uint32_t flags) { auto glProgram = _programState->getGLProgram(); glProgram->use(); // auto loc = glProgram->getUniformLocation("u_color"); // glProgram->setUniformLocationWith4fv(loc, &_color.r, 1); // loc = glProgram->getUniformLocation("u_factor"); // glProgram->setUniformLocationWith1f(loc, _factor); // loc = glProgram->getUniformLocation("u_texture_shadow"); // glProgram->setUniformLocationWith1i(loc, 1); // loc = glProgram->getUniformLocation("u_light_color"); // glProgram->setUniformLocationWith3fv(loc, &_lightColor.r, 1); // loc = glProgram->getUniformLocation("u_light_direction"); // glProgram->setUniformLocationWith3fv(loc, &_lightDirection.x, 1); // loc = glProgram->getUniformLocation("u_speed"); // glProgram->setUniformLocationWith1f(loc, _speed*25); // GL::bindTexture2DN(1, _textureShadow->getName()); auto loc = glProgram->getUniformLocation("u_texture_shadow"); glProgram->setUniformLocationWith1i(loc, 0); GL::bindTexture2DN(0, _textureShadow->getName()); loc = glProgram->getUniformLocation("u_shadow_time"); glProgram->setUniformLocationWith1f(loc, _time); loc = glProgram->getUniformLocation("u_unit_width"); glProgram->setUniformLocationWith1f(loc, DDConfig::battleCubeWidth()*1.5); /* loc = glProgram->getUniformLocation("u_texture_be_attacked"); glProgram->setUniformLocationWith1i(loc, 2); GL::bindTexture2DN(2, _textureBeAttacked->getName()); loc = glProgram->getUniformLocation("u_texture_be_cured"); glProgram->setUniformLocationWith1i(loc, 3); GL::bindTexture2DN(3, _textureBeCured->getName()); loc = glProgram->getUniformLocation("u_para_ambient_color"); glProgram->setUniformLocationWith4fv(loc, &_paraAmbientColor.x, 1); loc = glProgram->getUniformLocation("u_para_attacked_cured"); glProgram->setUniformLocationWith4fv(loc, &_paraAttackedCured.x, 1); loc = glProgram->getUniformLocation("u_lights"); glProgram->setUniformLocationWith4fv(loc, &_lights[0].x, DDConfig::NUM_LIGHT); loc = glProgram->getUniformLocation("u_lights_color"); glProgram->setUniformLocationWith4fv(loc, &_lightsColor[0].x, DDConfig::NUM_LIGHT); loc = glProgram->getUniformLocation("u_building_animation_index"); glProgram->setUniformLocationWith1i(loc, _buildingAnimationIndex); */ glProgram->setUniformsForBuiltins(transform); glBindBuffer(GL_ARRAY_BUFFER, _vbo); if (_dirty) { _dirty = false; glBufferData(GL_ARRAY_BUFFER, sizeof(ShadowCoverVertexFormat)*(DDConfig::BATTLE_NUM+2)*(DDConfig::BATTLE_NUM+2), _vertexData, GL_STREAM_DRAW); } if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(_vao); } else { // TODO } // glDisable(GL_BLEND); glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // shadow cover打开depth test同时在fsh中对a为0的进行discard,以保证重合交叠处不会交叠而加深。 // glEnable(GL_DEPTH_TEST); // glDepthMask(true); // TODO 画两次,第一次,较淡的色彩,且灰度,同时position 缩小一些,造成广泛的偏移,来作为迷雾的投影。 第二次,正常的画。 glDrawArrays(GL_POINTS, 0, (DDConfig::BATTLE_NUM+2)*(DDConfig::BATTLE_NUM+2)); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } glBindBuffer(GL_ARRAY_BUFFER, 0); // glDisable(GL_DEPTH_TEST); // glDepthMask(false); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, (DDConfig::BATTLE_NUM+2)*(DDConfig::BATTLE_NUM+2)); CHECK_GL_ERROR_DEBUG(); }
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 PixelNode::onDraw(const cocos2d::Mat4 &transform, uint32_t flags) { auto glProgram = _programState->getGLProgram(); glProgram->use(); auto loc = glProgram->getUniformLocation("u_cbf_opacity"); glProgram->setUniformLocationWith1f(loc, _opacityAsAlpha ? getOpacity()/255.f : 1.f); loc = glProgram->getUniformLocation("u_posexpand"); glProgram->setUniformLocationWith1f(loc, _opacityAsPosExpand ? _posexpand*(1.f - getOpacity()/255.f) : 0.f); loc = glProgram->getUniformLocation("u_mixcolor"); glProgram->setUniformLocationWith4fv(loc, &(_mixColor.x), 1); loc = glProgram->getUniformLocation("u_y_cut"); glProgram->setUniformLocationWith1f(loc, _yCut); glProgram->setUniformsForBuiltins(transform); glBindBuffer(GL_ARRAY_BUFFER, _vbo); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(_vao); } else { // TODO } if (_blend) { glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } else { glDisable(GL_BLEND); } glEnable(GL_CULL_FACE); // shadow cover打开depth test同时在fsh中对a为0的进行discard,以保证重合交叠处不会交叠而加深。 glEnable(GL_DEPTH_TEST); glDepthMask(true); if (_stencil) { glEnable(GL_STENCIL_TEST); // Draw floor glStencilFunc(GL_ALWAYS, 1, 0xFF); // Set any stencil to 1 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glStencilMask(0xFF); // Write to stencil buffer glDepthMask(GL_FALSE); // Don't write to depth buffer glClear(GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (0 by default) } if (_stenciled) { glEnable(GL_STENCIL_TEST); glStencilFunc(GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1 glStencilMask(0x00); // Don't write anything to stencil buffer glDepthMask(GL_TRUE); // Write to depth buffer } glDrawArrays(GL_TRIANGLES, 0, _count); if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } glDisable(GL_STENCIL_TEST); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); glDepthMask(false); glEnable (GL_BLEND); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_count); CHECK_GL_ERROR_DEBUG(); }
void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type) { switch (type) { case kShaderType_PositionTextureColor: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag); break; case kShaderType_PositionTextureColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); break; case kShaderType_PositionTextureColorAlphaTest: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); break; case kShaderType_PositionTextureColorAlphaTestNoMV: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag); break; case kShaderType_PositionColor: p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag); break; case kShaderType_PositionColorTextureAsPointsize: p->initWithByteArrays(ccPositionColorTextureAsPointsize_vert ,ccPositionColor_frag); break; case kShaderType_PositionColor_noMVP: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); break; case kShaderType_PositionTexture: p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag); break; case kShaderType_PositionTexture_uColor: p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); break; case kShaderType_PositionTextureA8Color: p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); break; case kShaderType_Position_uColor: p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag); p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); break; case kShaderType_PositionLengthTexureColor: p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); break; case kShaderType_LabelDistanceFieldNormal: p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag); break; case kShaderType_LabelDistanceFieldGlow: p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag); break; case kShaderType_UIGrayScale: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTexture_GrayScale_frag); break; case kShaderType_LabelNormal: p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag); break; case kShaderType_LabelOutline: p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag); break; case kShaderType_CameraClear: p->initWithByteArrays(ccCameraClearVert, ccCameraClearFrag); break; default: CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__); return; } p->link(); p->updateUniforms(); CHECK_GL_ERROR_DEBUG(); }
void RawStencilBufferTest::onDisableStencil() { glDisable(GL_STENCIL_TEST); CHECK_GL_ERROR_DEBUG(); }
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"); }
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"); }