Forums > Jitter

Orange Book shader example

February 17, 2009 | 11:24 am

Hello list im a newbie to Jitter shaders, just got my hands on the orange book and now trying to get on of their examples, Glyphbomb, working in Jitter.
As soon as I attach the shader to the GL object the object disappears. Below you’ll find the maxpatch and the shader and attached the texture image file.
Did anybody already got this example working in Jitter, or is there a shader master who can have a look at my attempt and see where i failed and point me in the right direction?

Best regards
Lev

The code for the vertex and fragment shader are in the book, but can also be found online here:
http://www.3dshaders.com/shaders2ndEd/CH10-glyphbomb.vert.txt

http://www.3dshaders.com/shaders2ndEd/CH10-glyphbomb.frag.txt

—–

max v2;
#N vpatcher 425 63 1292 714;
#P origin 0 -17;
#P window setfont "Sans Serif" 9.;
#P newex 161 67 61 196617 delay 1000;
#P newex 143 40 74 196617 loadmess bang;
#P newex 323 217 145 196617 jit.gl.shader foo @name shady;
#P message 323 148 98 196617 read glyphbomb.jxs;
#P message 143 266 94 196617 texture mytexture;
#P message 323 263 68 196617 shader shady;
#P message 143 148 79 196617 read glyphs.jpg;
#P newex 143 184 105 196617 jit.qt.movie 256 256;
#P newex 143 217 170 196617 jit.gl.texture foo @name mytexture;
#P newex 212 346 145 196617 jit.window foo @depthbuffer 1;
#P user jit.fpsgui 59 234 60 196617 0;
#P toggle 15 105 15 0;
#P newex 15 131 55 196617 qmetro 20;
#P newex 15 153 55 196617 t b erase b;
#P newex 143 308 356 196617 jit.gl.gridshape foo @color 1 1 1 1 @scale 0.5 @shape plane @blend_enable 1;
#P newex 15 346 195 196617 jit.gl.render foo @erase_color 0. 0. 0. 1.;
#P fasten 14 0 4 0 148 81 20 81;
#P connect 4 0 3 0;
#P connect 3 0 2 0;
#P fasten 1 0 0 0 148 335 20 335;
#P connect 2 0 0 0;
#P fasten 2 1 0 0 42 234 20 234;
#P connect 2 2 5 0;
#P connect 14 0 9 0;
#P fasten 2 2 8 0 64 178 148 178;
#P fasten 9 0 8 0 148 180 148 180;
#P connect 8 0 7 0;
#P fasten 10 0 1 0 328 299 148 299;
#P fasten 11 0 1 0 148 299 148 299;
#P fasten 14 0 15 0 148 62 166 62;
#P fasten 15 0 12 0 166 116 328 116;
#P connect 12 0 13 0;
#P pop;

the shader should be named: glyphbomb.jxs


Shader for generating glyph texture













< ![CDATA[

uniform float SpecularContribution;
uniform vec3 LightPosition;
uniform float ScaleFactor;

varying float LightIntensity;
varying vec2 TexCoord;

void main()
{
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
vec3 lightVec = normalize(LightPosition – ecPosition);
vec3 reflectVec = reflect(-lightVec, tnorm);
vec3 viewVec = normalize(-ecPosition);
float diffuse = max(dot(lightVec, tnorm), 0.0);
float spec = 0.0;

if(diffuse > 0.0)
{
spec = max(dot(reflectVec, viewVec), 0.0);
spec = pow(spec, 16.0);
}

float diffusecontribution = 1.0 – SpecularContribution;
LightIntensity = diffusecontribution * diffuse * 2.0 +
SpecularContribution * spec;

TexCoord = gl_MultiTexCoord0.st * ScaleFactor;

gl_Position = ftransform();
}

]]>

< ![CDATA[

#define TWO_PI 6.28318

uniform vec4 ModelColor;

uniform sampler2D GlyphTex;
uniform sampler2D RandomTex;

uniform float ColAdjust;
uniform float ScaleFactor;
uniform float Percentage;
uniform float SamplesPerCell;
uniform float RO1;

uniform bool RandomScale;
uniform bool RandomRotate;

varying vec2 TexCoord;
varying float LightIntensity;

void main()
{
vec4 color = ModelColor;
vec2 cell = floor(TexCoord);
vec2 offset = TexCoord – cell;

for (int i = -1; i < = int (RandomRotate); i++)
{
for (int j = -1; j < = int (RandomRotate); j++)
{
vec2 currentCell = cell + vec2(float(i), float(j));
vec2 currentOffset = offset – vec2(float(i), float(j));

vec2 randomUV = currentCell * vec2(RO1);

for (int k = 0; k < int (SamplesPerCell); k++)
{
vec4 random = texture2D(RandomTex, randomUV);
randomUV += random.ba;

if (random.r < Percentage)
{
vec2 glyphIndex;
mat2 rotator;
vec2 index;
float rotationAngle, cosRot, sinRot;

index.s = floor(random.b * 10.0);
index.t = floor(ColAdjust * 10.0);

if (RandomRotate)
{
rotationAngle = TWO_PI * random.g;
cosRot = cos(rotationAngle);
sinRot = sin(rotationAngle);
rotator[0] = vec2(cosRot, sinRot);
rotator[1] = vec2(-sinRot, cosRot);
glyphIndex = -rotator *
(currentOffset – random.rg);
}
else
{
glyphIndex = currentOffset – random.rg;
}

if (RandomScale)
glyphIndex /= vec2(0.5 * random.r + 0.5);

glyphIndex =
(clamp(glyphIndex, 0.0, 1.0) + index) * 0.1;

vec4 image = texture2D(GlyphTex, glyphIndex);

if (image.r != 1.0)
color.rgb = mix(random.rgb * 0.7, color.rgb,
image.r);
}
}
}
}

gl_FragColor = color * LightIntensity;

}

]]>


February 17, 2009 | 12:47 pm

I think vade made something like this in his "v002" shader library, look it up.


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