Some question about transformation

Started by
10 comments, last by steven katic 10 years ago
Hello, I think my previous postings were a bit confusing. I just wanted to clarify that in this posting. I have a plane and attached figure shows two positions of the plane, (a) no rotation and (b) with rotation where rotation axis is defined by ax + by + cz.

Now, the plane will rotate, but it will always look without rotation as shown in figure (a).

I think I need to play around with my glutReshape function which looks like follows:

/*******************************/
void glutReshape(int width, int height)
{

width = glutGet(GLUT_WINDOW_WIDTH);
height = glutGet(GLUT_WINDOW_HEIGHT);

if (width && height)
{
glViewport(0, 0, width, height);

nearDist = 150.0 / tan((kFovY / 2.0) * kPI / 180.0);
farDist = nearDist + 100.0;
aspect = (double) width / height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(kFovY, aspect, 0.1, farDist+20);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt( 0.0, 0.0, (nearDist + 5.0), // camera/ eye
0.0, 0.0, 0.0, // center
0.0, 1.0, 0.0); // up vector

}
}
/*******************************/
But I fail to correctly manipulate it. When rotation axis will move from (0, 0, 1) to (ax + by + bz), the eye/ camera will be changed from ( 0, 0, nearDist +5) to {(nearDist+5)*ax, (nearDist+ 5)*ay, (nearDist + 5)*az}. Is it right?

But what about the up vector? How will it change for this rotation? How can I calculate new up vector? Also what other parameters in my glutReshape function needs to be changed?

Please provide me some suggestion so that I can fix this problem.

Thanks in advance.
Advertisement

from what I am understanding, you wish to manipulate plane by rotating it not in its world transformation but in view transformation. You could effectively do this by making glulookat matrix corespond to plane normal you have as the rotaion definition. In that case you can make eye-at vector to have the direction of that normal. Meaning, have lookat point to be the plane center (or so) always, and compute eye position vector from lookat+rotationnormal*radius - lookat and rotationnormal are vectors and radius is a scalar. With this you would rotate around plane with distance of radius (if rotationnormal is unit vector)

Thank you very much for the reply. I think I have calculated the new eye point in my posting as you suggested. But how to find the new up vector? Please provide me some suggestion. Thanks

glut is not a graphical specification, is just for handling input and output.

glutReshape is intended for other things, it doesn't really care about what you're drawing, that callback is called when you reshape the window: http://www.opengl.org/resources/libraries/glut/spec3/node48.html

You can do whatever you want in that function, but it'll be called only once (when the window is created) if you don't change the size somehow. If you're reshaping your windows every frame based on what's inside ignore what I've said, but it sounds like a really weird idea for a game or an application.

Thank you very much for the reply. I think I have calculated the new eye point in my posting as you suggested. But how to find the new up vector? Please provide me some suggestion. Thanks

you will set only eye and lookat vectors, keep the up vector constantly (0,1,0). Up vector does not have to change very likely in your case, changing up vector affects camera rotation around the (lookat minus eye) axis, what is the effect of rotating the very screen to left or right moment. It is rarely an intent. As I adviced, keep up vector (0,1,0) and lookat vector constant and compute the eye by formula I have provided- this will result in camera spectating the plane from the direction you have provided by the unit vector- if this is what will work for your intent.

Hi JohnnyCode, I have worked with only changing eye position calculating from new look at vector and used it in gluLookAt function, but the plane does not look square as it should be, it still looks trapezoidal like Figure (b). That's why I'm thinking whether I need to change other prameters in gluLookAt function. Suppose if the plane is rotated parallel to the xz plane, then still it should look like the picture inFigure (a). Then do you think only change in eye position will work, I don't need to change the up vector?

Please provide some suggestions.

Thanks in advance.

that is the result of the projection matrix transformation being perspective. if you use orthogonal projection matrix, it will be like figure a. Orthogonal projection is simplified perspective projection. you shoud have a glut function for orthogonal perspective matrix building from parameters.

An identity matrix is an orthogonal projection matrix case witth scale 1.0 in all three directions. Meaning that if you do not use projection transformation matrix at all (setting it to identity), it will be like using orthogonal projection with scale 1.0.

Thank you very much for the reply. Could you be a bit clearer, if possible with example? Do I really need to switch to glortho to accomplish this? I would like to use perspective projection. I think my view volume will be rotated in the latter case. I saw Nate Robbin's tutorial but a confused how to make it work.

perspective projection will always result in deformation by depth, there is nothing to adress about this into view matrix. All you can do is to set the object incredibly far- yet this will only minimize the effect. In case of precise observing, perspective matrix is never used. View frustum of a perspective matrix is a pyramid and it results in not "real" size of objects, while ortho projection view frustum is a cube/quad and it results in real size of objects when onserved, not dependant on distance from camera. Though this effect does not match real phenomena of observing, it is critical for architectural softwares or image manipulation softwares and genaraly softwares that need to see precise placing in all three directions.

you can switch from orthogonal projection to perspective projection though - at any time

Perhaps the goal is to find a position and orientation for the camera (i.e. a view transform) so that the plane does not change w.r.t. the view space although it is changed in the world space? If so, then the math is as follows (I'm using matrices for transformations because problems like this can be described with matrices very compact; I further use column vectors as is common when dealing with OpenGL):

a) The initial situation is given by a world transform M0 of the plane and a view transform V0 of the camera. Then the transform of the plane in view space, i.e. its placement relative to the camera, is given as

V0 * M0

It seems from the OP that M0 is just the identity matrix and V0 is just the inverse of a simple translation of the camera.

b) When the plane's transform is changed in world space to current Mi, you want a adapted view transform Vi. The transform of the plane in view space is again the product of both. However, you want the plane in view space to be the same as in the initial situation, hence

Vi * Mi = V0 * M0

Solving this for the unknown view transform gives

Vi = V0 * M0 * Mi-1

This result makes sense, because it undoes the current plane transform (see the inverse Mi) and lets the initials transform remain (see the V0 * M0 which is what we had in the beginning).

There are several ways of implementing the above solution. The best and future-proof way is to drop usage of OpenGL's matrix routines (with the exception of glLoadMatrix as long as you deal with the fixed function pipeline) inclusive the companion routines of GLU. Instead, use a math library like GLM and handle transforms yourself. As can be seen here, solving problems like this can easily be done in matrices (if matrices itself are understood, of course) but are seriously error-prone when using OpenGL directly.

Are you able to switch to using a matrix library yet, or do you really need to stick with OpenGL's matrix stack?

This topic is closed to new replies.

Advertisement