I am drawing lines with cocos2dx primitives. My code looks like this:
OnTouchMoved:
Touch *touch = touches[0];
Point start = touch->getLocationInView();
start = Director::getInstance()->convertToGL(start);
Point end = touch->getPreviousLocationInView();
end = Director::getInstance()->convertToGL(end);
targetTexture->begin();
if (end.getDistance(start) > 2)
{
drawCurPoint(start, end);
}
drawCurPoint looks like this:
Point dir = end - start;
dir = dir.getPerp();
Point perpendicular = dir.getNormalized();
Point A = start + (perpendicular * (DOT_RADIUS / 2));
Point B = start - (perpendicular * (DOT_RADIUS / 2));
Point C = end + (perpendicular * (DOT_RADIUS / 2));
Point D = end - (perpendicular * (DOT_RADIUS / 2));
Point poly[4] = {A, C, D, B};
dot->drawSolidPoly(poly, 4, Color4F::BLACK);
dot->drawDot(end, DOT_RADIUS / 2.0, Color4F::BLACK);
And it works but the lines are horribly aliased… now I know there exist this topic:
But Chano’s solution does not work for me Actually there are some strange things happening… for example the canvas size is smaller 4 times! And the lines don’t appear at all
Does somebody know how to add anti-aliasing to the lines I’m drawing? I’m using cocos2dx v. 3.4 (if it matters)
I too would very much like an answer to this.
Using Cocos2d-x v2.x I could get nice anti-aliased DrawNode rendering on all platforms by using smoothstep in the shader.
Now using v3/v4 I can’t seem to get it working (so far.)
Thanks in advance!
For getting smoothlines in DrawNode, open ccShader_PositionColorLengthTexture.frag and comment out lines 36-38 (basically use smoothstep when available).
@jarsj
The same problem as above… when I comment out the lines then its not drawing any lines. Well actually it does… but you cant see them… The program responds to the drawings correctly but you cant see the lines at all… also it destroys my ScrollView
EDIT:
I did some changes to the “brush” colour and actually you can see the lines on black background but the aliasing is still killing my eyes (somehow it’s even worse)
@jarsj
Your solution does not work for me However now with the line 25 uncommented I can finally see the line I am drawing but its still aliased Whats your code for drawing the lines? Can you show me the code itself?
Mine looks like this:
In the init() method:
dot = DrawNode::create();
this->addChild(dot);
later:
Point dir = end - start;
dir = dir.getPerp();
Point perpendicular = dir.getNormalized();
Point A = start + (perpendicular * (DOT_RADIUS / 2));
Point B = start - (perpendicular * (DOT_RADIUS / 2));
Point C = end + (perpendicular * (DOT_RADIUS / 2));
Point D = end - (perpendicular * (DOT_RADIUS / 2));
Point poly[4] = {A, C, D, B};
dot->drawDot(end, DOT_RADIUS / 2.0, color);
dot->drawSolidPoly(poly, 4, color);
DOT_RADIUS is set to 14.
#define DOT_RADIUS 14.0f
Am I missing something? Maybe something with the render texture? Or dont know… I cant get that anti aliasing to work
The sad part about this solution is that this blurs ONLY the drawDot part:
dot->drawDot(end, DOT_RADIUS / 2.0, color);
The solidPoly is not being blurred:
dot->drawSolidPoly(poly, 4, color);
Why is it so? Or how can I replace the SolidPoly part so I get a line without holes in it? Because when I comment out the SolidPoly part I will draw only Dots and when I move my finger fast I get holes in the line I draw. I’m so close yet so far! HELP!