Tilting of an Opengl Crane

Asked

Viewed 126 times

3

I implemented all the commands of a crane, only lacked the inclination of it around its axis on top, only that I can not reproduce this inclination, because it seems that I have to make translational effects and scale at the same time to adjust, but it doesn’t seem to be the best way to do it, look at the problem generated: inserir a descrição da imagem aqui

It wasn’t supposed to go off the rails?

Code I made: http://pastebin.com/JkEQ3un7

 //Base do guindaste é fixa
 glPushMatrix();
    glScalef(2.0, 0.5, 1.0);
    cubo();
 glPopMatrix();

    glRotatef( angGiro, 0.0, 1.0, 0.0 );
    {
            //Base cilindrica de cima da base
            // ...
            //Base vertical longa ou Haste vertical
            // ...

            // inclinacao
                    //Parte cilindrica na ponta de cima da base vertical longa
                    // ...
            glRotatef( angInclinacao, 0, 0, 1 );
            {
                    //Cubo horizontal longo ou Viga
                    glPushMatrix();
                            glTranslatef(2-deslViga, 2.75, 0); // deslViga é o deslocamento da viga e diminui metade do que e aumenta/diminui a metade do que o comprimento da viga para poder fazer a relacao de translacao e escala ficarem corretos
                            glScalef( compViga, 0.3, 0.15);
                            cubo();
                    glPopMatrix();

                    //Parte cilindrica na ponta direita da base horizontal
                    glPushMatrix();
                            glTranslatef( compViga, 2.75, -0.0375);
                            glScalef( 0.2, 0.2, 0.075);
                            cilindro();
                    glPopMatrix();

                    //Parte vertical longa e fina no final da parte cilindrica direita
                    // ...
  • 1

    I forgot to post the code: http://pastebin.com/JkEQ3un7

  • Welcome to Stack Overflow, Jonathas! When asking a question, please place the relevant code snippets (where applicable) in the question itself, even if accompanied by a link to the full code. I edited your question with an example of how this can be done.

  • Oops, really new, sorry and thank you.

  • 2

    Corrected version of the problem for those who are curious, has been corrected thanks to the help of our friend mgibsonbr. http://pastebin.com/QQ6C2MRK, Thank you.

1 answer

5


You seem to be applying the rotation before translating to the point around which the spin should occur. I have no experience with Opengl, but I noticed in your code that the only operations that are not among a glPushMatrix and a glPopMatrix are rotation operations (glRotatef).

Like that response in gamedev.SE indicates, although the usual order of transformations is "scale", then "rotation", then "translation", when you want to rotate an object not around its own center but around some other point, the order must be "scale", "translation", "rotation" and again "translation".

If you want the girder to rotate around one of its ends, and not around its center, it is necessary to do the operations in the order cited:

inserir a descrição da imagem aqui

So that your code would look like this (note: as I said, I have no experience with Opengl, guide yourself by the image above and not only by the code below):

// inclinacao

        //Parte cilindrica na ponta de cima da base vertical longa
        // ...

glTranslatef(0, 2.75, 0); // Desloca verticalmente da origem até o ponto de rotação
glRotatef( angInclinacao, 0, 0, 1 ); // Após girar
{
        //Cubo horizontal longo ou Viga
        glPushMatrix();
                glTranslatef(2-deslViga, 0, 0); // Após mover na horizontal
                glScalef( compViga, 0.3, 0.15); // Após aplicar a escala
                cubo();
        glPopMatrix();

        // ...

Where I put "..." is to keep doing that, moving only in relation to the origin, and not to the rotation point. So where do you do glTranslatef( compViga, 2.75, -0.0375) you will switch to glTranslatef( compViga, 0, -0.0375), glTranslatef( compViga, 1.75, 0.0 ) exchange for glTranslatef( compViga, -1, 0.0 ), etc..

  • Dude, you’re fucked, I confess I would never expect an answer as complete as yours, I didn’t even know the right stackoverflow, but apparently I have to go here more, helped me a lot to understand the problem, and really, the problem of Opengl and the Matrices of real geometry is that translocation, rotation and scale are reversed so that if on paper Voce would do a rotation first, programming Voce does the translation first, and vice versa, thank you very much for the answer, abração.

  • @Jonathasmoreira Yes, Opengl matrices are "per column" (column major) while in Directx they are "per line" (Row major), and the operations with matrices are post-multiplied (and not pre), so that the transformations first made in the code will last. I tried to play that in the answer code, I don’t know if I could. P.S. If an answer solved your question, you have the option to mark it as "accept" by clicking on the icon on the left side of it. If you only partially solved it, you can also leave it open and wait for alternative responses from other users.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.