Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. // RayCaster is a class for ray casting algorithm.
  2. //
  3. // Members:
  4. // Objs - the list of objects.
  5. // PixelScreen - the screen.
  6. // Cam - the camera.
  7. // Lgts - the lights.
  8. //
  9. type RayCaster struct {
  10. Objs *general.Objects
  11. PixelScreen *screen.Screen
  12. Cam *camera.Camera
  13. Lgts *light.Lights
  14. }
  15.  
  16. // TraceRay is a function to trace a ray through a pixel.
  17. //
  18. // Parameters:
  19. // coloredScreen - the screen to be painted.
  20. // lp - pixel line index.
  21. // cp - pixel column index.
  22. //
  23. // Returns:
  24. // the colored screen painted at that position.
  25. //
  26. func (rcaster *RayCaster) TraceRay(coloredScreen *screen.ColoredScreen, lp, cp int) {
  27. screenPoint := rcaster.PixelScreen.PixelToCamera(cp, lp, 1.0, rcaster.Cam, 0.5, 0.5)
  28. line := entity.ExtractLine(entity.Point{Coordinates: []float64{0, 0, 0}}, screenPoint)
  29. color := make([]int, 3)
  30.  
  31. closestT := math.MaxFloat64
  32. closestObjIdx := -1
  33. // intersecting objects
  34. for objIdx, obj := range rcaster.Objs.ObjList { // iterating through all objects
  35. for _, triangle := range obj.Triangles { // iterating through all triangles of an object
  36. points := make([]entity.Point, 3)
  37. for pi := 0; pi < 3; pi++ { // getting triangle points
  38. points[pi] = obj.Vertices.Points[triangle.Vertices[pi]]
  39. }
  40. t, _, intersected := line.IntersectTriangle(points)
  41. if t >= 1 && intersected {
  42. if t < closestT {
  43. closestT = t
  44. closestObjIdx = objIdx
  45. }
  46. }
  47. }
  48. }
  49. // intersecting lights
  50. lightClosest := false
  51. for _, lgt := range rcaster.Lgts.LightList {
  52. sphere := entity.InitSphere(lgt.LightPosition, lgt.Radius)
  53. ts, intersected := line.IntersectSphere(sphere)
  54. if intersected && (ts[0] >= 1 || ts[1] >= 1) {
  55. if ts[0] <= closestT || ts[1] <= closestT {
  56. lightClosest = true
  57. if ts[0] <= ts[1] && ts[0] >= 1 {
  58. closestT = ts[0]
  59. } else {
  60. closestT = ts[1]
  61. }
  62. }
  63. }
  64. }
  65. if !lightClosest {
  66. if closestObjIdx != -1 {
  67. color = rcaster.Objs.ObjList[closestObjIdx].Color
  68. }
  69. coloredScreen.Colors[lp][cp] = color
  70. } else {
  71. coloredScreen.Colors[lp][cp] = []int{255, 255, 255}
  72. }
  73.  
  74. }
  75.  
  76. // Run is a function to run the ray casting.
  77. //
  78. // Parameters:
  79. // none
  80. //
  81. // Returns:
  82. // the colored screen.
  83. //
  84. func (rcaster *RayCaster) Run() *screen.ColoredScreen {
  85. coloredScreen := screen.InitColoredScreen(rcaster.PixelScreen.Width, rcaster.PixelScreen.Height)
  86. for i := 0; i < rcaster.PixelScreen.Height; i++ {
  87. for j := 0; j < rcaster.PixelScreen.Width; j++ {
  88. rcaster.TraceRay(&coloredScreen, i, j)
  89. }
  90. }
  91. return &coloredScreen
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement