Well, since the language you are using is not precise enough to convert to a formula, perhaps you can explain what you are trying to accomplish. What do a, b and c represent?
What do you want the half-angle for? Is the resulting magnitude significant? If you just want a direction, and A and B are already norm vectors, A+B will always point in the 'halfway' direction, except for 1 bad case I describe below. You will probably want to normalize the result.
The reason why you want A and B to already be normalized is to make them the same length, so they are added together with the same 'weight.' If A is twice as long as B, then the angle won't be halfway beween A and B, it'll be heavily slanted more towards A. However, if A and B are already known to be the same length (suppose A and B are both length of 3), then the initial normalization is not necessary.
If A and B aren't normalized or the same length, you want to do:
An = A Bn = B
An.normalize(); Bn.normalize();
C = An + Bn;
C.normalize();
C is the normalized, unit length vector halfway between A and B.
One thing you have to be prepared for is the case where A and B are opposites. If A == -B, then A+B will be zero, and you won't have a half angle vector using this method. In this special case, A and B are a straight line; a 180 degree angle, and there are two possible half angles.
I'm calculating the light of a pixel using the Phong shading model. To calculate the diffuse light, I need the half vector of: The hit point towards the light source and hit point towards the camera.
const Vector3 viewDir = -ray.d; // d is a unit vector const Lights *lightlist = scene.lights(); // loop over all of the lights Lights::const_iterator lightIter; for (lightIter = lightlist->begin(); lightIter != lightlist->end(); ++lightIter) { PointLight* pLight = *lightIter;
Vector3 l = pLight->position() - hit.P; // find the vector from the hit point to the light //l = l.normalized(); Vector3 I = pLight->color(); L += I * ka; // add ambient component // add specular highlights HitInfo hit2; if( !scene.trace(hit2, Ray(hit.P, pLight->position()), 0.001f) ) { L += I * kd * fabs(dot(l.normalized(), hit.N.normalized())); // add diffuse component Vector3 H = ((l + ray.d) * 0.5f).normalized(); // find the half vector, H L += I * ks * pow(fabs(dot(hit.N.normalized(), H)), m); } }
return L; }
The H vector is calculate like this: Vector3 H = ((l + ray.d) * 0.5f).normalized(); // find the half vector, H
l is not normalized. I don't think ray.d is normalized as well.
If I write Vector3 H = ((l.normalized() + ray.d.normalized()) * 0.5f).normalized(); the results don't seem to be correct - no diffuse light is visible on my objects (spheres). With the prev equation specular highlights appear correctly (I think).
I'm calculating the light of a pixel using the Phong shading model. To calculate the diffuse light, I need the half vector of: The hit point towards the light source and hit point towards the camera.
Are you sure that's what you want? The diffuse light in the Phong reflection model does not depend on the camera position.