Search code examples
arraysopengltexturescocos2d-xtexture2d

Fill the area with texture cocos2d-x


I've been trying to make "Obstacle" class which builds box2d body by array of points and draw the area which my body covers. As for body, it works totally ok, I receive array of points, build b2PolygonShape and so on. BUT, I really don't know, how to fill the area with color or texture which was built by points array. Here's my draw() method:

void Obstacle::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)

{ CC_NODE_DRAW_SETUP();

glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
GL::bindTexture2D(obstacleTexture->getName());
//DrawPrimitives::setDrawColor4F(1.0, 1.0, 0.0, 1.0);

glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)shapePoints.size());

}

vertices is the array of points which I use for creating b2body.


Solution

  • You should triangulate the polygon shape you built in order to draw.

    poly2tri is a good option for triangulating shapes: https://code.google.com/p/poly2tri/

    After triangulating your shape, map texture coordinates or set the vertex colors.