UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on September 04, 2009, 02:42:32 AM

Title: Sparkler
Post by: kevin on September 04, 2009, 02:42:32 AM
  Sparkler

   This little effect is a combination vertex rotation a few other recent demo effects..

[pbcode]

   setfps 50

   SceneProjectionX#   =300
   SceneProjectionY#   =300

   NumberOfVertex      =1000

   // Size of a single vertex (XYZ)
   SrcVertSize            =12
   DestVertSize         =20      


   // Array to hold the none rotated vertex
   Dim VertexFrame1#(2      ,(NumberOfVertex+1))
   Dim VertexFrame2#(2      ,(NumberOfVertex+1))

   // Array to hold the rotated vertex
   Dim RotatedVertex#(4,(NumberOfVertex+1))

   
   for t= 0 To NumberOfVertex
      xd = Rndrange(-90,90)
      x0 = (Cos(xd) * 10) * (Cos(t) * 10)
      y0 = (Cos(xd) * 10) * (Sin(t) * 10)
      z0 = Sin(xd) * 100
      VertexFrame2#(0,t)=x0*6
      VertexFrame2#(1,t)=y0*6
      VertexFrame2#(2,t)=z0*6
   Next


do

   width=245
   height=256
   scale=100
   BackdropColour=rndrgb()
   BackdropIMage=Make_Background_Block(Width,Height,Scale,BackdropColour)



   // Size of the vertex cube
   ObjectSizeX         =500
   ObjectSizeY         =500
   ObjectSizeZ         =500




   //   Objects Base X/Y position
   basex#=0
   basey#=0
   basez#=800

   // Precalc the Depth que shade array to shade the dots based upon the dots depth from the viewer
   Dim ShadeBufferBuffer(20000)
   NumberOfVertexdepth#=3000
   scaler#=100/NumberOfVertexdepth#   
;   col=rndrgb()
   col=BackdropColour
   col=rgbalphamult(col,$505050)
   For lp=0 To NumberOfVertexdepth#
      ShadeBufferBuffer(lp)=RGBFade(col,(NumberOfVertexdepth#-lp)*scaler#)
   Next   


 Sw=GetScreenWidth()
 Sh=GetScreenHeight()
 
 
 Screen=NewFXimage(sw,sh)
   rendertoimage screen

   // Create a camera to draw the scene (with depth ordering)
   CreateCamera 1

   // Turn CLS off in the camera.  This allows us to perform an Asycn CLS while the
   // vertex are being rotataed
   CameraCls 1,off

   rendertoscreen   
   
   // --------------------------------------------------------------------------------
   // Main loop  ---------------------------------------------------------------------
   // --------------------------------------------------------------------------------
   StartingTime=Timer()

   Do
   
      // Tell PB to capture draw commands to the Scene buffer
         CaptureToScene

      // Clear the scene at start of the frame, so there's no left over junk in there from a previous frame
         ClsScene

         capturedepth 8000

         TimePast=Timer()-StartingTime


         PixelsPerTick#=(GetImageWidth(Backdropimage)/4)/1000.0
         Xpos=PixelsPerTick#*TimePast
         customtileimage backdropimage,Xpos,0,FALSE

         DegreesPerTick#=(1.01*60)/1000.0
         ax#=WrapAngle(DegreesPerTick#*TimePast,0)

         DegreesPerTick#=(0.42*60)/1000.0
         ay#=WrapAngle(DegreesPerTick#*TimePast,0)

         DegreesPerTick#=(0.9*60)/1000.0
         az#=WrapAngle(DegreesPerTick#*TimePast,0)


      // Get Address of Array source vertex
         SrcAddress      =GetArrayPtr(VertexFrame2#())+PBArraystruct_Size

      // Get address of rotated vertex (none projected)  
         DestAddress      =GetArrayPtr(RotatedVertex#())+PBArraystruct_Size

      // Rotated the vertex list
         RotateVertexListXYZ SrcAddress,SrcVertSize,DestAddress,DestVertSize,baseX#,basey#,baseZ#,ax#,ay#,az#,NumberOfVertex   

         xc=sw/2
         yc=sh/2
inkmode 1+64

      // Draw the list of camera. So they can drawn in order when the
         For vert=0 to NumberOfVertex-1
            z#=RotatedVertex#(2,Vert)
            If z#>10
               Capturedepth z#*2
               X#=xc+(RotatedVertex#(0,Vert)*SceneProjectionx#)/z#
               Y#=yc+(RotatedVertex#(1,Vert)*SceneProjectiony#)/z#
               linec xc,yc,x#,y#,ShadeBufferBuffer(z#)
            endif                  
         next
inkmode 1
   
      // draw the scene
      DrawCamera 1


      drawimage screen,0,0,false
;       drawrotatedimage  Screen,0,0,0,0.5,1,0,0,false+8

      SetCursor 0,0
      speed#=10

      If UpKey() Then basez#=basez#-speed#
      If DownKey() Then  basez#=basez#+speed#

      If basez#>3000 Then Basez#=3000
      If basez#<-3000 Then Basez#=-3000

   
      Print FPS()

      Sync

   Loop spacekey()=true
   
   flushkeys

   deletecamera 1   
   deleteimage Screen
   deleteimage BackdropIMage
   
loop



Function CustomTileImage(ThisImage,Xpos,Ypos,Transparent)

   sw=GetSCreenWidth()
   sh=GetSCreenHeight()
   
  Width=GetIMageWidth(ThisImage)
  Height=GetIMageHeight(ThisIMage)

   if Xpos>0 then Xpos=Xpos*-1
  if Ypos>0 then Ypos=Ypos*-1

  Xpos=mod(Xpos,width)
  Ypos=mod(Ypos,height)
 
  x1=Xpos
  while X1>0
     x1=x1-Width
  endwhile

  y1=Ypos
  while Y1>0
     Y1=Y1-Height
  endwhile

  dec height
  for Y=y1 to (y1+Sh*2) step height
        for X=x1 to (x1+Sw*3)+1 step width
              drawrotatedimage  ThisImage,X,Y,angle#,1,1,0,0,Transparent
       next
   next

EndFunction    


Function Make_Background_Block(Width,Height,Scale,C1)

    w=(Width*Scale)/100.0
    h=(Height*Scale)/100.0
   
   TempImage=NewFXImage(w,h)
   rendertoimage TempImage

    cx=Width/2
    cy=height/2
   boxc 0,0,width,height,true,c1

    radius=getdistance2d(0,0,w*0.4,h*0.4)
     inkmode 1+2048
      for lp=radius to 0 step -2
            i=55+(200*(float(lp)/Radius))
          if i then circlec w/2,h/2,lp,true,rgb(i,i,i)
     next            
 
   inkmode 1
   
   // since we're using a full screen tiled grid, we can double the
   // size of the tiles in order to make it easier for the graphics cards
   // blitter to the draw the fragments.
   while W<256
      W=W*2
   endwhile   

   while H<256
      h=h*2
   endwhile   

   ThisImage=NewfxImage(w,h)
   rendertoimage ThisImage
   TileIMage TempImage,0,0,false
   deleteimage TempImage
   rendertoscreen
EndFunction ThisImage
 
[/pbcode]