Sign in to follow this  
ahmetyortanli

changing camera angle in a scene with lots of independent objects???

Recommended Posts

hi to all i am new comer to this forum... i have lots of independent obnject in my scene which was actually 2d and every object have abilities such as rotating, dragging and etc. now i change my world to 3d and i want to change these rotate,drag,.. properties from any camera angle because now my camera is dynamic. the problem is i could not form the logic, what should i do to improve my 3d scene to 3d scene with dynamic camera... and a question: in my original 2d scene i use glLoadIdentity after rotating , transforming and drawing each object. now in 3d i think that glLoadIdentity cause a problem and i should use glpush/popmatrix. am i right or is it ok to use glLoadIdentity after every object drawing operation.

Share this post


Link to post
Share on other sites
Quote:
Original post by ahmetyortanli
hi to all
i am new comer to this forum...
i have lots of independent object in my scene which was actually 2d and every object have abilities such as rotating, dragging and etc.
now i change my world to 3d and i want to change these rotate,drag,.. properties from any camera angle because now my camera is dynamic.
the problem is i could not form the logic, what should i do to improve my 3d scene to 3d scene with dynamic camera...

If I understand your question correctly, you want to know how to:
- determine which object the mouse points to
- drag this object in the direction the mouse moves (which is within the x,y plane of the camera rather than object x,y plane)
- rotate this object using the camera space axis

Question one has two solutions. Use OpenGL picking or determine a ray shooting from the mouse into the scene and determine intersections with (bounding boxes of) selectable objects. Although the second option may sound much harder (due to math), it's actually preferrable IMO.
The other two questions can be solved by transforming the camera x,y and z-axis to object space (i.e. multipling (1,0,0), (0,1,0) and (0,0,1) with the transposed of the modelview matrix). You can then use these 3 vectors as a base for your rotations and translations, rather than the object axis. If you're not realy into vector math this might be tricky, but there is no 'math-free' solution.

Quote:
Original post by ahmetyortanli
and a question:
in my original 2d scene i use glLoadIdentity after rotating , transforming and drawing each object. now in 3d i think that glLoadIdentity cause a problem and i should use glpush/popmatrix. am i right or is it ok to use glLoadIdentity after every object drawing operation.


Assuming you use the OpenGL matrix stack properly:
- you have a projection matrix (gluPerspective/glFrustum) on the projection stack and nothing else
- the first matrix you load on the modelview stack is the view matrix (e.g. gluLookAt)
- after this first matrix, you apply model specific transformations
In this setup it becomes clear that loading identity on the modelview stack after loading a model matrix is incorrect, because it will wipe out the view matrix you used. glPush and glPop are much better. A typical use would be:

// setup projection here
glMatrixMode(GL_MODELVIEW);
gluLookAt(...);

for all models
{
glPushMatrix();
glTranslate(model.position);
glRotate(model.orientation);
glScale(model.scale);
model.draw();
glPopMatrix();
}



Tom

Share this post


Link to post
Share on other sites
thanks for answering
but i have no problem with object rotation and etc.
i am confused with rotation or dragging in different camera angles. in clear, when i change my camera position and rotate something , i think it rotates with respect to default camera view not for my new camera view (and same for dragging).

and thanks for answer of my second question. it really helps me.

Share this post


Link to post
Share on other sites
Quote:
Original post by ahmetyortanli
thanks for answering
but i have no problem with object rotation and etc.
i am confused with rotation or dragging in different camera angles. in clear, when i change my camera position and rotate something , i think it rotates with respect to default camera view not for my new camera view (and same for dragging).


I probably worded it ackwardly but that's what I ment to say. My solution, transforming the x,y,z camera axis to the object will solve precisely this problem. To elaborate on this solution:
The transposed modelview matrix, for a given object, transforms vectors from camera space to object space. The three axis of your camera expressed in camera space are always x:(1,0,0), y:(0,1,0), z:(0,0,1) by definition. This is true in all situations, no matter how your camera is positioned in the scene. When we multiply these vectors with the transposed modelview, they will be transformed to the coordinate system of a specific object. At that point you know what the direction of the camera x,y and z-axis are from the perspective of that specific object. So if your mouse moved 3% of the screen to the right (x-direction in camera space), the proper x-translation would be:
x-trans = 0.03 * x'.
x' is the transformed x-axis: x' = modelview.transposed() * vector(1,0,0).

There will definately be other solutions, but if you have limited experience with 3D vector/matrix math, they will all probably a bit tricky.

Tom

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