Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

glTranslate()

Using Default Window (2 x 2)

 

Original Triangle

Translated Triangle

Both Triangles

Exercises

 

Original Triangle

 

#include "stdafx.h"

#include <GL/glut.h>

void mydisplay()

{

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);       

        glVertex2f(-0.5, -0.5);       

        glVertex2f(-0.5, 0.5);       

        glVertex2f(0.5, 0.5);       

    glEnd();

    glFlush();

}

int main(int argc, char** argv)

{

    glutCreateWindow("A Triangle");    

    glutDisplayFunc(mydisplay);   

    glutMainLoop();

}

   

Triangle After Translation

 #include "stdafx.h"

#include <GL/glut.h>

void mydisplay()

{

    glClear(GL_COLOR_BUFFER_BIT);

    glTranslatef(0.5f, 0.5f, 0.0f);

    glBegin(GL_TRIANGLES); 

      glVertex2f(-0.5, -0.5);       

      glVertex2f(-0.5, 0.5);       

      glVertex2f(0.5, 0.5);       

    glEnd();

    glFlush();

}

int main(int argc, char** argv)

{

    glutCreateWindow("A Triangle");    

    glutDisplayFunc(mydisplay);   

    glutMainLoop();

}

 

   
Both Triangles

#include "stdafx.h"

#include <GL/glut.h>

void mydisplay()

{

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES); 

      glVertex2f(-0.5, -0.5);       

      glVertex2f(-0.5, 0.5);       

      glVertex2f(0.5, 0.5);       

    glEnd();

    glTranslatef(0.5f, 0.5f, 0.0f);

    glBegin(GL_TRIANGLES); 

      glVertex2f(-0.5, -0.5);       

      glVertex2f(-0.5, 0.5);       

      glVertex2f(0.5, 0.5);       

    glEnd();

    glFlush();

}

int main(int argc, char** argv)

{

    glutCreateWindow("A Triangle");    

    glutDisplayFunc(mydisplay);   

    glutMainLoop();

}