Effects and shaders for material animations
Author: Tim Moore <timoore@redhat.com>
This commit is contained in:
parent
6ca9a4868c
commit
bfaaef8aef
6 changed files with 149 additions and 0 deletions
14
Effects/material-diffuse.eff
Normal file
14
Effects/material-diffuse.eff
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PropertyList>
|
||||
<name>Effects/material-off</name>
|
||||
<inherits-from>Effects/model-default</inherits-from>
|
||||
<technique n="10">
|
||||
<pass>
|
||||
<program>
|
||||
<vertex-shader n="0">Shaders/mode-diffuse.vert</vertex-shader>
|
||||
<vertex-shader n="1">Shaders/mat-anim.vert</vertex-shader>
|
||||
<fragment-shader n="0">Shaders/mat-anim.frag</fragment-shader>
|
||||
</program>
|
||||
</pass>
|
||||
</technique>
|
||||
</PropertyList>
|
14
Effects/material-off.eff
Normal file
14
Effects/material-off.eff
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PropertyList>
|
||||
<name>Effects/material-off</name>
|
||||
<inherits-from>Effects/model-default</inherits-from>
|
||||
<technique n="10">
|
||||
<pass>
|
||||
<program>
|
||||
<vertex-shader n="0">Shaders/mode-off.vert</vertex-shader>
|
||||
<vertex-shader n="1">Shaders/mat-anim.vert</vertex-shader>
|
||||
<fragment-shader n="0">Shaders/mat-anim.frag</fragment-shader>
|
||||
</program>
|
||||
</pass>
|
||||
</technique>
|
||||
</PropertyList>
|
40
Shaders/mat-anim.frag
Normal file
40
Shaders/mat-anim.frag
Normal file
|
@ -0,0 +1,40 @@
|
|||
// -*-C++-*-
|
||||
|
||||
// Shader for use with material animations
|
||||
varying vec4 diffuse, constantColor, matSpecular;
|
||||
varying vec3 normal, lightDir, halfVector;
|
||||
varying float fogCoord, alpha;
|
||||
|
||||
uniform sampler2D texture;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 n, halfV;
|
||||
float NdotL, NdotHV, fogFactor;
|
||||
vec4 color = constantColor;
|
||||
vec4 texel;
|
||||
vec4 fragColor;
|
||||
vec4 specular = vec4(0.0);
|
||||
n = normalize(normal);
|
||||
if (!gl_FrontFacing)
|
||||
n = -n;
|
||||
NdotL = max(dot(n, lightDir), 0.0);
|
||||
if (NdotL > 0.0) {
|
||||
color += diffuse * NdotL;
|
||||
halfV = normalize(halfVector);
|
||||
NdotHV = max(dot(n, halfV), 0.0);
|
||||
if (gl_FrontMaterial.shininess > 0.0)
|
||||
specular.rgb = (matSpecular
|
||||
* gl_LightSource[0].specular.rgb
|
||||
* pow(NdotHV, gl_FrontMaterial.shininess));
|
||||
}
|
||||
color.a = alpha;
|
||||
// This shouldn't be necessary, but our lighting becomes very
|
||||
// saturated. Clamping the color before modulating by the texture
|
||||
// is closer to what the OpenGL fixed function pipeline does.
|
||||
color = clamp(color, 0.0, 1.0);
|
||||
texel = texture2D(texture, gl_TexCoord[0].st);
|
||||
fragColor = color * texel + specular;
|
||||
fogFactor = exp(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord);
|
||||
gl_FragColor = mix(gl_Fog.color, fragColor, fogFactor);
|
||||
}
|
39
Shaders/mat-anim.vert
Normal file
39
Shaders/mat-anim.vert
Normal file
|
@ -0,0 +1,39 @@
|
|||
// -*-C++-*-
|
||||
|
||||
// Shader that uses OpenGL state values to do per-pixel lighting
|
||||
//
|
||||
// The only light used is gl_LightSource[0], which is assumed to be
|
||||
// directional.
|
||||
//
|
||||
// Diffuse colors come from the gl_Color, ambient from the material. This is
|
||||
// equivalent to osg::Material::DIFFUSE.
|
||||
vec4 ambientColor();
|
||||
vec4 diffuseColor();
|
||||
vec4 specularColor();
|
||||
vec4 emissionColor();
|
||||
|
||||
varying vec4 diffuse, constantColor, matSpecular;
|
||||
varying vec3 normal, lightDir, halfVector;
|
||||
varying float alpha, fogCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
|
||||
vec3 ecPosition3 = vec3(gl_ModelViewMatrix * gl_Vertex) / ecPosition.w;
|
||||
gl_Position = ftransform();
|
||||
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
|
||||
normal = gl_NormalMatrix * gl_Normal;
|
||||
lightDir = normalize(vec3(gl_LightSource[0].position));
|
||||
halfVector = normalize(gl_LightSource[0].halfVector.xyz);
|
||||
diffuse = diffuseColor() * gl_LightSource[0].diffuse;
|
||||
// Super hack: if diffuse material alpha is less than 1, assume a
|
||||
// transparency animation is at work
|
||||
if (gl_FrontMaterial.diffuse.a < 1.0)
|
||||
alpha = gl_FrontMaterial.diffuse.a;
|
||||
else
|
||||
alpha = diffuse.a;
|
||||
constantColor = emissionColor()
|
||||
+ ambientColor() * (gl_LightModel.ambient + gl_LightSource[0].ambient);
|
||||
fogCoord = abs(ecPosition3.z);
|
||||
matSpecular = specularColor();
|
||||
}
|
21
Shaders/mode-diffuse.vert
Normal file
21
Shaders/mode-diffuse.vert
Normal file
|
@ -0,0 +1,21 @@
|
|||
// -*-C++-*-
|
||||
|
||||
vec4 ambientColor()
|
||||
{
|
||||
return gl_FrontMaterial.ambient;
|
||||
}
|
||||
|
||||
vec4 diffuseColor()
|
||||
{
|
||||
return gl_Color;
|
||||
}
|
||||
|
||||
vec4 specularColor()
|
||||
{
|
||||
return gl_FrontMaterial.specular;
|
||||
}
|
||||
|
||||
vec4 emissionColor()
|
||||
{
|
||||
return gl_FrontMaterial.emission;
|
||||
}
|
21
Shaders/mode-off.vert
Normal file
21
Shaders/mode-off.vert
Normal file
|
@ -0,0 +1,21 @@
|
|||
// -*-C++-*-
|
||||
|
||||
vec4 ambientColor()
|
||||
{
|
||||
return gl_FrontMaterial.ambient;
|
||||
}
|
||||
|
||||
vec4 diffuseColor()
|
||||
{
|
||||
return gl_FrontMaterial.diffuse;
|
||||
}
|
||||
|
||||
vec4 specularColor()
|
||||
{
|
||||
return gl_FrontMaterial.specular;
|
||||
}
|
||||
|
||||
vec4 emissionColor()
|
||||
{
|
||||
return gl_FrontMaterial.emission;
|
||||
}
|
Loading…
Reference in a new issue