
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-04-04 标题cocos2dx学习笔记CCDrawingPrimitives分类编程 / C C / cocos2dx 标签cocos2dx·CCDrawingPrimitivescocos2dx学习笔记CCDrawingPrimitivesopengles常用的几何图像绘制CCDrawingPrimitives.cpp代码笔记先备份下opengles常用的几何图像如下图。图像来自【 http://blog.php1.cn/article/details/1151 】opengles常用的几何图像绘制CCDrawingPrimitives.cpp代码笔记代码笔记写的比较简单很多用到的上下文代码也一并放到了注释里面。看起来可能会比较乱。/* 初始化方法从方法中可以看出使用的是Shader_Position_uColor这个shader它的内容为 【 ccShader_Position_uColor_vert.h 】和【ccShader_Position_uColor_frag.h 】 里面有a_positionattribute和u_coloru_pointSize两个uniform 其中a_position在 void CCShaderCache::loadDefaultShader(CCGLProgram *p, int type) { case kCCShaderType_Position_uColor: p-initWithVertexShaderByteArray(ccPosition_uColor_vert, ccPosition_uColor_frag); p-addAttribute(aVertex, kCCVertexAttrib_Position); break; } 这个代码段中被bind但是作者感觉这里可能有点问题 p-addAttribute(aVertex, kCCVertexAttrib_Position);应该改为 p-addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); 但是后面的代码仍然可以继续工作。比较奇怪暂时没有找到原因。 */staticvoidlazy_init(void){if(!s_bInitialized){//// Position and 1 color passed as a uniform (to simulate glColor4ub )//s_pShaderCCShaderCache::sharedShaderCache()-programForKey(kCCShader_Position_uColor);s_pShader-retain();//获取常量的句柄s_nColorLocationglGetUniformLocation(s_pShader-getProgram(),u_color);CHECK_GL_ERROR_DEBUG();s_nPointSizeLocationglGetUniformLocation(s_pShader-getProgram(),u_pointSize);CHECK_GL_ERROR_DEBUG();s_bInitializedtrue;}}// When switching from backround to foreground on android, we want the params to be initialized againvoidccDrawInit(){lazy_init();}voidccDrawFree(){CC_SAFE_RELEASE_NULL(s_pShader);s_bInitializedfalse;}/* void ccGLEnableVertexAttribs( unsigned int flags ) { ccGLBindVAO(0); bool enablePosition flags kCCVertexAttribFlag_Position; if( enablePosition ! s_bVertexAttribPosition ) { if( enablePosition ) glEnableVertexAttribArray( kCCVertexAttrib_Position ); else glDisableVertexAttribArray( kCCVertexAttrib_Position ); s_bVertexAttribPosition enablePosition; } bool enableColor (flags kCCVertexAttribFlag_Color) ! 0 ? true : false; if( enableColor ! s_bVertexAttribColor ) { if( enableColor ) glEnableVertexAttribArray( kCCVertexAttrib_Color ); else glDisableVertexAttribArray( kCCVertexAttrib_Color ); s_bVertexAttribColor enableColor; } bool enableTexCoords (flags kCCVertexAttribFlag_TexCoords) ! 0 ? true : false; if( enableTexCoords ! s_bVertexAttribTexCoords ) { if( enableTexCoords ) glEnableVertexAttribArray( kCCVertexAttrib_TexCoords ); else glDisableVertexAttribArray( kCCVertexAttrib_TexCoords ); s_bVertexAttribTexCoords enableTexCoords; } } *///画点voidccDrawPoint(constCCPointpoint){lazy_init();ccVertex2F p;p.xpoint.x;p.ypoint.y;//首先enable输入点矩阵ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);//选用当前的shaders_pShader-use();//刷新内置的矩阵s_pShader-setUniformsForBuiltins();//更新颜色和大小的常量值s_pShader-setUniformLocationWith4fv(s_nColorLocation,(GLfloat*)s_tColor.r,1);s_pShader-setUniformLocationWith1f(s_nPointSizeLocation,s_fPointSize);#ifdefEMSCRIPTENsetGLBufferData(p,8);glVertexAttribPointer(kCCVertexAttrib_Position,2,GL_FLOAT,GL_FALSE,0,0);#else//将坐标点写入attributeglVertexAttribPointer(kCCVertexAttrib_Position,2,GL_FLOAT,GL_FALSE,0,p);#endif// EMSCRIPTEN//画一个点glDrawArrays(GL_POINTS,0,1);CC_INCREMENT_GL_DRAWS(1);}voidccDrawPoints(constCCPoint*points,unsignedintnumberOfPoints){lazy_init();。。。。。 。。。。。//绘制独立的点。glDrawArrays(GL_POINTS,0,(GLsizei)numberOfPoints);CC_SAFE_DELETE_ARRAY(newPoints);CC_INCREMENT_GL_DRAWS(1);}voidccDrawLine(constCCPointorigin,constCCPointdestination){lazy_init();。。。。。 。。。。。//顶点两两连接为多条线段构成。glDrawArrays(GL_LINES,0,2);CC_INCREMENT_GL_DRAWS(1);}voidccDrawRect(CCPoint origin,CCPoint destination){。。。。。 。。。。。}voidccDrawSolidRect(CCPoint origin,CCPoint destination,ccColor4F color){。。。。。 。。。。。}voidccDrawPoly(constCCPoint*poli,unsignedintnumberOfPoints,boolclosePolygon){lazy_init();。。。。。 。。。。。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);}voidccDrawSolidPoly(constCCPoint*poli,unsignedintnumberOfPoints,ccColor4F color){lazy_init();。。。。。 。。。。。//以一个点为三角形公共顶点组成一系列相邻的三角形。glDrawArrays(GL_TRIANGLE_FAN,0,(GLsizei)numberOfPoints);CC_SAFE_DELETE_ARRAY(newPoli);CC_INCREMENT_GL_DRAWS(1);}//画圆voidccDrawCircle(constCCPointcenter,floatradius,floatangle,unsignedintsegments,booldrawLineToCenter,floatscaleX,floatscaleY){lazy_init();//先计算要画的点intadditionalSegment1;if(drawLineToCenter)additionalSegment;constfloatcoef2.0f*(float)M_PI/segments;GLfloat*vertices(GLfloat*)calloc(sizeof(GLfloat)*2*(segments2),1);if(!vertices)return;for(unsignedinti0;isegments;i){floatradsi*coef;GLfloat jradius*cosf(radsangle)*scaleXcenter.x;GLfloat kradius*sinf(radsangle)*scaleYcenter.y;verticesj;verticesk;}vertices[(segments1)*2]center.x;vertices[(segments1)*21]center.y;s_pShader-use();s_pShader-setUniformsForBuiltins();s_pShader-setUniformLocationWith4fv(s_nColorLocation,(GLfloat*)s_tColor.r,1);//enable输入点的矩阵ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);#ifdefEMSCRIPTENsetGLBufferData(vertices,sizeof(GLfloat)*2*(segments2));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)segmentsadditionalSegment);free(vertices);CC_INCREMENT_GL_DRAWS(1);}voidCC_DLLccDrawCircle(constCCPointcenter,floatradius,floatangle,unsignedintsegments,booldrawLineToCenter){。。。。。 。。。。。}voidccDrawQuadBezier(constCCPointorigin,constCCPointcontrol,constCCPointdestination,unsignedintsegments){。。。。。 。。。。。}voidccDrawCatmullRom(CCPointArray*points,unsignedintsegments){。。。。。 。。。。。}voidccDrawCardinalSpline(CCPointArray*config,floattension,unsignedintsegments){。。。。。 。。。。。}//画贝塞尔曲线voidccDrawCubicBezier(constCCPointorigin,constCCPointcontrol1,constCCPointcontrol2,constCCPointdestination,unsignedintsegments){lazy_init();//先计算出需要画的点ccVertex2F*verticesnewccVertex2F;floatt0;for(unsignedinti0;isegments;i){vertices.xpowf(1-t,3)*origin.x3.0f*powf(1-t,2)*t*control1.x3.0f*(1-t)*t*t*control2.xt*t*t*destination.x;vertices.ypowf(1-t,3)*origin.y3.0f*powf(1-t,2)*t*control1.y3.0f*(1-t)*t*t*control2.yt*t*t*destination.y;t1.0f/segments;}vertices.xdestination.x;vertices.ydestination.y;s_pShader-use();s_pShader-setUniformsForBuiltins();s_pShader-setUniformLocationWith4fv(s_nColorLocation,(GLfloat*)s_tColor.r,1);//enable输入点的矩阵ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);#ifdefEMSCRIPTENsetGLBufferData(vertices,(segments1)*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)segments1);CC_SAFE_DELETE_ARRAY(vertices);CC_INCREMENT_GL_DRAWS(1);}//更新保存的颜色voidccDrawColor4F(GLfloat r,GLfloat g,GLfloat b,GLfloat a){s_tColor.rr;s_tColor.gg;s_tColor.bb;s_tColor.aa;}//更新保存的点的大小voidccPointSize(GLfloat pointSize){s_fPointSizepointSize*CC_CONTENT_SCALE_FACTOR();//TODO :glPointSize( pointSize );}//更新保存的颜色voidccDrawColor4B(GLubyte r,GLubyte g,GLubyte b,GLubyte a){s_tColor.rr/255.0f;s_tColor.gg/255.0f;s_tColor.bb/255.0f;s_tColor.aa/255.0f;}