0

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 :)

Tcll
  • 7,140
  • 1
  • 20
  • 23
  • You can use [Barycentric coordinates](http://en.wikipedia.org/wiki/Barycentric_coordinate_system), which you may already be computing depending on your implementation of `PointInTriangle`. Multiply each vertex's color by its corresponding barycentric coordinate, then sum them to get the interpolated color. – Colonel Thirty Two Sep 15 '14 at 16:39
  • ok, yea, so just run the same implementation using colors instead of vertices right?? (color alone) I will try that :) – Tcll Sep 16 '14 at 17:26
  • might need a bit of help though (mainly with the visual understanding) >.> – Tcll Sep 16 '14 at 17:45
  • ^ found a resource to help me out here: http://www.cs.virginia.edu/~gfx/Courses/2012/IntroGraphics/lectures/12-BarycentricCoords.pdf – Tcll Sep 16 '14 at 17:59
  • yeh, that didn't help much, I'm still just as confused as ever... I've rolled back and edited my code with the contents of the function, hope that'll help clear a few loose ends. :) – Tcll Sep 16 '14 at 18:55
  • ok, this is helping me out alot better: http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-9-ray-triangle-intersection/barycentric-coordinates/ I'll post my code for the answer once I get it working ;) – Tcll Sep 16 '14 at 19:08
  • just need to know if ray.dir (on the site) is the same as p (my code)?? – Tcll Sep 16 '14 at 19:37

0 Answers0