我正在做的是为一个名为 ComputerCraft 的 Minecraft mod 构建一个类似于 GL 的粗略 API……
我已经得到了几乎所有需要的东西,并且能够在黑色 BG 上绘制一个白色三角形。
我现在要做的是从给定的网格数据中找到“像素”的颜色......
这是我的代码:(LUA)
-- static 3D data (structured with classes not displayed)
local data = triangle.Create(
facepoint.Create(
vector.Create(0.1,0.1,0.0), --position
vector.Create(255,0,0) --color
),
facepoint.Create(
vector.Create(0.9,0.1,0.0),
vector.Create(0,255,0)
),
facepoint.Create(
vector.Create(0.5,0.9,0.0),
vector.Create(0,0,255)
)
)
local m = peripheral.wrap('left') --gets the adv. monitor to draw on
m.setTextScale(0.5) --pseudo-pixels
local w, h = m.getSize()
local pw,ph= 1.0/w, 1.0/h -- 0.0 at top-left
local cr = 1.0/255
local p = vector.Create(0.0,0.0,0.0)
for y=1,h do
local posy = ((y-1)*ph)+(ph*0.5)
for x=1,w do
local posx = ((x-1)*pw)+(pw*0.5)
p = vector.update(posx,posy,0.0)
local t = data
local va,ca,vb,cb,vc,cc = t.a.vert,t.a.color,t.b.vert,t.b.color,t.c.vert,t.c.color
-- Compute vectors
local v0,v1,v2 = vector.Create(vc.x-va.x,vc.y-va.y,0.0), vector.Create(vb.x-va.x,vb.y-va.y,0.0), vector.Create(vp.x-va.x,vp.y-va.y,0.0)
-- Compute dot products
local dot00 = (v0[1]*v0[1])+(v0[2]*v0[2])
local dot01 = (v0[1]*v1[1])+(v0[2]*v1[2])
local dot02 = (v0[1]*v2[1])+(v0[2]*v2[2])
local dot11 = (v1[1]*v1[1])+(v1[2]*v1[2])
local dot12 = (v1[1]*v2[1])+(v1[2]*v2[2])
-- Compute barycentric coordinates
local invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
local u = (dot11 * dot02 - dot01 * dot12) * invDenom
local v = (dot00 * dot12 - dot01 * dot02) * invDenom
if (u >= 0) and (v >= 0) and (u + v < 1) then -- Check if point is in triangle
-- calculate color
else setColor(0,0,0) end
m.write(' ')
end
end
从代码中,我已经验证了点(或“像素”(m.write('')))在三角形内......
但是如何验证“像素”的颜色?
谢谢 :)