MODULE: DrawAntialiased v1.1

Started by Kweepa, Tue 12/01/2010 08:43:30

Previous topic - Next topic

Kweepa



Allows you to draw antialiased lines and circles onto a drawing surface, like so:
Code: ags

DrawingSurface *surf = Room.GetDrawingSurfaceForBackground();
surf.DrawAntialiasedLine(x1, y1, x2, y2, transparency);
surf.DrawAntialiasedCircle(x, y, radius, transparency);
surf.DrawAntialiasedFilledCircle(x, y, radius, transparency);
surf.Release();


Demo in here:
http://www.kweepa.org/step/ags/tech/RopeTest.zip (900k)

Download here:
http://www.kweepa.org/step/ags/tech/DrawAntialiased.zip (2k)

For AGS 3.x.
Note: requires a 16 or 32 bit game and a 16 or 32 bit surface (for example, when drawing to the background, you must have imported a 16 or 32 bit image as the background). If you don't do this, the lines and circles will look blocky and the game will run slowly.

Enjoy!
Steve

Version History:
v1.1 Name change; added circles; extended DrawingSurface
v1.0 Lines only
Still waiting for Purity of the Surf II

SSH

Pretty cool, but why does Roger have a rope coming out of THERE exactly?  :=
12

Layabout

Wow, the rope test is really really impressive. I saw the rope 'demo' in the other thread, but combined with AA, it's superb.

Just a quick question, would it be possible to use the rope in the example to be affected by 'systems' such as wind. It would be cool to have it simulate power lines in a room without having to worry about animating something so tedious. (I know it's a bit off topic, but hey!)
I am Jean-Pierre.

ThreeOhFour

Holy crap Steve!

That's the most bestest thing I ever saw  :o :D

GarageGothic

Very nice work. I'll have to take a closer look at the rope physics script later today - I was wanting to integrate more physics puzzles in my next game but never thought of using rope. I'm guessing I'll have to look into how to model gravity on objects hanging from the end of the rope too.

monkey0506

Very nice demo. My question isn't about that though...

Why take a DrawingSurface as a parameter instead of using an extender method like:

Code: ags
import void DrawAntiAliasedLine(this DrawingSurface*, int x1, int y1, int x2, int y2, int transparency);

DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
surface.DrawAntialiasedLine(x1, y1, x2, y2, transparency);
surface.Release();


No technical difference except the fact that it provides automatic parameter validation of the DrawingSurface and prevents the possibility of null pointer references. :=

From an aesthetic viewpoint it would group the function together with the rest of the DrawingSurface functions and make it more streamlined with the built-in code. You already said it's for AGS 3.0+.

But in any case, very nice work, as usual. :=

Kweepa

#6
Extender what now? :=
Sounds like a good idea. I'll do that.

RE: the rope comments above.
Easy to apply wind. Just like gravity.
Harder to add weights and make it look good. Add stronger force to the end of the rope. Need to shorten the rope to make it less stretchy, and maybe add another (invisible) direct link from the start of the rope to the end to reduce stretching some more. Or do more constraint iterations (but then it gets slow). Perhaps a different physics approach would be better.
Still waiting for Purity of the Surf II

Kweepa

Bump for new version:
:= Name change
:= Added circles and filled circles
:= Changed to extend DrawingSurface, as suggested by the monkey
Still waiting for Purity of the Surf II

abstauber

 :o
Amazing as always!
Great job!

Danman

Great I have been wanting for something like this to play with. Good work.



monkey0506

Hey Steve, I was thinking of using this module for something, but I need the lines to be thicker than 1 pixel. Any possibility you could add an optional thickness parameter like the built-in DrawingSurface.DrawLine function has? :D Thanks!!

Kweepa

#11
Hmm, not so easy.
As a workaround you could draw the line multiple times with 1px offsets.
Code: ags

  float dy = (x2 - x1);
  float dx = (y2 - y1);
  float len = Maths.Sqrt(dx*dx + dy*dy);
  
  if (len > 0.0001)
  {
    dy = dy/len;
    dx = dx/len; // EDIT - FORGOT THIS LINE
    float offset = -0.5*IntToFloat(thickness-1);
    int i = 0;
    while (i < thickness)
    {
      surf.DrawAntialiasedLine(x1 + offset*dx, y1 + offset*dy, x2 + offset*dx, y2 + offset*dy);
      offset = offset + 1.0;
      i++;
    }
  }

Which is obviously pretty slow :(
Still waiting for Purity of the Surf II

GarageGothic

I guess it depends on the effect you want (i.e. should the anti-aliased area also grow or just the width of the opaque pixels). If you just want a thick solid line with soft edges, it should be possible to do the opaque pixels either using DrawLine repeatedly or drawing two triangles (for very wide lines) - and then draw two anti-aliased lines, one on each side of the aliased line.

tzachs

I had a puzzle with a rope in my game, I originally thought of manually animating the whole thing, but then I remembered this module, and it:
A. Looks much more impressive than what I had planned.
B. Took a lot less time.
C. Saved my life!

So I guess I just wanted to say thanks for this great module!

Dualnames

D. Also helps with the ladies! ;)
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Ryan Timothy B

#15
Hey Steve,

Using the rope test demo, is there any chance of being able to remove those AA gaps between the vertices of the rope joints, having it solid?
Like the center pixel always being 100% no matter what.

I've studied the code as much as I can without frying my brain.  It's gotta be the worst thing about using other people's code.
Heck, it even took me an hour just to make it draw on a sprite for an object instead of the background.  Now I can use it anywhere, even above the chosen characters and objects.  Even works with foreground walkbehinds.  Yay!

Edit: It doesn't show up all that badly on the black backgrounds, but really badly on colored backgrounds.  Like this:

(background is totally ripped from some random adventure game)

I understand why it's doing it, it's using floats for the vertices and judging the transparency on the edges by that.  What is the easiest way to make it convert to Int before it calculates the AA?  Obviously the visual of the 'rope' won't be as smooth anymore with ints, but at least it'll be consistent in opacity.

Thanks.

Hmm.. Now that I think about it, having the 33 rope points in int form, it'll still have AA issues between the two points.
So perhaps the only solution would be to increase the AA to 3 pixels wide/high in the areas in needs it based on the point before and after..  Basically how the photoshop line tool works, always with a strong center of the line.
How could I do this?

Kweepa

Since it's your birthday, I decided to take a look at this.
The gaps in the rope are more exaggerated because you have rendered the rope to a sprite first, so the alpha channel is on/off rather than smooth, and any pixels that are less than 50% won't render at all.
I'm out of time to look into this further - gotta go to work - but it may be possible to retain the alpha by drawing single pixel images of varying alpha to the sprite rather than a single pixel image of varying transparency.
Take a look at the "plot" function in AntiAliasedLine.asc and try replacing
gSurfaceToDrawOn.DrawImage(y,x,gPixel.Graphic,t);
with
gSurfaceToDrawOn.DrawImage(y,x,gPixel[t/4]);
where gPixel is an array of sprites of varying alpha (you'd need to create these in a paint package and load them in, then set the array up before drawing any lines).

Perhaps someone like monkey can help you further since it's your birthday and all :)
Still waiting for Purity of the Surf II

Ryan Timothy B

#17
Quote from: SteveMcCrea on Tue 18/05/2010 15:14:55
Since it's your birthday, I decided to take a look at this.
That's the only reason I decided to ask last night.  Now get it working!!   ;D

Edit:  I removed the last portion of this post because I've got it working! Yay!  It suddenly hit me, you're drawing the gPixel at different transparencies sometimes in the same X,Y location - like layers.  I kept clearing the X,Y pixel of the sprite to match the background every time it went to draw a pixel.  So that's why they appeared as light AA in those broken connections.

Right on.  Thanks for this awesome module, you're my hero Steve.

Monsieur OUXX

Steve, may I ask you if you based your implementation on Xiaolin Wu's algorithm?

Do you know if there are algorithms out there that are more "space consuming"* and less "time-consuming"* ?
The more the better.

* as they say in algorithmics dialect


 

Kweepa

I did, yes - as it says in the source code :)
I doubt you'd get significant savings from another algorithm. Specifically, I doubt you'd get more than a 50% speedup, without making significant assumptions. If you have some assumptions (for example, start and end are always going to be on exact pixels, maximum line length is X, ...) then for example you could precache some lines and just pick a precached line.
Still waiting for Purity of the Surf II

SMF spam blocked by CleanTalk