Change Orientation of 3D coordinate system

Started by
11 comments, last by tamlam 4 years, 7 months ago

Hello.

I attached the picture below to describe my problem.

I am using OpenGL to do an animation about rolling 3D coordinate OXYZ from A to B to C.

Input: coordinate of cube: (0,0,0), (1,0,0) , (2,0,0), the cube rolling along its edge

so the coordinate of cube center will rotate along the curve as the dash line.

Problem:

I created a function call DrawCoordSystem to draw Oxyz of the cube center.

I could do rolling from 1st position to 2nd position; HOWEVER, at the 2nd rolling, when I call the DrawCoordSystem it will create oxyz within the same orientation (at the wrong way in the image)

I do not know how to save the orientation of oxyz after the 1st rolling.

Please help me to comments on the solution.

Thanks a lot

 

imageTam.jpg

Advertisement

Usually opengl-ly (right handed coordinate system) z goes towards the viewer, x to the right and y up in model and world space. May that's confusing things ?

In model space, you would translate your object so that the rotation point/axis becomes the object's center, then rotate it by some amount, then translate it back. Following view- and projection transformations.

18 minutes ago, Green_Baron said:

Usually opengl-ly (right handed coordinate system) z goes towards the viewer, x to the right and y up in model and world space. May that's confusing things ?

In model space, you would translate your object so that the rotation point/axis becomes the object's center, then rotate it by some amount, then translate it back. Following view- and projection transformations.

Hi Baron, thanks for reply.

I understand your answer, but you may not understand what I mean. Let me clarify again.

I COULD do rotate the cube through the edge.

Right rolling: 1st center XYZ (X-right, Z-up, -back)

2nd center X-down, Z-right, Y-back

3rd center: X-left, Z-down, Y-back.....

My problem: although I see the coordinate system rotate,1st, 2nd, 3rd  center are still (X-right, Z-up, -back)

Whenever I call DrawArrow function, it always draws oxyz.

 

Pls. post your code to avoid guessing.

If I got you right, the second rotation does not apply anymore, right?

In this case you probably forget stacking your transformations or made a coding mistake. To solve the latter problem, we need code as @Green_Baron said.

Hi @DerTroll and @Green_Baron Here is my code 

//OctVoxel is a class that saved the centers of cubes

//Draw3DcoordArrowGL is used to draw three arrows ox oy oz from cube centers

void DrawOXarrow(CVector3d currentCoord, GLdouble D) {
    GLdouble x1 = currentCoord.x;
    GLdouble y1 = currentCoord.y;
    GLdouble z1 = currentCoord.z;

    GLdouble x2 = currentCoord.x + 0.5;
    GLdouble y2 = currentCoord.y;
    GLdouble z2 = currentCoord.z;

    double x = x2 - x1;
    double y = y2 - y1;
    double z = z2 - z1;
    double L = sqrt(x*x + y * y + z * z);

    GLUquadricObj *quadObj;

    glPushMatrix();

    glTranslated(x1, y1, z1);

    if ((x != 0.) || (y != 0.)) {
        glRotated(atan2(y, x) / RADPERDEG, 0., 0., 1.);
        glRotated(atan2(sqrt(x*x + y * y), z) / RADPERDEG, 0., 1., 0.);
    }
    else if (z < 0) {
        glRotated(180, 1., 0., 0.);
    }

    glTranslatef(0, 0, L - 4 * D);

    quadObj = gluNewQuadric();
    gluQuadricDrawStyle(quadObj, GLU_FILL);
    gluQuadricNormals(quadObj, GLU_SMOOTH);
    gluCylinder(quadObj, 2 * D, 0.0, 4 * D, 32, 1);
    gluDeleteQuadric(quadObj);

    quadObj = gluNewQuadric();
    gluQuadricDrawStyle(quadObj, GLU_FILL);
    gluQuadricNormals(quadObj, GLU_SMOOTH);
    gluDisk(quadObj, 0.0, 2 * D, 32, 1);
    gluDeleteQuadric(quadObj);

    glTranslatef(0, 0, -L + 4 * D);

    quadObj = gluNewQuadric();
    gluQuadricDrawStyle(quadObj, GLU_FILL);
    gluQuadricNormals(quadObj, GLU_SMOOTH);
    gluCylinder(quadObj, D, D, L - 4 * D, 32, 1);
    gluDeleteQuadric(quadObj);

    quadObj = gluNewQuadric();
    gluQuadricDrawStyle(quadObj, GLU_FILL);
    gluQuadricNormals(quadObj, GLU_SMOOTH);
    gluDisk(quadObj, 0.0, D, 32, 1);
    gluDeleteQuadric(quadObj);

    glPopMatrix();
}

void Draw3DcoordArrowGL(int rotAngle, double Xcoord, double Ycoord, double Zcoord,
    double Xdir, double Ydir, double Zdir, GLdouble D)
{
    CVector3d temp;
    temp.x = Xcoord;
    temp.y = Ycoord;
    temp.z = Zcoord;

    glTranslatef(Xcoord, Ycoord, Zcoord);
    glRotatef(rotAngle, Xdir, Ydir, Zdir);

    DrawOXarrow(temp, D);
    DrawOYarrow(temp, D - 0.02);
    DrawOZarrow(temp, D + 0.02);

    glTranslatef(-1.5, 0.5, 0.5);//const
}

void DrawCubeRolling() {

    OctVoxel cb = newCube[numberCube];

    //only for cube rolling right-up or left-up

    for (int a = 0; a < cubeNum; a++)
    {
        if (newCube[a].getSelected())
        {
            glPushMatrix();
            glLineWidth(1.9f);
            std::cout << "+++++++++++    right   ++++++++++++ " << std::endl;
            if (newCube[a].getRightRolling()) {

                //for Right rolling direction
                glTranslatef(cb.getCoordX() - 0.5, cb.getCoordY() - 0.5, 0.0);
                glRotatef(angleRotation, cb.getDirectionX(), cb.getDirectionY(), cb.getDirectionZ());
                glTranslatef(-0.5, 0.5, 0.5);//const    
                glColor3f(0.5, 0.5, 1.0); glutSolidCube(1);
                Draw3DcoordArrowGL(angleRotation, cb.getCoordX() - 0.5, cb.getCoordY() - 0.5, 0.0,
                    cb.getDirectionX(), cb.getDirectionY(), cb.getDirectionZ(), 0.02);
            }
            else {
                //for up rolling direction - no need now
               std::cout << "------------     up     -----------" << std::endl;

            }

            glPopMatrix();
        }

    }
    if (angleRotation >= 90)
    {
        newCube[numberCube].setSelected(false); //cb will stop
        numberCube += 1;                        //move to next cb

        numberCube %= cubeNum;
        newCube[numberCube].setSelected(true);
        angleRotation = 0;

    }

}

//--------

 

When I make //Draw3DcoordArrowGL to comments in void DrawCubeRolling() {..}, the cube rolls along its edges perfectly without rolling 3 arrows.

The problem is that glutSolidCube(1) is a function from OpenGl that I could not add or change anything. 

Awww, thanks ... but i can't help with this one because i never had contact with OpenGL 1 ... sorry.

Can you post the whole code on GitHub?

 

8 hours ago, taby said:

Can you post the whole code on GitHub?

 

Sorry I can not publish the code at this moment. Which part do you need to clarify? 

Please let some feedbacks from the above source which relates to my problem.

Write a new code that implements this portion that you've already pasted into this topic. That way you can post the whole code.

This topic is closed to new replies.

Advertisement