Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, i am new to opengl and im confused on how to use gltranslate/glrotate to let the gl_quads move sidewards, any help? following is my vertices for the gl_quads:

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(0.3,0,0);
    //glTranslatef(0.7,0,0);
    //glRotatef(45.0,0.0,1.0,0.0);

    glBegin(GL_QUADS);
        glColor3f(1.0f, 0.5f, 0.0f);     // Orange
        glVertex2f(0,0);

        glColor3f(1.0f, 0.0f, 0.0f);     // Red
        glVertex2f(0.4,0);

        glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
        glVertex2f(0.4,0.4);

        glColor3f(0.0f, 0.0f, 1.0f);     // Blue
        glVertex2f(0,0.4);


    glEnd();
    glPopMatrix();
    glutSwapBuffers();

}


What I have tried:

i have tried moving the position and the angle but i should see the "live" mode of it moving sidewards
Posted
Updated 19-Mar-18 10:02am

1 solution

The standard way of doing this is to have some parameter change over time. Since you want to have it move sideways then adjust the translation in the x direction. For animating it, usually people set a timer and increment the translation on each tick. In the windows world this means handling the WM_TIMER message. Here is a sample timer handling routine for an MFC window :

C++
void MyWindow::OnTimer( UINT_PTR eventId )
{
    if( eventId == MyTimerId )
    {
        m_TranslationX += increment;
        Invalidate();     // cause the window to be redrawn
    }
    __super::OnTimer( eventId );
}

// the translation call in your display function will become :

glTranslatef( m_Translation, 0, 0 );
If you want to, you can increment m_Translation until it reaches a certain value and then decrement it for a while and this will cause the quads to move to the right and then to the left.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900