Guest User

Moteur 3D

a guest
Dec 7th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 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,cumul,point,i
  51.     point = Point2D(self.p1.x,self.p1.y)
  52.     dx = self.p2.x - self.p1.x
  53.     dy = self.p2.y - self.p1.y
  54.     point:disp(gc)
  55.     cumul = dx / 2
  56.     point.x = point.x + 1
  57.     while point.x <= self.p2.x do
  58.         cumul = cumul + dy
  59.         if cumul >= dx then
  60.             cumul = cumul - dx
  61.             point.y = point.y + 1
  62.         end
  63.         point:disp(gc)
  64.     end
  65. end
  66.  
  67.  
  68.  
  69. Point3D = class()
  70.  
  71. function Point3D:init(x,y,z)
  72.     self.x = x
  73.     self.y = y
  74.     self.z = z
  75.     self.p2d = Point2D(0,0)
  76.     self.zoom = 1
  77.     self.inscreen = true
  78.     self.colour = {0,0,0}
  79. end
  80.  
  81. function Point3D:to2D()
  82.     local t = (screen:width() / 2) / (self.z + 5)
  83.     self.p2d.x = self.x * t + (screen:width() / 2)
  84.     self.p2d.y = self.y * t + (screen:height() / 2)
  85. end
  86.  
  87. function Point3D:isInScreen()
  88.     self.p2d:isInScreen()
  89.     self.inscreen = self.p2d.inscreen
  90. end
  91.  
  92. function Point3D:disp(gc)
  93.     self:isInScreen()
  94.     self:to2D()
  95.     if self.inscreen then
  96.         gc:setColorRGB(RGB(self.colour))
  97.         gc:fillRect(self.p2d.x - ((self.zoom - 1) / 2),self.p2d.y - ((self.zoom - 1) / 2),self.zoom,self.zoom)
  98.     end
  99. end
  100.  
  101. Line3D = class()
  102.  
  103. function Line3D:init(x1,y1,z1,x2,y2,z2)
  104.     self.point1 = Point3D(x1,y1,z1)
  105.     self.point2 = Point3D(x2,y2,z2)
  106.     self.inscreen = true
  107.     self.colour = {0,0,0}
  108.     self.zoom = 1
  109. end
  110.  
  111. function Line3D:to2D()
  112.     self.point1:to2D()
  113.     self.point2:to2D()
  114. end
  115.  
  116. function Line3D:disp(gc)
  117.     self:to2D()
  118.     line2d = Line2D(self.point1.p2d.x, self.point1.p2d.y, self.point2.p2d.x, self.point2.p2d.y)
  119.     line2d:disp(gc)
  120. end
  121.  
  122.  
  123.  
  124. --main
  125.  
  126. 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}
  127. 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}
  128. 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}
  129.  
  130. local k = 1
  131.  
  132. function on.paint(gc)
  133.     local line = Line3D(lx[k],ly[k],lz[k],lx[k+1],ly[k+1],lz[k+1])
  134.     line:disp(gc)
  135. end
  136.  
  137. function on.charIn(ch)
  138.     if ch == '+' and k < 23 then
  139.         k = k + 2
  140.     end
  141.     screen:invalidate()
  142. end
Add Comment
Please, Sign In to add comment