Main Menu

Motion Blur

Started by kevin, October 21, 2008, 10:00:10 AM

Previous topic - Next topic

kevin

 Motion Blur Via Alpha Channel

   There's any number of the approaches to doing motion blur.  The most common and the stable diet of perhaps a decade or more worth demos, and those modern bullet hell styled shoot'em'ups, is achieved by simply dimming the frame buffer and drawing over it.   So you take the old image, dim it a little or a lot (for the amount of the blur), then draw over it again.  That's it.   You can do the same thing for individual sprites etc.  

   In this demo we're drawing a windmill styled shape on a temp cache, this buffer is then dimmed, rather cleared between frames and then drawn to the scene.  The result is that you see, it's current position plus past positions blurred together.

  This one was written in PB1.64, it might have to be modified for use with 1.63 or older.

PlayBASIC Code: [Select]
   setfps 60
openscreen 800,600,32,2
windmillsize=200
img=newimage(windmillsize,windmillsize)
prepareafximage img


cog=Create_Cog_Shape(windmillsize/2,windmillsize/2*0.2,15)


Alpha=128
speed#=2.5

Do
; draw a backdrop
c1=rgb(60,110,30)
c2=rgb(200,70,10)
shadebox 0,0,800,600,c1,c1,c2,c2

; depreciate alpha of buffer
rendertoimage img
inkmode 1+2048
boxc 0,0,1000,1000,true,argb(Alpha,155,155,155)
inkmode 1

; refresh buffer foreground
rotateshape cog,angle#,1
ink argb(255,255,155,055)
drawshape cog,windmillsize/2,windmillsize/2,2
angle#=wrapangle(Angle#,-speed#)

; renderscene
rendertoscreen
x1=400
y1=300
line x1,y1,200,800
line x1,y1,600,800
drawimage img,x1-(windmillsize/2),y1-(windmillsize/2),true

; control speed an 'blur length'
if upkey() then Alpha=ClipRange(Alpha+2,2,256)
if downkey() then Alpha=ClipRange(Alpha-2,2,256)
if leftkey() then Speed#=Speed#+0.05
if rightkey() then Speed#=Speed#-0.05

text 0,0,alpha
text 0,20,speed#

sync
Loop



Function Create_Cog_Shape(outterRadius,InnerRadius,sides)
sides=sides -(mod(sides,2))
EdgeCount =Sides*2
VertexCount =Sides*2
ThisShape =NewShape(VertexCount+1,EdgeCount+1)

For lp=0 to sides
x#=Cosradius(angle#,OutterRadius)
y#=Sinradius(angle#,OutterRadius)
SetshapeVertex thisshape,Numberofvertex,x#,y#
inc Numberofvertex
x#=Cosradius(angle#,InnerRadius)
y#=Sinradius(angle#,InnerRadius)
SetshapeVertex Thisshape,Numberofvertex,x#,y#
inc Numberofvertex
angle#=angle#+(360.0/Sides)
next


For Vertex=0 to Numberofvertex-2 step 2
NextVertex=vertex+1
if NextVertex=>Numberofvertex then nextVertex=Numberofvertex-NextVertex
SetshapeEdge Thisshape,Edge,vertex,Nextvertex
inc edge

if Outline
SrcVertex=Vertex
NextVertex2=vertex+2
if NextVertex2=>Numberofvertex then nextVertex2=Numberofvertex-NextVertex
else
SrcVertex=NextVertex
NextVertex2=vertex+3
if NextVertex2=>Numberofvertex then nextVertex2=Numberofvertex-NextVertex
endif

SetshapeEdge Thisshape,Edge,Srcvertex,Nextvertex2
inc edge

Outline=1-Outline
next

EndFunction ThisShape




ATLUS

its not blur image does not run

thaaks

Works fine here on my machine.

ATLUS, try cursor up and down keys to modify the blur factor, use cursor left and right to change rotation speed.

@BlinkOk: See, it does work in real time   ;) You just need to know how. Correction: You just need to know a guy who knows how  ;D

Thanks to Kevin for this wonderful example and teaching lesson!

ATLUS

Quote from: thaaks on October 21, 2008, 01:09:22 PM
Works fine here on my machine.

ATLUS, try cursor up and down keys to modify the blur factor, use cursor left and right to change rotation speed.

@BlinkOk: See, it does work in real time   ;) You just need to know how. Correction: You just need to know a guy who knows how  ;D

Thanks to Kevin for this wonderful example and teaching lesson!
i see code and try click arows keys. Its not monitor blur.