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


    Feb 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

    • Feb 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
    • Feb 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 | 6:19 pm
      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
    • Dec 04 2014 | 8: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:
      max 6.1.8 macbook pro 2.8 intel core 2 duo, 8 gig, 10.9.5, NVIDIA GeForce 9400M 256 Mo