• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sheep19

half vector of two vectors

15 posts in this topic

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

Share this post


Link to post
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
0

Share this post


Link to post
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).
0

Share this post


Link to post
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)
0

Share this post


Link to post
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?
0

Share this post


Link to post
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.
0

Share this post


Link to post
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
// 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;
}
[/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).
0

Share this post


Link to post
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.
0

Share this post


Link to post
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]

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

Share this post


Link to post
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]

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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
I meant specular light, not diffuse, my bad again. Edited by sheep19
0

Share this post


Link to post
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?
0

Share this post


Link to post
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"?
0

Share this post


Link to post
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
1

Share this post


Link to post
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)
0

Share this post


Link to post
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.
0

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0