half vector of two vectors

This topic is 1945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I want to calculate the half vector of two vectors.

(In the picture below, I want to calculate vector [b]C[/b])

[img]http://i50.tinypic.com/2na0hs5.jpg[/img]

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.

Share on other sites
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. Edited by Inferiarum

Share on other sites
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).

Share on other sites
[quote name='alvaro' timestamp='1348170515' post='4982144']
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).
[/quote]

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

(I'm not using a or b after that)

Share on other sites
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?

Share on other sites
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.

Share on other sites
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:

[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
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;
}
[/CODE]

The H vector is calculate like this:
Vector3 H = ((l + ray.d) * 0.5f).normalized(); // find the half vector, H

[b]l[/b] is not normalized.
I don't think [b]ray.d[/b] 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).

Share on other sites
[quote name='sheep19' timestamp='1348280573' post='4982552']
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.
[/quote]
Are you sure that's what you want? The diffuse light in the Phong reflection model does not depend on the camera position.

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

[quote name='sheep19' timestamp='1348280573' post='4982552']
I don't think ray.d is normalized as well.
[/quote]

Share on other sites
[quote name='alvaro' timestamp='1348324115' post='4982664']
[quote name='sheep19' timestamp='1348280573' post='4982552']
const Vector3 viewDir = -ray.d; // d is a unit vector
[/quote]

[quote name='sheep19' timestamp='1348280573' post='4982552']
I don't think ray.d is normalized as well.
[/quote]

[/quote]

Sorry. Most of that code was given to us by the lab assistant, and I didn't notice it [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
I meant specular light, not diffuse, my bad again. Edited by sheep19

Share on other sites
Perhaps your problem has nothing to do with computing the vector half-way between two other vectors.

I suggest you set up a very simple situation so instead of "the results don't look correct", you can actually get a specific call to this code that doesn't do the right thing. Then fire up the debugger and see where it's going wrong.

For instance (make a picture ignoring the coordinate z to follow along), if you look from the origin along (1,0,0) at a sphere with center (3,-1,0) and radius sqrt(2), you will hit it at (2,0,0). At that point the normal to the sphere is (-sqrt(0.5),sqrt(0.5),0). If you put a light source at (2,10,0), you should be smack in the middle of the specular highlight. What does your code do in that case?

Share on other sites
Ok, here are the results:

With 1 point light:
[img]http://i48.tinypic.com/2e21dly.png[/img]

With 90 point lights (in a 30x30 grid)
[img]http://i48.tinypic.com/2e3tn6a.png[/img]

I understand why the 1st image looks like that - because it's only one light source, and it is directional.

The 2nd one is much better. I think it is "correct"?

Share on other sites
You should not add a diffuse or specular contribution if the dot product between the normal and the direction to the light is negative (you have "fabs" in there, which creates the artifact in the middle part of the sphere on the first picture).

EDIT: [url="http://en.wikipedia.org/wiki/Phong_reflection_model"]Wikipedia [/url]seems to agree:
[quote]Although the above formulation is the common way of presenting the Phong reflection model, each term should only be included if the term's dot product is positive. (Additionally, the specular term should only be included if the dot product of the diffuse term is positive.)[/quote] Edited by alvaro

Share on other sites
Oh, I didn't know that. Thanks a lot!

Here it is now:
[img]http://i50.tinypic.com/2h8bzex.jpg[/img]

(rendering is much faster as well)

Share on other sites
If you put the light above the object, or perhaps a little bit behind the camera, you'll get a better view of the specular highlight and you'll have a better idea of whether it looks right.

Share on other sites
[quote name='alvaro' timestamp='1348414089' post='4982936']
If you put the light above the object, or perhaps a little bit behind the camera, you'll get a better view of the specular highlight and you'll have a better idea of whether it looks right.
[/quote]

It does - I moved the camera and saw it, but I didn't send that picture.