Forums > Jitter

a simple question about programmable shaders

August 25, 2006 | 2:39 pm

i would like to make a vertex program that applies a certain deformation to a plane based on the contents of a jitter matrix. i.e., i have a plane and i’d like to displace certain points on this plane based on a jit-matrix that i’m writing into with jit.lcd or some such.

i’m not sure how to access the contents of a jit.matrix from within my glsl shader program.

any hints?

thanks,
a


August 25, 2006 | 2:59 pm

You could tranfer the lcd contents to a texture and access the texture from
within the shader. Ofcouse you’ll have to bind both the texture and shader
program to the plane using the normal @texture and @shader attributes.
Instead of using the rgba values to influence the color of the plane, you
can then use them to do something else. I’m not really sure you can access
the texture "sampler2D" in the vertex program, but I suspect you can.

hth, -thijs

On 8/25/06, Ali Momeni wrote:
>
>
> i would like to make a vertex program that applies a certain deformation
> to a plane based on the contents of a jitter matrix. i.e., i have a plane
> and i’d like to displace certain points on this plane based on a jit-matrix
> that i’m writing into with jit.lcd or some such.
>
> i’m not sure how to access the contents of a jit.matrix from within my
> glsl shader program.
>
> any hints?
>
> thanks,
> a
>
>


August 25, 2006 | 3:18 pm

i’m with you so far;
however, what i need is to access particular positions in my texture within my vertext program. is tex0 indexted like a matrix, as in tex0[x][y] would be the x,y point in my texture?


August 25, 2006 | 3:52 pm

You access a texture by instantiating a the sampler2D for every texture.
Then to access its content you call the texture2D method with 2 arguments.
The first being the texture id, tex0, and the second one being the array of
texture coordinates (st / xy in case of a 2D texture). You can use the
coordinates generated automatically by opengl (according to the texturing
mode), but in your case you would calculate the coordinates yourself. So the
answer is yes, you can access your texture like an array, only not like
tex0[x,y] but through use of the texture2D method.

I don’t have experience with this either, but I have the orange book sitting
next to me:-) I suggest you check it out online.

-thijs

On 8/25/06, Ali Momeni wrote:
>
>
> i’m with you so far;
> however, what i need is to access particular positions in my texture
> within my vertext program. is tex0 indexted like a matrix, as in tex0[x][y]
> would be the x,y point in my texture?
>


August 25, 2006 | 4:01 pm

still with you, i’ve discovered texture2DRect but am not sure how to provide the vec2 coor for the point i wanna access.

first, is the 2nd argument of texture2DRect in integers as in the pixel position in the texture, or is it a float between 0. and 1. for x and y?

and second, how could i access the automattically generated coordinates by OpenGL?

say, you happen to have a linke for the orange book online? or you bought the hard copy….


August 25, 2006 | 4:16 pm

ah, now i have a new problem.
it seems that i’m not allowed to use textures from the outside like tex0 and tex1 and … in a vertex program.

i get this error:

ERROR: Implementation limit of 0 active vertex shader samplers (e.g., maximum number of supported image units) exceeded, vertex shader uses 1 samplers
• error: — END GLSL INFO LOG: jDang:/Users/ali/Documents/_for-MaxMSP/_current/shaders/gravity-lighting1.jxs –

this so called implementation limit of 0 seems to be rather low, no? :)

a


August 25, 2006 | 4:42 pm

Hi Ali,
Texture access is not allowed in the current version of vertex
programs that both OSX and Windows support. For this, you’re going to
have to wait for the next version of GLSL to make its way from
standard to actually being implemented in the OS. This kind of thing
and a new type of shader called Geometry shaders are what’s coming in
the future.

For now, you’re going to have to use a per-vertex value such as color,
tangent, bitangent, or specular array (basically one of jit.gl.mesh’s
inputs). You’ll access these using the built in GLSL variables for
accessing fixed function values like gl_Color or gl_Normal and just
ignore the name and use it how you want. So, this is entirely
possible to do now, just not with textures. If you need more
direction on how to do this, let me know and I’ll see if I can whip
out an example.

wes


August 25, 2006 | 4:50 pm

first, is the 2nd argument of texture2DRect in integers as in the pixel
> position in the texture, or is it a float between 0. and 1. for x and y?

I think its 0-1

> and second, how could i access the automattically generated coordinates by
> OpenGL?

gl_MultiTexCoord0.st

> say, you happen to have a linke for the orange book online? or you bought
> the hard copy….

my bad, I made a wrong assumption that the orange book was available online
too, just like the other opengl books. I have a hardcopy. About the errors
you’re getting.. I can’t help with that. Like I said I don’t have experience
with this stuff. It’s time for someone with more knowledge to take over.

good luck, -thijs


August 25, 2006 | 4:59 pm

hi wes,
ah, i see; so the implimentation limit of 0 is not a jitter thing but rather a GLSL standard thing.

as for the present solution, i think i understand;
i need to get the data from outside (i.e. from jitter) into my glsl thru other means…
so if i want to displace points on my plane forinstance, i should write color/normal/… values into the vector for the shape to which i’m applying my shader, and then recover them in my glsl code with gl_Color, gl_Normal and such….?
if you happen to have a little example easily whipped out, i’d be most grateful.

a


August 25, 2006 | 5:06 pm

These are the state variables that you can bind vertex shader
attributes to. The correspond precisely to their jit.gl.mesh’s
namesake.:

"POSITION"
"NORMAL"
"TANGENT"
"BITANGENT"
"TEXCOORD"

I had a patch lying around that already did this, so here you go.
There’s a patch and a shader:

—————————-Vertex Displace
Patch———————————
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#N vpatcher 10 59 336 296;
#P window setfont "Sans Serif" 9.;
#N vpatcher 10 59 256 244;
#P inlet 51 52 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 123 50 29 196617 t b f;
#P newex 50 97 102 196617 jit.op @op * @val 0.;
#P newex 50 75 55 196617 jit.matrix;
#P inlet 123 30 15 0;
#P outlet 50 119 15 0;
#P fasten 4 0 2 0 128 72 55 72;
#P connect 5 0 2 0;
#P connect 2 0 3 0;
#P connect 3 0 0 0;
#P connect 1 0 4 0;
#P connect 4 1 3 1;
#P pop;
#P newobj 207 129 42 196617 p Scale;
#N vpatcher 10 59 256 244;
#P inlet 51 52 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 123 50 29 196617 t b f;
#P newex 50 97 102 196617 jit.op @op * @val 0.;
#P newex 50 75 55 196617 jit.matrix;
#P inlet 123 30 15 0;
#P outlet 50 119 15 0;
#P connect 5 0 2 0;
#P fasten 4 0 2 0 128 72 55 72;
#P connect 2 0 3 0;
#P connect 3 0 0 0;
#P connect 1 0 4 0;
#P connect 4 1 3 1;
#P pop;
#P newobj 156 129 42 196617 p Scale;
#P newex 105 162 53 196617 jit.pack 3;
#N vpatcher 10 59 256 244;
#P inlet 51 52 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 123 50 29 196617 t b f;
#P newex 50 97 102 196617 jit.op @op * @val 0.;
#P newex 50 75 55 196617 jit.matrix;
#P inlet 123 30 15 0;
#P outlet 50 119 15 0;
#P fasten 4 0 2 0 128 72 55 72;
#P connect 5 0 2 0;
#P connect 2 0 3 0;
#P connect 3 0 0 0;
#P connect 1 0 4 0;
#P connect 4 1 3 1;
#P pop;
#P newobj 105 129 42 196617 p Scale;
#P newex 34 74 63 196617 jit.unpack 3;
#P inlet 239 46 15 0;
#P inlet 188 46 15 0;
#P inlet 137 46 15 0;
#P inlet 34 54 15 0;
#P outlet 105 184 15 0;
#P button 105 74 15 0;
#P connect 2 0 6 0;
#P fasten 5 0 0 0 244 67 110 67;
#P fasten 4 0 0 0 193 67 110 67;
#P connect 0 0 7 0;
#P fasten 6 0 7 0 39 116 110 116;
#P connect 7 0 8 0;
#P connect 8 0 1 0;
#P fasten 9 0 8 1 161 152 131 152;
#P connect 3 0 7 1;
#P fasten 10 0 8 2 212 157 152 157;
#P fasten 6 1 9 0 56 106 161 106;
#P connect 4 0 9 1;
#P fasten 6 2 10 0 73 98 212 98;
#P connect 5 0 10 1;
#P pop;
#P newobj 487 404 116 196617 p Displace;
#P flonum 598 377 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 560 377 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P comment 17 463 112 196617 vertex attr test shader;
#P newex 223 150 48 196617 loadbang;
#P message 101 417 30 196617 read;
#P flonum 522 377 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 378 268 271 196617 jit.expr 3 float32 10 10 @expr "norm[0]"
"norm[1]" "1";
#P button 8 89 15 0;
#P message 10 417 89 196617 read vd.attr2.jxs;
#P newex 10 441 132 196617 jit.gl.shader attr @name at;
#P button 223 170 15 0;
#P newex 223 189 130 196617 jit.matrix 3 float32 10 10;
#P newex 223 212 352 196617 jit.expr @expr "snorm[0]" "snorm[1]"
"0.9*hypot(snorm[0]\, snorm[1])";
#P newex 223 448 368 196617 jit.gl.mesh attr @blend_enable 1
@depth_enable 1 @auto_normals 1 @shader at;
#P toggle 135 54 15 0;
#P message 135 74 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 135 34 33 196617 p Esc;
#P newex 135 92 151 196617 jit.window attr @depthbuffer 1;
#P message 96 92 34 196617 reset;
#P newex 96 113 186 196617 jit.gl.handle attr @inherit_transform 1;
#P toggle 33 69 15 0;
#P newex 33 88 57 196617 qmetro 60;
#P newex 33 113 50 196617 t b erase;
#P newex 33 138 157 196617 jit.gl.render attr @camera 0 0 4;
#P connect 15 0 14 0;
#P fasten 19 0 14 0 106 436 15 436;
#P connect 3 0 2 0;
#P fasten 16 0 1 0 13 108 38 108;
#P connect 2 0 1 0;
#P fasten 1 1 0 0 78 134 38 134;
#P fasten 1 0 0 0 38 134 38 134;
#P fasten 4 0 0 0 101 134 38 134;
#P connect 5 0 4 0;
#P connect 7 0 9 0;
#P connect 9 0 8 0;
#P connect 8 0 6 0;
#P connect 20 0 13 0;
#P connect 13 0 12 0;
#P connect 12 0 11 0;
#P connect 11 0 10 0;
#P fasten 11 0 10 3 228 268 360 268;
#P fasten 11 0 17 0 228 235 383 235;
#P fasten 17 0 24 0 383 344 492 344;
#P connect 24 0 10 6;
#P fasten 18 0 24 1 527 398 527 398;
#P fasten 22 0 24 2 565 398 562 398;
#P fasten 23 0 24 3 603 398 597 398;
#P window clipboard copycount 25;

———————————————–vertex distaplce
shader———————————–



Textured glass-like material lit with a point light w/ two specular
components calculated per pixel.

Position of Light Source

Position

Tangent

Primary Specular Color

Secondary Specular Color

Base Color

Primary Specular Multiplier

Secondary Specular Multiplier

Specularity Adjustment










< ![CDATA[
/*
*
* Derek Gerstmann – derek@cycling74.com
* Copyright 2005 – Cycling ’74
*
* GLSL vertex program for a textured glass-like material lit by a
* point light source.
*
*/

uniform vec3 lightpos; // position of light source
attribute vec3 position;
attribute vec3 tangent;

varying vec3 lightdir; // direction of light source
varying vec3 viewdir; // view direction
varying vec3 normal; // surface normal

void main(void)
{
// perform standard transform on vertex
gl_Position = gl_ModelViewProjectionMatrix*vec4((position+tangent), 1.);

// normalize view direction
viewdir = -normalize(vec3(gl_ModelViewMatrix * gl_Vertex));

// normalize light direction
lightdir = normalize(lightpos);

// get texture coordinate
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

// transform normal into view space
normal = normalize(gl_NormalMatrix * gl_Normal);
}
]]>

< ![CDATA[
/*
*
* Derek Gerstmann – derek@cycling74.com
* Copyright 2005 – Cycling ’74
*
* GLSL fragment program for a textured glass-like material lit by a
* point light source.
*
*/

varying vec3 lightdir; // light direction
varying vec3 viewdir; // view direction
varying vec3 normal; // surface normal

uniform vec4 basecolor; // base glass color
uniform vec4 specular2; // secondary specular color
uniform vec4 specular1; // main specular color

uniform float ks1; // specular multiplier
uniform float ks2; // dim specular multiplier

uniform float shininess; // specular adjustment

void main (void)
{
vec4 color;

// compute the reflection vector
vec3 reflect = normalize( 2.0 * dot( normal, lightdir) * normal – lightdir );

// compute specular intensity
float specularity = max(dot(viewdir, reflect), 0.0);
specularity = specularity * specularity;

// add in the base and secondary specular color
color = basecolor + ks2 * specularity * specular2;

// add in the primary specular color
specularity = pow(specularity, shininess) * ks1;
color += specularity * specular1;

// set the outgoing fragment color
gl_FragColor = min(color, vec4(1.0));
}
]]>


August 25, 2006 | 5:12 pm

i get the patch but i’m not sure if you included all of the shader called vd.attr2.jxs….

would you please post that again?

a


August 25, 2006 | 5:15 pm

On Aug 25, 2006, at 9:59 AM, Ali Momeni wrote:

> as for the present solution, i think i understand;
> i need to get the data from outside (i.e. from jitter) into my glsl
> thru other means…
> so if i want to displace points on my plane forinstance, i should
> write color/normal/… values into the vector for the shape to
> which i’m applying my shader, and then recover them in my glsl code
> with gl_Color, gl_Normal and such….?

This is probably easiest to accomplish using jit.gl.mesh.

FWIW, ALI, since you need to submit the data as another matrix of
information, you might find this easier to do with standard CPU based
jitter matrix operator objects if it’s not an incredibly complex
calculation.

-Joshua


August 25, 2006 | 5:19 pm

Here you go again. It was at the bottom of my previous email:



Textured glass-like material lit with a point light w/ two specular
components calculated per pixel.

Position of Light Source

Position

Tangent

Primary Specular Color

Secondary Specular Color

Base Color

Primary Specular Multiplier

Secondary Specular Multiplier

Specularity Adjustment










< ![CDATA[
/*
*
* Derek Gerstmann – derek@cycling74.com
* Copyright 2005 – Cycling ’74
*
* GLSL vertex program for a textured glass-like material lit by a
* point light source.
*
*/

uniform vec3 lightpos; // position of light source
attribute vec3 position;
attribute vec3 tangent;

varying vec3 lightdir; // direction of light source
varying vec3 viewdir; // view direction
varying vec3 normal; // surface normal

void main(void)
{
// perform standard transform on vertex
gl_Position = gl_ModelViewProjectionMatrix*vec4((position+tangent), 1.);

// normalize view direction
viewdir = -normalize(vec3(gl_ModelViewMatrix * gl_Vertex));

// normalize light direction
lightdir = normalize(lightpos);

// get texture coordinate
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

// transform normal into view space
normal = normalize(gl_NormalMatrix * gl_Normal);
}
]]>

< ![CDATA[
/*
*
* Derek Gerstmann – derek@cycling74.com
* Copyright 2005 – Cycling ’74
*
* GLSL fragment program for a textured glass-like material lit by a
* point light source.
*
*/

varying vec3 lightdir; // light direction
varying vec3 viewdir; // view direction
varying vec3 normal; // surface normal

uniform vec4 basecolor; // base glass color
uniform vec4 specular2; // secondary specular color
uniform vec4 specular1; // main specular color

uniform float ks1; // specular multiplier
uniform float ks2; // dim specular multiplier

uniform float shininess; // specular adjustment

void main (void)
{
vec4 color;

// compute the reflection vector
vec3 reflect = normalize( 2.0 * dot( normal, lightdir) * normal – lightdir );

// compute specular intensity
float specularity = max(dot(viewdir, reflect), 0.0);
specularity = specularity * specularity;

// add in the base and secondary specular color
color = basecolor + ks2 * specularity * specular2;

// add in the primary specular color
specularity = pow(specularity, shininess) * ks1;
color += specularity * specular1;

// set the outgoing fragment color
gl_FragColor = min(color, vec4(1.0));
}
]]>


August 25, 2006 | 5:25 pm

thanks joshua.
if what you mean is that i might as well do everything on the CPU with jitter, then yes, i can and have tried that; it works fine and i get about 16fps on my macbook pro; i’d like to get about 60fps so i thought i’d try another route…. :)


August 25, 2006 | 5:44 pm

incidentally, i just tried my jitter/CPU patch which i’d developed with 4.6 beta 1 and jitter 1.6 UB beta 1 and it now crashes max.

it gives this error before crashing:

•error: jit.gl.pbuffer: unable to get a valid pixel format!
•error: jit.gl.texture: error attaching texture to readback mecahnism for capture!

oddly enough, the patch works in 4.5.7/1.6 running in rosette on my machine!

must be a readback from GPU problem cuz i use the gaussian blur shader and try get back the data into a jitter matrix…

any thoughts?


August 25, 2006 | 6:09 pm

> any thoughts?

Yes, please please please post a proper bug report with instructions
about making it crash and a patch that demonstrates. Otherwise, this
is hairy stuff and it’s never going to get fixed.

thanks,
wes


August 25, 2006 | 6:38 pm

alright,
i’m trying to do unlike my usual self and actually reduce the complicated patch to the sipmlest version that crashes, but i’m getting very confused with something.

i get hundreds of these:

• error: jit.gl.texture: error attaching texture to readback mechanism for capture!
jit.gl.texture: error submitting texture data: GL Error: Invalid operation
• error: jit.gl.texture: unable to create texture!
jit.gl.texture: error submitting texture data: GL Error: Invalid operation

but i don’t have any jit.gl.texture objects in my patch…?!
i have a "jit.gl.slab.gauss6x foo" abstraction, a "jit.gl.videoplane", and a "jit.gl.render" and that’s it for gl objects… who’s causing the errors?

a


August 25, 2006 | 6:48 pm

On Aug 25, 2006, at 11:38 AM, Ali Momeni wrote:

> i get hundreds of these:
> • error: jit.gl.texture: unable to create texture!
> jit.gl.texture: error submitting texture data: GL Error: Invalid
> operation
>
> but i don’t have any jit.gl.texture objects in my patch…?!
> i have a "jit.gl.slab.gauss6x foo" abstraction, a
> "jit.gl.videoplane", and a "jit.gl.render" and that’s it for gl
> objects… who’s causing the errors?

jit.gl.slab internally uses a texture for each input as well as the
output.

-Joshua


August 25, 2006 | 6:51 pm

On Aug 25, 2006, at 11:38 AM, Ali Momeni wrote:

> i get hundreds of these:
> • error: jit.gl.texture: error attaching texture to readback
> mechanism for capture!
> jit.gl.texture: error submitting texture data: GL Error: Invalid
> operation
>
> i have a "jit.gl.slab.gauss6x foo" abstraction, a
> "jit.gl.videoplane", and a "jit.gl.render" and that’s it for gl
> objects… who’s causing the errors?

Btw, make sure that jit.gl.slab’s args (in this case "foo"), are
using the same name as the rendering context! This is something which
may have worked by chance in 1.5.X but not 1.6

-Joshua


August 26, 2006 | 10:25 am

indeed my gaussian blur has the same context name as my rendered.

i wonder if what i’m trying to do (deform the vertext positions 3d form based on a matrix that i create in jitter) isn’t more easily done with one of the other languages supported by jitters programmable shaders implementation like Cg or arb….
i know very little about them and how they differ from glsl…

a


August 26, 2006 | 8:36 pm

On Aug 26, 2006, at 3:25 AM, Ali Momeni wrote:

> i wonder if what i’m trying to do (deform the vertext positions 3d
> form based on a matrix that i create in jitter) isn’t more easily
> done with one of the other languages supported by jitters
> programmable shaders implementation like Cg or arb….
> i know very little about them and how they differ from glsl…

Not likely. They all expose roughly the same HW functionality.

-Joshua


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