Guest User

vertex

a guest
Jun 28th, 2013
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | None | 0 0
  1.  
  2. --[[
  3. This is a modified vector API to include vertex information for texture mapping
  4. (UV coordinates).
  5.  
  6. ]]--
  7. local vertex = {
  8.         -- note that arithmetic functions return VECTORS- and lose UV info
  9.     add = function( self, o )
  10.         return vector.new(
  11.             self.x + o.x,
  12.             self.y + o.y,
  13.             self.z + o.z
  14.         )
  15.     end,
  16.     sub = function( self, o )
  17.         return vector.new(
  18.             self.x - o.x,
  19.             self.y - o.y,
  20.             self.z - o.z
  21.         )
  22.     end,
  23.     -- This is not true of multiplication
  24.     mul = function( self, m )
  25.         return vertex.new(
  26.             self.x * m,
  27.             self.y * m,
  28.             self.z * m,
  29.             self.u, self.v
  30.         )
  31.     end,
  32.     dot = function( self, o )
  33.         return self.x*o.x + self.y*o.y + self.z*o.z
  34.     end,
  35.     cross = function( self, o )
  36.         return vector.new(
  37.             self.y*o.z - self.z*o.y,
  38.             self.z*o.x - self.x*o.z,
  39.             self.x*o.y - self.y*o.x
  40.         )
  41.     end,
  42.     length = function( self )
  43.         return math.sqrt( self.x*self.x + self.y*self.y + self.z*self.z )
  44.     end,
  45.     normalize = function( self )
  46.         return self:mul( 1 / self:length() )
  47.     end,
  48.     tostring = function( self )
  49.         return self.x..","..self.y..","..self.z..",("..self.u..","..self.v..")"
  50.     end,
  51. }
  52.  
  53. local vmetatable = {
  54.     __index = vertex,
  55.     __add = vertex.add,
  56.     __sub = vertex.sub,
  57.     __mul = vertex.mul,
  58.     __unm = function( v ) return v:mul(-1) end,
  59.     __tostring = vertex.tostring,
  60. }
  61.  
  62. function new( x, y, z, u, v )
  63.     local v = {
  64.         x = x or 0,
  65.         y = y or 0,
  66.         z = z or 0,
  67.         u = u or 0,
  68.         v = v or 0
  69.     }
  70.     setmetatable( v, vmetatable )
  71.     return v
  72. end
Advertisement
Add Comment
Please, Sign In to add comment