lua questions


    Jan 01 2008 | 10:58 pm
    hi, all (err... wesly) i have been doing some lua coding on an xp machine.
    i have a few questions, 1. something strange about the screentoworld routine, when z is set to 0. or 1. i get unusable results. when at 0.95 its almost there, why? 2. i use display lists, is that good? were do i plug the gl states in that situation. i want blend effects. i commented the lines i think i need. 3. when i enter fullscreen i lose the drawing.
    thanks , it's fun coding in lua. and i feel can tackle some of nehe's opengl tutorial with it. posting on the list so i hope it will not break in the forum.
    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 BuildLists() end init()
    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")
    ++++++++++++++++++ patch ++++++++++++++++

    • Jan 02 2008 | 3:24 am
      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")
    • Jan 02 2008 | 3:26 am
      Sorry, there was a slight typo in my script in the last post. Here's a fixed version:
      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 dest_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")
    • Jan 02 2008 | 11:18 am
      thanks wes, lua is so flexible
      i'm still having difficulties with the placement of gl.Enable("BLEND") gl.BlendFunc(1,6) gl.ClearColor(0,0,0,0.5) my intuition tells me it should be in the init function, but no go.
      On Jan 2, 2008 5:26 AM, Wesley Smith wrote:
      > Sorry, there was a slight typo in my script in the last post. Here's > a fixed version: > > > 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 > dest_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") >
    • Jan 02 2008 | 12:51 pm
      try this:
      gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA)
      like the redbook says, but without the GL_ http://www.cs.rutgers.edu/~decarlo/428/gl_man/blendfunc.html
      hth, erik
      Quote: yair r. wrote on Wed, 02 January 2008 04:18 ---------------------------------------------------- > thanks wes, lua is so flexible > > i'm still having difficulties with the placement of > gl.Enable("BLEND") > gl.BlendFunc(1,6) > gl.ClearColor(0,0,0,0.5) > my intuition tells me it should be in the init function, but no go.
    • Jan 02 2008 | 4:42 pm
      On Jan 2, 2008 4:51 AM, Erik Mijwaard wrote: > > > try this: > > gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA) >
      Indeed, this is the way to go. The Jitter constants for blend functions just enumerate the list of enums. In jit.gl.lua, the enums have become strings corresponding to the name of the enum. BTW, if you get errors from the ONE enum and you have the latest version of jit.gl.lua for your platform, let me know.
      wes
    • Jan 02 2008 | 5:05 pm
      ye, i got an error. im on beta4 ob3d_draw_preamble initial: GL Error: Invalid enumeration
      function init() --initialize the dispaly list if(this.context ~= 0) then BuildLists() gl.Enable("BLEND") gl.BlendFunc("ONE", "ONE_MINUS_SRC_ALPHA") --gl.ClearColor(1,1,0,1) end end
      On Jan 2, 2008 6:42 PM, Wesley Smith wrote:
      > On Jan 2, 2008 4:51 AM, Erik Mijwaard wrote: > > > > > > try this: > > > > gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA) > > > > Indeed, this is the way to go. The Jitter constants for blend > functions just enumerate the list of enums. In jit.gl.lua, the enums > have become strings corresponding to the name of the enum. BTW, if > you get errors from the ONE enum and you have the latest version of > jit.gl.lua for your platform, let me know. > > wes >
    • Jan 02 2008 | 5:33 pm
      ok, I'll have an update soon. I'm assuming you're on windows?
      wes
      On Jan 2, 2008 9:05 AM, yair reshef wrote: > ye, i got an error. im on beta4 > ob3d_draw_preamble initial: GL Error: Invalid enumeration > > > function init() > --initialize the dispaly list > if(this.context ~= 0) then > BuildLists() > gl.Enable ("BLEND") > > gl.BlendFunc("ONE", "ONE_MINUS_SRC_ALPHA") > --gl.ClearColor(1,1,0,1) > end > end > > > > > On Jan 2, 2008 6:42 PM, Wesley Smith < wesley.hoke@gmail.com> wrote: > > > > > > > > > > On Jan 2, 2008 4:51 AM, Erik Mijwaard < erik@videotroopers.com> wrote: > > > > > > > > > try this: > > > > > > gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA) > > > > > > > Indeed, this is the way to go. The Jitter constants for blend > > functions just enumerate the list of enums. In jit.gl.lua, the enums > > have become strings corresponding to the name of the enum. BTW, if > > you get errors from the ONE enum and you have the latest version of > > jit.gl.lua for your platform, let me know. > > > > wes > > > > > > > > > > > > > >
    • Jan 02 2008 | 6:11 pm
      ok, I've updated to beta4.1 with a new windows external fixing the ONE enum problem. Let me know if you still have issues. wes
      On Jan 2, 2008 9:33 AM, Wesley Smith wrote: > ok, I'll have an update soon. I'm assuming you're on windows? > > wes > > > On Jan 2, 2008 9:05 AM, yair reshef wrote: > > ye, i got an error. im on beta4 > > ob3d_draw_preamble initial: GL Error: Invalid enumeration > > > > > > function init() > > --initialize the dispaly list > > if(this.context ~= 0) then > > BuildLists() > > gl.Enable ("BLEND") > > > > gl.BlendFunc("ONE", "ONE_MINUS_SRC_ALPHA") > > --gl.ClearColor(1,1,0,1) > > end > > end > > > > > > > > > > On Jan 2, 2008 6:42 PM, Wesley Smith < wesley.hoke@gmail.com> wrote: > > > > > > > > > > > > > > > On Jan 2, 2008 4:51 AM, Erik Mijwaard < erik@videotroopers.com> wrote: > > > > > > > > > > > > try this: > > > > > > > > gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA) > > > > > > > > > > Indeed, this is the way to go. The Jitter constants for blend > > > functions just enumerate the list of enums. In jit.gl.lua, the enums > > > have become strings corresponding to the name of the enum. BTW, if > > > you get errors from the ONE enum and you have the latest version of > > > jit.gl.lua for your platform, let me know. > > > > > > wes > > > > > > > > > > > > > > > > > > > > > > > >
    • Jan 02 2008 | 8:52 pm
      the "one" enumeration error disappeared but i tried the others and "ZERO" gave the error.
      On Jan 2, 2008 8:11 PM, Wesley Smith wrote:
      > ok, I've updated to beta4.1 with a new windows external fixing the ONE > enum problem. Let me know if you still have issues. > wes > > On Jan 2, 2008 9:33 AM, Wesley Smith wrote: > > ok, I'll have an update soon. I'm assuming you're on windows? > > > > wes > > > > > > On Jan 2, 2008 9:05 AM, yair reshef wrote: > > > ye, i got an error. im on beta4 > > > ob3d_draw_preamble initial: GL Error: Invalid enumeration > > > > > > > > > function init() > > > --initialize the dispaly list > > > if(this.context ~= 0) then > > > BuildLists() > > > gl.Enable ("BLEND") > > > > > > gl.BlendFunc("ONE", "ONE_MINUS_SRC_ALPHA") > > > --gl.ClearColor(1,1,0,1) > > > end > > > end > > > > > > > > > > > > > > > On Jan 2, 2008 6:42 PM, Wesley Smith < wesley.hoke@gmail.com> wrote: > > > > > > > > > > > > > > > > > > > > On Jan 2, 2008 4:51 AM, Erik Mijwaard < erik@videotroopers.com> > wrote: > > > > > > > > > > > > > > > try this: > > > > > > > > > > gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA) > > > > > > > > > > > > > Indeed, this is the way to go. The Jitter constants for blend > > > > functions just enumerate the list of enums. In jit.gl.lua, the > enums > > > > have become strings corresponding to the name of the enum. BTW, if > > > > you get errors from the ONE enum and you have the latest version of > > > > jit.gl.lua for your platform, let me know. > > > > > > > > wes > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
    • Jan 02 2008 | 9:31 pm
      argh. thanks for checking. I had tried doing some "fancy" scripting on the enum definitions from GLEW and apparently there are several definitions of ZERO and ONE. I'll get this patched up with a full update in a few days.
      wes
      On Jan 2, 2008 12:52 PM, yair reshef wrote: > the "one" enumeration error disappeared but i tried the others and "ZERO" > gave the error. > > > > > > On Jan 2, 2008 8:11 PM, Wesley Smith < wesley.hoke@gmail.com> wrote: > > ok, I've updated to beta4.1 with a new windows external fixing the ONE > > enum problem. Let me know if you still have issues. > > wes > > > > > > > > > > On Jan 2, 2008 9:33 AM, Wesley Smith wrote: > > > ok, I'll have an update soon. I'm assuming you're on windows? > > > > > > wes > > > > > > > > > On Jan 2, 2008 9:05 AM, yair reshef wrote: > > > > ye, i got an error. im on beta4 > > > > ob3d_draw_preamble initial: GL Error: Invalid enumeration > > > > > > > > > > > > function init() > > > > --initialize the dispaly list > > > > if(this.context ~= 0) then > > > > BuildLists() > > > > gl.Enable ("BLEND") > > > > > > > > gl.BlendFunc("ONE", "ONE_MINUS_SRC_ALPHA") > > > > --gl.ClearColor(1,1,0,1) > > > > end > > > > end > > > > > > > > > > > > > > > > > > > > On Jan 2, 2008 6:42 PM, Wesley Smith < wesley.hoke@gmail.com> wrote: > > > > > > > > > > > > > > > > > > > > > > > > > On Jan 2, 2008 4:51 AM, Erik Mijwaard < erik@videotroopers.com > > wrote: > > > > > > > > > > > > > > > > > > try this: > > > > > > > > > > > > gl.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA) > > > > > > > > > > > > > > > > Indeed, this is the way to go. The Jitter constants for blend > > > > > functions just enumerate the list of enums. In jit.gl.lua, the > enums > > > > > have become strings corresponding to the name of the enum. BTW, if > > > > > you get errors from the ONE enum and you have the latest version of > > > > > jit.gl.lua for your platform, let me know. > > > > > > > > > > wes > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >