axmolengine / axmol

Axmol Engine – A Multi-platform Engine for Desktop, XBOX (UWP) and Mobile games. (A fork of Cocos2d-x-4.0)
https://axmol.dev
MIT License
868 stars 195 forks source link

Draw Polygon wrong #1319

Closed NgVThangBz closed 10 months ago

NgVThangBz commented 1 year ago

Currently DrawNode->drawPolygon is using the wrong triangulation method (from cocos2dx), which leads to complex and concave polylines being drawn incorrectly.

why not replace the more accurate triangulation with triangulation algorithms (poly2tri...??)

cocos2dx v3 now has a new update to draw polygon more accurately (not released yet), i haven't tried it but maybe it will work, or use poly2tri to be able to triangulate correctly than

rh101 commented 1 year ago

Currently DrawNode->drawPolygon is using the wrong triangulation method (from cocos2dx), which leads to complex and concave polylines being drawn incorrectly.

Do you have an example, and by that I mean screenshots of incorrect and correct rendering?

cocos2dx v3 now has a new update to draw polygon more accurately (not released yet), i haven't tried it but maybe it will work, or use poly2tri to be able to triangulate correctly than

There are no new versions of Cocos2d-x v3, and unlikely for any others to be released given that the Cocos team haven't worked on it for over 2 years. So, what exactly do you mean by that statement?

NgVThangBz commented 1 year ago
NgVThangBz commented 1 year ago

I just tested cocos2dx v3, and they work wrong, don't worry about them, sorry

rh101 commented 1 year ago

I just tested cocos2dx v3, and they work wrong, don't worry about them, sorry

Sorry, that's what I actually meant. There were no new PRs or updates listed against cocos2d-x v3 with polygon related code, so I wasn't sure what you were referring to.

Anyhow, I'm pretty sure poly2tri was updated a while back by @aismann, so perhaps he may be able to give his input regarding this.

aismann commented 1 year ago
  • You can see the image below: the blue color uses poly2tri for triangulation, and the red color uses the default version. image_2023-09-08_13-36-42
  • I'm not talking about released versions, I'm talking about DrawNode code on the contributed v3 branch,

@NgVThangBz Your vertices list from this example plz. And yes its known (see cpp-tests DrawNode tests) => cocos2dx 4.0 issue (implementation)

NgVThangBz commented 1 year ago

[0] = {x=290.250000 y=98.1250000 } [1] = {x=235.000000 y=90.8750000 } [2] = {x=270.500000 y=109.875000 } [3] = {x=235.000000 y=119.125000 } [4] = {x=275.250000 y=145.875000 } [5] = {x=249.500000 y=145.875000 } [6] = {x=249.500000 y=178.125000 } [7] = {x=275.250000 y=187.375015 } [8] = {x=294.750488 y=168.333344 } [9] = {x=311.250000 y=181.125000 } [10] = {x=257.000000 y=213.625015 } [11] = {x=338.500000 y=193.125000 } [12] = {x=300.000000 y=211.125015 } [13] = {x=333.750000 y=211.125015 } [14] = {x=368.250000 y=206.625000 } [15] = {x=377.000000 y=178.125000 } [16] = {x=421.750000 y=170.125000 } [17] = {x=416.250000 y=115.375000 } [18] = {x=391.250000 y=157.875000 } [19] = {x=338.500000 y=131.625000 } [20] = {x=362.750000 y=131.625000 } [21] = {x=362.750000 y=106.875000 } [22] = {x=306.500000 y=119.125000 } [23] = {x=324.250000 y=85.1250000 } [24] = {x=227.500000 y=61.8750000 } image_2023-09-11_08-19-32 im create new polygon, please check

aismann commented 1 year ago

@NgVThangBz Thats issue comes with Cocos2dx 4.0.

NgVThangBz commented 1 year ago

yes it happens with both 3.17 and 4.0 and it leads to axmol also crashing, so let's find a solution, currently I'm using poly2tri to solve the problem.

aismann commented 1 year ago

It happens only with filled polygon: see here: DrawNode::drawPoly: SolidPoly/Polygon cannot render concave structure correct. #829 The changed draw order is not a solution for complex concave polygons.

Thats the reason for wrong filled polygons (using GL_TRIANGLE_FAN): (screenshot from here:) image

Here a possible solution (pseudo code):

if isConvex
    use the Cocos2dx version
else
   create triangles and fill each

Another idea is to use the class AutoPolygon/PolygonInfo class to create the triangles

The problem is on each solution is it filled only (no border line) => workaround: drawPoly for the border)

aismann commented 1 year ago

@halx99 you can also add tags like on #829

aismann commented 1 year ago

yes it happens with both 3.17 and 4.0 and it leads to axmol also crashing, so let's find a solution, currently I'm using poly2tri to solve the problem.

@NgVThangBz Do you have a PR?

NgVThangBz commented 1 year ago

I simply used poly2tri to create the triangles, and colored them @aismann

iAndyHD3 commented 1 year ago

do we have an axmol test where this issue can be seen? if not please add

aismann commented 1 year ago

I simply used poly2tri to create the triangles, and colored them

@NgVThangBz You used an app like paint, right? Polztri makes only the triangles like autopolygon or get you a filled Polygon with polyti directly

aismann commented 1 year ago

do we have an axmol test where this issue can be seen? if not please add

we have: image

and an info too: image

NgVThangBz commented 1 year ago

yes, instead of using , I use poly2tri and triangulate them, what's left is just to color each triangle, This solves my problem, however I still think a modification of the drawPolygon method is needed image_2023-09-13_08-07-31

aismann commented 1 year ago

@NgVThangBz What is globalToDrawing(pt)doing (code snippet)?

NgVThangBz commented 1 year ago

@aismann it's just for frame conversion, you can ignore it

aismann commented 1 year ago

Ok I have a solution: Coming PR needs some more tests datas, @NgVThangBz maybe you have some more? Format:

Vec2 vertices[] = { 
   {290.250000, 98.1250000 },
    {235.000000, 90.8750000 },
...
    {324.250000, 85.1250000 },
    {227.500000, 61.8750000 } };

image

aismann commented 1 year ago

@halx99 you can remove the "help wanted".

NgVThangBz commented 1 year ago

I have created 2 more data (files below), in the file there are 2 Vec2 lists: -data1 is manually generated,

image_2023-09-15_08-36-15

-data2 is taken from a in the svg file I read, (very complicated),I think it can be later photo_2023-09-15_08-40-03

data here: polyLineData.txt

aismann commented 1 year ago

Looks good (but there are some issues which i have to fix/changes before I can make the PR): image

aismann commented 1 year ago

I think I need some more time (not all is working at this moment)! @halx99 please add 'long story', 'enhancement' tag However I will add some more 'new' drawing methodes too: eg: drawPolyline with thickness image

aismann commented 1 year ago

Here a preview of the drawPolyline with thickness image

aismann commented 11 months ago

I opening an issue on poly2tri https://github.com/jhasse/poly2tri/issues/54

image

@NgVThangBz Maybe you want check your data also for correctness?

Thats the poly2tri result (axmol has the same of course): image

NgVThangBz commented 11 months ago

@aismann maye my data wrong, you can check again with data: polyLineData.txt but i think axmol like poly2tri is fine

aismann commented 11 months ago

@NgVThangBz I got an exeption with your new data on p2t.exe (poly2tri testbed) Please check it with poly2tri first. image

aismann commented 11 months ago

@halx99 Short info: I will have no time to work on it this year.

aismann commented 10 months ago

Small update (seems it works now correct) image

aismann commented 10 months ago

@NgVThangBz can be closed now.

NgVThangBz commented 10 months ago

@aismann yes, thank you, it good

aismann commented 10 months ago

@aismann yes, thank you, it good

@NgVThangBz You are the author, you can close it (im not) ;)