PRKit is an addition library to the cocos2d-x framework to easily fill polygons with texture.
PRKit efficiently triangulates the vertices with the provided triangulator.
PRKit provided by – http://precognitiveresearch.com/
Triangulator provided by – John W. Ratcliff
Link to github http://github.com/dmazzella/cocos2d-x-PRKit.git
Lets take look to some parts that concerns us. (Skip to usage if not intrested in details)
PRFilledPolygon overrides the draw method and provides 2 implementations. The first one is OpenGL ES 1.1 and the second OpenGL ES 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
... bool PRFilledPolygon::initWithPointsandTextureusingTriangulator(Vector2dVector polygonPoints, CCTexture2D *fillTexture, PRRatcliffTriangulator* polygonTriangulator) { //important - since opengl es 2 uses shaders a shader has to be set setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture)); triangulator = polygonTriangulator; setTexture(fillTexture); setPoints(polygonPoints); return true; } void PRFilledPolygon:: calculateTextureCoordinates() { for (int j = 0; j < areaTrianglePointCount; j++) { textureCoordinates[j] = ccpMult(areaTrianglePoints[j], 1.0f/texture->getPixelsWide()*CC_CONTENT_SCALE_FACTOR()); textureCoordinates[j].y = 1 - textureCoordinates[j].y; } } #if 0 void PRFilledPolygon::draw() { //CCNode::draw(); glDisableClientState(GL_COLOR_ARRAY); // we have a pointer to vertex points so enable client state glBindTexture(GL_TEXTURE_2D, texture->getName()); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glVertexPointer(2, GL_FLOAT, 0, areaTrianglePoints); glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates); glDrawArrays(GL_TRIANGLES, 0, areaTrianglePointCount); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //Restore texture matrix and switch back to modelview matrix glEnableClientState(GL_COLOR_ARRAY); } #else // Replace the draw method with this void PRFilledPolygon::draw(){ CC_NODE_DRAW_SETUP(); ccGLBindTexture2D( this->texture->getName() ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); ccGLBlendFunc( blendFunc.src, blendFunc.dst); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords ); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, areaTrianglePoints); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates); glDrawArrays(GL_TRIANGLES, 0, areaTrianglePointCount); } #endif ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } Vector2dVector a; a.push_back( Vector2d(0,0)); a.push_back( Vector2d(200,0)); a.push_back( Vector2d(200,200)); a.push_back( Vector2d(0,200)); CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("texture_pattern.png"); PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(a,texture); filledPolygon->setPosition(ccp(10,10)); this->addChild(filledPolygon); return true; } |