PDA

View Full Version : moving objects relative to grid & screen



incognito
06-21-2002, 01:43 AM
I am working on a 3D graphic editor (like TrueSpace, only far more simple).
I have a 3D grid, who can be rotated and translated, these transformations affecting also the objects in the scene.

Now, my problem is that when I select one object to translate him to my right, the object is translated to the right of the grid. So if the grid is rotated 90 degrees on right after Y axe, the object will actualy move away, further from me.

That's not really the type of navigation I would like to have for my application http://www.opengl.org/discussion_boards/ubb/wink.gif
This is what I'm using right now:

DrawObject()
{
glLoadIdentity();
gluLookAt(...);

glTranslatef(m_grid.x, m_grid.y, m_grid.z);
glRotatef(m_grid.rotx, 1, 0, 0);
glRotatef(m_grid.roty, 0, 1, 0);
glRotatef(m_grid.rotz, 0, 0, 1);

glTranslatef(pObj->x, pObj->y, pObj->z);
glRotatef(pObj->rotx, 1, 0, 0);
glRotatef(pObj->roty, 0, 1, 0);
glRotatef(pObj->rotz, 0, 0, 1);

glBegin();
theObject;
glEnd();
}

Gavin
06-21-2002, 02:13 AM
Try using PushMatrix and PopMatrix around each draw func. (inc grid)

Batti
06-21-2002, 10:14 AM
Hi!
Oh, I know this problem, having it for many, many weeks now with two scrollbars, which I would like to use relative to the screen. Itīs the same in green and I couldnīt get it solved. I posted it, too and got answers, but I didnīt really understand them. As I was afraid that the computer might eat me, I didnīt ask further.
Search for "scrollbars relative to screen", maybe you do understand it.
If you have the final solution or any good idea, please let me know.
Regards, Batti

06-21-2002, 04:39 PM
Hi,

I don't know if I understand your question well, but I think you want to transform the object first and then the grid.
All the transformations you do are cumulative. (if you don't pop/push or use glLoadIdentity to initialize the matrix)

You actually want to have a "seperate" transformation for each object.

for example:

/* Position the camera.
* (which are also transforms)
*/
for (i = 0; i < num_objects; i++) {
glPushMatrix();
glRotate();
glTranslate();
drawobject(i);
glPopMatrix();
}

Jan

Misfit
06-22-2002, 08:57 AM
I will explaint it a bit better. (my English is not so well)

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

The MODELVIEW matrix is now initialized. It is "empty", and all the transformations we do will affect it.

If we would do now a glTranslatef(2.0f, 0.0f, 0.0f) we move 2 units on the x-axis to the right. Everything that we draw after this is affected by this translation.

If we would call again glTranslatef(2.0f, 0.0f, 0.0f) we end up at 4, 0, 0...
This is the same with rotations, they are also cumulative.

So, if you have a large static world with one model walking in it, you would do:

1. glLoadIdentity() of the MODELVIEW matrix.
2. Translate and rotate the world / position the camera.
3. draw all the static objects.
4. glPushMatrix()
This remembers where we are at the moment.
5. translate and rotate the model
6. draw the model
7. glPopMatrix()
This returns us to the previous matrix.

Hope this clarifies some things.