1
\$\begingroup\$

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) :

enter image description here

If I only draw lines, it works fine :

enter image description here

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) :

enter image description here

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?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Where and how do you declare _polyVertices and _polyTexCoords? \$\endgroup\$
    – bummzack
    Commented Jul 3, 2012 at 7:02
  • \$\begingroup\$ I don't know the IDE you're using, but I would add watches to _polyVertices, _polyTexCoords and _nPolyVertices in the loop. Maybe that's gonna reveal the root of the problem. \$\endgroup\$
    – Marton
    Commented Jul 3, 2012 at 7:26
  • \$\begingroup\$ @bummzack argh i did not put enough space memory in the array... thanks for the help ;-) \$\endgroup\$
    – Paul
    Commented Jul 3, 2012 at 11:12
  • \$\begingroup\$ @Marton thanks Marton for the answer, alright it works now, thanks anyway \$\endgroup\$
    – Paul
    Commented Jul 3, 2012 at 11:12

1 Answer 1

0
\$\begingroup\$

Ok, i had to put more space in the array that keep all the vertices, it works now, thanks

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .