Reputation: 152
I hope you all are doing very fine.
I'm currently working on wpf application. I'm a newbie to wpf. I'm creating a scene which represents a lab in wpf. I'm having a very hard time with setting up camera. I don't know where the origin of camera starts i.e (0,0,0) or default position. I was playing with values of position but I cannot pin point the location of camera. I've to zoom out and put cursor to
<ModelVisual3D>
to view the position of camera. and also I dont know what these lines show
<PerspectiveCamera Position="-40,40,40"/> //a little modification via mouse to show these three lines appropriate
When I use position= 0,0,1 it stays inside window but when I use 1,0,2 it vanished and have to relocate it.
There is a similar code available on stackoverflow, it says:
Position="12.5,50,20" LookDirection="0,-.7,-0.35" //it works perfectly :(
My question is that how to understand this position property and look direction(completely no idea, Have searched it but looking for simple explanation). Similar code works perfectly but when I try to write my own, I get stuck in position of camera.
Please help me out. All suggestions are welcome. I'm using VS2012 with .Net 4.5
Upvotes: 4
Views: 2202
Reputation: 353
I guess the problem stems from the fact that whenever one specifies a LookDirection one often thinks this value is a point in space the camera is looking at. Wrong! For example, specifying LookPosition equal to 0,0,0 does not mean the camera is looking towards the origin as some of the earlier comments suggest.
The solution lays in the word 'LookDirection' itself: it mentions 'direction'. Now, although the way one specifies the LookPosition in XAML is the same as the Position of the camera is specified, using three comma separated and ordered numbers, these numbers mean something different in both cases. Position is of type Point3D while LookDirection is of type Vector3D. Therefore, when entering the value of the LookDirection, one in fact specifies a vector. Not a point.
Without going into too much vector details it's necessary to mention that the vector we're talking about here is an Euclidean vector that is characterized by a magnitude and a direction. In case the vector is defined by three comma separated, ordered numbers (a triplet), the vector is called a coordinate vector. Such a coordinate vector always implies the existence of a base and an origin and it's this origin that is important to find the direction the camera is looking in.
To find out what your camera is aiming at (and why you might not see the thing you want to see), follow this easy procedure: imagine yourself standing in the origin (0,0,0) while looking to the point specified by the LookDirection. It's this very direction the camera, although located in another point, is also looking in.
In the picture below an object (box) is located in the origin. The Position of our camera is set to (-2,1,2). Question: what should the LookDirection be set to in order to see the box on our screen? Suppose while standing at (0,0,0) you're looking in the direction of e.g. (-2,1,-1). Will the box be visible on your screen? The answer is no because by setting the LookDirection to (-2,1,-1) your camera is pointed in the direction of the point (-4,2,1). This point is found by sliding (translating) the vector's initial point to the point the camera is located and then take the vector's terminal point.
So, what point should we actually set the LookDirection to? To find this out put yourself again in the origin (0,0,0) and look in the same direction as the red arrow is pointing. Imagine for a moment you standing there physically. In that case you would feel the red arrow pointing in your back while your sight would somehow follow a line that is an extension of the red arrow. Every point on this imaginary arrow extension that you can see (not in your back), can serve as a direction vector, that is, a valid LookDirection value. For instance, (2,-1,-2) is a good one. Moreover, every multiple of this value would be ok. Therefore, (4,-2,-4) will also work. So does (1,-0.5,-1). It's not necessary it's a multiple; non-multiples such as (2.1,-1,-2) are also allowed but in in this case you'll notice the object on your screen is not longer centered. Remember, the object is located right in the origin.
Let's take it it to the next level: the object is not located in the origin but e.g. in (-2,1,-1) while the camera is still in (-2,1,2). What should be the LookDirection value? Again, imagine yourself standing in the origin and looking in the same direction you want the camera to be pointed in in order to see something on your screen. There is a difference with the previous example in which the red arrow was pointing in your back. That in fact was a special case: unless the object is also located in the origin, the red arrow will not point in your back as the second example shows. Again, draw an imaginary line starting at the origin where you're standing and following your sight. All points on this line you are able to see while you are standing in the origin are valid LookDirection values. For example the point (0,0,-3). But also, as in the previous example, all multiples are ok. Pay attention though, (0,0,3) is also a multiple but this point is located in your back when standing in the origin. Therefore, this is not a good LookDirection value.
Let us conclude by offering a general rule to find a proper LookDirection value. Take the point you want the camera to focus on and subtract the point indicating the camera position. The value you get is a proper LookDirection value. Let us apply this rule to the two examples. In the first example we have (0,0,0) - (-2,1,2) resulting in (2,-1,-2). In the second example we get a value by subtracting (-2,1,2) from (-2,1,-1). This equals (0,0,-3).
Upvotes: 3