Forums > Jitter

Jit.gl.lua: how to bind a texture?


February 11, 2007 | 9:52 pm

Hello List,

I am new here, reading quite a few years the list and taking big advantage of it, great resource, hope you can help me with the following.

Last week I started to discover jit.gl.lua. I read the reference, opened and editted some of the examples and I think I will rewrite some of my patches using Lua to get rid of some JS and jit.gl.sketch-spagetti. There is only one basic issue that I can’t understand, and that is how to simply bind a texture to a rectangle.

With jit.gl.sketch I used to do it like this, assuming that i have a jit.gl.texture object called ‘mytexture’ in my patch.

glenable texture,
glbindtexture mytexture,
plane 0.5,
glunbindtexture

Easy, works like a charm.

After reading the Lua-manual and the LuaGL-reference I would think it should be something like this in Lua:

gl.Enable("TEXTURE");
gl.BindTexture("TEXTURE", mytexture); — or should i use jit.gl.bindtexture instead?
gl.Rect(0., -0.5, 0.5, 0.)
gl.Disable("TEXTURE")

This gives me all kinds of errors (Enumeration, nil values) , i tried many different ways to write the code down, all just gave me a rectangle without a texture.

After taking a look at the red and bluebook I found out that the second argument in gl.bindtexture has to be an imageArray and thats were I get lost, because I suppose there’s an easier way to accomplish this. I dont know where to find the answer, is it the Lua-syntax which is a bit different than openGL, is it openGL I should dive into or am I missing something very obvious here? Your help is very welcome, if you need a sample-patch I will submit it.

Best regards,
Erik

February 12, 2007 | 6:06 am

Hi Erik,
Yes, for texture stuff in jit.gl.lua, use jit.gl.bindtexture. There
are a few reasons I designed it this way. Using standard OpenGL calls
is a little problematic in Lua because they don’t by nature accept
jit.matrix objects but raw pixel buffers and jit.gl.texture already
has all of the facilities for handling jit.matrix objects built in.
Jitter has some texture commands available in jit.gl.drawinfo.h which
is where jit.gl.bindtexture comes from. This command allows you to
bind textures without having to attach them to a specific jit.gl
object. The arguments are the jit.gl.texture object’s name and the
texture unit you’re binding the texture to which is generally 0.
Also, don’t forget to follow up every jit.gl.bindtexture call with a
jit.gl.unbindtexture call. Below is an example.

wes

tex = jit.new("jit.gl.texture", this.drawto)

function jit_matrix(name)
tex:jit_matrix(name)
end

function draw()
jit.gl.bindtexture(tex.name, 0)
gl.Color(1., 1., 1., 1.)
gl.Begin("QUADS")
–top left
gl.TexCoord(0., 1.)
gl.Vertex(-0.5, 0.5, 0.)

–bottom left
gl.TexCoord(0., 0.)
gl.Vertex(-0.5, -0.5, 0.)

–bottom right
gl.TexCoord(1., 0.)
gl.Vertex(0.5, -0.5, 0.)

–top right
gl.TexCoord(1., 1.)
gl.Vertex(0.5, 0.5, 0.)
gl.End()
jit.gl.unbindtexture(tex.name, 0)
end

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 162 237 64 196617 prepend call;
#P flonum 361 169 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 361 190 42 196617 rate $1;
#P message 247 168 83 196617 read dishes.mov;
#P message 295 190 27 196617 stop;
#P message 261 190 31 196617 start;
#P flonum 204 169 35 9 0.5 0 1 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P toggle 162 169 15 0;
#P newex 162 189 52 196617 metro 30;
#P message 325 190 31 196617 clear;
#P newex 162 216 103 196617 jit.qt.movie 320 240;
#P newex 162 261 167 196617 jit.gl.lua test @file tex_exampl.lua;
#P toggle 132 55 15 0;
#P message 132 75 68 196617 fullscreen $1;
#N vpatcher 30 89 166 253;
#P window setfont "Sans Serif" 9.;
#P newex 50 71 35 196617 sel 27;
#P newex 50 50 40 196617 key;
#P outlet 50 93 15 0;
#P connect 1 0 2 0;
#P connect 2 0 0 0;
#P pop;
#P newobj 132 35 33 196617 p Esc;
#P newex 132 93 151 196617 jit.window test @depthbuffer 1;
#P message 93 93 34 196617 reset;
#P newex 93 114 186 196617 jit.gl.handle test @inherit_transform 1;
#P toggle 30 74 15 0;
#P newex 30 93 57 196617 qmetro 30;
#P newex 30 114 50 196617 t b erase;
#P newex 30 139 86 196617 jit.gl.render test;
#P connect 21 0 10 0;
#P connect 11 0 21 0;
#P connect 20 0 19 0;
#P connect 15 0 13 1;
#P fasten 13 0 11 0 167 214 167 214;
#P fasten 12 0 11 0 330 211 167 211;
#P fasten 16 0 11 0 266 211 167 211;
#P fasten 17 0 11 0 300 211 167 211;
#P fasten 18 0 11 0 252 211 167 211;
#P fasten 19 0 11 0 366 211 167 211;
#P connect 14 0 13 0;
#P connect 8 0 6 0;
#P connect 9 0 8 0;
#P connect 7 0 9 0;
#P connect 5 0 4 0;
#P fasten 1 0 0 0 35 135 35 135;
#P fasten 1 1 0 0 75 135 35 135;
#P fasten 4 0 0 0 98 135 35 135;
#P connect 2 0 1 0;
#P connect 3 0 2 0;
#P window clipboard copycount 22;

February 12, 2007 | 2:32 pm

Thank you Wesly, that’s a great push in the right direction, it makes a lot clear to me.

May 11, 2014 | 11:19 am

Hello everyone,

I’ve tried to bind jit.qt.movie to jit.gl.tex in jit.gl.lua. but I could not understand how to do.it is very difficult to use jitter object in Lua.

Do you know a solution for this problem or sample code?

local gl = require("opengl")
local GL = gl

local tex = jit.new("jit.gl.texture", this.drawto)
local mov = jit.new("jit.qt.movie");

function scriptload()

mov:read("crystal04.png");
— tex:jit_matrix(mov:name); I could not understand how to write bind texture with jit.qt.movie
end

function draw()
jit.gl.bindtexture(tex.name, 0)
gl.Color(1., 1., 1., 1.)

gl.Begin(GL.QUADS)
–top left
gl.TexCoord(0., 1.)
gl.Vertex(-0.5, 0.5, 0.)

–bottom left
gl.TexCoord(0., 0.)
gl.Vertex(-0.5, -0.5, 0.)

–bottom right
gl.TexCoord(1., 0.)
gl.Vertex(0.5, -0.5, 0.)

–top right
gl.TexCoord(1., 1.)
gl.Vertex(0.5, 0.5, 0.)
gl.End()

jit.gl.unbindtexture(tex.name, 0)
end

December 4, 2014 | 12:55 pm

EDIT | REPLY
I modified the patch from Wesley to use a different composition mode. (1,6) or (4,1)

I’m trying to figure why I’m getting the error:
jit.gl.texture: setting active texture target.: GL Error: Invalid enumeration

when I add these lines into the script:
gl.Enable(GL.BLEND)
gl.BlendFunc(1,6)

lua:
local gl=require("opengl")
local GL=gl

tex = jit.new("jit.gl.texture", this.drawto)

function jit_matrix(name)
tex:jit_matrix(name)
end

function draw()
gl.Enable(GL.BLEND)
gl.BlendFunc(1,6)

gl.Color(1, 1, 1, 1)
jit.gl.bindtexture(tex.name, 0)

gl.Begin(GL.QUADS)
–top left
gl.TexCoord(0., 1.)
gl.Vertex(-0.5, 0.5, 0.)

–bottom left
gl.TexCoord(0., 0.)
gl.Vertex(-0.5, -0.5, 0.)

–bottom right
gl.TexCoord(1., 0.)
gl.Vertex(0.5, -0.5, 0.)

–top right
gl.TexCoord(1., 1.)
gl.Vertex(0.5, 0.5, 0.)
gl.End()
jit.gl.unbindtexture(tex.name, 0)
end

patch:

– Pasted Max Patch, click to expand. –

max 6.1.8
macbook pro 2.8 intel core 2 duo, 8 gig, 10.9.5, NVIDIA GeForce 9400M 256 Mo

Viewing 5 posts - 1 through 5 (of 5 total)

Forums > Jitter