What I'm doing is building a rough GL-like API for a Minecraft mod called ComputerCraft...
I've gotten pretty much everything needed and was able to draw a white triangle on a black BG.
What I'm trying to do now is find the color for the "pixel" from the mesh data given...
here's my code: (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
from the code, I've already validated the point (or "pixel" (m.write(' ')) is within the triangle...
but how do I validate the color for the "pixel"??
thanks :)