bool MGRDrawNode::init() { _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR)); ensureCapacity(512); ensureCapacityGLPoint(64); ensureCapacityGLLine(256); 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; return true; }
void MGRDrawNode::drawLine(const Vec2& origin, const Vec2& destination, const Color4F& color) { ensureCapacityGLLine(2); V2F_C4B_T2F* point = (V2F_C4B_T2F*)_bufferGLLine + _bufferCountGLLine; V2F_C4B_T2F a = {origin, Color4B(color), Tex2F(0.0, 0.0)}; V2F_C4B_T2F b = {destination, Color4B(color), Tex2F(0.0, 0.0)}; *point = a; *(point + 1) = b; _bufferCountGLLine += 2; _dirtyGLLine = true; }
void RoadNode::drawLine(const cocos2d::Vec3& src, const cocos2d::Vec3& dest) { ensureCapacityGLLine(2); V3F_T2F *point = (V3F_T2F*)(_bufferGLLine + _bufferCountGLLine); V3F_T2F a = {src,Tex2F(0.0, 0.0)}; V3F_T2F b = {dest,Tex2F(0.0, 0.0)}; *point = a; *(point+1) = b; _bufferCountGLLine += 2; _dirtyGLLine = true; }
void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color) { unsigned int vertext_count; if(closePolygon) { vertext_count = 2 * numberOfPoints; ensureCapacityGLLine(vertext_count); } else { vertext_count = 2 * (numberOfPoints - 1); ensureCapacityGLLine(vertext_count); } V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine); unsigned int i = 0; for(; i<numberOfPoints-1; i++) { V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)}; V2F_C4B_T2F b = {poli[i+1], Color4B(color), Tex2F(0.0, 0.0)}; *point = a; *(point+1) = b; point += 2; } if(closePolygon) { V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)}; V2F_C4B_T2F b = {poli[0], Color4B(color), Tex2F(0.0, 0.0)}; *point = a; *(point+1) = b; } _bufferCountGLLine += vertext_count; }
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; }
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; }