mirror.bb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;Mirror in the wall
  2. ;Only works on one plane at the moment
  3. ;David Bird
  4. ;[email protected]
  5. Graphics3D 640,480
  6. SetBuffer BackBuffer()
  7. lit=CreateLight()
  8. cam=CreateCamera()
  9. tex0=LoadTexture("tex0.bmp")
  10. ;Create a camera
  11. CameraRange cam,1,200
  12. ;Create a sphere to visualise the camera
  13. cam_cube=CreateCube()
  14. EntityTexture cam_cube,tex0
  15. ScaleEntity cam_cube,.5,.5,.5
  16. ;Create an flipped cube to place everything in
  17. test=CreateCube()
  18. ScaleMesh test,10,10,10
  19. FlipMesh test
  20. EntityTexture test,tex0
  21. ;Add a mirror.
  22. Create_Mirror(0,0,9,5,0)
  23. While Not KeyDown(1)
  24. If KeyDown(203) MoveEntity cam,-.1,0,0
  25. If KeyDown(205) MoveEntity cam,.1,0,0
  26. If KeyDown(44) MoveEntity cam,0,0,-.1
  27. If KeyDown(30) MoveEntity cam,0,0,.1
  28. PositionEntity cam_cube,EntityX(cam),EntityY(cam),EntityZ(cam)
  29. TurnEntity cam_cube,2,1,4
  30. ;Do updates of mirrors
  31. Update_Mirrors(cam)
  32. RenderWorld
  33. Flip
  34. Wend
  35. ;Free up all mirrors
  36. free_Mirrors()
  37. FreeEntity cam
  38. EndGraphics
  39. End
  40. Type Mirror
  41. Field plane
  42. Field camera
  43. Field texture
  44. End Type
  45. ;Add a mirror to the scene
  46. Function Create_Mirror(x#,y#,z#,size#,yrot#)
  47. a.mirror=New mirror
  48. a\plane=CreateMesh()
  49. surf=CreateSurface(a\plane)
  50. ;Add all vertices to the surface. (Simple plane)
  51. AddVertex(surf,1*size,1*size,0,0,0)
  52. AddVertex(surf,-1*size,1*size,0,1,0)
  53. AddVertex(surf,-1*size,-1*size,0,1,1)
  54. AddVertex(surf,1*size,-1*size,0,0,1)
  55. AddTriangle(surf,0,2,1)
  56. AddTriangle(surf,0,3,2)
  57. ;the idea of a mirror is a camera behind the mirror
  58. a\camera=CreateCamera(a\plane)
  59. ;which is rendered to a texture
  60. CameraViewport a\camera,0,0,256,256
  61. a\texture=CreateTexture(256,256)
  62. EntityTexture a\plane,a\texture
  63. ;postion mirror where you want it
  64. PositionEntity a\plane,x,y,z
  65. ;rotate it on y to the given
  66. TurnEntity a\plane,0,yrot,0
  67. End Function
  68. ;Update all mirrors
  69. Function Update_Mirrors(camera)
  70. ox#=EntityX(camera,True);Get the position of the main camera in x
  71. oz#=EntityZ(camera,True);and in z
  72. HideEntity camera ;hide the main camera
  73. For a.mirror=Each mirror ;do updates First
  74. dx#=EntityX(a\camera,True)-ox ;Get the angle between the mirror
  75. dz#=EntityZ(a\camera,True)-oz ;and the main camera.
  76. ang#=ATan2(dx,dz)+EntityYaw(a\plane);taking into account yaw of mirror
  77. mag#=1/(Sqr(dx^2+dz^2)) ;Magnify according to distance.
  78. RotateEntity a\camera,0,180+ang,0 ;rotate the mirrors camera to the correct orientation
  79. CameraZoom a\camera,0.5+mag ;set the magnification of the mirror
  80. Next
  81. UpdateWorld ;Update everything.
  82. For a.mirror=Each mirror ;rendering loop here. (all mirrors)
  83. ShowEntity a\camera ;show the camera
  84. RenderWorld ;render
  85. CopyRect 0,0,256,256,0,0,0,TextureBuffer(a\texture);Copy the render to texture
  86. HideEntity a\camera ;Hide the camera again
  87. Next
  88. ShowEntity camera ;Show Main camera again.
  89. End Function
  90. ;Free up all mirrors here.
  91. Function Free_Mirrors()
  92. For a.mirror=Each mirror
  93. If a\camera FreeEntity a\camera
  94. If a\plane FreeEntity a\plane
  95. If a\texture FreeTexture a\texture
  96. Delete a
  97. Next
  98. End Function