openfl icon indicating copy to clipboard operation
openfl copied to clipboard

Using lineTo/moveTo breaks other shape.

Open grapefrukt opened this issue 11 years ago • 6 comments

screenshot-2015-05-05-10 37 03

Using the lineTo() and moveTo() functions in the drawing api on next makes any circles previously drawn disappear.

package;

import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.events.Event;

class Main extends Sprite {

    var shape:Shape;

    public function new() {
        super();

        shape = new Shape();
        shape.x = stage.stageWidth / 2;
        shape.y = stage.stageHeight / 2;
        addChild(shape);

        // this call works as intended, calling repeatedly here is still fine
        render();

        // calling the render function on ENTER_FRAME will make this bug appear, comment this out and everything is fine
        addEventListener(Event.ENTER_FRAME, render);
    }

    function render(e:Event = null) {
        shape.graphics.clear();

        // draw a pink circle
        shape.graphics.beginFill(0xff00ff);
        shape.graphics.drawCircle(0, 0, 150);
        // endFill() or not, does not seem to make a difference

        // this will make the circle disappear WHEN called on ENTER_FRAME
        // draw a yellow rectangle using moveTo/lineTo
        shape.graphics.beginFill(0xffff00);
        shape.graphics.moveTo(0, 0);
        shape.graphics.lineTo(100, 0);
        shape.graphics.lineTo(100, 100);
        shape.graphics.lineTo(0, 100);

    }
}

I have confirmed this on Windows (Lime 2.3.3, OpenFL 3.0.3) and I'm pretty sure it happens on mobile and other desktop platforms too. Flash is fine, as is HTML5.

grapefrukt avatar May 05 '15 08:05 grapefrukt

Drawing the circle after the rectangle does not make any difference.

grapefrukt avatar May 05 '15 08:05 grapefrukt

Tested it on Android (Kit Kat 4.4.2 on Motorola RazrD3 and LG GPad V500) and doesn't work, as you said.

Also tested on OpenFL 3.0.1/3.0.2, with the same results. It only works properly if compiled on legacy mode (-Dv2 -Dlegacy).

Something similar (I guess?) happened to me while I was building a class do animate my logo. I used drawRect to create a bounding box and used moveTo and lineTo to draw some triangles, which made the rectangle look like this (darker triangles are the rectangle from drawRect, light gray is the triangle from the animation): rectpolytris

yuigoto avatar May 06 '15 01:05 yuigoto

screenshot-2015-05-06-11 15 31

I had seen that issue as well, but I didn't find a way to reliably reproduce it until just now. Interestingly both the bugs occur regardless of the order the drawing is done in, even if I draw the pink rectangle after the yellow lineTo/moveTo shape, it will still break. As will the circle.

package;

import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.events.Event;

class Main extends Sprite {

    var shape:Shape;

    public function new() {
        super();

        shape = new Shape();
        shape.x = stage.stageWidth / 2 - 100;
        shape.y = stage.stageHeight / 2 - 50;
        addChild(shape);

        // this call works as intended, calling repeatedly here is still fine
        render();

        // calling the render function on ENTER_FRAME will make this bug appear, comment this out and everything is fine
        addEventListener(Event.ENTER_FRAME, render);
    }

    function render(e:Event = null) {
        shape.graphics.clear();

        // draw a pink rectangle
        shape.graphics.beginFill(0xff00ff);
        shape.graphics.drawRect(100, 0, 100, 100);

        // this will make the pink rectangle render with its vertices out of order 
        // making it look like an "hourglass" instead of the proper rectangle shape
        shape.graphics.beginFill(0xffff00);
        shape.graphics.moveTo(0, 0);
        shape.graphics.lineTo(100, 0);
        shape.graphics.lineTo(100, 100); // this call is optional, the bug will still happen without it
    }
}

grapefrukt avatar May 06 '15 09:05 grapefrukt

This isn't explicitly connected to ENTER_FRAME, running the exact same code using a regular timer will show the same behavior. The first "render" is fine, anything after that, is not.

grapefrukt avatar May 06 '15 09:05 grapefrukt

I have managed to narrow down the problem to the GraphicsRenderer class. I'm not 100% sure if my understanding of this is correct, however. (I'm using OpenFL 3.0.6, Lime 2.4.1)

When lineTo() gets called, then buildComplexPoly() is also called in the GraphicsRenderer class. buildComplexPoly() has this snippet (GraphicsRenderer, lines 164-167)

bucket = prepareBucket(path, glStack);
var fill = bucket.getData(Fill);
fill.drawMode = glStack.gl.TRIANGLE_FAN;
fill.verts = points;

For some reason, this affects all of the drawModes of the other draw commands, ie. rectangle and circle. For example, the buildRectangle() should apparently use GL_TRIANGLE_STRIP, but the setting from buildComplexPoly() messes with that.

Quickly inserting this line in buildRectangle() (after line 508, the if branch)

if(fill != null) {
    fill.drawMode = glStack.gl.TRIANGLE_STRIP;
    var verts = fill.verts;
    var indices = fill.indices;

will temporarily make the rectangle properly render.

yay

I have not tested this out for buildCircle() yet, but I would assume a similar problem.

Unfortunately, even if I have (probably) found out the cause, I still have no idea how to fix it, as I might be missing a crucial part of how GraphicsRenderer works. I still don't know why the fill's drawMode is carried over both subsequent and preceding calls.

Please do correct me if I have missed anything.

Anheurystics avatar May 16 '15 01:05 Anheurystics

Oh, sorry guys I totally forgot to push the fix :cry: It should be fixed once this PR is merged https://github.com/openfl/openfl/pull/664

mrcdk avatar May 16 '15 01:05 mrcdk