This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What exactly is the UP vector in OpenGL's LookAt function?
This is related to: What exactly is the UP vector in OpenGL's LookAt function?
If the call is:
gluLookAt(512, 384, 2000,
512, 384, 0,
0.0f, 1.0f, 0.0f);
If I am sitting on a chair looking straight ahead, holding an iPad right in front of my eyes, then my top of my head is pointing up the sky. Therefore the (0, 1, 0) for the UP vector, as on the 3rd row. How about if I change it to (0, 0.00001, 1)? That means I am almost lying down, with now my face and eyes facing the sky. So how come the result is exactly the same as when I use (0, 1, 0)?
What could you possibly expect to happen?
You pass 3 sets of values: a camera position, a position for the camera to look at, and the direction of up. In your analogy, if you're looking up at the sky, you're not looking at your iPad. Therefore, your look-at position must have changed along with your up direction. And if you didn't change your look-at position, then what do you expect to happen when you change the up direction?
The up direction only affects where up is relative to where you're looking. If you want to change what you're looking at, you must actually change the look-at point. That's why it's there.
After more trial and error, as I just started learning OpenGL for one day, is that, the Up vector must have some components in the plane that is "normal" (or perpendicular) to the camera to target vector.
In other words, in the example, it is from (512, 384, 2000) to (512, 384, 0), so the vector is only in the Z-direction. The Up vector must have some components on the XY plane (XY plane is the one that is perpendicular to the vector that has only the Z direction).
If there is no x and no y component, that is, if they are both 0, then on my iPad 2, the image is not displayed at all. So the Up vector deals with rotation in the XY plane in this case, and not case about the Z direction at all.
Related
I want to rotate my model Matix in x, y, and z direction, but it rotates in an unexpected way.
I use Qt.
QMatrix4x4 mMatrix;
mMatrix.setToIdentity();
mMatrix.rotate(yAngle, QVector3D(0, 1, 0));
mMatrix.rotate(zAngle, QVector3D(0, 0, 1));
mMatrix.translate(cube->getPosition());
After the first rotation the followed rotations rotate around the base of the new model Matrix, while I want the followed rotations to rotate around the origin.
I drew a little sketch so my problem might be clearer (black shows how it is right now, green is how i want it to be):
The green arrow shows how i want it to be.
I think you will get the desired result if you simply reverse the order of calls you make on the QMatrix4x4 class. To combine the two rotations:
QMatrix4x4 mMatrix;
mMatrix.setToIdentity();
mMatrix.rotate(zAngle, QVector3D(0, 0, 1));
mMatrix.rotate(yAngle, QVector3D(0, 1, 0));
The documentation is kind of lacking, but from looking at the QMatrix4x4 source code, I'm getting the impression that it applies matrix operations the way it's more or less standard with matrix libraries that are used for OpenGL, and the way the OpenGL fixed pipeline used to work.
This means that when you combine matrices, the new matrix is multiplied from the right. As a result, when the combined matrix is applied to vectors, the last specified sub-transformation is applied to the vectors first. Or putting it differently, you specify transformations in the reverse of the order you want them applied.
In your example, it you want to rotate around the y-axis first, then around the z-axis, you need to specify the z-rotation first, then the y-rotation.
I got figured it out by myself, each object needs to have its own rotation Matrix / Quaterunion.
A new rotation around the world origin ist done by creating a rotation matrix with the wanted rotation and right multiply the exiting rotation matrix of the object.
Well, if you want the green arrow thing to happen, then it means that you want a rotation around the x-axis, 90 degrees in the negative direction (or 270 degrees in the positive direction).
To make things simple, think of it like this:
Which of the axes there remains the same? x, right? Since x doesn't change, it looks like you're rotating around the x, and you are.
Now, point your thumb towards the direction that positive x is directed at and casually close the rest of your fingers like a cylinder. The direction that those rest of your fingers curling at is the direction of the rotation.
Since you want a rotation of 90 degrees in the opposite direction that those 4 fingers of yours are curling at, or 270 in the same direction... yeah.
(1, 0, 0) would be the direction vector pointing towards the positive x. While I don't know so much about the functions you're using, my guess is that the second call should be:
mMatrix.rotate(270, QVector3D(1, 0, 0));
Instead, or optionally -90 if it supports negative values.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What exactly is the UP vector in OpenGL's LookAt function?
This is related to: What exactly is the UP vector in OpenGL's LookAt function?
If the call is:
gluLookAt(512, 384, 2000,
512, 384, 0,
0.0f, 1.0f, 0.0f);
If I am sitting on a chair looking straight ahead, holding an iPad right in front of my eyes, then my top of my head is pointing up the sky. Therefore the (0, 1, 0) for the UP vector, as on the 3rd row. How about if I change it to (0, 0.00001, 1)? That means I am almost lying down, with now my face and eyes facing the sky. So how come the result is exactly the same as when I use (0, 1, 0)?
What could you possibly expect to happen?
You pass 3 sets of values: a camera position, a position for the camera to look at, and the direction of up. In your analogy, if you're looking up at the sky, you're not looking at your iPad. Therefore, your look-at position must have changed along with your up direction. And if you didn't change your look-at position, then what do you expect to happen when you change the up direction?
The up direction only affects where up is relative to where you're looking. If you want to change what you're looking at, you must actually change the look-at point. That's why it's there.
After more trial and error, as I just started learning OpenGL for one day, is that, the Up vector must have some components in the plane that is "normal" (or perpendicular) to the camera to target vector.
In other words, in the example, it is from (512, 384, 2000) to (512, 384, 0), so the vector is only in the Z-direction. The Up vector must have some components on the XY plane (XY plane is the one that is perpendicular to the vector that has only the Z direction).
If there is no x and no y component, that is, if they are both 0, then on my iPad 2, the image is not displayed at all. So the Up vector deals with rotation in the XY plane in this case, and not case about the Z direction at all.
I am attempting to cast a ray from the center of the screen and check for collisions with objects.
When rendering, I use these calls to set up the camera:
GL11.glRotated(mPitch, 1, 0, 0);
GL11.glRotated(mYaw, 0, 1, 0);
GL11.glTranslated(mPositionX, mPositionY, mPositionZ);
I am having trouble creating the ray, however. This is the code I have so far:
ray.origin = new Vector(mPositionX, mPositionY, mPositionZ);
ray.direction = new Vector(?, ?, ?);
My question is: what should I put in the question mark spots? I.e. how can I create the ray direction from the pitch and roll?
I answered a question not unlike your's just recently. So I suggest you read this: 3d coordinate from point and angles
This applies to your question as well, only that you don't want just a point, but a ray. Well, remember that a point can be assumed a displacement-from-origin vector and that a ray is defined as
r(t) = v*t + s
In your case, s is the camera position, and v would be a point relative to the camera's position. You figure the rest (or ask, if things are still unclear).
Trying to understand gluLookAt, especially the last 3 parameters.
Can someone please explain ?
gluLookAt(camera[0], camera[1], camera[2], /* look from camera XYZ */
0, 0, 0, /* look at the origin */
0, 1, 0); /* positive Y up vector */
What exactly does it mean by "positive Y up vector" ?
Is it possible to have the last up-vector 3 parameters as all 1s, e.g. 1, 1, 1 ? And, if it is possible, what exactly does it mean ?
Is it possible for the up vector to have value more than 1, e.g. 2, 3, 4 ?
Thanks.
Sketchup to the rescue!
Your image has an 'up' to it that can be separate from the world's up. The blue window in this image can be thought of as the 'near-plane' that your imagery is drawn on: your monitor, if you will. If all you supply is the eye-point and the at-point, that window is free to spin around. You need to give an extra 'up' direction to pin it down. OpenGL will normalize the vector that you supply if it isn't unit length. OpenGL will also project it down so that it forms a 90 degree angle with the 'z' vector defined by eye and at (unless you give an 'up' vector that is in exactly the same direction as the line from 'eye' to 'at'). Once 'in' (z) and 'up' (y) directions are defined, it's easy to calculate the 'right' or (x) direction from those two.
In this figure, the 'supplied' up vector is (0,1,0) if the blue axis is in the y direction. If you were to give (1,1,1), it would most likely rotate the image by 45 degrees because that's saying that the top of the blue window should be pointed toward that direction. Consequently the image of the guy would appear to be tipped (in the opposite direction).
The "up vector" of gluLookAt is just the way the camera is oriented. If you have a camera at a position, looking directly at an object, there is one source of freedom still remaining: rotation. Imagine a camera pointed directly at an object, fixed in place. But, your camera can still rotate, spinning your image. The way OpenGL locks this rotation in place is with the "up vector."
Imagine (0, 0, 0) is directly at your camera. Now, the "up vector" is merely a coordinate around your camera. Once you have the "up vector," though, OpenGL will spin your camera around until directly the top of your camera is facing the coordinate of the "up vector".
If the "up vector" is at (0, 1, 0), then your camera will point normally, as the up vector is directly above the camera, so the top of the camera will be at the top, so your camera is oriented correctly. Move the up vector to (1, 1, 0), though, and in order to point the top of the camera to the "up vector," your camera will need to rotate be 45 degrees, rotating the entire image by 45 degrees.
This answer was not meant as an in-depth tutorial, but rather as a way to grasp the concept of the "up vector," to help better understand the other excellent answers to your question.
first 3 parameters are camera position
next 3 parameters are target position
the last 3 parameters represent the rolling of camera.
Very important thing use gluLookAt after "glMatrixMode(GL_MODELVIEW);"
Other useful hint always specify it 0,0,1 for last 3 parameters.
In general co-ordinates are written as x,y,z. z is up vector.
the 0,1,0 leads in confusion as x,z,y.
so best thing leave it to 0,0,1.
the last vector, also known as the cameras up-vector defines the orientation of the camera.
imagine a stick attached to the top of a "real" camera. the stick's direction is the up-vector.
by changing it from (0,1,0) you can do sideways rolling.
this is basic question. im having touble setting the camra not how to set it but what values should i set it to. is there any app that can help with setting camra like you set it and it gives you the values or can you explain what the values stand for and how to are they scaled.
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (value, value, value), // the camera position
&D3DXVECTOR3 (value, value, value), // the look-at position
&D3DXVECTOR3 (value, value, value));
The D3DXMatrixLookAtLH function is generating a camera matrix, that gets stored in your matView.
After that, the function gets three vectors:
position
look-at
up
Those three vectors stand for:
the position of your camera - where your camera is. It could be (0,0,0) for example. (Those are your x, y and z coordinates in the world.)
the look-at point - this is where your camera looks at. It consists of your position + your (usually) normalized view direction. So when you stand at (0,0,0) and want to look down the negative z-axis, your look-at point is (0,0,-1). If you stand at (1,2,3) and look down the negative x-axis, it's (0,2,3)
the up-vector points up - normally this is (0,1,0).
If you move around now, without looking around, the position and the look-at vectors change to reflect your new position.
If you stand still and look around, only the look-at point changes.
The up-vector only changes if you roll the camera.
There are plenty of nice camera tutorials out there, that show you how to change those three vectors when looking around with the camera - for example this one.