I salute from Sasuke18 on my first snippet page. I told him where to find you.
Quote: "Hello, I'm new here! I was searching the internet for Direct X 11 plugins & came across this plugin & I must say... It looks AMAZING! Keep up the GREAT work!
Sasuke"
Please forgive me, that set pixel parameter issue was a mistake on my part for referencing an invalid array. It works now; I am happy to post this new snippet; but please do explain if I must create a light sources using shaders; at present the coloured spheres feature no highlights or shades; must I create shaders for all my snippets to feature lighting?
` Floating Spheres
` Ziggurat (DX11 for DBPRO by Rudolpho) - Basics
` Snippet by Chris Tate : [email protected]
//--//--//--//--//--//--//--//--//--// --//--//--//--//--//--//--//--//--//--//
` --> The following snippet creates falling sheres
`----------------------------------------------------
` Ensure that this program includes DX11Constants.dba
INCLUDEGUARD DX11_CONSTANTS
`----------------------------------------------------
` Set the program's meta-data
Global ProgramName$ = "Falling Spheres"
Global ExitPrompt$ : ExitPrompt$ = ""
Global StartTime
Global DX11PluginIntroDuration = 5000
`----------------------------------------------------
` Declare settings for the application
Type DX11SettingsType
ShowFPS
FPSLabelID
WelcomeMessageID
WelcomeMessageExpired
WelcomeMessageDelaySeconds
WelcomeMessage$
WelcomeMessageFont$
StatisticFont$
StartWithFullScreen
ResolutionX
ResolutionY
WindowTitle$
StartWithDirectXVersion
Endtype
Global DX11Settings as DX11SettingsType
`----------------------------------------------------
` Initialize Ziggurat using our specification package
DX11Settings.ResolutionX = 1024
DX11Settings.ResolutionY = 768
DX11Settings.ShowFPS = 1
DX11Settings.StatisticFont$ = "Arial"
DX11Settings.StartWithFullScreen = False
DX11Settings.WindowTitle$ = ProgramName$
DX11Settings.StartWithDirectXVersion = DXVERSION_11_0` Set to DXVERSION_10_0 in consideration for out-dated hardware
DX11Settings.WelcomeMessageDelaySeconds = 10
DX11Settings.WelcomeMessage$ = "A demonstration of falling spheres in Direct X11"
DX11Settings.WelcomeMessageFont$ = "Arial"
InitializeDirectX11(DX11Settings)
` Note that the following is all that is performed by InitializeDirectX11()
` DX11 Init ResolutionX, ResolutionY, StartWithFullScreen, WindowTitle$, StartWithDirectXVersion
`----------------------------------------------------
` Translate the camera to view the scene origin
Global MainCamera : MainCamera = DX11 Get Main Camera()
DX11 Set Camera Position MainCamera, 0, 0, -150
`----------------------------------------------------
` Establish a global force of gravity and wind
Global Force
Force = 1 : null = Make Vector3(Force)
Set Vector3 Force, 2-RND(20), -50, 2-RND(20)
`----------------------------------------------------
` Set our random seed to something variable and use it to pick a random number of spheres
Randomize Timer()
numberOfSpheres = RND(100) + 200
Global Dim Spheres(numberOfSpheres)
`----------------------------------------------------
` Store some colours for the spheres
Global Dim Colours(5)
Global Dim ColourTextures(5)
Colours(0) = 0xFFFF0000 : Colours(1) = 0xFFFFFF00 : Colours(2) = 0xFFFF00FF
Colours(3) = 0xFF0000FF : Colours(4) = 0xFF00FF00 : Colours(5) = 0xFF00FFFF
doNotGenerateMipMaps = 0
` Create coloured images
For colourIndex = 0 to 5
pixelMapSource = DX11 Create PixelMap(1, 1) ` In the current version, the colour overload is not working
DX11 Set PixelMap Pixel pixelMapSource, 0, 0, Colours(colourIndex)
ColourTextures(colourIndex) = DX11 Create Image From PixelMap(pixelMapSource, doNotGenerateMipMaps)
DX11 Delete PixelMap pixelMapSource ` Throw it away, we do not need the pixel map any more
Next colourIndex
` Create some spheres, shall we!
For sphereIndex = 1 to numberOfSpheres
sphere = DX11 Create Object Sphere(3)
scale# = 0.2+(RND(200)*0.01)
DX11 Set Object Scale sphere, scale#, scale#, scale#
DX11 Set Object Texture sphere, ColourTextures(RND(5)) ` ************************* Here < < < <
DX11 Set Object Position sphere, RND(200)-100, RND(200)+200, RND(200)-100
Spheres(sphereIndex) = sphere
Next sphereIndex
//==================================================
` Main loop
`----------------------------------------------------
StartTime = Timer()
While Not DX11 Quit()
` Make the spheres fall
For sphereIndex = 1 to numberOfSpheres
sphere = Spheres(sphereIndex)
x# = DX11 GET OBJECT POSITION X(sphere)
y# = DX11 GET OBJECT POSITION Y(sphere)
z# = DX11 GET OBJECT POSITION Z(sphere)
DX11 Draw Text 5, 20, "Time based delta: " + Str$(Delta#,4)
Inc x#, X Vector3( Force ) * Delta# `(2.0-DX11 Get Object Scale Y(sphere)) * Delta#
Inc y#, Y Vector3( Force ) * Delta# ` DX11 Get Object Scale Y(sphere) * Delta#
Inc z#, X Vector3( Force ) * Delta# ` (2.0-DX11 Get Object Scale Y(sphere)) * Delta#
If y# < - 100
x# = RND(200)-100
y# = RND(200) + 200
z# = RND(200)-100
Endif
DX11 Set Object Position sphere, x#, y#, z#
Next sphereIndex
` Update the system
Update()
EndWhile
` End the program (Ziggurat will shut down and clear its own resources)
end
`----------------------------------------------------
//==================================================
Function Update()
` Handle FPS label
If DX11Settings.ShowFPS
If DX11Settings.FPSLabelID = 0
DX11Settings.FPSLabelID = DX11 Create Label( "", 5, 5 )
Endif
DX11 Set Label Text DX11Settings.FPSLabelID, "FPS: " + str$(dx11 get screen fps())
Else
If DX11Settings.FPSLabelID <> 0
DX11 Delete Label DX11Settings.FPSLabelID : DX11Settings.FPSLabelID = 0
Endif
Endif
` Handle welcome message
If DX11Settings.WelcomeMessageExpired = 0
If DX11Settings.WelcomeMessageID = 0
DX11Settings.WelcomeMessageID = DX11 Create Label( "", 5, DX11Settings.ResolutionY - 20 )
Endif
Local messageStartTime : messageStartTime = StartTime + DX11PluginIntroDuration
If messageStartTime <= Timer()
If messageStartTime + ( DX11Settings.WelcomeMessageDelaySeconds * 250.0 ) => Timer()
DX11 Set Label Text DX11Settings.WelcomeMessageID, TransitionText$( DX11Settings.WelcomeMessage$, StartTime + DX11PluginIntroDuration, DX11Settings.WelcomeMessageDelaySeconds * 250.0 )
Else
If Timer() < StartTime + DX11PluginIntroDuration + ( DX11Settings.WelcomeMessageDelaySeconds * 1000.0 )
`DX11 Set Label Text DX11Settings.WelcomeMessageID, DX11Settings.WelcomeMessage$
Else
DX11 Delete Label DX11Settings.WelcomeMessageID : DX11Settings.WelcomeMessageID = 0
DX11Settings.WelcomeMessageExpired = 1
EndIf
Endif
EndIf
Else
`DX11 Set Label Text DX11Settings.WelcomeMessageID, "_"
EndIf
` Update the Delta
LastTick = CurrentTick
CurrentTick = DX11 Get High Resolution Tick()
` Update program delta using the method demonstrated by the plugin creator
Delta# = DX11 Get High Resolution Delta Time(LastTick, CurrentTick)
` Update Ziggurat here to simplify the loop block
DX11 Sync
EndFunction
//==================================================
Function InitializeDirectX11(settings as DX11SettingsType)
DX11 Init settings.ResolutionX, settings.ResolutionY, settings.StartWithFullScreen, settings.WindowTitle$, settings.StartWithDirectXVersion
` Set the exit prompt
If ExitPrompt$ <> ""
Exit Prompt ExitPrompt$, "Exit " + ProgramName$
EndIf
` Declare program delta using the method demonstrated by the plugin creator
Global CurrentTick as double integer
Global LastTick as double integer
` Create coefficient for elementary time based movement
Global Delta#
EndFunction
//==================================================
Function TransitionText$( text$, startTime, delayInMilliseconds )
If text$ = "" Then ExitFunction " "
If startTime > Timer()
ExitFunction ""
Endif
Local endTime : endTime = startTime + delayInMilliseconds
If Timer() > endTime
ExitFunction text$
Endif
Local duration# : duration# = endTime - startTime
Local ratio# : ratio# = ((Timer()-startTime) * 1.0) / duration#
Local newText$ = " "
Local length : length = Len(text$)
If length > 0 Then newText$ = SubString$( text$, 1, length * ratio# )
`newText$ = Str$(duration#,4) + " - " + Str$(ratio#,4)
EndFunction newText$
//==================================================
Function SubString$( text$, start, length)
Local lastCharacterIndex : lastCharacterIndex = start + length
Local result$
For characterIndex = start to lastCharacterIndex
result$ = result$ + Mid$(text$, characterIndex)
Next characterIndex
EndFunction result$
