Skip to content
Extraits de code Groupes Projets
Valider 1d8b4426 rédigé par ultrakatiz's avatar ultrakatiz
Parcourir les fichiers

added more flexibility to constructors

parent e1b21567
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -24,9 +24,8 @@ end
-- Returns this camera's projection matrix
function camera:projection()
local xres, yres, _, _ = aegisub.video_size()
local sx = 1 / math.atan(self.fov / 2)
local sy = self.aspect / math.atan(self.fov / 2)
local sy = self.aspect * sx
local n, f = self.near, self.far
local m = matrix.new(4, 4,
{sx, 0, 0, 0,
......
Ce diff est replié.
......@@ -11,7 +11,9 @@ light.__index = light
-- Creates a new light with transform t and RGB value diff and spec
function light.new(t, diff, spec)
local l = {transform = t, diffuse = diff or vector.new(3, {255, 255, 255}), specular = spec or vector.new(3, {255, 255, 255})}
diff = diff and vector.new(diff) or 255 * vector.one(3)
spec = spec and vector.new(spec) or 255 * vector.one(3)
local l = {transform = t, diffuse = diff, specular = spec}
setmetatable(l, light)
return l
end
......@@ -20,5 +22,5 @@ end
-- Clones this light
function light:clone()
return light.new(self.transform, table.copy(self.diffuse), table.copy(self.specular))
return light.new(self.transform, self.diffuse:clone(), self.specular:clone())
end
......@@ -19,7 +19,7 @@ end
-- Clones this matrix
function matrix:clone()
return matrix.new(self.rows, self.cols, self.values)
return matrix.new(self.rows, self.cols, table.copy(self.values))
end
-- Returns a string representing the matrix
......@@ -175,3 +175,7 @@ function matrix.__eq(m1, m2)
return true
end
function matrix.__pow(m)
return m * m
end
......@@ -7,8 +7,18 @@ quaternion = {class = "quaternion"}
quaternion.__index = quaternion
-- Creates the quaternion ai + bj + dk + d
-- If a is a quaternion, clones it
-- If a is a vector, takes its first four values (0 if overflow)
-- If a is a table, takes its first four values (0 if overflow)
function quaternion.new(a, b, c, d)
local q = {x = a or 0, y = b or 0, z = c or 0, w = d or 1}
if (type(a) == "table") then
if (getmetatable(a) == quaternion) then
a, b, c, d = a:unpack()
elseif(getmetatable(a) == vector) then
d = a:get(4) c = a:get(3) b = a:get(2) a = a:get(1)
else d = a[4] c = a[3] b = a[2] a = a[1] end
end
local q = {x = a or 0, y = b or 0, z = c or 0, w = d or 0}
setmetatable(q, quaternion)
return q
end
......@@ -140,3 +150,20 @@ function quaternion:to_matrix()
0, 0, 0, 1})
return m
end
-- ********** OPERATOR OVERLOADING **********
-- * binary operator overload
-- Returns either a quaternion which is q * o if o is a quaternion,
-- or rotates o by q if o is a vector
function quaternion.__mul(q, o)
if (getmetatable(o) == quaternion) then return quaternion.mul_quaternion(q, o)
elseif (getmetatable(o) == vector) then return quaternion.mul_vector(q, o)
else return o end
end
-- == binary operator overlaod
-- Returns "coordinates of these quaternions are all equal"
function quaternion.__eq(q1, q2)
return q1.x == q2.x and q1.y == q2.y and q1.z == q2.z and q1.w == q2.w
end
......@@ -21,9 +21,9 @@ material.__index = material
function material.new(amb, diff, spec, sh, al)
local m = {ambient = vector.new(3, amb) or vector.new(3, {1, 1, 1}),
diffuse = vector.new(3, diff) or vector.new(3, {1, 1, 1}),
specular = vector.new(3, spec) or vector.new(3, {1, 1, 1}),
local m = {ambient = amb and vector.new(amb) or vector.one(3),
diffuse = diff and vector.new(diff) or vector.one(3),
specular = spec and vector.new(spec) or vector.one(3),
shininess = sh or 1,
alpha = al or 0}
setmetatable(m, material)
......@@ -134,13 +134,15 @@ function shape:draw(subs, line, tags, cull, cam, ambient, lights)
face_center = (1 / #vs) * face_center
if (ambient or lights) then
local color = face.shade(n, face_center, self.material, lights, ambient, cam)
local colorstr = string.format("{\\c%s\\alpha%s}",
ass_color(color:x(), color:y(), color:z()),
ass_alpha(self.material.alpha)
)
l.text = colorstr .. l.text
end
-- layers must be between 0 and 9999999999
l.layer = math.floor(-face_center:z())
l.effect = "fx"
......
......@@ -10,14 +10,19 @@ transform.__index = transform
-- Creates a new transform with position p, rotation r, scale s and parent par
-- p and s should be of size 3, r is a quaternion, and par may be nil
function transform.new(p, r, s, par)
p = p or vector.zero(3)
r = r or quaternion.identity()
s = s or vector.one(3)
p = p and vector.new(p) or vector.zero(3)
r = r and quaternion.new(r) or quaternion.identity()
s = s and vector.new(s) or vector.one(3)
local t = {pos = p, rot = r, scl = s, parent = par}
setmetatable(t, transform)
return t
end
-- Clones this transform
function transform:clone()
return transform.new(self.pos:clone(), self.rot:clone(), self.scl:clone(), self.parent)
end
-- Returns a string that represents this transform
function transform:tostring()
return "(pos = (" .. self.pos:tostring() .. "), "
......
......@@ -5,8 +5,13 @@ vector.__index = vector
-- Creates a vector of size n, initialized with values vals
-- Uninitialized values are set to 0
-- If first argument n is a table, then vals becomes n and n becomes #n
function vector.new(n, vals)
if (type(n) == "table") then vals = n n = #n end
if (type(n) == "table") then
if (getmetatable(n) == vector) then
vals = table.copy(n.values) n = n.size
else vals = n n = #n end
end
vals = vals or {}
local v = {size = n or 3, values = {}}
for i = 1, v.size do
......@@ -17,7 +22,7 @@ function vector.new(n, vals)
end
-- Clones this vector
function vector:clone() return vector.new(self.size, self.values) end
function vector:clone() return vector.new(self.size, table.copy(self.values)) end
-- Returns this vector's coordinates, separated by commas
function vector:tostring()
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter