#include "Include\Common.dba"

   `Setup Display
      ScreenWidth#=desktop width() : ScreenHeight#=desktop height()   
      set display mode 640,480,32,1,0,0,ScreenWidth#,ScreenHeight#
      sync on : sync rate 0
      hide mouse
      autocam off
      InitCommonGlobals()

   `Set Text
      set text font "Verdana"
      set text size 15
      set text to bold
      ink rgb(255,255,255),0

   `Camera
      Aspect#=ScreenWidth#/ScreenHeight#
      Fov#=125
      Fov#=2.0*atan(tan(Fov#*0.5)/Aspect#)
      CameraDist#=-200
      position camera 0,0,-150
      set camera view 0,0,ScreenWidth#,ScreenHeight#
      set camera aspect 0,Aspect#
      set camera fov 0,Fov#
      xrotate camera 30
      color backdrop rgb(64,64,64)

   `Textures
      load image "Resources/rockwall.png",1
      load image "Resources/rockwall normalmap.png",2
      load image "Resources/rockwall height.png",3
      load image "Resources/Flare.png",20

   `Load effect
      load effect "Shaders/Parallax Occlusion Mapping.fx",1,0
      set vector4 Vector4(1),0.1,0.1,0.1,0
      set effect constant vector 1,"AmbientColor",Vector4(1)
      set vector4 Vector4(1),1,1,1,0
      set effect constant vector 1,"LightColor",Vector4(1)
      set effect constant float 1,"LightRange",750

   `Create object
      make object sphere 1,75,25,25:scale object texture 1,0,5,3
      set object effect 1,1
      texture object 1,0,1
      texture object 1,1,2
      texture object 1,2,3
      make object box 2,4000,5,4000:scale object texture 2,0,50,50
      position object 2,0,-75,0
      set object effect 2,1
      texture object 2,0,1
      texture object 2,1,2
      texture object 2,2,3

   `Make Light Flare
      make object plain 20,50,50
      texture object 20,20
      ghost object on 20
      set object light 20,0

   `Start loop
      do

   `Control Camera
      position camera 0,0,0
      if mouseclick()=0
         xrotate camera camera angle x()-mousemovey()*0.25
         yrotate camera camera angle y()+mousemovex()*0.25
         if camera angle x()>80 then xrotate camera 80
         if camera angle x()<-60 then xrotate camera -60
      endif
      if mouseclick()>0 then CameraDist#=CameraDist#-mousemovey()*0.25
      CameraDist#=CameraDist#+mousemovez()*0.25
      if upkey()=1 then CameraDist#=CameraDist#+2.5
      if downkey()=1 then CameraDist#=CameraDist#-2.5
      if CameraDist#>-20 then CameraDist#=-20
      if CameraDist#<-300 then CameraDist#=-300
      move camera CameraDist#
      if camera position y()<-60 then position camera camera position x(),-60,camera position z()

   `Rotate object
      if keystate(19)=1 and NoPress=0
         if RotateObject=0 then RotateObject=1 else RotateObject=0
         NoPress=1
      endif
      if RotateObject=0 then rotate object 1,0,object angle y(1)+0.25,0

   `Change Object
      if Spacekey()=1 and NoPress=0
         delete object 1
         inc ChangeObject
         if ChangeObject>2 then ChangeObject=0
         if ChangeObject=0 then make object sphere 1,75,25,25:scale object texture 1,0,5,3
         if ChangeObject=1 then make object box 1,75,75,75:scale object texture 1,0,2,2
         if ChangeObject=2 then make object cylinder 1,75:scale object texture 1,0,6,2
         set object effect 1,1
         texture object 1,0,1
         texture object 1,1,2
         texture object 1,2,3
         NoPress=1
      endif

    `Move Light
      if KEYSTATE(38)=1 and MoveLight=1 and NoPress=0 then MoveLight=0:NoPress=1
      if KEYSTATE(38)=1 and MoveLight=0 and NoPress=0 then MoveLight=1:NoPress=1
      if MoveLight=0
         Ang#=wrapvalue(Ang#+0.25)
         LightX#=cos(Ang#)*125
         LightY#=cos(Ang#)*25
         LightZ#=sin(Ang#)*125
      endif

   `Set Light
      if shiftkey()=1 and NoPress=0
         if MoveLight=1
            LightX#=camera position x()
            LightY#=camera position y()
            LightZ#=camera position z()
         else
            set vector4 Vector4(1),100+rnd(155),100+rnd(155),100+rnd(155),0
            set vector4 Vector4(1),(x vector4(Vector4(1))/255)^2.2,(y vector4(Vector4(1))/255)^2.2,(z vector4(Vector4(1))/255)^2.2,0
            set effect constant vector 1,"LightColor",Vector4(1)
            set effect constant float 1,"LightRange",600+rnd(400)
         endif
         NoPress=1
      endif

   `Update Light
      set vector4 Vector4(1),LightX#,LightY#,LightZ#,0
      set effect constant vector 1,"LightPosition",Vector4(1)
      position object 20,LightX#,LightY#,LightZ#
      point object 20,camera position x(),camera position y(),camera position z()

   `Text
      text 20,20,"Parallax Occlusion Mapping - Fps "+str$(screen fps())
      text 20,50,"Space - Change Object      R - Rotate Object      L - Move Light      Shift - Set Light"

   `End loop
      if scancode()=0 then NoPress=0
      mousemovex()=0
      mousemovey()=0
      sync
      loop
