Drawing texture does not work anymore with a small amount of triangles
- by Paul
When i draw lines, the vertices are well connected. But when i draw the texture inside the triangles, it only works with i<4 in the for loop, otherwise with i<5 for example, there is a EXC_BAD_ACCESS message, at @synthesize textureImage = _textureImage. I don't understand why. (The generatePolygons method seems to work fine as i tried to draw lines with many vertices as in the second image below. And textureImage remains the same for i<4 or i<5 : it's a 512px square image).
Here are the images :
What i want to achieve is to put the red points and connect them to the y-axis (the green points) and color the area (the green triangles) :
If i only draw lines, it works fine :
Then with a texture color, it works for i<4 in the loop (the red points in my first image, plus the fifth one to connect the last y) :
But then, if i set i<5, the debug tool says EXC_BAD_ACCESS at the synthesize of _textureImage. Here is my code :
I set a texture color in HelloWordLayer.mm with :
CCSprite *textureImage = [self spriteWithColor:color3 textureSize:512];
_terrain.textureImage = textureImage;
Then in the class Terrain, i create the vertices and put the texture in the draw method :
@implementation Terrain
@synthesize textureImage = _textureImage; //EXC_BAD_ACCESS for i<5
- (void)generatePath2{
CGSize winSize = [CCDirector sharedDirector].winSize;
float x = 40;
float y = 0;
for(int i = 0; i < kMaxKeyPoints+1; ++i) {
_hillKeyPoints[i] = CGPointMake(x, y);
x = 150 + (random() % (int) 30);
y += 30;
}
}
-(void)generatePolygons{
_nPolyVertices = 0;
float x1 = 0;
float y1 = 0;
int keyPoints = 0;
for (int i=0; i<4; i++){ /* HERE : 4 = OK / 5 = crash */
//V0: at (0,0)
_polyVertices[_nPolyVertices] = CGPointMake(x1, y1);
_polyTexCoords[_nPolyVertices++] = CGPointMake(x1, y1);
//V1: to the first "point"
_polyVertices[_nPolyVertices] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);
_polyTexCoords[_nPolyVertices++] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);
keyPoints++; //from point at index 0 to 1
//V2, same y as point n°2:
_polyVertices[_nPolyVertices] = CGPointMake(0, _hillKeyPoints[keyPoints].y);
_polyTexCoords[_nPolyVertices++] = CGPointMake(0, _hillKeyPoints[keyPoints].y);
//V1 again
_polyVertices[_nPolyVertices] = _polyVertices[_nPolyVertices-2];
_polyTexCoords[_nPolyVertices++] = _polyVertices[_nPolyVertices-2];
//V2 again
_polyVertices[_nPolyVertices] = _polyVertices[_nPolyVertices-2];
_polyTexCoords[_nPolyVertices++] = _polyVertices[_nPolyVertices-2];
//V3 = same x,y as point at index 1
_polyVertices[_nPolyVertices] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);
_polyTexCoords[_nPolyVertices] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);
y1 = _polyVertices[_nPolyVertices].y;
_nPolyVertices++;
}
}
- (id)init {
if ((self = [super init])) {
[self generatePath2];
[self generatePolygons];
}
return self;
}
- (void) draw {
//glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, _textureImage.texture.name);
glColor4f(1, 1, 1, 1);
glVertexPointer(2, GL_FLOAT, 0, _polyVertices);
glTexCoordPointer(2, GL_FLOAT, 0, _polyTexCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nPolyVertices);
glColor4f(1, 1, 1, 1);
for(int i = 1; i < 40; ++i) {
ccDrawLine(_polyVertices[i-1], _polyVertices[i]);
}
// restore default GL states
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
Do you see anything wrong in this code?
Thanks for your help