PDA

View Full Version : Fixed Functionality Pipeline Shader



jelly
02-09-2005, 01:16 PM
Hi, I was just wondering if there is a standard shader / set of shaders for re-creating the normal OpenGL fixed functionality pipeline rendering? It seems a bit of a pain to have to go and write it all - plus I'll probably get it wrong!

Thanks,

- Andy.

Peter_dup1
02-10-2005, 05:55 AM
Originally posted by jelly:
Hi, I was just wondering if there is a standard shader / set of shaders for re-creating the normal OpenGL fixed functionality pipeline rendering? It seems a bit of a pain to have to go and write it all - plus I'll probably get it wrong!

Thanks,

- Andy.I wanted to do the same a while ago, and couldn't find any when I looked, so I think you'll have to write your own. The orange book has most of the information you need. Below is a vertex shader which might get you started. As far as I can tell, there isn't any mechanism for determining which lights or texture units are active, so your shader will either need to make assumptions, or have a bit of extra information passed through to it via some uniforms. This shader simply assumes that light 0 is a point light.

Peter

void PointLight(in int i,
in vec3 eye,
in vec3 ecPosition3,
in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
inout vec4 specular)
{
float nDotVP;
float nDotHV;
float pf;
float attenuation;
float d;
vec3 VP;
vec3 halfVector;

VP = vec3(gl_LightSource[i].position) - ecPosition3;
d = length(VP);
VP = normalize(VP);
attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +
gl_LightSource[i].linearAttenuation * d +
gl_LightSource[i].quadraticAttenuation * d * d);
halfVector = normalize(VP + eye);
nDotVP = max(0.0, dot(normal, VP));
nDotHV = max(0.0, dot(normal, halfVector));

if( nDotVP == 0.0 )
pf = 0.0;
else
pf = pow(nDotHV, gl_FrontMaterial.shininess);

ambient += gl_LightSource[i].ambient;
diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation;
specular += gl_LightSource[i].specular * pf * attenuation;
}

void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
vec3 ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;
vec3 eye = -normalize(ecPosition3);
vec3 normal = gl_NormalMatrix * gl_Normal;

vec4 amb;
vec4 diff;
vec4 spec;

PointLight(0, eye, ecPosition3, normal, amb, diff, spec);
gl_FrontColor = gl_FrontLightModelProduct.sceneColor +
amb * gl_FrontMaterial.ambient +
diff * gl_FrontMaterial.diffuse +
spec * gl_FrontMaterial.specular;
gl_FrontColor = vec4( 1.0, 1.0, 1.0, 1.0);
}

Peter_dup1
02-10-2005, 06:00 AM
Uh, yeah - I forgot, you will have to remove the last line in the main function;

gl_FrontColor = vec4( 1.0, 1.0, 1.0, 1.0);

left over from some debugging I was doing ;-)

jelly
02-13-2005, 10:02 AM
Thanks, this is a good starting point. It's a shame that there isn't a standard set of shaders that you can use for GLSL. I think nVidia supply one written in Cg, maybe a conversion is in order.

Also, I was wondering if anyone knows of any good web sites for sharing glsl shaders? I've written some that others could probably use, and I'm sure others have too. All the shaders I've found seem to be on individuals sites rather than a community site. There should be something on opengl.org really.

- Andy.

Relic
02-14-2005, 02:11 AM
How about www.shadertech.com (http://www.shadertech.com)