A simple question…maybe: all shaders that i’ve tried (taken by Rendermonkey or Shader Designer) are usefull only if use it on a mesh without translating and rotating it (object space=world space), if a make a rotation or a translation illumination is not correctly calculated. The problem, i think, is that i have to multiply lightPosition by the inverse model view matrix but i’ve tried in all way without results. This is a simple shader that is correct only when object and world space are equal, someone knows what i have to add to obtain correct illumination?
[VERTEX SHADER]
//
// Simple vertex shader for wood
//
// Author: John Kessenich
//
// Copyright (c) 2002-2004 3Dlabs Inc. Ltd.
//
// See 3Dlabs-License.txt for license information
//
varying float lightIntensity;
varying vec3 Position;
uniform vec3 lightPosition0;
uniform float Scale;
void main(void)
{
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
Position = vec3(gl_Vertex) * Scale;
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
lightIntensity = max(dot(normalize(lightPosition0 - vec3(pos)), tnorm), 0.0) * 1.5;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
[FRAGMENT SHADER]
//
// Simple fragment shader for wood
//
// Author: John Kessenich
//
// Copyright (c) 2002-2004 3Dlabs Inc. Ltd.
//
// See 3Dlabs-License.txt for license information
//
uniform float GrainSizeRecip;
uniform vec3 DarkColor;
uniform vec3 spread;
varying float lightIntensity;
varying vec3 Position;
void main (void)
{
//
// cheap noise
//
vec3 location = Position;
vec3 floorvec = vec3(floor(10.0 * Position.x), 0.0, floor(10.0 * Position.z));
vec3 noise = Position * 10.0 - floorvec - 0.5;
noise *= noise;
location += noise * 0.12;
//
// distance from axis
//
float dist = location.x * location.x + location.z * location.z;
float grain = dist * GrainSizeRecip;
//
// grain effects as function of distance
//
float brightness = fract(grain);
if (brightness > 0.5)
brightness = (1.0 - brightness);
vec3 color = DarkColor + brightness * spread;
brightness = fract(grain * 7.0);
if (brightness > 0.5)
brightness = 1.0 - brightness;
color -= brightness * spread;
//
// also as a function of lines parallel to the axis
//
brightness = fract(grain * 47.0) * 0.60;
float line = fract(Position.z + Position.x);
float snap = floor(line * 20.0) * (1.0/20.0);
if (line < snap + 0.006)
color -= brightness * spread;
//
// apply lighting effects from vertex processor
//
color = clamp(color * lightIntensity, 0.0, 1.0);
gl_FragColor = vec4(color, 1.0);
}