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

glTranslate (), glRotate(), glScale()       Exercise

 

Parameters

angle rotating - in counter clockwise direstion, x, y, and z specify the vector about which you are going to rotate

 

 

#include "stdafx.h"

#include <GL/glut.h>

 

void init(void)

{

   glClearColor (0.0, 0.0, 0.0, 0.0);

   glShadeModel (GL_FLAT);

}

 

void draw_triangle(void)   //Triangle definition - called numerous times

{

   glBegin (GL_LINE_LOOP);

      glVertex2f(0.0, 25.0);

      glVertex2f(25.0, -25.0);

      glVertex2f(-25.0, -25.0);

   glEnd();

}

 

void display(void)

{

   glClear (GL_COLOR_BUFFER_BIT);

   glColor3f (1.0, 1.0, 1.0);

 

   glLoadIdentity ();

   glColor3f (1.0, 1.0, 1.0);

   draw_triangle ();             //draw 1st triangle, centered at origin (default)

 

   glEnable (GL_LINE_STIPPLE);

   glLoadIdentity ();

   glTranslatef (-20.0, 0.0, 0.0);     //translate origin to x,y,z position listed

   draw_triangle ();         //draw second triangle, centered at x = -20, y = 0, z = 0

 

   glLoadIdentity ();

   glScalef (1.5, 0.5, 1.0);           //scale the triangle

   draw_triangle ();

 

   glLoadIdentity ();

   glRotatef (90.0, 0.0, 0.0, 1.0);   //rotate the triangle 90 degrees counter clockwise about z axis

   draw_triangle ();

   glDisable (GL_LINE_STIPPLE);

 

   glFlush ();

}

 

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glMatrixMode (GL_PROJECTION);

   glLoadIdentity ();

   if (w <= h)

      glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w, 50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);

   else

      glOrtho (-50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);

   glMatrixMode(GL_MODELVIEW);

}

 

int main(int argc, char** argv)

{

   glutInit(&argc, argv);

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

   glutInitWindowSize (500, 500);

   glutInitWindowPosition (100, 100);

   glutCreateWindow ("Translate and Rotate");

   init ();

   glutDisplayFunc(display);

   glutReshapeFunc(reshape);

   glutMainLoop();

   return 0;

}