Wednesday, October 24, 2012

Drawing smooth lines in Cocos2d 2.x

So when I originally coded Tracer I used cocos2d 1.0 which used openGL 1.1 and there was this great method I found online which you can find here on the cocos2d forms http://www.cocos2d-iphone.org/forum/topic/21368 However when I upgraded to cocos2d 2.0 I could no longer use this because cocos2d 2.x uses OpenGL2.0. I couldn't find a solution for drawing smooth lines online that was exactly what I needed so I just looked at the cocos2d source code and modified the original method to make it work. Hopefully this will save you some time if you need the same.
-(void) drawSmoothLine: (CGPoint) pos1: (CGPoint) pos2: (ccColor4F) someColor
{
    [shaderProgram_ use];
    [shaderProgram_ setUniformsForBuiltins];
 [shaderProgram_ setUniformLocation:-1 with4fv:(GLfloat*) &someColor.r count:1];
    
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );
    GLfloat lineVertices[12];
    CGPoint dir, tan;
    
    float width = 1.2f;
    dir.x = pos2.x - pos1.x;
    dir.y = pos2.y - pos1.y;
    float len = sqrtf(dir.x*dir.x+dir.y*dir.y);
    if(len<0.00001)
        return;
    dir.x = dir.x/len;
    dir.y = dir.y/len;
    tan.x = -width*dir.y;
    tan.y = width*dir.x;
    
    lineVertices[0] = pos1.x + tan.x;
    lineVertices[1] = pos1.y + tan.y;
    lineVertices[2] = pos2.x + tan.x;
    lineVertices[3] = pos2.y + tan.y;
    lineVertices[4] = pos1.x;
    lineVertices[5] = pos1.y;
    lineVertices[6] = pos2.x;
    lineVertices[7] = pos2.y;
    lineVertices[8] = pos1.x - tan.x;
    lineVertices[9] = pos1.y - tan.y;
    lineVertices[10] = pos2.x - tan.x;
    lineVertices[11] = pos2.y - tan.y;
    
    ccColor4F vertices[6];
    ccColor4F color1 = {rCol, gCol, bCol, 0};
    ccColor4F color2 = {rCol, gCol, bCol, opacity};
    vertices[0] = color1;
    vertices[1] = color1;
    vertices[2] = color2;
    vertices[3] = color2;
    vertices[4] = color1;
    vertices[5] = color1;
    
 glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, lineVertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, vertices);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
    
}
Now you may notice that when you draw lines there are tiny dots that appear in the line, well that's because of the way the triangles are drawn and I'll get into that in the next post and how to get rid of it! Also, if you use the above, make sure to go into ccConfig.h and enable CC_ENABLE_GL_STATE_CACHE, I got a huge performance improvement from that. In the above method, the values rCol, gCol and bCol are just the current rgb values which you can manually modify or get from the passed in ccColor4f value.

Pavan
Dutch Cheese Games

11 comments:

  1. Thank you for sharing. But I don't know where in my project to put this code...

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hey Ntdhat,

    Well you can put this function anywhere in your code and then call it in your draw function. Let's say you have some array of CGPoints called points, then you would do something like

    -(void) draw {
    ccColor4f color = {1.0, 0.0, 0.0, 1.0}; //red line
    for (int i = 0; i < points.size; i++) {
    [self drawSmoothLine: ponts[i] : points[i+1] : color];
    }
    }

    ReplyDelete
  4. Hey, I have a friend that is trying to develop an app and will like to use your services, do you have a contact number or email?

    ReplyDelete
  5. Trekking in Nepal|Asian country|Asian nation is delighted to welcome to you small however wonderful country in Nepal. After you can have unbounded adventures, there are attraction sharing and unforgettable in your life. Asian country trekking company, found by the team of ardent and therefore the trekking specialists is dedicated to impart you a fondest memory throughout your Nepal holiday with us. We have been serving not here in Nepal solely, additionally in Tibet, India and Kingdom of Bhutan additionally. No matter, what variety of trip you decide on in Nepal. Either Nepal trekking , rafting and cannoning, Nepal Tour , mount, Nepal Peak Climbing , jungle safaris and Nepal trekking activities. Nepal traveling with us is quite simply a quick to remain somewhere; it's a true reference to the individuals, the distinctive rural area, distinctive culture, the Nepali cookery, the setting and therefore the flora and fauna of the country. Nepal tea house trekking giving for tiny trekking teams and massive trekking teams. We tend to are giving you, as your alternative trekking service additionally. You'll resolve our trekking guide are going to be the nice and cozy welcome and government trended license to guide the Nepal historical place And Nepal mountain range ranges .
    It's some common trekking trails at below.
    Trekking in Nepal
    Nepal Trekking
    Everest Trekking
    Everest Base Camp Trekking
    Annapurna Trekking
    Langtang Valley Trekking
    Mustang Trekking
    Manaslu Trekking
    Nepal Hiking
    Nepal Tour
    Nepal Helicopter tour
    Boundless Adventure Pvt. Ltd.
    GPO Box: 11670 Thamel, Kathmandu, Nepal
    Phone No.: +977 1 4701885
    Fax No.: +977 1 4701884
    Cell No.: +977 9851033819 (Dambar)
    Email: info@boundlessadventure.com
    Email: boundlesstrek@gmail.com

    ReplyDelete
  6. This is great things to know some things by this sites Himalayan Recreation operating inbound outbound travel deals. here are many trekking agency & among of all this is one of best one Himalayan Recreation is also one of believable travel news portal and trekking agency from Nepal, we are leading our adventures activities to Everest base camp trek, Annapurna base camp trek, Ghorepani Poon hill trek, Langtang Gosainkunda Trek, & others many.. Nepal tour which has been fashioned to every guest’s need and want. Apart from that we have come up with wonderful itineraries focusing for your experience with the local culture and natural beauty. However, the pre-made itineraries are subjected to alter so as to fit all of our guests’ anticipations. Himalayan Recreation is an officially recognized and licensed company by the Ministry of Culture, Tourism and Civil Aviation of the Government of Nepal.

    for more info:
    email: info@aboutnepaltreks.com
    websites: www.aboutnepaltreks.com
    Contact number: 00977-014388009
    Thamel, Kathmandu Nepal.

    ReplyDelete
  7. Guide Nepal Trek is a local, natively run, Nepali company. Although we're quite a newly established travel company, our staff and guides have plenty of experience - having climbed and trekked these beautiful mountains countless times before. We've been showing off the culture and natural beauty of our country for decades. All our tours are well managed - our cheerful staff, guides and porters are ready to make your next holiday the trekking adventure of a lifetime.

    On our treks and tours, we take supreme care of our clients; we're at the ready with excellent service and local knowledge for you to enjoy. Our staff and guides (including porters, the backbone of any Nepali trekking adventure) are well taken care of, with insurance, medical care and extra incentive after the trips.foe more information http://www.guidenepaltrek.com/

    ReplyDelete
  8. http://trentgrimsey.blogspot.com/2012/06/june-2012-oceania-championships-new.html?showComment=1375953912861#c7560737451690933879

    ReplyDelete
  9. http://trentgrimsey.blogspot.com/2012/06/june-2012-oceania-championships-new.html?showComment=1375953912861#c7560737451690933879

    ReplyDelete
  10. Annapurna Base Camp Trek start pokhara to ghorepani poon hill and continue to ABC Trekking, this trip can complete Annapurna Base Camp Trek 7 days as well and Also if have enough time can make Annapurna Base Camp Trek 10 days. Annapurna Base Camp Trek route is very beautiful and Perfect Himalayan scenery. the only make Annapurna Base Camp trek difficulty. June to August and JanuaryFebruary.Annapurna Base Camp Trek distance is around 200 miles. there are many Travel has been read Annapurna Base Camp Trek blog. September to November is one of the Best Season makes the trek to Annapurna Base Camp.

    ReplyDelete