Slope of a Crane in Opengl

3

I have implemented all the commands of a crane, it just lacked its inclination around its axis on top, but I can not reproduce this inclination, as it seems that I have to do translation effects and scale at the same time to adjust, however does not seem to be the best way to do this, look at the generated problem:

Was not to get off the axis understand?

Code I did: link

 //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
                    // ...
    
asked by anonymous 12.12.2015 / 02:12

1 answer

5

You seem to be applying the rotation before translation 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 between glPushMatrix and glPopMatrix are rotation operations ( glRotatef ).

As this answer 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 any other point, the order must be "scale", "translation", "rotation" and again " translation. "

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

Soyourcodewouldlooklikethis(note:asIsaid,I'mnotexperiencedwithOpenGL,beguidedbytheaboveimageandnotjustbythecodebelow):

//inclinacao//Partecilindricanapontadecimadabaseverticallonga//...glTranslatef(0,2.75,0);//DeslocaverticalmentedaorigematéopontoderotaçãoglRotatef(angInclinacao,0,0,1);//Apósgirar{//CubohorizontallongoouVigaglPushMatrix();glTranslatef(2-deslViga,0,0);//ApósmovernahorizontalglScalef(compViga,0.3,0.15);//Apósaplicaraescalacubo();glPopMatrix();//...

WhereIput"..." is to continue doing this, moving only in relation to the origin, not the point of rotation. So, where you do glTranslatef( compViga, 2.75, -0.0375) you'll switch to glTranslatef( compViga, 0, -0.0375) , glTranslatef( compViga, 1.75, 0.0 ) exchange to glTranslatef( compViga, -1, 0.0 ) , etc.

    
12.12.2015 / 08:48