lua questions

Jan 1, 2008 at 10:58pm

lua questions

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
++++++++++++++++
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 22 154 311 9109513 jit.gl.lua nehe @file nehe1_displaylist.lua
@autogarbage 1 @autowatch 1;
#P toggle 123 71 15 0;
#P message 123 91 68 9109513 fullscreen $1;
#N vpatcher 30 89 166 253;
#P window setfont “Sans Serif” 9.;
#P newex 50 71 35 9109513 sel 27;
#P newex 50 50 40 9109513 key;
#P outlet 50 93 15 0;
#P connect 1 0 2 0;
#P connect 2 0 0 0;
#P pop;
#P newobj 123 51 33 9109513 p Esc;
#P newex 123 109 70 9109513 jit.window nehe;
#P toggle 21 45 15 0;
#P newex 21 64 57 9109513 qmetro 30;
#P newex 21 85 50 9109513 t b erase;
#P newex 21 111 77 9109513 jit.gl.render nehe;
#P connect 6 0 4 0;
#P connect 7 0 6 0;
#P connect 5 0 7 0;
#P fasten 1 0 0 0 26 106 26 106;
#P fasten 1 1 0 0 66 106 26 106;
#P connect 2 0 1 0;
#P connect 3 0 2 0;
#P window clipboard copycount 9;

#35172
Jan 2, 2008 at 3:24am

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

#119502
Jan 2, 2008 at 3:26am

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

#119503
Jan 2, 2008 at 11:18am

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

#119504
Jan 2, 2008 at 12:51pm

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.

#119505
Jan 2, 2008 at 4:42pm

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

#119506
Jan 2, 2008 at 5:05pm

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
>

#119507
Jan 2, 2008 at 5:33pm

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

#119508
Jan 2, 2008 at 6:11pm

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

#119509
Jan 2, 2008 at 8:52pm

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

#119510
Jan 2, 2008 at 9:31pm

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

#119511

You must be logged in to reply to this topic.