• Create Account

## half vector of two vectors

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1sheep19  Members

494
Like
0Likes
Like

Posted 20 September 2012 - 10:18 AM

I want to calculate the half vector of two vectors.

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

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.

### #2Inferiarum  Members

739
Like
0Likes
Like

Posted 20 September 2012 - 11:10 AM

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, 20 September 2012 - 11:10 AM.

### #3Álvaro  Members

20254
Like
0Likes
Like

Posted 20 September 2012 - 01:48 PM

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).

### #4sheep19  Members

494
Like
0Likes
Like

Posted 20 September 2012 - 02:37 PM

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)

### #5Álvaro  Members

20254
Like
0Likes
Like

Posted 20 September 2012 - 03:05 PM

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?

### #6carangil  Members

518
Like
0Likes
Like

Posted 20 September 2012 - 05:58 PM

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.

### #7sheep19  Members

494
Like
0Likes
Like

Posted 21 September 2012 - 08:22 PM

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
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).

### #8Álvaro  Members

20254
Like
0Likes
Like

Posted 22 September 2012 - 08:21 AM

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.

### #9Álvaro  Members

20254
Like
0Likes
Like

Posted 22 September 2012 - 08:28 AM

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

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

### #10sheep19  Members

494
Like
0Likes
Like

Posted 22 September 2012 - 08:42 AM

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

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

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

Edited by sheep19, 22 September 2012 - 08:43 AM.

### #11Álvaro  Members

20254
Like
0Likes
Like

Posted 22 September 2012 - 01:39 PM

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?

### #12sheep19  Members

494
Like
0Likes
Like

Posted 22 September 2012 - 09:27 PM

Ok, here are the results:

With 1 point light:

With 90 point lights (in a 30x30 grid)

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"?

### #13Álvaro  Members

20254
Like
1Likes
Like

Posted 22 September 2012 - 10:14 PM

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: Wikipedia seems to agree:

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.)

Edited by alvaro, 22 September 2012 - 10:17 PM.

### #14sheep19  Members

494
Like
0Likes
Like

Posted 23 September 2012 - 05:54 AM

Oh, I didn't know that. Thanks a lot!

Here it is now:

(rendering is much faster as well)

### #15Álvaro  Members

20254
Like
0Likes
Like

Posted 23 September 2012 - 09:28 AM

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.

### #16sheep19  Members

494
Like
0Likes
Like

Posted 23 September 2012 - 03:02 PM

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.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.