Advertisement
neuronix

Moteur 3D

Dec 7th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local screen = platform.window
  2.  
  3. local RGB = unpack
  4.  
  5. function addToGC(name, func)
  6.     local tab = platform.withGC(getmetatable)
  7.     tab[name] = func
  8. end
  9.  
  10. Point2D = class()
  11.  
  12. function Point2D:init(x,y)
  13.     self.x = x
  14.     self.y = y
  15.     self.zoom = 1
  16.     self.colour = {0,0,0}
  17.     self.inscreen = true
  18. end
  19.  
  20. function Point2D:isInScreen()
  21.     if self.x >= 0 and self.x < screen.width() and self.y >= 0 and self.y < screen.height() then
  22.         self.inscreen = true
  23.     else
  24.         self.inscreen = false
  25.     end
  26. end
  27.  
  28. function Point2D:disp(gc)
  29.     self:isInScreen()
  30.     if self.inscreen then
  31.         gc:setColorRGB(RGB(self.colour))
  32.         gc:fillRect(self.x - ((self.zoom - 1) / 2), self.y - ((self.zoom - 1) / 2), self.zoom, self.zoom)
  33.     end
  34. end
  35.  
  36. Line2D = class()
  37.  
  38. function Line2D:init(x1,y1,x2,y2)
  39.     self.p1 = Point2D(x1,y1)
  40.     self.p2 = Point2D(x2,y2)
  41.     self.colour = {0,0,0}
  42.     self.inscreen = true
  43. end
  44.  
  45. function Line2D:isInScreen()
  46.     --a faire
  47. end
  48.  
  49. function Line2D:disp(gc)
  50.     local dx,dy,err,e2,sx,sy
  51.     dx = math.abs(self.p2.x-self.p1.x)
  52.     if self.p1.x < self.p2.x then sx = 1 else sx = -1 end
  53.     dy = -math.abs(self.p2.y-self.p1.y)
  54.     if self.p1.y < self.p2.y then sy = 1 else sy = -1 end
  55.     err = dx + dy
  56.     local point = Point2D(self.p1.x,self.p1.y)
  57.     while point.x ~= self.p2.x or point.y ~= self.p2.y do
  58.         point:disp(gc)
  59.         e2 = 2*err
  60.         if e2 >= dy then
  61.             err = err + dy
  62.             point.x = point.x + sx
  63.         end
  64.         if e2 <= dx then
  65.             err = err + dx
  66.             point.y = point.y + sy
  67.         end
  68.     end
  69. end
  70.  
  71.  
  72.  
  73. Point3D = class()
  74.  
  75. function Point3D:init(x,y,z)
  76.     self.x = x
  77.     self.y = y
  78.     self.z = z
  79.     self.p2d = Point2D(0,0)
  80.     self.zoom = 1
  81.     self.inscreen = true
  82.     self.colour = {0,0,0}
  83. end
  84.  
  85. function Point3D:to2D()
  86.     local t = (screen:width() / 2) / (self.z + 5)
  87.     self.p2d.x = self.x * t + (screen:width() / 2)
  88.     self.p2d.y = self.y * t + (screen:height() / 2)
  89. end
  90.  
  91. function Point3D:isInScreen()
  92.     self.p2d:isInScreen()
  93.     self.inscreen = self.p2d.inscreen
  94. end
  95.  
  96. function Point3D:disp(gc)
  97.     self:isInScreen()
  98.     self:to2D()
  99.     if self.inscreen then
  100.         gc:setColorRGB(RGB(self.colour))
  101.         gc:fillRect(self.p2d.x - ((self.zoom - 1) / 2),self.p2d.y - ((self.zoom - 1) / 2),self.zoom,self.zoom)
  102.     end
  103. end
  104.  
  105. Line3D = class()
  106.  
  107. function Line3D:init(x1,y1,z1,x2,y2,z2)
  108.     self.point1 = Point3D(x1,y1,z1)
  109.     self.point2 = Point3D(x2,y2,z2)
  110.     self.inscreen = true
  111.     self.colour = {0,0,0}
  112.     self.zoom = 1
  113. end
  114.  
  115. function Line3D:to2D()
  116.     self.point1:to2D()
  117.     self.point2:to2D()
  118. end
  119.  
  120. function Line3D:disp(gc)
  121.     self:to2D()
  122.     line2d = Line2D(self.point1.p2d.x, self.point1.p2d.y, self.point2.p2d.x, self.point2.p2d.y)
  123.     line2d:disp(gc)
  124. end
  125.  
  126.  
  127.  
  128. --main
  129.  
  130. lx = {-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,1,1,1,1,-1}
  131. ly = {-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,1,-1,1,-1,1,1,1,1,1,1,1,1,1}
  132. lz = {-1,1,1,1,1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,1,1,1,1,-1,-1,-1}
  133.  
  134. function on.paint(gc)
  135.     for k = 1, (#lx - 1) do
  136.         line = Line3D(lx[k],ly[k],lz[k],lx[k+1],ly[k+1],lz[k+1])
  137.         line:disp(gc)
  138.     end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement