Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (843)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Shadow Performance  (Read 2889 times)
0 Members and 1 Guest are viewing this topic.
Offline mickleness

Senior Newbie

« Posted 2007-07-30 21:45:50 »

Currently our apps render a lot of  simple drop shadows.

Our approach is very basic:
1.  Render a silhouette of the image in grayscale
2.  Apply a gaussian blur (via a ConvolveOp)
3.  Render this underneath the original image at a certain offset

(This is basically applied from Vincent Hardy's "Java2D Graphics" book.)

The catch is that the ConvolveOp takes a long time for large images.  This makes sense to you and me: a 800x600 image, with a blur matrix of 6x6, comes to about 17.2 million calculations.   But our users aren't impressed with this explanation.  Smiley  So my questions are:
1.  Is there a pure Java approach to a faster shadow?  It doesn't have to be a kosher Gaussian blur... anything that allows varying degrees of blurring the edges is fine.

2.  Is there another road we should look down?  JOGL, JNI, etc?
Offline 2playgames

Junior Devvie

« Reply #1 - Posted 2007-07-30 22:44:16 »

determine the direction of the shadow, start at the opposite edge of the image and loop towards that direction. when you reach the end of the image (a.k.a. encounter a transparent pixel) start to draw black pixels, and fade it out after some more loops

Offline keldon85

Senior Devvie

Medals: 1

« Reply #2 - Posted 2007-07-30 23:39:20 »

Hmmm, run JPhotoBrush and perform the gausian blur/ smooth! Maybe you're doing something wrong!

Maybe (a) 3x3 smooth, (b) reduce pixels to 50%, (c) 3x3 smooth reduced image and display that! Are you doing something odd, like creating lots of new buffers each time!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mickleness

Senior Newbie

« Reply #3 - Posted 2007-07-31 17:34:40 »

Thanks for the replies.

I'm not entirely sure what 2playgames is suggesting.  Will that let me achieve a blurred arbitrary shape?

I looked at JPhotoBrush: I think this performs a much smaller blur than what we perform.  We let our kernel size range from 0 (so it's an unblurred silhouette) to about 30 pixels (to give a decent blur).

keldon85 was right: there was a significant memory leak in our existing model.  We were using Vincent Hardy's CompositeOp to run 2 filters consecutively.  It turns out the first filter was always passing "null" as its destination image, so we were making a new image.  So I cleaned that up.

Also I wrote my own ConvolveOp that skipped continuous blocks of the same color.  So for large continuous shapes, our shadows now render in about half the time they used to.  For discontinuous shapes (like text) rendering time is about the same.

So rendering a shadow with a kernel size of 25 on a 400x400 square now takes about 131 ms on my iMac.  (It used to take 282 ms.)

We'd still be very interested in other approaches to shadows, but at least we can report back that we made some progress.  Smiley
Offline keldon85

Senior Devvie

Medals: 1

« Reply #4 - Posted 2007-07-31 18:55:02 »

Maybe try not calculating pixels you will not be able to see, which will only make gains as more pixels are obscured. There are a few things you can do when creating large blurs (like 30 pixels), and that is to borrow from mip mapping. You first compute low resolution versions of the image depending on the accuracy you want, for example you might store image/1, image/2, image/4 and so on, or just image/1, image/4, image/16. Then rather than doing a 61x61 convolution you end up doing much less calculations by using the lower resolution version of the image as it is further away.

For example call the centre pixel when applying the convolution (0,0), rather than computing for all (i,j) where -31<i<31 and -31<j<31, you use lower the lower resolutions to calculate further pixels since they make less of a difference anyway. Right now the complexity is O(n^2) per pixel where the blur size is n/2, you should be able to use this technique to get it to O(n log n).

Pages: [1]
  ignore  |  Print  

EgonOlsen (37 views)
2018-06-10 19:43:48

EgonOlsen (20 views)
2018-06-10 19:43:44

EgonOlsen (39 views)
2018-06-10 19:43:20

DesertCoockie (186 views)
2018-05-13 18:23:11

nelsongames (122 views)
2018-04-24 18:15:36

nelsongames (121 views)
2018-04-24 18:14:32

ivj94 (861 views)
2018-03-24 14:47:39

ivj94 (122 views)
2018-03-24 14:46:31

ivj94 (758 views)
2018-03-24 14:43:53

Solater (139 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!