void GLESDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); const float32 k_segments = 16.0f; int vertexCount=16; const float32 k_increment = 2.0f * b2_pi / k_segments; float32 theta = 0.0f; GLfloat* glVertices = new GLfloat[vertexCount*2]; for (int i = 0; i < k_segments; ++i) { b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta)); glVertices[i*2]=v.x * mRatio; glVertices[i*2+1]=v.y * mRatio; theta += k_increment; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); delete[] glVertices; }
void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* old_vertices, int vertexCount, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); b2Vec2* vertices = new b2Vec2[vertexCount]; for( int i=0;i<vertexCount;i++) { vertices[i] = old_vertices[i]; vertices[i] *= mRatio; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); CC_INCREMENT_GL_DRAWS(2); CHECK_GL_ERROR_DEBUG(); delete[] vertices; }
void CCGSpell::onDraw(const Mat4 &transform, uint32_t flags) { // 启用attributes变量输入,顶点坐标,纹理坐标,颜色 ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); _pShaderProgram->use(); _pShaderProgram->setUniformsForBuiltins(transform); // 绑定纹理到纹理槽0 ccGLBindTexture2D(_texture->getName()); long offset = (long)&_quad; // vertex int diff = offsetof(V3F_C4B_T2F, vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (void*)(offset + diff)); // texCoods diff = offsetof(V3F_C4B_T2F, texCoords); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (void*)(offset + diff)); // color diff = offsetof(V3F_C4B_T2F, colors); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (void*)(offset + diff)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CC_INCREMENT_GL_DRAWS(1); }
void drawPoint( const Point& point ) { lazy_init(); Vertex2F p; p.x = point.x; p.y = point.y; GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION ); s_shader->use(); s_shader->setUniformsForBuiltins(); s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1); s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize); #ifdef EMSCRIPTEN setGLBufferData(&p, 8); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &p); #endif // EMSCRIPTEN glDrawArrays(GL_POINTS, 0, 1); CC_INCREMENT_GL_DRAWS(1); }
void CCLayerColor::draw() { CC_NODE_DRAW_SETUP(); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color ); // // Attributes // #ifdef EMSCRIPTEN setGLBufferData(m_pSquareVertices, 4 * sizeof(ccVertex2F), 0); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLBufferData(m_pSquareColors, 4 * sizeof(ccColor4F), 1); glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, m_pSquareVertices); glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, m_pSquareColors); #endif // EMSCRIPTEN ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst ); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CC_INCREMENT_GL_DRAWS(1); }
void ccDrawSolidPoly( const CCPoint *poli, unsigned int numberOfPoints, ccColor4F color ) { lazy_init(); s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &color.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); // XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed ccVertex2F* newPoli = new ccVertex2F[numberOfPoints]; // iPhone and 32-bit machines optimization if( sizeof(CCPoint) == sizeof(ccVertex2F) ) { glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, poli); } else { // Mac on 64-bit for( unsigned int i=0; i<numberOfPoints; i++) { newPoli[i] = vertex2( poli[i].x, poli[i].y ); } glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, newPoli); } glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints); CC_SAFE_DELETE_ARRAY(newPoli); CC_INCREMENT_GL_DRAWS(1); }
void EXSunShineSprite::draw() { ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); m_sBlendFunc.src = GL_DST_COLOR; m_sBlendFunc.dst = GL_ONE; ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst ); this->getShaderProgram()->use(); this->getShaderProgram()->setUniformsForBuiltins(); ccGLBindTexture2D( this->getTexture()->getName() ); #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); CC_INCREMENT_GL_DRAWS(1); }
void ccDrawCubicBezier(const CCPoint& origin, const CCPoint& control1, const CCPoint& control2, const CCPoint& destination, unsigned int segments) { lazy_init(); ccVertex2F* vertices = new ccVertex2F[segments + 1]; float t = 0; for(unsigned int i = 0; i < segments; i++) { vertices[i].x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x; vertices[i].y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y; t += 1.0f / segments; } vertices[segments].x = destination.x; vertices[segments].y = destination.y; s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1); CC_SAFE_DELETE_ARRAY(vertices); CC_INCREMENT_GL_DRAWS(1); }
void RedSprite::draw() { ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); getShaderProgram()->use(); getShaderProgram()->setUniformsForBuiltins(); ccGLBindTexture2D(getTexture()->getName()); // attributes. #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); CC_INCREMENT_GL_DRAWS(1); }
void drawQuadBezier(const Point& origin, const Point& control, const Point& destination, unsigned int segments) { lazy_init(); Vertex2F* vertices = new Vertex2F[segments + 1]; float t = 0.0f; for(unsigned int i = 0; i < segments; i++) { vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x; vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y; t += 1.0f / segments; } vertices[segments].x = destination.x; vertices[segments].y = destination.y; s_shader->use(); s_shader->setUniformsForBuiltins(); s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1); GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION ); #ifdef EMSCRIPTEN setGLBufferData(vertices, (segments + 1) * sizeof(Vertex2F)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); #endif // EMSCRIPTEN glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1); CC_SAFE_DELETE_ARRAY(vertices); CC_INCREMENT_GL_DRAWS(1); }
void FXLightning::render(){ float angle = atan2f(end.y - start.y , end.x - start.x); float x = cosf(angle - M_PI/2.0) * width; float y = sinf(angle - M_PI/2.0) * width; float rx = cosf(angle - M_PI/2.0) * spread; float ry = sinf(angle - M_PI/2.0) * spread; static int counter = 0; int n_samples = ccpDistance(start, end)/480 * K_NUMBER_OF_LIGHTNING_SAMPLES; if (n_samples > K_NUMBER_OF_LIGHTNING_SAMPLES) { n_samples = K_NUMBER_OF_LIGHTNING_SAMPLES; } for (int i=0; i < n_samples; i+=2) { CCPoint p = ccpLerp(start, end, i/(float)(n_samples-1)); float r = ((rand()%100) / 100.0 - 0.5) * 7; // float r = 20* noise1(counter*speed); float w = 0; if (i < n_samples*0.5) { w = i/(float)n_samples * 3; } else{ w = (n_samples-i-2)/(float)n_samples * 3; } w = clampf(w, 0, 1); if (tapper == 0) { vtx[ i+0 ].x = p.x + x + rx*r*w; vtx[ i+0 ].y = p.y + y + ry*r*w; vtx[ i+1 ].x = p.x - x + rx*r*w; vtx[ i+1 ].y = p.y - y + ry*r*w; } else{ vtx[ i+0 ].x = p.x + x*tapper*(1-i/(float)n_samples) + rx*r; vtx[ i+0 ].y = p.y + y*tapper*(1-i/(float)n_samples) + ry*r; vtx[ i+1 ].x = p.x - x*tapper*(1-i/(float)n_samples) + rx*r; vtx[ i+1 ].y = p.y - y*tapper*(1-i/(float)n_samples) + ry*r; } counter+=5; } pShaderProgram->use(); pShaderProgram->setUniformsForBuiltins(); ccGLBindTexture2D(texture->getName()); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vtx); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, uvs); glDrawArrays(GL_TRIANGLE_STRIP, 0,n_samples); //DEBUG // glDrawArrays(GL_LINE_STRIP, 0, n_samples); CC_INCREMENT_GL_DRAWS(1); }
void HueSprite::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 ); m_shader->use(); m_shader->setUniformsForBuiltins(); if (m_pobTexture != NULL) { //ccGLBindTexture2D( m_texure1->getName()); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_pobTexture->getName()); } else { ccGLBindTexture2D(0); } glUniformMatrix3fv(m_loc_hue, 1, GL_FALSE, (GLfloat*)&m_mat); // // 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(); CC_INCREMENT_GL_DRAWS(1); CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw"); }
// overriding draw method void CCParticleSystemQuad::draw() { CCAssert(!m_pBatchNode,"draw should not be called when added to a particleBatchNode"); CC_NODE_DRAW_SETUP(); ccGLBindTexture2D( m_pTexture->getName() ); ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst ); CCAssert( m_uParticleIdx == m_uParticleCount, "Abnormal error in particle quad"); #if CC_TEXTURE_ATLAS_USE_VAO // // Using VBO and VAO // glBindVertexArray( m_uVAOname ); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]); #endif glDrawElements(GL_TRIANGLES, (GLsizei) m_uParticleIdx*6, GL_UNSIGNED_SHORT, 0); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif glBindVertexArray( 0 ); #else // // Using VBO without VAO // #define kQuadSize sizeof(m_pQuads[0].bl) ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex ); glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]); // 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]); glDrawElements(GL_TRIANGLES, (GLsizei) m_uParticleIdx*6, GL_UNSIGNED_SHORT, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); }
// overriding draw method void ParticleSystemQuad::draw() { CCASSERT(!_batchNode,"draw should not be called when added to a particleBatchNode"); CC_NODE_DRAW_SETUP(); GL::bindTexture2D( _texture->getName() ); GL::blendFunc( _blendFunc.src, _blendFunc.dst ); CCASSERT( _particleIdx == _particleCount, "Abnormal error in particle quad"); #if CC_TEXTURE_ATLAS_USE_VAO // // Using VBO and VAO // GL::bindVAO(_VAOname); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); #endif glDrawElements(GL_TRIANGLES, (GLsizei) _particleIdx*6, GL_UNSIGNED_SHORT, 0); #if CC_REBIND_INDICES_BUFFER glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif #else // // Using VBO without VAO // #define kQuadSize sizeof(_quads[0].bl) GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX ); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); // 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) _particleIdx*6, GL_UNSIGNED_SHORT, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); }
void CCSprite3D::draw() { // m_bDepthTestEnabled = 0 == glIsEnabled(GL_DEPTH_TEST) ? false : true; // // CCDirector::sharedDirector()->setDepthTest(true); // CC_NODE_DRAW_SETUP(); // getShaderProgram()->use(); // getShaderProgram()->setUniformsForBuiltins(); CCPoint const & origin = ccp(0, 0); CCPoint const & destination = ccp(100, 100); ccVertex3F vertices[3] = { // {origin.x, origin.y, 100}, {destination.x, origin.y, 200}, {destination.x, destination.y, 100}, {origin.x, destination.y, 200} }; ccTex2F uv[3] = { // {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f} }; ccColor4B colors[3] = { // {255, 255, 255, 255}, {128, 128, 128, 128}, {128, 128, 128, 128}, {255, 255, 255, 255} }; ccGLBindTexture2D(mCustSkin->getName()); ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex ); glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, uv); glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors); glDrawArrays(GL_TRIANGLES, 0, 3); CHECK_GL_ERROR_DEBUG(); CC_INCREMENT_GL_DRAWS(1); // m_pModel->draw(); // static unsigned int uiStartFrame = 0, uiEndFrame = 182; // static float fAnimSpeed = 16.5f; // ((CCModelMd2*)m_pModel)->animate(fAnimSpeed, uiStartFrame, uiEndFrame, true); // CCDirector::sharedDirector()->setDepthTest(m_bDepthTestEnabled); }
//------------------------------------------------------------- //- Render //- Renders a specific frame of the MD2 model //------------------------------------------------------------- void CCModelMd2::draw(unsigned int uiFrame) { float* m_pUV = new float[m_Head.numTriangles * 6]; memset(m_pUV, 0, m_Head.numTriangles * 6); float* m_pVertices = new float[m_Head.numTriangles * 9]; memset(m_pVertices, 0, m_Head.numTriangles * 9); int uvIdx = 0; int vertexIdx = 0; for (int i =0; i < m_Head.numTriangles; i++) { m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[0]].x; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[0]].y; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[0]].z; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[1]].x; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[1]].y; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[1]].z; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[2]].x; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[2]].y; m_pVertices[vertexIdx++] = m_pFrames[uiFrame].verts[m_pTriangles[i].vertIndices[2]].z; m_pUV[uvIdx++] = m_pTexCoords[m_pTriangles[i].texIndices[0]].s; m_pUV[uvIdx++] = m_pTexCoords[m_pTriangles[i].texIndices[0]].t; m_pUV[uvIdx++] = m_pTexCoords[m_pTriangles[i].texIndices[1]].s; m_pUV[uvIdx++] = m_pTexCoords[m_pTriangles[i].texIndices[1]].t; m_pUV[uvIdx++] = m_pTexCoords[m_pTriangles[i].texIndices[2]].s; m_pUV[uvIdx++] = m_pTexCoords[m_pTriangles[i].texIndices[2]].t; } ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords); if(!m_bIsCustomSkin) { if (m_pSkins) { ccGLBindTexture2D(m_pSkins[m_uiSkin].m_Image->getName()); } } else { ccGLBindTexture2D(m_pCustSkin->getName()); } glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, m_pVertices); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, m_pUV); glDrawArrays(GL_TRIANGLES, 0, m_Head.numTriangles * 3); CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); CC_SAFE_DELETE_ARRAY(m_pUV); CC_SAFE_DELETE_ARRAY(m_pVertices); }
void FilledPolygon::onDraw(const Mat4 &transform, uint32_t flags) { GL::bindTexture2D(_texture->getName()); GL::blendFunc(_blendFunc.src, _blendFunc.dst); getGLProgramState()->apply(transform); glDrawArrays(GL_TRIANGLES, 0, (GLsizei)_verticesCount); CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); }
void ccDrawPoly( const CCPoint *poli, unsigned int numberOfPoints, bool closePolygon ) { lazy_init(); s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); glLineWidth(1); // iPhone and 32-bit machines optimization if( sizeof(CCPoint) == sizeof(ccVertex2F) ) { #ifdef EMSCRIPTEN setGLBufferData((void*) poli, numberOfPoints * sizeof(CCPoint)); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, poli); #endif // EMSCRIPTEN if( closePolygon ) glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) numberOfPoints); else glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) numberOfPoints); } else { // Mac on 64-bit // XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed ccVertex2F* newPoli = new ccVertex2F[numberOfPoints]; for( unsigned int i=0; i<numberOfPoints;i++) { newPoli[i].x = poli[i].x; newPoli[i].y = poli[i].y; } #ifdef EMSCRIPTEN setGLBufferData(newPoli, numberOfPoints * sizeof(ccVertex2F)); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, newPoli); #endif // EMSCRIPTEN if( closePolygon ) glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) numberOfPoints); else glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) numberOfPoints); CC_SAFE_DELETE_ARRAY(newPoli); } CC_INCREMENT_GL_DRAWS(1); }
/** * 绘制 */ void MapCoordLine::draw(void) { 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(); ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst ); if (m_pobTexture != NULL) { ccGLBindTexture2D( m_pobTexture->getName() ); } else { ccGLBindTexture2D(0); } // // Attributes // ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex ); #define kQuadSize sizeof(m_sQuad.bl) if (points.size()) { long offset = (long)&points[0]; // 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_LINES, 0, points.size()); } CHECK_GL_ERROR_DEBUG(); CC_INCREMENT_GL_DRAWS(1); CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw"); }
void ccDrawCardinalSpline( CCPointArray *config, float tension, unsigned int segments ) { lazy_init(); ccVertex2F* vertices = new ccVertex2F[segments + 1]; unsigned int p; float lt; float deltaT = 1.0f / config->count(); for( unsigned int i=0; i < segments+1;i++) { float dt = (float)i / segments; // border if( dt == 1 ) { p = config->count() - 1; lt = 1; } else { p = dt / deltaT; lt = (dt - deltaT * (float)p) / deltaT; } // Interpolate CCPoint pp0 = config->get(p-1); CCPoint pp1 = config->get(p+0); CCPoint pp2 = config->get(p+1); CCPoint pp3 = config->get(p+2); CCPoint newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt); vertices[i].x = newPos.x; vertices[i].y = newPos.y; } s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*)&s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); #ifdef EMSCRIPTEN setGLBufferData(vertices, (segments + 1) * sizeof(ccVertex2F)); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); #endif // EMSCRIPTEN glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1); CC_SAFE_DELETE_ARRAY(vertices); CC_INCREMENT_GL_DRAWS(1); }
void ccDrawLines(ccVertex2F* vertices,int size) { lazy_init(); s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_LINE_STRIP, 0, size); CC_INCREMENT_GL_DRAWS(1); }
void GHDebugDrawLayer::DrawSolidCircleWithCenter(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color, CCGLProgram* mShaderProgram, GLint mColorLocation, float mRatio) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); const float32 k_segments = 16.0f; int vertexCount=16; const float32 k_increment = 2.0f * b2_pi / k_segments; float32 theta = 0.0f; std::vector<GLfloat> glVertices(vertexCount*2); for (int32 i = 0; i < k_segments; ++i) { b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta)); glVertices[i*2]=v.x * mRatio; glVertices[i*2+1]=v.y * mRatio; theta += k_increment; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, &glVertices[0]); glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); // Draw the axis line GHDebugDrawLayer::DrawSegmentWithPoint1(center, center+radius*axis, color, mShaderProgram, mColorLocation, mRatio); CC_INCREMENT_GL_DRAWS(2); CHECK_GL_ERROR_DEBUG(); }
void ccDrawPoints( const CCPoint *points, unsigned int numberOfPoints ) { lazy_init(); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); s_pShader->setUniformLocationWith1f(s_nPointSizeLocation, s_fPointSize); // XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed ccVertex2F* newPoints = new ccVertex2F[numberOfPoints]; // iPhone and 32-bit machines optimization if( sizeof(CCPoint) == sizeof(ccVertex2F) ) { #ifdef EMSCRIPTEN setGLBufferData((void*) points, numberOfPoints * sizeof(CCPoint)); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, points); #endif // EMSCRIPTEN } else { // Mac on 64-bit for( unsigned int i=0; i<numberOfPoints;i++) { newPoints[i].x = points[i].x; newPoints[i].y = points[i].y; } #ifdef EMSCRIPTEN // Suspect Emscripten won't be emitting 64-bit code for a while yet, // but want to make sure this continues to work even if they do. setGLBufferData(newPoints, numberOfPoints * sizeof(ccVertex2F)); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, newPoints); #endif // EMSCRIPTEN } glDrawArrays(GL_POINTS, 0, (GLsizei) numberOfPoints); CC_SAFE_DELETE_ARRAY(newPoints); CC_INCREMENT_GL_DRAWS(1); }
void drawCircle( const Point& center, float width, float height, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY) { lazy_init(); width *= 0.5f; height *= 0.5f; int additionalSegment = 1; if (drawLineToCenter) additionalSegment++; const float coef = 2.0f * (float)M_PI/segments; GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1); if( ! vertices ) return; for(unsigned int i = 0;i <= segments; i++) { float rads = i*coef; GLfloat j = width * cosf(rads + angle) * scaleX + center.x; GLfloat k = height * sinf(rads + angle) * scaleY + center.y; vertices[i*2] = j; vertices[i*2+1] = k; } vertices[(segments+1)*2] = center.x; vertices[(segments+1)*2+1] = center.y; s_shader->use(); s_shader->setUniformsForBuiltins(); s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1); GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION ); #ifdef EMSCRIPTEN setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); #endif // EMSCRIPTEN glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments+additionalSegment); ::free( vertices ); CC_INCREMENT_GL_DRAWS(1); }
void ShaderSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw"); // CCAssert(, "If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called"); CC_NODE_DRAW_SETUP(); // // 启用attributes变量输入,顶点坐标,纹理坐标,颜色 // ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex ); ccGLBlendFunc(this->getBlendFunc().src, this->getBlendFunc().dst); this->getShaderProgram()->use(); this->getShaderProgram()->setUniformsForBuiltins(); // 绑定纹理到纹理槽0 ccGLBindTexture2D(this->getTexture()->getName()); #define kQuadSize sizeof(this->_quad.bl) long offset = (long)&this->_quad; // 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, "CCSprite - draw"); }
void CCLayerColor::draw() { CC_NODE_DRAW_SETUP(); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color ); // // Attributes // glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, m_pSquareVertices); glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, m_pSquareColors); ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst ); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CC_INCREMENT_GL_DRAWS(1); }
void ccDrawLine( const CCPoint& origin, const CCPoint& destination ) { lazy_init(); ccVertex2F vertices[2] = { {origin.x, origin.y}, {destination.x, destination.y} }; s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_LINES, 0, 2); CC_INCREMENT_GL_DRAWS(1); }
void ccDrawCircle(const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, bool fill) { lazy_init(); int additionalSegment = 1; if (drawLineToCenter) additionalSegment++; const float coef = 2.0f * (float)M_PI/segments; GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1); if( ! vertices ) return; for(unsigned int i = 0;i <= segments; i++) { float rads = i*coef; GLfloat j = radius * cosf(rads + angle) * scaleX + center.x; GLfloat k = radius * sinf(rads + angle) * scaleY + center.y; vertices[i*2] = j; vertices[i*2+1] = k; } vertices[(segments+1)*2] = center.x; vertices[(segments+1)*2+1] = center.y; s_pShader->use(); s_pShader->setUniformsForBuiltins(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); #ifdef EMSCRIPTEN setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2)); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); #endif // EMSCRIPTEN glDrawArrays(fill ? GL_TRIANGLE_FAN : GL_LINE_STRIP, 0, (GLsizei)segments + additionalSegment); free( vertices ); CC_INCREMENT_GL_DRAWS(1); }
void CPrimitiveScene::draw() { CCCamera* pCamera = CCDirector::sharedDirector()->getRunningScene()->getCamera(); pCamera->setEyeXYZ(0,0,10); pCamera->setCenterXYZ(-50.0f, -50.0f, 0); // open yellow poly ccDrawColor4B(255, 255, 0, 255); glLineWidth(10); ccVertex3F vertices[] = { vertex3(0,0,0), vertex3(50,50,0), vertex3(100,50,0), vertex3(100,100,0), vertex3(50,100,0.1) }; s_pShader->use(); s_pShader->setUniformForModelViewProjectionMatrix(); s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); int numberOfPoints = 5; // XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed ccVertex3F* newPoli = new ccVertex3F[numberOfPoints]; // iPhone and 32-bit machines optimization if( sizeof(CCPoint) == sizeof(ccVertex3F) ) { glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, vertices); } else { // Mac on 64-bit for( unsigned int i=0; i<numberOfPoints;i++) { newPoli[i] = vertex3( vertices[i].x, vertices[i].y, vertices[i].z ); } glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, newPoli); } glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints); CC_SAFE_DELETE_ARRAY(newPoli); CC_INCREMENT_GL_DRAWS(1); }
void drawSolidPoly( const Point *poli, unsigned int numberOfPoints, Color4F color ) { lazy_init(); s_shader->use(); s_shader->setUniformsForBuiltins(); s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &color.r, 1); GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION ); // XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed Vertex2F* newPoli = new Vertex2F[numberOfPoints]; // iPhone and 32-bit machines optimization if( sizeof(Point) == sizeof(Vertex2F) ) { #ifdef EMSCRIPTEN setGLBufferData((void*) poli, numberOfPoints * sizeof(Point)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli); #endif // EMSCRIPTEN } else { // Mac on 64-bit for( unsigned int i=0; i<numberOfPoints;i++) { newPoli[i] = Vertex2F( poli[i].x, poli[i].y ); } #ifdef EMSCRIPTEN setGLBufferData(newPoli, numberOfPoints * sizeof(Vertex2F)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoli); #endif // EMSCRIPTEN } glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints); CC_SAFE_DELETE_ARRAY(newPoli); CC_INCREMENT_GL_DRAWS(1); }