I'm trying to use a simple vertex/fragment program pair on a Radeon
9700 (under 7R28) to render some objects with per-pixel lighting (a
single point light). I am getting unexpected results, most definitely
due to a bug in my ARB programs, but I cannot spot the problem...
Basically, there is a single point light in the center of the ceiling.
Nothing fancy, really, but I am seeing arbitrary artefacts plus
incorrect lighting... I am certainly making some wrong computations
because if you look at the ceiling, you'll notice that the point light
looks more like a spot light -- and then some...
Vertex program:
!!ARBvp1.0
# Standard attributes and outputs mapping.
ATTRIB inPosition = vertex.position;
ATTRIB inNormal = vertex.normal;
OUTPUT outPosition = result.position;
OUTPUT outEyePosition = result.texcoord[0];
OUTPUT outNormal = result.texcoord[1];
# Standard parameters.
PARAM half = { 0.5, 0.5, 0.5, 0.5 };
PARAM two = { 2.0, 2.0, 2.0, 2.0 };
# Temporaries for transform operations.
TEMP eyePosition, eyePositionHomogeneous, eyeProjection;
# Compute position in clip coordinates (needed by fixed-function
stages).
DP4 outPosition.x, state.matrix.mvp.row[0], inPosition;
DP4 outPosition.y, state.matrix.mvp.row[1], inPosition;
DP4 outPosition.z, state.matrix.mvp.row[2], inPosition;
DP4 outPosition.w, state.matrix.mvp.row[3], inPosition;
# Compute position in eye and homogeneous coordinates (needed for
lighting).
DP4 eyePositionHomogeneous.x, state.matrix.modelview.row[0], inPosition;
DP4 eyePositionHomogeneous.y, state.matrix.modelview.row[1], inPosition;
DP4 eyePositionHomogeneous.z, state.matrix.modelview.row[2], inPosition;
DP4 eyePositionHomogeneous.w, state.matrix.modelview.row[3], inPosition;
RCP eyeProjection.w, eyePositionHomogeneous.w;
MUL eyePosition, eyePositionHomogeneous, eyeProjection.w;
MOV outEyePosition.xyz, eyePosition;
# Compute normal in eye coordinates.
TEMP normal;
DP3 normal.x, state.matrix.modelview.invtrans.row[0], inNormal;
DP3 normal.y, state.matrix.modelview.invtrans.row[1], inNormal;
DP3 normal.z, state.matrix.modelview.invtrans.row[2], inNormal;
MOV outNormal, normal;
# Finally, we pass through the vertex color.
MOV result.color.primary, vertex.color;
END
Fragment program:
!!ARBfp1.0
# Standard attributes and outputs mapping.
ATTRIB inEyePosition = fragment.texcoord[0];
ATTRIB inEyeNormal = fragment.texcoord[1];
OUTPUT outColor = result.color;
# Standard parameters.
PARAM half = { 0.5, 0.5, 0.5, 0.5 };
PARAM two = { 2.0, 2.0, 2.0, 2.0 };
# Main incoming color.
TEMP color;
MOV color, fragment.color.primary;
# Apply lighting on all used lights.
ALIAS normal = inEyeNormal;
TEMP coefficients, lighting;
TEMP ambient, diffuse, specular;
# Point/spot lights require additional variables.
# We also transform the eye in negated normalized coords.
TEMP eye;
MOV eye, inEyePosition;
DP3 eye.w, eye, eye;
RSQ eye.w, eye.w;
MUL eye.xyz, eye, -eye.w;
TEMP attenuation, distanceSquared, inverseDistance, direction,
halfVector;
# Compute direction vector from light to surface.
SUB direction, state.light[0].position, inEyePosition;
# Compute length of direction vector and normalize it.
DP3 distanceSquared.yz, direction, direction;
RSQ inverseDistance.yw, distanceSquared.y;
MUL direction, direction, inverseDistance.y;
# Compute attenuation.
DST attenuation, distanceSquared, inverseDistance;
DP3 attenuation, attenuation, state.light[0].attenuation;
# Compute normalized half-vector.
ADD halfVector, direction, eye;
DP3 distanceSquared.y, halfVector, halfVector;
RSQ inverseDistance.y, distanceSquared.y;
MUL halfVector, halfVector, inverseDistance.y;
# Compute normal . light direction and normal . light half vector.
DP3 coefficients.x, normal, direction;
DP3 coefficients.y, normal, halfVector;
# Compute lighting coefficients with attenuation.
MOV coefficients.w, state.material.shininess;
LIT lighting, coefficients;
MUL lighting.xyz, lighting, attenuation;
# Add lighting contribution.
MAD ambient, lighting.x, state.lightprod[0].ambient, ambient;
MAD diffuse, lighting.y, state.lightprod[0].diffuse, diffuse;
MAD specular, lighting.z, state.lightprod[0].specular, specular;
# Compute complete lighting contribution.
MOV lighting, state.material.emission;
ADD_SAT lighting, lighting, ambient;
ADD_SAT lighting, lighting, diffuse;
ADD_SAT lighting, lighting, specular;
MUL_SAT color, color, lighting;
# Output the computed color.
MOV outColor, color;
END
Anybody see where the problem(s) might be?
Thank you,
Dario
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Mac-opengl mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/mac-opengl/email@hidden