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

cleaned up shape.lua

parent 87cc341d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -4,28 +4,38 @@ require("kara3d.quaternion") ...@@ -4,28 +4,38 @@ require("kara3d.quaternion")
require("kara3d.transform") require("kara3d.transform")
require("utils") require("utils")
-- ********** DEFINITION AND GENERIC FUNCTIONS **********
shape = {class = "shape"} shape = {class = "shape"}
shape.__index = shape shape.__index = shape
face = {class = "face"} face = {class = "face"}
face.__index = face face.__index = face
function face.new(v, n, t) -- Creates a new shape with transform t and faces f
local f = {vertices = v or {}, normal = n or -vector.cardinal(3, 3), tags = t}
setmetatable(f, face)
return f
end
function shape.new(t, f) function shape.new(t, f)
local s = {transform = t, faces = f or {}} local s = {transform = t, faces = f or {}}
setmetatable(s, shape) setmetatable(s, shape)
return s return s
end end
-- Creates a new face with vertices c, normal n and tags t
function face.new(v, n, t)
local f = {vertices = v or {}, normal = n or -vector.cardinal(3, 3), tags = t}
setmetatable(f, face)
return f
end
-- Clones this shape
function shape:clone() function shape:clone()
return shape.new(self.transform, table.copy(self.faces)) return shape.new(self.transform, table.copy(self.faces))
end end
-- ********** INSTANCE FUNCTIONS **********
-- Generates a line for each face of this shape, with the indicated tags
-- The boolean cull determines if the faces facing away from the screen are
-- drawn or not
function shape:draw(subs, line, tags, cull) function shape:draw(subs, line, tags, cull)
local l = table.copy(line) local l = table.copy(line)
local p = self.transform:position(true) local p = self.transform:position(true)
...@@ -38,7 +48,7 @@ function shape:draw(subs, line, tags, cull) ...@@ -38,7 +48,7 @@ function shape:draw(subs, line, tags, cull)
local face = self.faces[i] local face = self.faces[i]
local n = quaternion.mul_vector(r, face.normal) local n = quaternion.mul_vector(r, face.normal)
if not (cull and n:z() <= 0) then if not (cull and n:z() > 0) then
local vs = face.vertices local vs = face.vertices
local v = matrix.mul_vector(m, vs[#vs] + c) - p local v = matrix.mul_vector(m, vs[#vs] + c) - p
...@@ -65,14 +75,16 @@ function shape:draw(subs, line, tags, cull) ...@@ -65,14 +75,16 @@ function shape:draw(subs, line, tags, cull)
return "" return ""
end end
--[[ -- ********** BASIC SHAPE FUNCTIONS **********
1________5
/| /| -- Returns a cube shape of side length size, that follows the transform t
2/__|____/6 | Y -- Schematic of the vertices:
| 3|_ _|_ _|7 |__ X -- 1________5
| / | / / -- /| /|
4|/______|/8 Z -- 2/__|____/6 |
]]-- -- | 3|_ _|_ _|7 __ X
-- | / | / /|
-- 4|/______|/8 Z Y
function shape.cube(t, size) function shape.cube(t, size)
local hs = size / 2 local hs = size / 2
local vs = {vector.new(3, {-hs, -hs, -hs}), vector.new(3, {-hs, -hs, hs}), local vs = {vector.new(3, {-hs, -hs, -hs}), vector.new(3, {-hs, -hs, hs}),
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter