Skip to content

refactor CCDrawNode: part2 #19972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 58 additions & 113 deletions cocos/2d/CCDrawNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,64 +37,9 @@

NS_CC_BEGIN

// Vec2 == CGPoint in 32-bits, but not in 64-bits (OS X)
// that's why the "v2f" functions are needed
static Vec2 v2fzero(0.0f,0.0f);

static inline Vec2 v2f(float x, float y)
{
return {x, y};
}

static inline Vec2 v2fadd(const Vec2 &v0, const Vec2 &v1)
{
return v2f(v0.x+v1.x, v0.y+v1.y);
}

static inline Vec2 v2fsub(const Vec2 &v0, const Vec2 &v1)
{
return v2f(v0.x-v1.x, v0.y-v1.y);
}

static inline Vec2 v2fmult(const Vec2 &v, float s)
{
return v2f(v.x * s, v.y * s);
}

static inline Vec2 v2fperp(const Vec2 &p0)
{
return v2f(-p0.y, p0.x);
}

static inline Vec2 v2fneg(const Vec2 &p0)
{
return v2f(-p0.x, - p0.y);
}

static inline float v2fdot(const Vec2 &p0, const Vec2 &p1)
{
return p0.x * p1.x + p0.y * p1.y;
}

static inline Vec2 v2fnormalize(const Vec2 &p)
{
Vec2 r(p.x, p.y);
r.normalize();
return v2f(r.x, r.y);
}

static inline Vec2 __v2f(const Vec2 &v)
{
//#ifdef __LP64__
return v2f(v.x, v.y);
// #else
// return * ((Vec2*) &v);
// #endif
}

static inline Tex2F __t(const Vec2 &v)
static inline Tex2F v2ToTex2F(const Vec2 &v)
{
return *(Tex2F*)&v;
return {v.x, v.y};
}

static const float EPSILON=0.0000000001f;
Expand Down Expand Up @@ -177,9 +122,9 @@ V2F_C4B_T2F_Triangle * Triangulate::processTriangles(const Vec2 *verts,V2F_C4B_T
for (int i = 0; i < n-2; i++)
{
V2F_C4B_T2F_Triangle tmp = {
{verts[0], Color4B(fillColor), __t(v2fzero)},
{verts[i+1], Color4B(fillColor), __t(v2fzero)},
{verts[i+2], Color4B(fillColor), __t(v2fzero)},
{verts[0], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
{verts[i+1], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
{verts[i+2], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
};

*triangles++ = tmp;
Expand Down Expand Up @@ -210,9 +155,9 @@ V2F_C4B_T2F_Triangle * Triangulate::processTriangles(const Vec2 *verts,V2F_C4B_T
a = V[u]; b = V[v]; c = V[w];

V2F_C4B_T2F_Triangle tmp = {
{verts[a], Color4B(fillColor), __t(v2fzero)},
{verts[b], Color4B(fillColor), __t(v2fzero)},
{verts[c], Color4B(fillColor), __t(v2fzero)},
{verts[a], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
{verts[b], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
{verts[c], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
};
*triangles++ = tmp;
m++;
Expand Down Expand Up @@ -801,66 +746,66 @@ void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const
unsigned int vertex_count = 6*3;
ensureCapacity(vertex_count);

Vec2 a = __v2f(from);
Vec2 b = __v2f(to);
Vec2 a = from;
Vec2 b = to;


Vec2 n = v2fnormalize(v2fperp(v2fsub(b, a)));
Vec2 t = v2fperp(n);
Vec2 n = ((b - a).getPerp()).getNormalized();
Vec2 t = n.getPerp();

Vec2 nw = v2fmult(n, radius);
Vec2 tw = v2fmult(t, radius);
Vec2 v0 = v2fsub(b, v2fadd(nw, tw));
Vec2 v1 = v2fadd(b, v2fsub(nw, tw));
Vec2 v2 = v2fsub(b, nw);
Vec2 v3 = v2fadd(b, nw);
Vec2 v4 = v2fsub(a, nw);
Vec2 v5 = v2fadd(a, nw);
Vec2 v6 = v2fsub(a, v2fsub(nw, tw));
Vec2 v7 = v2fadd(a, v2fadd(nw, tw));
Vec2 nw = n * radius;
Vec2 tw = t * radius;
Vec2 v0 = b - (nw + tw);
Vec2 v1 = b + (nw - tw);
Vec2 v2 = b - nw;
Vec2 v3 = b + nw;
Vec2 v4 = a - nw;
Vec2 v5 = a + nw;
Vec2 v6 = a - (nw - tw);
Vec2 v7 = a + (nw + tw);


V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);

V2F_C4B_T2F_Triangle triangles0 = {
{v0, Color4B(color), __t(v2fneg(v2fadd(n, t)))},
{v1, Color4B(color), __t(v2fsub(n, t))},
{v2, Color4B(color), __t(v2fneg(n))},
{v0, Color4B(color), v2ToTex2F(-(n + t))},
{v1, Color4B(color), v2ToTex2F(n - t)},
{v2, Color4B(color), v2ToTex2F(-n)},
};
triangles[0] = triangles0;

V2F_C4B_T2F_Triangle triangles1 = {
{v3, Color4B(color), __t(n)},
{v1, Color4B(color), __t(v2fsub(n, t))},
{v2, Color4B(color), __t(v2fneg(n))},
{v3, Color4B(color), v2ToTex2F(n)},
{v1, Color4B(color), v2ToTex2F(n - t)},
{v2, Color4B(color), v2ToTex2F(-n)},
};
triangles[1] = triangles1;

V2F_C4B_T2F_Triangle triangles2 = {
{v3, Color4B(color), __t(n)},
{v4, Color4B(color), __t(v2fneg(n))},
{v2, Color4B(color), __t(v2fneg(n))},
{v3, Color4B(color), v2ToTex2F(n)},
{v4, Color4B(color), v2ToTex2F(-n)},
{v2, Color4B(color), v2ToTex2F(-n)},
};
triangles[2] = triangles2;

V2F_C4B_T2F_Triangle triangles3 = {
{v3, Color4B(color), __t(n)},
{v4, Color4B(color), __t(v2fneg(n))},
{v5, Color4B(color), __t(n) },
{v3, Color4B(color), v2ToTex2F(n)},
{v4, Color4B(color), v2ToTex2F(-n)},
{v5, Color4B(color), v2ToTex2F(n) },
};
triangles[3] = triangles3;

V2F_C4B_T2F_Triangle triangles4 = {
{v6, Color4B(color), __t(v2fsub(t, n))},
{v4, Color4B(color), __t(v2fneg(n)) },
{v5, Color4B(color), __t(n)},
{v6, Color4B(color), v2ToTex2F(t - n)},
{v4, Color4B(color), v2ToTex2F(-n) },
{v5, Color4B(color), v2ToTex2F(n)},
};
triangles[4] = triangles4;

V2F_C4B_T2F_Triangle triangles5 = {
{v6, Color4B(color), __t(v2fsub(t, n))},
{v7, Color4B(color), __t(v2fadd(n, t))},
{v5, Color4B(color), __t(n)},
{v6, Color4B(color), v2ToTex2F(t - n)},
{v7, Color4B(color), v2ToTex2F(t + n)},
{v5, Color4B(color), v2ToTex2F(n)},
};
triangles[5] = triangles5;

Expand Down Expand Up @@ -891,44 +836,44 @@ void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColo

for (int i = 0; i < count; i++)
{
Vec2 v0 = __v2f(verts[(i-1+count)%count]);
Vec2 v1 = __v2f(verts[i]);
Vec2 v2 = __v2f(verts[(i+1)%count]);
Vec2 v0 = verts[(i-1+count)%count];
Vec2 v1 = verts[i];
Vec2 v2 = verts[(i+1)%count];

Vec2 n1 = v2fnormalize(v2fperp(v2fsub(v1, v0)));
Vec2 n2 = v2fnormalize(v2fperp(v2fsub(v2, v1)));
Vec2 n1 = ((v1 - v0).getPerp()).getNormalized();
Vec2 n2 = ((v2 - v1).getPerp()).getNormalized();

Vec2 offset = v2fmult(v2fadd(n1, n2), 1.0f / (v2fdot(n1, n2) + 1.0f));
Vec2 offset = (n1 + n2) * (1.0f / (Vec2::dot(n1, n2) + 1.0f));
extrude[i] = {offset, n2};
}

for(int i = 0; i < count; i++)
{
int j = (i+1)%count;
Vec2 v0 = __v2f(verts[i]);
Vec2 v1 = __v2f(verts[j]);
Vec2 v0 = verts[i];
Vec2 v1 = verts[j];

Vec2 n0 = extrude[i].n;

Vec2 offset0 = extrude[i].offset;
Vec2 offset1 = extrude[j].offset;

Vec2 inner0 = v2fsub(v0, v2fmult(offset0, borderWidth));
Vec2 inner1 = v2fsub(v1, v2fmult(offset1, borderWidth));
Vec2 outer0 = v2fadd(v0, v2fmult(offset0, borderWidth));
Vec2 outer1 = v2fadd(v1, v2fmult(offset1, borderWidth));
Vec2 inner0 = v0 - offset0 * borderWidth;
Vec2 inner1 = v1 - offset1 * borderWidth;
Vec2 outer0 = v0 + offset0 * borderWidth;
Vec2 outer1 = v1 + offset1 * borderWidth;

V2F_C4B_T2F_Triangle tmp1 = {
{inner0, Color4B(borderColor), __t(v2fneg(n0))},
{inner1, Color4B(borderColor), __t(v2fneg(n0))},
{outer1, Color4B(borderColor), __t(n0)}
{inner0, Color4B(borderColor), v2ToTex2F(-n0)},
{inner1, Color4B(borderColor), v2ToTex2F(-n0)},
{outer1, Color4B(borderColor), v2ToTex2F(n0)}
};
*cursor++ = tmp1;

V2F_C4B_T2F_Triangle tmp2 = {
{inner0, Color4B(borderColor), __t(v2fneg(n0))},
{outer0, Color4B(borderColor), __t(n0)},
{outer1, Color4B(borderColor), __t(n0)}
{inner0, Color4B(borderColor), v2ToTex2F(-n0)},
{outer0, Color4B(borderColor), v2ToTex2F(n0)},
{outer1, Color4B(borderColor), v2ToTex2F(n0)}
};
*cursor++ = tmp2;
}
Expand Down