For my current project, I'm essentially looking for a way to do a texture-filled polygon. The texture should not stretch or warp, but it should repeat to fill the entire polygon. The polygon may be concave or convex.
Basically, I want the same thing that ccDrawPoly() does, but instead of just an outline, it would fill with a repeating texture. I'm an openGL noob, so keep that in mind.
Think of creating a polygon in Photoshop then clicking the paint bucket to fill it with a pattern.
So far, I've tried the following approaches with very little success.
1) Modified CCTexture2D and adding a "drawInPolygon" method, like this:
- (void) drawInPolygon: (CGPoint *) poli points: (int) points
{
GLfloat coordinates[] = { 0.0f, _maxT,
_maxS, _maxT,
0.0f, 0.0f,
_maxS, 0.0f };
glBindTexture(GL_TEXTURE_2D, _name);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glVertexPointer(2, GL_FLOAT, 0, poli);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glDrawArrays(GL_TRIANGLE_FAN, 0, points);
}
This has led to the texture being stretched and rotated... and looking awful, and it also requires that the polygon is convex, so I've even got a library that makes a polygon convex by triangulation.
2) Implementing a CCRibbon object as a child of my node that has the vertices.
This kinda works, but it looks ugly.
3) CCRenderTexture, which doesn't work because the levels I'm creating are well over 5000 px wide and it seems silly to create huge textures in memory when it's just made up of one smaller texture repeated and clipped.
---
Does anyone have any advice for me? Am I missing something obvious here, like maybe just setting the background to be a huge CCSprite that has GL_REPEAT in its texture options and clipping it somehow?
I've searched these forums for a few days now and haven't really had a lot of luck.
Any help you could provide would be greatly appreciated!
