It’s severe. I’m getting hit with a rendering time 3x as long as using OpenGL FFP.
and this is only for vertex lighting.
my fragment program for bump mapping is useless. I get bored between frames
.
In case you or anyone wants to see what I’m doing here’s the vertex shader:
#pragma debug(on)
vec4 ecPosition; //homogeneous eye coordinate vector.
vec3 ecPosition3; //nonhomogeneous eye coordinate vector.
varying vec3 normal; //calculated vertex normal.
varying vec3 eye; //eye vector.
struct TexInfo
{
uniform sampler2D tex; //texture unit
int application; //application(modulate,decal,interpolate,etc...)
float transp; //overall texture tranparency level
int method; //method(reflective,projected,etc...)
int enabled; //is this texture unit enabled?
};
//max texture units exposed to application is currently 4. There are some issues with iterating over an array inside a loop which is controlled
//by a uniform variable. driver issues? not currently implemented? I have no idea at the moment.
uniform TexInfo textureInfo[4];
const int MAX_LIGHTS = 8; //restricted to 8 lights for now
uniform int lightEnabled[MAX_LIGHTS]; //is light n enabled?
//######################################################################################### LIGHTING
//......------=========================================================------......
// Calculate light contribution from Directional Light i
//====================================================================
void DirectionalLight(in int i, in vec3 normal, inout vec4 ambient,inout vec4 diffuse, inout vec4 specular)
{
float nDotVP; //normal . light direction
float nDotHV; //normal . light half vector
float pf; //power factor
nDotVP = max(0.0,dot(normal,vec3(gl_LightSource[i].position)));
nDotHV = max(0.0,dot(normal,vec3(gl_LightSource[i].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;
specular += gl_LightSource[i].specular*pf;
}
//......------=========================================================------......
// Calculate light contribution from Point Light i
//====================================================================
void PointLight(in int i,in vec3 normal,inout vec4 ambient,inout vec4 diffuse,inout vec4 specular)
{
float nDotVP; //normal . light direction
float nDotHV; //normal . light half vector
float pf; //power factor
float attenuation; //computed attenuation factor
float d; //distance from surface to light source
vec3 VP; //direction from surface to light position
vec3 halfVector; //direction of maximum highlights
//compute distance between surface to light position
VP = vec3 (gl_LightSource[i].position) - ecPosition3;
//compute distance between surface and light position
//d = length(VP);
//normalize the vector from surface to light position
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;
}
//......------=========================================================------......
// Calculate light contribution from Spot Light i
//====================================================================
void SpotLight(in int i,in vec3 normal,inout vec4 ambient,inout vec4 diffuse,inout vec4 specular)
{
float nDotVP; //normal . light direction
float nDotHV; //normal . light half vector
float pf; //power factor
float spotDot; //cosine of angle between spotlight
float spotAttenuation; //spotlight attenuation factor
float attenuation;//computed attenuation factor
float d; //distance from surface to light source
vec3 VP; //direction from surface to light position
vec3 halfVector; //direction of maximum highlights
//compute distance between surface to light position
VP = vec3 (gl_LightSource[i].position) - ecPosition3;
//compute distance between surface and light position
d = length(VP);
//normalize the vector from surface to light position
VP = normalize(VP);
attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + gl_LightSource[i].linearAttenuation * d + gl_LightSource[i].quadraticAttenuation * d * d);
//see if point on surface is inside cone of illumination
spotDot = dot(-VP,gl_LightSource[i].spotDirection);
if(spotDot < gl_LightSource[i].spotCosCutoff)
spotAttenuation = 0.0;
else
spotAttenuation = pow(spotDot,gl_LightSource[i].spotExponent);
//combine the spotlight and distance attenuation
attenuation *= spotAttenuation;
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;
}
//......------=========================================================------......
// Iterate through all lights and accumulate each one's contribution
//====================================================================
void PerformBasicLighting()
{
//Perform lighting calculations
vec4 amb;
vec4 diff;
vec4 spec;
//clear the light intensity accumulators
amb = vec4(0.0);
diff = vec4(0.0);
spec= vec4(0.0);
//int i = 0;
//loop through all lights, compute contribution from each.
for(int i=0; i < 8.0; i++) //hardcoded at the moment (8.0 = Maximum lights in scene)
{
if(lightEnabled[i] != 0)
{
if(gl_LightSource[i].position.w == 0.0)
DirectionalLight(i,normal,amb,diff,spec);
else if(gl_LightSource[i].spotCutoff == 180.0)
PointLight(i,normal, amb, diff, spec);
else
SpotLight(i,normal, amb, diff, spec);
}
}
vec4 color;
color = gl_FrontMaterial.diffuse * gl_LightModel.ambient +
/*amb * gl_FrontMaterial.ambient + */
diff * gl_FrontMaterial.diffuse;
color += spec * gl_FrontMaterial.specular;
color.a = gl_FrontMaterial.diffuse.a;
gl_FrontColor = color;
}
//##################################################################################################### TEXTURING
//......------=========================================================------......
// Calculate Sphere Map texture Coordinates
//====================================================================
vec2 SphereMap(in vec3 ecPosition3, in vec3 normal)
{
float m;
vec3 r,u;
u = normalize(ecPosition3);
r = reflect(u,normal);
m = 2.0*sqrt(r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0));
return vec2 (r.x / m + 0.5, r.y / m + 0.5);
}
//......------=========================================================------......
// Calculate Reflection Map texture Coordinates
//====================================================================
vec3 ReflectionMap(in vec3 ecPosition3, in vec3 normal)
{
//float NdotU,m;
vec3 u;
u = normalize(ecPosition3);
return (reflect(u,normal));
}
//......------=========================================================------......
// Generate Texture Coordinates
//====================================================================
void TexCoordGen()
{
const int PROJECTIVE = 0;
const int REFLECTIVE = 1;
vec3 reflection;
vec2 sphereMap;
for(int i=0; i < 4; i++)
{
if(textureInfo[i].enabled != 0)
{
/*if(textureInfo[i].method == PROJECTIVE)
{
gl_TexCoord[i].s = dot(ecPosition,gl_EyePlaneS[i]);
gl_TexCoord[i].t = dot(ecPosition,gl_EyePlaneT[i]);
gl_TexCoord[i].p = dot(ecPosition,gl_EyePlaneR[i]);
gl_TexCoord[i].q = dot(ecPosition,gl_EyePlaneQ[i]);
}*/
if(textureInfo[i].method == REFLECTIVE)
{
reflection = ReflectionMap(ecPosition3,normal);
gl_TexCoord[i] = vec4(reflection,1.0);
}
else if(textureInfo[i].method == -1)
{
gl_TexCoord[i] = gl_MultiTexCoord0;
}
gl_TexCoord[i] *= gl_TextureMatrix[i];
}
}
}
//#################################################################################################### MAIN
void main()
{
gl_Position = ftransform();
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
ecPosition = gl_ModelViewMatrix*gl_Vertex;
ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;
eye = -normalize(ecPosition3);
normal = normalize(gl_NormalMatrix * gl_Normal);
normal *= gl_NormalScale;
PerformBasicLighting();
TexCoordGen();
}
I’m sort of at a loss. I’m hoping it’s driver issues.