Grid shader

Aug 25, 2009 at 10:28am

Grid shader

Hi all,

I’m trying to convert the bricks shader so it will generate a grid. So far so good, no problems with generating the grid.
The thing is : I would like to give an offset value to the shader for positioning/shifting the grid. This works, but at certain values the grid disappears and I can’t figure out which of the functions is returning the wrong value which causes this.
Any ideas?


Shader for generating a grid







< ![CDATA[
//
//
//
//
uniform vec3 lightpos;

const float ks = 0.3;
const float kd = 1.0 – ks;

varying float lightintensity;
varying vec2 modelpos;

void main(void)
{
vec3 pos = vec3 (gl_ModelViewMatrix * gl_Vertex);
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
vec3 lightVec = normalize(lightpos – pos);
vec3 reflectVec = reflect(-lightVec, tnorm);
vec3 viewVec = normalize(-pos);
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);
}

modelpos = gl_Vertex.xy;
gl_Position = ftransform();
}
]]>

< ![CDATA[
//
// Fragment shader for Grid
//

uniform vec3 gridcolor, bkgcolor;
uniform vec2 bricksize;
uniform vec2 brickpct;
uniform vec2 offset;

varying vec2 modelpos;
varying float lightintensity;

void main(void)
{
vec3 color;
vec2 position, useBrick;

position = modelpos + offset / bricksize;

position = fract(position);

useBrick = step(position, brickpct);

color = mix(gridcolor, bkgcolor, useBrick.x * useBrick.y );
gl_FragColor = vec4 (color, 1.0);
}
]]>

#45219
Aug 26, 2009 at 5:41pm

please provide a zip file with shader and example patch.

#162955

You must be logged in to reply to this topic.