Forums > Jitter

glActiveTexture and glUseProgramObject

September 16, 2007 | 7:57 pm

hello.
i’m trying to get my lua, shader and gl chops up to snuff.
there’s some great example shaders in the apple developer examples folder.

a couple that i’ve tried to implement require the use of the gl commands glActiveTexture and glUseProgramARB.
below is a snippet of the code i’m trying to implement (a fur shader example):

glColor3f(0.9, 0.65, 0.4);
glEnable(GL_COLOR_MATERIAL);
glCallList(objectList);
glUseProgramObjectARB(program_object);
glColor3f(1.0, 1.0, 1.0);
glDisable(GL_COLOR_MATERIAL);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, noiseTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, colorTexture);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
for (i = 1; i < 10; i++)
{
//set shader param
glCallList(objectList);
}
glUseProgramObjectARB(NULL);

by my best translation, is is drawing the object once with a flat color, then enabling the shader program, and drawing it 10 more times, changing a parameter to the shader each time.
my first guess was lua, but it doesn’t quite cut it:
a snippet of the last attempt.
two gl.texture objects are sent into this:

fur = jit.new("jit.gl.shader", this.drawto)
fur.file = "fur.jxs"
fur.automatic = 0
fur.name = "fur_shader"

function noiseTex(texture, name)
noiseTexName = name
end

function colorTex(texture, name)
colorTexName = name
end

function draw()
gl.Color(0.9, 0.65, 0.4);
gl.Enable(‘COLOR_MATERIAL’);
drawObject()
gl.Color(1.0, 1.0, 1.0);
gl.Disable(‘COLOR_MATERIAL’);
gl.Enable(‘BLEND’);
gl.BlendFunc (‘SRC_ALPHA’, ‘ONE_MINUS_SRC_ALPHA’);
local textures = { noiseTexName, colorTexName }
fur.texture = textures
gl.Enable(‘CULL_FACE’);
for i=1, 10 do
– set shader param
fur:draw()
drawObject()
end
end

—————————–

my only thought at this point is that i can possibly use the shader message to my gl objects to mimic the functionality of glUseProgramObjectARB, is it possible to turn a shader on and off in one render::draw command?
what about the glActiveTexture? i’m not even sure what this does for this particular example, so it’s hard to find a suitable replacement. i’m pretty sure gl.texture handles this stuff, but how would i make one texture object active over the other?

well, i’m not expecting much, but any ideas would be appreciated.
shader is attached in case anyone cares to take a peak.

thanks.
-rob


September 16, 2007 | 10:18 pm

To use a shader with geometry in Lua, you do the following:

shader:bind()
drawobject()
shader:unbind()

This is equivalent to
glUseProgramARB(myProgram)
drawobject()
glUseProgramARB(NULL)

To use multiple textures (equivalent of active texture/bind texture stuff) do

jit.gl.bindtexture(tex1.name, 0)
jit.gl.bindtexture(tex2.name, 1)
drawobject()
jit.gl.unbindtexture(tex1.name, 1)
jit.gl.unbindtexture(tex2.name, 0)

wes


September 16, 2007 | 10:19 pm

I messed up the names in the unbindtexture part (they’re reversed) but
you get the point.

wes


September 16, 2007 | 11:12 pm

indeed i do.
many thanks.

one last quick question.

3D textues, possible with jit.gl.texture?
the doc says it can take a matrix with less than 3 dimensions, but it defaults to 3 dimensions correct?

it appears if i try to send a jit.gl.texture a 3d matrix, it doesn’t work. i guess it’s not supposed to.


September 16, 2007 | 11:58 pm

I haven’t tried in a while, but I believe I did get it to work once.
Try setting @rectangle 0.

wes

On 9/16/07, Robert Ramirez wrote:
>
> indeed i do.
> many thanks.
>
> one last quick question.
>
> 3D textues, possible with jit.gl.texture?
> the doc says it can take a matrix with less than 3 dimensions, but it defaults to 3 dimensions correct?
>
> it appears if i try to send a jit.gl.texture a 3d matrix, it doesn’t work. i guess it’s not supposed to.
>


September 17, 2007 | 4:25 am

well, i gave it the community college try, but no worky.
maybe you (anyone) can sprinkle some pixy dust on it

just bang one of the bfg’s and turn on the metro.
shader attached.

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P comment 262 24 151 196617 < --- doesn't work at all;
#P button 247 24 15 0;
#P newex 247 52 268 196617 jit.bfg 1 float32 64 64 64 @basis fractal.mono @scale 5.;
#P newex 316 307 71 196617 prepend shape;
#P user ubumenu 316 281 72 196617 0 1 1 0;
#X add sphere;
#X add torus;
#X add cylinder;
#X add opencylinder;
#X add cube;
#X add opencube;
#X add plane;
#X add circle;
#X prefix_set 0 0 0;
#P number 261 284 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 261 306 54 196617 dim $1 $1;
#P window linecount 2;
#P newex 316 333 252 196617 jit.gl.gridshape erode @automatic 0 @smooth_shading 1 @lighting_enable 1 @depth_enable 1;
#B color 5;
#P window linecount 1;
#P message 353 119 97 196617 getparamval Offset;
#N vpatcher 76 174 349 498;
#P outlet 28 276 15 0;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 28 246 27 196617 f;
#P newex 45 178 38 196617 f 0.01;
#P window linecount 0;
#P newex 45 202 34 196617 * -1.;
#P newex 28 129 27 196617 t f f;
#P newex 45 151 188 196617 if $f1 < = -0.45 || $f1 >= 0.25 then bang;
#P newex 28 106 40 196617 + 0.01;
#P newex 28 74 27 196617 f 0.;
#P inlet 28 32 15 0;
#P connect 0 0 1 0;
#P connect 1 0 2 0;
#P connect 2 0 4 0;
#P connect 4 0 7 0;
#P connect 7 0 8 0;
#P fasten 7 0 1 1 33 268 97 268 97 68 50 68;
#P connect 4 1 3 0;
#P connect 3 0 6 0;
#P connect 6 0 5 0;
#P fasten 5 0 2 1 50 235 94 235 94 100 63 100;
#P fasten 5 0 6 1 50 228 92 228 92 173 78 173;
#P pop;
#P newobj 492 95 69 196617 p calc_offset;
#P button 227 24 15 0;
#P newex 227 73 253 196617 jit.bfg 1 float32 64 64 @basis fractal.mono @scale 5.;
#P newex 454 199 32 196617 print;
#P message 283 120 67 196617 getparamlist;
#P newex 36 174 193 196617 jit.gl.handle erode @inherit_transform 1;
#P newex 227 97 231 196617 jit.gl.texture erode @dim 64 64 64 @rectangle 0;
#P newex 137 63 66 196617 t b b b b b;
#P toggle 181 234 15 0;
#P message 181 252 45 196617 sync $1;
#P toggle 108 234 15 0;
#P newex 69 233 35 196617 sel 27;
#P message 108 252 68 196617 fullscreen $1;
#P newex 26 276 158 196617 jit.window erode @depthbuffer 1;
#P newex 24 233 40 196617 key;
#P user jit.fpsgui 47 129 60 196617 0;
#P number 74 38 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P toggle 29 38 15 0;
#P newex 29 64 55 196617 qmetro 20;
#P newex 29 100 66 196617 t b b b erase;
#P newex 29 195 93 196617 jit.gl.render erode;
#P newex 378 138 123 196617 pak param Offset 0. 0. 0.;
#P flonum 245 120 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 245 143 80 196617 param Scale $1;
#P newex 245 175 219 196617 jit.gl.slab erode @file erode.jxs;
#P comment 77 24 151 196617 kinda works but not really —>;
#P fasten 13 0 12 0 113 271 31 271;
#P fasten 16 0 12 0 186 271 31 271;
#P connect 8 0 7 0;
#P connect 7 0 6 0;
#P connect 20 0 5 0;
#P fasten 6 3 5 0 88 125 34 125;
#P connect 6 0 5 0;
#P connect 6 1 10 0;
#P fasten 11 0 14 0 29 252 66 252 66 231 74 231;
#P connect 9 0 7 1;
#P fasten 14 0 15 0 74 251 105 251 105 231 113 231;
#P connect 15 0 13 0;
#P fasten 6 2 18 0 70 122 106 122 106 60 142 60;
#P connect 17 0 16 0;
#P connect 24 0 23 0;
#P connect 23 0 19 0;
#P connect 32 0 19 0;
#P connect 3 0 2 0;
#P fasten 19 0 1 0 232 169 250 169;
#P connect 18 1 1 0;
#P fasten 26 0 1 0 358 174 250 174;
#P fasten 4 0 1 0 383 165 250 165;
#P connect 2 0 1 0;
#P fasten 21 0 1 0 288 165 250 165;
#P connect 33 0 32 0;
#P connect 29 0 28 0;
#P fasten 30 1 31 0 352 302 321 302;
#P fasten 1 0 27 0 250 330 321 330;
#P fasten 18 0 27 0 142 327 321 327;
#P connect 31 0 27 0;
#P fasten 28 0 27 0 266 327 321 327;
#P connect 1 1 22 0;
#P fasten 25 0 4 3 497 126 467 126;
#P fasten 18 4 25 0 198 90 209 90 209 50 497 50;
#P window clipboard copycount 35;


September 17, 2007 | 12:11 pm

It seems that it worked after jit.normalizing jit.bfg output(patch
below), but I’m not totally sure how it should look in the end.

It looked like this on my computer:

-nesa

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 227 76 68 196617 jit.normalize;
#P window linecount 2;
#P message 41 346 107 196617 ; jitter glreadback fbo;
#P window linecount 1;
#P comment 262 24 151 196617 < --- doesn't work at all;
#P button 247 24 15 0;
#P newex 247 52 268 196617 jit.bfg 4 float32 64 64 64 @basis
fractal.mono @scale 5.;
#P newex 316 307 71 196617 prepend shape;
#P user ubumenu 316 281 72 196617 0 1 1 0;
#X add sphere;
#X add torus;
#X add cylinder;
#X add opencylinder;
#X add cube;
#X add opencube;
#X add plane;
#X add circle;
#X prefix_set 0 0 0;
#P number 261 284 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 261 306 54 196617 dim $1 $1;
#P window linecount 2;
#P newex 316 333 252 196617 jit.gl.gridshape erode @automatic 0
@smooth_shading 1 @lighting_enable 1 @depth_enable 1 @shader ero;
#B color 5;
#P window linecount 1;
#P message 353 119 97 196617 getparamval Offset;
#N vpatcher 76 174 349 498;
#P outlet 28 276 15 0;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 28 246 27 196617 f;
#P newex 45 178 38 196617 f 0.01;
#P window linecount 0;
#P newex 45 202 34 196617 * -1.;
#P newex 28 129 27 196617 t f f;
#P newex 45 151 188 196617 if $f1 < = -0.45 || $f1 >= 0.25 then bang;
#P newex 28 106 40 196617 + 0.01;
#P newex 28 74 27 196617 f 0.;
#P inlet 28 32 15 0;
#P connect 0 0 1 0;
#P connect 1 0 2 0;
#P connect 2 0 4 0;
#P connect 4 0 7 0;
#P connect 7 0 8 0;
#P fasten 7 0 1 1 33 268 97 268 97 68 50 68;
#P connect 4 1 3 0;
#P connect 3 0 6 0;
#P connect 6 0 5 0;
#P fasten 5 0 2 1 50 235 94 235 94 100 63 100;
#P fasten 5 0 6 1 50 228 92 228 92 173 78 173;
#P pop;
#P newobj 492 95 69 196617 p calc_offset;
#P button 227 24 15 0;
#P newex 454 199 32 196617 print;
#P message 283 120 67 196617 getparamlist;
#P newex 36 174 193 196617 jit.gl.handle erode @inherit_transform 1;
#P newex 227 97 97 196617 jit.gl.texture erode;
#P newex 137 63 66 196617 t b b b b b;
#P toggle 181 234 15 0;
#P message 181 252 45 196617 sync $1;
#P toggle 108 234 15 0;
#P newex 69 233 35 196617 sel 27;
#P message 108 252 68 196617 fullscreen $1;
#P newex 26 276 158 196617 jit.window erode @depthbuffer 1;
#P newex 24 233 40 196617 key;
#P user jit.fpsgui 47 129 60 196617 0;
#P number 74 38 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P toggle 29 38 15 0;
#P newex 29 64 55 196617 qmetro 20;
#P newex 29 100 66 196617 t b b b erase;
#P newex 29 195 93 196617 jit.gl.render erode;
#P newex 378 138 123 196617 pak param Offset 0. 0. 0.;
#P flonum 245 120 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 245 143 80 196617 param Scale $1;
#P newex 245 175 217 196617 jit.gl.shader erode @file erode.jxs @name
ero;
#P comment 77 24 151 196617 kinda works but not really —>;
#P connect 31 0 35 0;
#P connect 32 0 31 0;
#P fasten 18 4 24 0 198 90 209 90 209 50 497 50;
#P fasten 24 0 4 3 497 126 467 126;
#P connect 1 1 22 0;
#P fasten 27 0 26 0 266 327 321 327;
#P connect 30 0 26 0;
#P fasten 18 0 26 0 142 327 321 327;
#P connect 19 0 26 0;
#P fasten 29 1 30 0 352 302 321 302;
#P connect 28 0 27 0;
#P fasten 21 0 1 0 288 165 250 165;
#P connect 2 0 1 0;
#P fasten 4 0 1 0 383 165 250 165;
#P fasten 25 0 1 0 358 174 250 174;
#P connect 18 1 1 0;
#P connect 3 0 2 0;
#P connect 35 0 19 0;
#P connect 17 0 16 0;
#P fasten 6 2 18 0 70 122 106 122 106 60 142 60;
#P connect 15 0 13 0;
#P fasten 14 0 15 0 74 251 105 251 105 231 113 231;
#P connect 9 0 7 1;
#P fasten 11 0 14 0 29 252 66 252 66 231 74 231;
#P connect 6 1 10 0;
#P connect 20 0 5 0;
#P fasten 6 3 5 0 88 125 34 125;
#P connect 6 0 5 0;
#P connect 7 0 6 0;
#P connect 8 0 7 0;
#P fasten 13 0 12 0 113 271 31 271;
#P fasten 16 0 12 0 186 271 31 271;
#P window clipboard copycount 36;


September 17, 2007 | 3:05 pm

On Sep 17, 2007, at 2:11 PM, nesa wrote:

> It seems that it worked after jit.normalizing jit.bfg output(patch
> below), but I’m not totally sure how it should look in the end.

Hey, that’s not the only thing you did!
You also skipped the slab and applied the shader directly to the
gridshape. And you didn’t check the shader.

Guilty as charged,
nesa


September 17, 2007 | 8:16 pm

however! you still made it work better than it did, so thanks for that.
i think it’s close.

if you have the apple developer package installed, compile the example/cocoa/glsl showcase xcode proj and check out the erode shader. that’s what it’s supposed to look like.


September 17, 2007 | 8:35 pm

I think you’re making this more complex than it needs to be. To get
this to work, you only need jit.gl.shader and jit.gl.texture and some
geometry to draw using the shader. Here’s how I would do it:

1) Make a 3D texture test with jit.gl.texture @rectangle 0 and sending
it a simple test 3D matrix like a gradient volume. Then use a super
simple shader to test the values and compare to the gradient to see if
you’re accessing the texture properly.

2) Next, incorporate the erode shader based on experience in step 1.

wes

On 9/17/07, Robert Ramirez wrote:
>
> however! you still made it work better than it did, so thanks for that.
> i think it’s close.
>
> if you have the apple developer package installed, compile the example/cocoa/glsl showcase xcode proj and check out the erode shader. that’s what it’s supposed to look like.
>


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