Candy Rope - Sinus Wave - Gouraud Strip Demos
This is update one of the example pack demos. While it's nicer than the original, it's mainly to stress test the gouraud strip functions..
How Does it work ?
This article goes through the BASIC concepts on show in this demo:
Building a Procedural Rope Effect in PlayBASIC
Watch Short
This is update one of the example pack demos. While it's nicer than the original, it's mainly to stress test the gouraud strip functions..
How Does it work ?
This article goes through the BASIC concepts on show in this demo:
Building a Procedural Rope Effect in PlayBASIC
Watch Short
PlayBASIC Code:
; This is pixel format test version of this demo ; Function keys f5/f6/f7/f8 to set the display depth ; Space key to add more sides of objects ; open a tall strip like screen for the short openscreen 800,1000,32,1 PositionScreen GetSCreenXpos(),0; GetScreenYpos() ;*0.30 ;Setfps 60 LoadFont "Courier New",1,48,0,8 fontdrawmode 1,1 ; force a known randomize sequence by seeding the generator randomize 8262 Global RopeEdges=3 Dim WaveRippleAngle#(10) ScreenWidth =GetScreenWidth() ScreenHeight =GetScreenHeight() ScreenDepth=32 Screen= NewFXImage(ScreenWidth,ScreenHeight) Type RopeVerts X1#,X2#, rgb1,rgb2 endtype For Tests=0 to 20 ; init the palette Dim Palette(RopeEdges+2) For lp=0 to RopeEdges-1 Palette(lp) =NiceRGB(lp,0.25,0.90) next Palette(lp+1) =Palette(0) Dim RopeSegs(Ropeedges) as Ropeverts Do if functionkeys(5) then ScreenDepth = 15 if functionkeys(6) then ScreenDepth = 16 if functionkeys(7) then ScreenDepth = 24 if functionkeys(8) then ScreenDepth = 32 if GetIMageDepth(Screen) <> ScreenDepth deleteimage Screen Screen =GetFreeImage() CReateFXImageEX Screen,GetScreenWidth(),GetScreenHeight(),ScreenDepth endif rendertoimage Screen // backdrop X1=ScreenWidth *0.00 X2=ScreenWidth *0.25 X3=SCreenWidth *0.50 X4=SCreenWidth *0.75 X5=SCreenWidth *1.00 lockbuffer PaletteBandSize=50 ThisRGB1=Palette(0) for ylp=0 to getscreenheight()-1 YOffset = mod(Ylp+ScrollY,PaletteBandSize*(RopeEdges-1)) Index = mod(yoffset/PaletteBandSize,(RopeEdges-1)) ThisRGB1=Palette(Index) ThisRGB2=Palette(Index+2) // Scale /blend level between bands Scale# = (Yoffset-(PaletteBandSize*Index)) / float(PaletteBandSize) ThisRGB = RgbAlphaBlend(ThisRGB1,ThisRGB2, Scale#*80) ThisRGB50 = Rgbfade(ThisRGB,50) ThisRGB20 = Rgbfade(ThisRGB,10) GouraudStripH x1,ThisRGB,X2,ThisRGB50,Ylp GouraudStripH x2,ThisRGB50,X3,ThisRGB20,Ylp GouraudStripH x3,ThisRGB20,X4,ThisRGB50,Ylp GouraudStripH x4,ThisRGB50,X5,ThisRGB,Ylp next DrawRope() unlockbuffer ScrollY=mod(ScrollY+1,PaletteBandSize*(RopeEdges-1)) renderimage Screen,0,0 Sync loop spacekey() RopeEdges++ flushkeys next end Function DrawRope() Static RadiusAngle RadiusAngle=wrapangle(RadiusAngle +1) radius=250+cos(RadiusChange)*50 ScReenHeight=GetScreenHeight() basex=GetScreenwidth()/2 // step the global angles AngleStep=2 WaveAngle#= WaveRippleAngle#(1) WaveAngle2#= WaveRippleAngle#(2) WaveAngle3#= WaveRippleAngle#(3) WaveAngle4#= WaveRippleAngle#(4) WaveRippleAngle#(1)=wrapangle(WaveAngle#,0.7*AngleStep) WaveRippleAngle#(2)=wrapangle(WaveAngle2#,1.2*AngleStep) WaveRippleAngle#(3)=wrapangle(WaveAngle3#,2.1*AngleStep) WaveRippleAngle#(4)=wrapangle(WaveAngle4#,-0.4*AngleStep) EdgeAngleStep#=360.0/(RopeEdges) BaseZ=1000 Segments=0 static BaseAngle# ThisRGB1 = Palette(RopeEdges) For lp=0 To RopeEdges-1 ThisRGB2 = Palette(lp+1) Angle#=BaseAngle#+(EdgeAngleStep#*lp) Angle2#=Angle#+EdgeAngleStep# X1#=Cosradius(angle#,Radius) X2#=CosRadius(angle2#,Radius) Z1#=basez+SinRadius(angle#,Radius) Z2#=basez+SinRadius(angle2#,Radius) Px1#=((X1#*400)/z1#) Px2#=((X2#*400)/z2#) If pX1#<=pX2# R=RgbR(ThisRgb1) G=RgbG(ThisRgb1) B=RgbB(ThisRGB1) R1=(R*500)/z1# G1=(G*500)/z1# B1=(B*500)/z1# R=RgbR(ThisRgb2) G=RgbG(ThisRgb2) B=RgbB(ThisRGB2) R2=(R*500)/z2# G2=(G*500)/z2# B2=(B*500)/z2# RopeSegs(Segments).x1=px1# RopeSegs(Segments).x2=px2# RopeSegs(Segments).rgb1=rgb(r1,g1,b1) RopeSegs(Segments).rgb2=rgb(r2,g2,b2) inc segments EndIf ThisRGB1=ThisRGB2 Next dec segments For ThisScanLine=0 To ScreenHeight-1 Xoffset=basex+cosradius(wAVEangle#,50) Xoffset=Xoffset+cosradius(wAVEangle2#+wAVEangle#,40) Xoffset=Xoffset+cosradius(wAVEangle3#,80) Xoffset=Xoffset+cosradius(wAVEangle4#,30) For lp=0 to Segments GouraudStripH Xoffset+RopeSegs(lp).x1,RopeSegs(lp).rgb1,Xoffset+RopeSegs(lp).x2,RopeSegs(lp).rgb2,ThisScanline ; GouraudStripV Xoffset+RopeSegs(lp).x1,RopeSegs(lp).rgb1,Xoffset+RopeSegs(lp).x2,RopeSegs(lp).rgb2,ThisScanline next waveangle# += 0.15 waveangle2# += 0.25 waveangle3# += 0.35 waveangle4# += 0.33 Next BaseAngle#++ EndFunction ; =============================================================== ; Golden Ratio based pleasant random colors ; - Each call with incrementing index gives nicely spaced hues ; =============================================================== constant GoldenRatio# = 0.6180339 ; ˜ (v5 - 1) / 2 Function NiceRGB(Index=0, Saturation#=0.75, Value#=0.88) Local Hue# ; Start from a random base hue, then walk by golden ratio ; (mod 1.0 to wrap around colour wheel) Hue# = mod(Rnd#(1) + (Index * GoldenRatio#),1) Hue# = Hue# - Floor(Hue#) ; fractional part only (mod 1) ; Optional: You can randomise S/V a little for more variety Saturation# = Saturation# + RndRange#(-0.15, 0.15) Value# = Value# + RndRange#(-0.1, 0.1) ; Clamp them If Saturation# < 0.4 Then Saturation#=0.4 If Saturation# > 0.98 Then Saturation#=0.98 If Value# < 0.5 Then Value#=0.5 If Value# > 0.98 Then Value#=0.98 #if playbasicversion =>165 Result = HSV2RGB(Hue#, Saturation#, Value#) #else Result=rndrgb() #Endif EndFunction Result Function RenderImage(ThisImage,xpos,ypos,Mode=false) if GetIMageStatus(ThisIMAGE) inkmode 1+2048 boxc 00,18,275,112,true,$a0a0a0 inkmode 1 Xpos+=10 Ypos+=20 ink $80fFFFFF Text XPos,Ypos ," Fps:"+Str$(fps()) Text XPos,Ypos+40 ,"Depth:"+Str$(GetIMageDepth(ThisIMage)) rendertoscreen lockbuffer drawimage ThisImage,0,0,false unlockbuffer endif EndFunction