XML Feeds

.

[jitter] lua questions

Wesley Smith wesley.hoke at gmail.com
Tue Jan 1 20:24:06 MST 2008


Hi Yair,
Welcome to Lua land.  I dig the feedback spiral.  The screentoworld
function is basically doing gluUnproject.  See the doc here:
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/gluUnProject.3.html

The 0 for Z depth in window coordinates is mapped to the near clip
plane value and 1 to the far clip value.  Z depth is non-linear and
thus annoyingly not intuitive to our typical sense of parameter values
mapping to euclidean coordinates.

As for displaylists, these things have to be created when there is a
valid context and _must_ be rebuilt when that context changes like
when you go fullscreen.  In jit.gl.lua, there are a few functions
defined to notify a script of things like context changes or scripts
loading happen. These are:

script_load
dest_changed
dest_closing
etc.
see the PDF doc for a complete list

So, to properly handle displaylists, You need to create it whenever
the context changes or your script reloads with a valid context.
jit.gl.lua has an attr called context which is 0 when there is no
valid context and non-zero when there is a valid context (it is also
unique per-context).

Here's a slight mod I made to your script (full script is further
below) using some of the nice aspects of having functions as
variables:

-- functions as variables!
function setOnReloadOrDestChanged(func)
	script_load = func
	det_changed = func
end
setOnReloadOrDestChanged(init)

--[[
This section is equivalent to the above

function script_load()
	init()
end

function dest_changed()
	init()
end
--]]





+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Full script
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
 autowatch = 1
print("compiled")
render_context = this.drawto
angle=0
mouseXY={0,0}
dir=1



function BuildLists()

    box = gl.GenLists(2)
    gl.NewList(box    ,"COMPILE");
        gl.Begin("QUADS")
            gl.Vertex (-0.5 , 0.5 , 0. )                -- Top Left
            gl.Vertex ( 0.5 , 0.5 , 0. )                -- Top Right
            gl.Vertex ( 0.5 ,-0.5 , 0. )            -- Bottom Right
            gl.Vertex (-0.5 ,-0.5 , 0. )
        gl.End();
    gl.EndList()
end
function init()
--initialize the dispaly list
	if(this.context ~= 0) then
	    BuildLists()
	end
end


-- functions as variables!
function setOnReloadOrDestChanged(func)
	script_load = func
	det_changed = func
end
setOnReloadOrDestChanged(init)

--[[
This section is equivalent to the above

function script_load()
	init()
end

function dest_changed()
	init()
end
--]]

function draw()
    gl.Clear("COLOR_BUFFER_BIT" , "DEPTH_BUFFER_BIT")
    --gl.LoadIdentity()
    --gl.Enable("BLEND")
    --gl.BlendFunc(1,6)
    --gl.ClearColor(1,1,0,1)
    rot()
    for i=0,1000 do
    gl.Translate(mouseXY[1],mouseXY[2],(i/1000)-1);
    gl.Rotate(angle,0,0,1)
    gl.Color(i/1000, math.random(), 1, i/1000)
    gl.CallList(box)
    end

end

function rot()
    angle = angle+dir
    if angle==15 then dir= -0.5
    elseif angle==0 then dir= 0.5
    end
end

--create the listener callback function
function callback(event)
    if(event.eventname == "mouseidle") then
        local _glXmouse = event.args[1]
        local _glYmouse = event.args[2]
        --need to transform mouse ccordinates to world domain
        --dono why, but 0.95 needed
        mouseXY=jit.gl.screentoworld({_glXmouse,_glYmouse, 0.95})
    end
end

--create a listener object
listener = jit.listener(render_context, "callback")


More information about the jitter mailing list