Volume Rendering and 2 bugs

I tried to create a callback loop for loading images with http.request, but it always seems to return the first image returned all throuh the loop. Also video recording doesn’t work in my super naive volume rendering. Here is the code and an image:

https://dl.dropbox.com/s/utq1axqwd9x2irt/Photo%202013-03-19%2021%2026%2021.jpg

The dataset is from http://www-graphics.stanford.edu/data/voldata/


--# Main
-- Slices

-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    R = 0
    v = Volume()
end

-- This function gets called once every frame
function draw()
    background(40, 40, 50)
    perspective(45)
    camera(0,0,600,0,0,0)
    rotate(R,0,1,0)
    v:draw()
end

function touched(touch)
    R = R + touch.deltaX
end

--# Volume
Volume = class()

function Volume:init(x)
    self.ms = {}    
    local sq = {
        vec3(0,0,0), vec3(1,0,0),
        vec3(1,1,0), vec3(1,1,0),
        vec3(0,1,0), vec3(0,0,0)      
    }
    local dz = 1.3
    local hd = 100*dz/2
    for i=1,99 do
        local m = mesh()
        m.shader = VolumeShader
        self:loadSlice(m, i)
        local vs = {}
        for j,v in ipairs(sq) do
            table.insert(vs, (v - vec3(.5,.5,0))*256 +
                vec3(0,0,i*dz - hd))
        end
        m.vertices = vs
        m.texCoords = sq       
        table.insert(self.ms, m)
    end 
    -- self:loadSlices(10) -- doesnt work, but in http request?
end

function Volume:loadSlice(m, i)
    local id = i
    if i < 10 then id = "00" .. id
    else id = "0" .. id end
    m.texture = "Dropbox:slices/mrbrain-8bit" .. id
end
function Volume:loadSlices(i)
    local id = i
    if i < 10 then id = "00" .. id
    else id = "0" .. id end
    local url = "https://dl.dropbox.com/s/ca9rgqr47iex53b/mrbrain-8bit"
    http.request(url..id..".png", function(data)
        self.ms[i].texture = data
        if i < 20 then
            self:loadSlices(i+1)
        end
    end)    
end

function Volume:draw()
    for i,v in ipairs(self.ms) do
        v:draw()
    end    
end


--# VolumeShader
VolumeShader = shader()
VolumeShader.vertexProgram = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

void main()
{
    vColor = color;
    vTexCoord = vec2(texCoord.x, texCoord.y);
    gl_Position = modelViewProjection * position;
}
]]
VolumeShader.fragmentProgram = [[
uniform lowp sampler2D texture;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

void main()
{
    lowp vec4 col = texture2D( texture, vTexCoord );
    if(col.r < .1) discard;
    gl_FragColor = col;
}
]]

Awesome! You’re really rolling out this 3D stuff. Cool!

Now recording works, will try to figure out what the difference is.
Edit: Seems like if I remove the line “vColor = color;” then video recording works again.

code which only load half a head since there is a http.request bug: https://gist.github.com/tnlogy/5219140

http://www.youtube.com/watch?v=SOtriszSUac