rendererdemo.bmx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. SuperStrict
  2. Framework mky.mojo2
  3. ?Not opengles
  4. Import brl.GLGraphics
  5. ?opengles
  6. Import sdl.sdlgraphics
  7. ?
  8. Import brl.pngloader
  9. Import brl.random
  10. Graphics 800, 600, 0
  11. Const NUM_LIGHTS:Int = 5
  12. Local canvas:TCanvas = New TCanvas.CreateCanvas()
  13. canvas.SetViewport( 0,0,GraphicsWidth(),GraphicsHeight())
  14. canvas.SetProjectionMatrix( Mat4Ortho( 0,640,0,480,-1,1 ) )
  15. Local tile:TImage
  16. Local shadowCaster:TShadowCaster
  17. Local renderer:TRenderer
  18. Local layer0:TMyLayer
  19. Local rimage:TImage
  20. 'create renderer
  21. renderer=New TRenderer
  22. renderer.SetAmbientLight( [0.1,0.1,0.1,1.0] )
  23. 'load some gfx
  24. tile=TImage.Load( "images/t3.png",0,0 )
  25. 'create layer 0
  26. layer0=New TMyLayer
  27. 'add some lights to layer
  28. For Local i:Int=0 Until NUM_LIGHTS
  29. Local light:TMyLight = New TMyLight
  30. light.color[i Mod 3] = 0.5
  31. layer0.lights.Push light
  32. Next
  33. For Local x:Int=0 Until 640 Step 128
  34. For Local y:Int=0 Until 480 Step 128
  35. layer0.DrawImage tile,x,y
  36. Next
  37. Next
  38. 'create simple rect shadow caster
  39. shadowCaster=New TShadowCaster.Create()
  40. shadowCaster.SetVertices( [0.0,0.0, 32.0,0.0, 32.0,32.0, 0.0,32.0] )
  41. 'draw some shadow casters
  42. For Local x:Int=100 Until 640 Step 220
  43. For Local y:Int=60 Until 480 Step 180
  44. layer0.SetColor 1,1,0
  45. layer0.DrawRect x-16,y-16,32,32
  46. layer0.SetColor 1,1,1
  47. layer0.AddShadowCasterXY shadowCaster,x-16,y-16
  48. Next
  49. Next
  50. 'add layer to renderer
  51. renderer.Layers.Push layer0
  52. Local angle:Float = 0
  53. Local ms:Int, me:Int
  54. While Not KeyDown(key_escape)
  55. 'move lights around a bit
  56. For Local i:Int=0 Until NUM_LIGHTS
  57. Local light:TMyLight=TMyLight(layer0.lights.Get(i))
  58. Local radius:Float=120.0
  59. 'Local an:Float=(i*360.0/NUM_LIGHTS)+(MilliSecs()/50.0)
  60. Local an:Float=(i*360.0/NUM_LIGHTS)+(angle)
  61. light.matrix[12]=Cos( an )*radius+320
  62. light.matrix[13]=Sin( an )*radius+240
  63. Next
  64. 'render scene
  65. renderer.Render(canvas)
  66. Flip
  67. angle :+ 0.5
  68. Wend
  69. 'create an orthographics projection matrix
  70. Function Mat4Ortho:Float[]( Left:Float,Right:Float,bottom:Float,top:Float,znear:Float,zfar:Float )
  71. Local w:Float=Right-Left,h:Float=top-bottom,d:Float=zfar-znear
  72. Return [ 2.0/w,.0,.0,.0, .0,2.0/h,.0,.0, .0,.0,2.0/d,.0, -(Right+Left)/w,-(top+bottom)/h,-(zfar+znear)/d,1.0 ]
  73. End Function
  74. Type TMyLight Implements ILight
  75. 'note: x,y,z,w go in last 4 components of matrix...
  76. Field matrix:Float[]=[1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,-100.0,1.0]
  77. Field color:Float[]=[0.2,0.2,0.2,1.0]
  78. Field Range:Float=400.0
  79. 'implement ILight interface...
  80. '
  81. Method LightMatrix:Float[]()
  82. Return matrix
  83. End Method
  84. Method LightType:Int()
  85. Return 1
  86. End Method
  87. Method LightColor:Float[]()
  88. Return color
  89. End Method
  90. Method LightRange:Float()
  91. Return Range
  92. End Method
  93. Method LightImage:TImage()
  94. Return Null
  95. End Method
  96. End Type
  97. Type TMyLayer Extends TDrawList Implements ILayer
  98. Field lights:TILightStack=New TILightStack
  99. Field _layerMatrix:Float[]=[1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0]
  100. Field _layerFogColor:Float[]=[0.0,0.0,0.0,0.0]
  101. 'implement ILayer interface...
  102. '
  103. Method LayerMatrix:Float[]()
  104. Return _layerMatrix
  105. End Method
  106. Method LayerFogColor:Float[]()
  107. Return _layerFogColor
  108. End Method
  109. Method LayerLightMaskImage:TImage()
  110. Return Null
  111. End Method
  112. Method EnumLayerLights( lights:TILightStack )
  113. For Local i:Int = 0 Until Self.lights.length
  114. lights.Push Self.lights.Get(i)
  115. Next
  116. End Method
  117. Method OnRenderLayer( drawLists:TDrawListStack )
  118. drawLists.Push Self
  119. End Method
  120. End Type