half vector of two vectors

Started by
14 comments, last by sheep19 11 years, 6 months ago
I want to calculate the half vector of two vectors.

(In the picture below, I want to calculate vector C)

2na0hs5.jpg


The vectors a and b are in 3D space - but I guess this doesn't make difference.

What I am currently doing is:
c.x = (a.x + b.x) / 2
c.y = (a.y + b.y) / 2
c.z = (a.z + b.z) / 2

It seems to be working for some examples on paper, but I dont't know if it is the correct way to do it.
Could someone enlighten me?

Thanks.
Advertisement
With what you are doing, the tip of c is directly in the middle of the line connecting the tips of a and b.

This may or may not be what you want to do.
Are all three vectors supposed to have unit length? If so, remember to normalize the result (i.e., divide each component by the length of the vector).

Are all three vectors supposed to have unit length? If so, remember to normalize the result (i.e., divide each component by the length of the vector).


That's what I am doing:
c = (a + b) * 0.5f; // a, b are NOT normalized. do they have to be?
c.normalize();

(I'm not using a or b after that)
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.

Here is my code:


Vector3 Phong::shade(const Ray& ray, const HitInfo& hit, const Scene& scene) const
{
Vector3 L = Vector3(0, 0, 0);

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.

const Vector3 viewDir = -ray.d; // d is a unit vector



I don't think ray.d is normalized as well.


You seem to be contradicting the comments in your own code.

[quote name='sheep19' timestamp='1348280573' post='4982552']
const Vector3 viewDir = -ray.d; // d is a unit vector



I don't think ray.d is normalized as well.


You seem to be contradicting the comments in your own code.
[/quote]

Sorry. Most of that code was given to us by the lab assistant, and I didn't notice it smile.png
I meant specular light, not diffuse, my bad again.

This topic is closed to new replies.

Advertisement