(Cocos2D)CCSprite Problem with Blending when using Custom Drawing
I hope someone here can help me...
I've been having problems with blending when using custom drawing code in my CCSprite. I modified CCSprite to take note of vertices like a grid, in order for me to move those vertices and distort the sprite. After doing so, whenever there's an image added to the scene before adding the modified sprite into the scene, their blending doesn't work. The transparent parts of the image becomes black and the modified sprite cannot overwrite them. I tried different blending modes on the modified sprite to no avail. I even put GL_DST_ALPHA but it just made my modified sprite completely disappear :|
he grid code I used is taken from CocosDistort by piferrari
In my CCSprite, I just added this code to the init method:
Code:
-(void) initGridPoints
{
GLint width = texture_.contentSizeInPixels.width;
GLint height = texture_.contentSizeInPixels.height;
int i, j;
int k;
int m;
if (_mass == NULL)
{
_mass = (MASS*) malloc(sizeof(MASS)*GRID_SIZE_X*GRID_SIZE_Y);
if (_mass == NULL)
{
fprintf(stderr,"Can't allocate memroy.\n");
exit(-1);
}
}
k = 0;
for (i = 0; i < GRID_SIZE_X; i++)
for (j = 0; j < GRID_SIZE_Y; j++)
{
_mass[k].nail = (i == 0 || j == 0 || i == GRID_SIZE_X - 1
|| j == GRID_SIZE_Y - 1);
_mass[k].x[0] = i/(GRID_SIZE_X - 1.0)*width;
_mass[k].x[1] = j/(GRID_SIZE_Y - 1.0)*height;
_mass[k].x[2] = -(CLIP_FAR - CLIP_NEAR)/2.0;
_mass[k].v[0] = 0.0;
_mass[k].v[1] = 0.0;
_mass[k].v[2] = 0.0;
_mass[k].t[0] = i/(GRID_SIZE_X - 1.0);
_mass[k].t[1] = j/(GRID_SIZE_Y - 1.0);
k++;
}
if (_spring == NULL)
{
_springCount = (GRID_SIZE_X - 1)*(GRID_SIZE_Y - 2)
+ (GRID_SIZE_Y - 1)*(GRID_SIZE_X - 2);
_spring = (SPRING *) malloc(sizeof(SPRING)*_springCount);
if (_spring == NULL)
{
fprintf(stderr, "rubber: Can't allocate memory.\n");
exit(-1);
}
}
k = 0;
for (i = 1; i < GRID_SIZE_X - 1; i++)
for (j = 0; j < GRID_SIZE_Y - 1; j++)
{
m = GRID_SIZE_Y*i + j;
_spring[k].i = m;
_spring[k].j = m + 1;
_spring[k].r = (height - 1.0)/(GRID_SIZE_Y - 1.0);
k++;
}
for (j = 1; j < GRID_SIZE_Y - 1; j++)
{
for (i = 0; i < GRID_SIZE_X - 1; i++)
{
m = GRID_SIZE_Y*i + j;
_spring[k].i = m;
_spring[k].j = m + GRID_SIZE_X;
_spring[k].r = (width - 1.0)/(GRID_SIZE_X - 1.0);
k++;
}
}
for ( i = 0; i < GRID_SIZE_Y - 1; i++ )
{
for ( j = 0; j < GRID_SIZE_X - 1; j++ )
{
k = i * GRID_SIZE_X + j;
_indices[k * 6 + 0] = ( i ) * GRID_SIZE_X + j;
_indices[k * 6 + 1] = ( i + 1 ) * GRID_SIZE_X + j;
_indices[k * 6 + 2] = ( i + 1 ) * GRID_SIZE_X + j + 1;
_indices[k * 6 + 3] = ( i ) * GRID_SIZE_X + j;
_indices[k * 6 + 4] = ( i + 1 ) * GRID_SIZE_X + j + 1;
_indices[k * 6 + 5] = ( i ) * GRID_SIZE_X + j + 1;
}
}
}