supagu
12-25-2004, 03:39 AM
i've written my first per-pixel lighting shader,
and its not optimised, i've looked at it, tryed moving some things to the vertex shader, but i loose visual quality.
any quick hints as to how i could optimise this?
phong.vert
varying vec3 eye;
varying vec3 light;
varying vec3 normal;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
eye = vec3( gl_ModelViewMatrix * gl_Vertex );
light = normalize( gl_LightSource[0].position.xyz - eye );
normal = normalize( gl_NormalMatrix * gl_Normal );
}
phong.frag
uniform sampler2D texture;
varying vec3 eye;
varying vec3 light;
varying vec3 normal;
void main()
{
vec3 nEye = normalize( eye );
vec3 nLight = normalize( light );
vec3 nNormal = normalize( normal );
vec3 reflectEye = reflect( nEye, nNormal );
float diff = clamp( dot( nLight, nNormal ), 0.0, 1.0 );
float spec = 0.0;
if( diff > 0.0 )
spec = clamp( pow( clamp( dot( reflectEye, nLight ), 0.0, 1.0 ), gl_FrontMaterial.shininess ), 0.0, 1.0);
vec4 difCol = texture2D( texture, vec2(gl_TexCoord[0]) ) *
((gl_LightSource[0].diffuse * diff) + gl_LightSource[0].ambient );
vec4 specCol = gl_LightSource[0].specular * spec * gl_FrontMaterial.specular;
gl_FragColor = difCol + specCol;
}
edit: also if you could say why you would do it so i can also understand and learn :)
cheers
and its not optimised, i've looked at it, tryed moving some things to the vertex shader, but i loose visual quality.
any quick hints as to how i could optimise this?
phong.vert
varying vec3 eye;
varying vec3 light;
varying vec3 normal;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
eye = vec3( gl_ModelViewMatrix * gl_Vertex );
light = normalize( gl_LightSource[0].position.xyz - eye );
normal = normalize( gl_NormalMatrix * gl_Normal );
}
phong.frag
uniform sampler2D texture;
varying vec3 eye;
varying vec3 light;
varying vec3 normal;
void main()
{
vec3 nEye = normalize( eye );
vec3 nLight = normalize( light );
vec3 nNormal = normalize( normal );
vec3 reflectEye = reflect( nEye, nNormal );
float diff = clamp( dot( nLight, nNormal ), 0.0, 1.0 );
float spec = 0.0;
if( diff > 0.0 )
spec = clamp( pow( clamp( dot( reflectEye, nLight ), 0.0, 1.0 ), gl_FrontMaterial.shininess ), 0.0, 1.0);
vec4 difCol = texture2D( texture, vec2(gl_TexCoord[0]) ) *
((gl_LightSource[0].diffuse * diff) + gl_LightSource[0].ambient );
vec4 specCol = gl_LightSource[0].specular * spec * gl_FrontMaterial.specular;
gl_FragColor = difCol + specCol;
}
edit: also if you could say why you would do it so i can also understand and learn :)
cheers