如何获得三角形内点的颜色?

时间:2014-09-14 22:29:33

标签: lua computercraft

我正在做的是为一个名为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

从代码中,我已经验证了这一点(或者&#34;像素&#34;(m.write(&#39;&#39;))在三角形内......
但是如何验证&#34;像素&#34; ??

的颜色

谢谢:)

0 个答案:

没有答案