Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2012, 10:35 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 8
WhiteWidget is on a distinguished road
Default (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;
    }
    }
    }
And I changed my draw method to:

Code:
    -(void) draw
    {
    [super draw];

    NSAssert(!usesBatchNode_, @"If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Unneeded states: -

    BOOL newBlend = blendFunc_.src != CC_BLEND_SRC || blendFunc_.dst != CC_BLEND_DST;
    if( newBlend )
    glBlendFunc( blendFunc_.src, blendFunc_.dst );

    #define kQuadSize sizeof(quad_.bl)
    glBindTexture(GL_TEXTURE_2D, [texture_ name]);

    long offset = (long)&quad_;

    int k = 0;
    int i, j;
    if(_mass == NULL) {
    NSLog(@"mass is null");
    return;
    }

    for (i = 0; i < GRID_SIZE_X - 1; i++)
    {
    for (j = 0; j < GRID_SIZE_Y - 1; j++)
    {
    GLfloat vertices[]= {
    _mass[k].x[0],_mass[k].x[1],_mass[k].x[2],
    _mass[k + 1].x[0],_mass[k + 1].x[1],_mass[k + 1].x[2],
    _mass[k + GRID_SIZE_Y + 1].x[0],_mass[k + GRID_SIZE_Y + 1].x[1],_mass[k + GRID_SIZE_Y + 1].x[2],
    _mass[k + GRID_SIZE_Y].x[0],_mass[k + GRID_SIZE_Y].x[1],_mass[k + GRID_SIZE_Y].x[2]
    };
    GLfloat tex[]={
    _mass[k].t[0], _mass[k].t[1],
    _mass[k + 1].t[0], _mass[k + 1].t[1],
    _mass[k + GRID_SIZE_Y + 1].t[0], _mass[k + GRID_SIZE_Y + 1].t[1],
    _mass[k + GRID_SIZE_Y].t[0], _mass[k + GRID_SIZE_Y].t[1]
    };

    glVertexPointer(3, GL_FLOAT, 0, vertices);

    glTexCoordPointer(2, GL_FLOAT, 0, tex);

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    k++;
    }
    k++;
    }

    // vertex
    NSInteger diff = offsetof( ccV3F_C4B_T2F, vertices);
    //glVertexPointer(3, GL_FLOAT, kQuadSize, (void*) (offset + diff) );

    // color
    diff = offsetof( ccV3F_C4B_T2F, colors);
    glColorPointer(4, GL_UNSIGNED_BYTE, kQuadSize, (void*)(offset + diff));

    // tex coords
    diff = offsetof( ccV3F_C4B_T2F, texCoords);
    //glTexCoordPointer(2, GL_FLOAT, kQuadSize, (void*)(offset + diff));

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    if( newBlend )
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);

    #if CC_SPRITE_DEBUG_DRAW == 1
    // draw bounding box
    CGPoint vertices[4]={
    ccp(quad_.tl.vertices.x,quad_.tl.vertices.y),
    ccp(quad_.bl.vertices.x,quad_.bl.vertices.y),
    ccp(quad_.br.vertices.x,quad_.br.vertices.y),
    ccp(quad_.tr.vertices.x,quad_.tr.vertices.y),
    };
    ccDrawPoly(vertices, 4, YES);
    #elif CC_SPRITE_DEBUG_DRAW == 2
    // draw texture box
    CGSize s = self.textureRect.size;
    CGPoint offsetPix = self.offsetPositionInPixels;
    CGPoint vertices[4] = {
    ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y),
    ccp(offsetPix.x+s.width,offsetPix.y+s.height), ccp(offsetPix.x,offsetPix.y+s.height)
    };
    ccDrawPoly(vertices, 4, YES);
    #endif // CC_SPRITE_DEBUG_DRAW

    }
WhiteWidget is offline   Reply With Quote
Reply

Bookmarks

Tags
blend, cocos2d, opengl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 378
8 members and 370 guests
apatsufas, comicool, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0