1

After reading a lot of guides about orbiting around camera and asking some additional question on SO I came up with SSCCE what I have so far. Maybe this way it will be easier for other to understand what I need and for me what is the answer.

So far it looks like this:

animation

the code for drawing objects:

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);

glTranslatef(2,0,0);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);

glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);

glTranslatef(4-1, 1.5-0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();

translate is position updated during navigation and rot is rotation.

The red box is a main container and everything is inside it. 'Camera' can go outside of it, but during rotation it should rotate as well. As for the small boxes I'd like to be able to orbit around each one of them. So far I can orbit around this green one, but when I move to purple then it doesn't work.

In my final app I will have hundreds of objects so I am not sure if current method of using only one glPushMatrix() glPopMatrix() is the best idea (I am concerned about proper placing them), but when I am using push pop for every object then each one rotates by it's own pivot point.

EDIT:

I've been working on my code and I managed to achieve something more then before. Although it's still not exactly what I'm looking for.

My code:

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
    glRotatef(-rot, 0.0, 1.0, 0.0);
    glTranslatef(4,0,0);
    glPushMatrix();

        glPushMatrix();
        glTranslatef(2,0,0);
        glColor3f(0.0,1.0,1.0);
        fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
        glPopMatrix();

        glPushMatrix();
        glTranslatef(-4, 1.5, 0);
        glColor3f(1.0,0.0,1.0);
        fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
        glPopMatrix();

        glPushMatrix();
        glTranslatef(-1, -0.5, 0);
        glColor3f(0.0,0.0,1.0);
        fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
        glPopMatrix();

    glPopMatrix();
glPopMatrix();

And how it looks now: enter image description here

so as you can see, I was able to move rotation point, but at the same time I've moved whole group inside box. And I don't want to do that.

sebap123
  • 2,541
  • 6
  • 45
  • 81

2 Answers2

1

After many changes and ideas I've finally figured out the proper way to achieve what I was looking for. It turned out that I needed to switch order of translations:

glPushMatrix();
glTranslatef(0, translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(translate[0], 0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();

glPushMatrix();
glTranslatef(0, translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(translate[0],0,0);

    glPushMatrix();
        glPushMatrix();
        glTranslatef(2,0,0);
        glColor3f(0.0,1.0,1.0);
        fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
        glPopMatrix();

        glPushMatrix();
        glTranslatef(-4, 1.5, 0);
        glColor3f(1.0,0.0,1.0);
        fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
        glPopMatrix();

        glPushMatrix();
        glTranslatef(-1, -0.5, 0);
        glColor3f(0.0,0.0,1.0);
        fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
        glPopMatrix();
    glPopMatrix();
glPopMatrix();

Now the rotation point is exactly in front of me no matter where the camera is.

sebap123
  • 2,541
  • 6
  • 45
  • 81
0

OpenGL rotating a camera around a point Maybe that answer could help you. I think you have to: translate-rotate-translate.

Community
  • 1
  • 1
sisco_0
  • 1
  • 2
  • Yeah, I also thought about it, but where I should add rotate and translate if I have only one push pop pair that's the question. – sebap123 Nov 18 '15 at 19:35