2D Transformations using OpenGL – Program Source Code


The following Source code performs the following 2 dimensional transformations:

  • Translations
  • Scaling
  • Rotation
  • Shearing
  • Reflection
  • Composite Transformations

Source Code:

#include 
#include 
#include 
#include 

typedef float Matrix3x3 [3][3];
Matrix3x3 theMatrix;
int NEdges;
float ptsIni[20][2];
float ptsFin[20][2];
float refpt[2];
float RotAngle;
float TransDistX,TransDistY;
float ScaleX,ScaleY;
int choice,choiceRef,choiceShear;
float slope,yIntercept;
float shearValue;

void matrixSetIdentity(Matrix3x3 m)
// Initialises the matrix as Unit Matrix
{
   int i, j;
   for (i=0; i<3; i++)
   for (j=0; j<3; j++)
   m[i][j] = (i == j);
}

void matrixPreMultiply(Matrix3x3 a , Matrix3x3 b)
// Multiplies matrix a times b, putting result in b
{
   int i,j;
   Matrix3x3 tmp;
   for (i = 0; i < 3; i++)
     for (j = 0; j < 3; j++)
       tmp[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j];
   for (i = 0; i < 3; i++)
     for (j = 0; j < 3; j++)
       theMatrix[i][j] = tmp[i][j];
}

void Translate(int tx, int ty)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   m[0][2] = tx;
   m[1][2] = ty;
   matrixPreMultiply(m, theMatrix);
}

void Scale(float sx , float sy)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   m[0][0] = sx;
   m[0][2] = (1 - sx)*refpt[0];
   m[1][1] = sy;
   m[1][2] = (1 - sy)*refpt[1];
   matrixPreMultiply(m , theMatrix);
}

void Rotate(float a)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   a = a*22/1260;
   m[0][0] = cos(a);
   m[0][1] = -sin(a) ;
   m[0][2] = refpt[0]*(1 - cos(a)) + refpt[1]*sin(a);
   m[1][0] = sin(a);
   m[1][1] = cos(a);
   m[1][2] = refpt[1]*(1 - cos(a)) - refpt[0]*sin(a);
   matrixPreMultiply(m , theMatrix);
}

void Reflect(int xy)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   if(xy == 2)
   m[1][1] = -1;
   if(xy == 3)
   m[0][0] = -1;
   matrixPreMultiply(m , theMatrix);
}

void Shear(int xy)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   if(xy == 1)
     m[0][1] = shearValue;
   if(xy == 2)
     m[1][0] = shearValue;
   matrixPreMultiply(m , theMatrix);
}void TransformPoints(void)
{
   int k;
   float tmp ;
   for (k = 0 ; k ");
  scanf("%d",&NEdges);
  printf("Enter %d Co-ordinate pairs \n",NEdges);
  int PlotCt;
  for(PlotCt=0 ; PlotCt");
  scanf("%d",&choice);
  switch(choice)
  {
    case 1:     printf("Enter reference point Co-ordinates:\n=>");
      scanf("%f %f",&refpt[0],&refpt[1]);
      printf("Enter Angle of Rotation\n=>");
      scanf("%f",&RotAngle);
      printf("Enter translation along X & Y\n=>");
      scanf("%f%f",&TransDistX , &TransDistY);
      printf("Enter Scaling ratios along X & Y\n=>");
      scanf("%f%f",&ScaleX , &ScaleY);
      break;
    case 2:     printf("Enter translation along X & Y\n=>");
      scanf("%f%f",&TransDistX , &TransDistY);
      break;
    case 3:     printf("Enter Angle of Rotation\n=>");
      scanf("%f",&RotAngle);
      printf("Enter reference point Co-ordinates:\n=>");
      scanf("%f %f",&refpt[0],&refpt[1]);
      break;
    case 4:     printf("Enter Scaling ratios along X & Y\n=>");
      scanf("%f%f",&ScaleX , &ScaleY);
      break;
    case 5:     printf("Enter your choice number for Reflection about:\n
                    1.Origin\n 2.X-axis\n 3.Y-axis\n 4.y = mx+c\n=>");
      scanf("%d",&choiceRef);
      if(choiceRef == 4)
      {
        printf("Enter m & c: \n=>");
        scanf("%f %f",&slope,&yIntercept);
      }
      break;      
    case 6:     printf("Enter your choice number to shear about:\n
                    1.x-axis\n 2.y-axis\n=>");
      scanf("%d",&choiceShear);
      printf("Enter shear value:\n=>");
      scanf("%f",&shearValue);
      break;
    default:    printf("Please enter a valid choice!!!\n");
      return 0;
  }
  glutDisplayFunc(display);
  glutMainLoop();
  return 0;
}

OUTPUT:

1 . Translation

INPUT :

4                                  // No. of vertices of Polygon
20      20                         //(x,y) Co-ordinates of Vertices
100     20
100     100
20      100
2                                  // Selected option 2 for Translation
-50      -140                      //  Translation along X & Y

OUTPUT :

2DTrans1

2 . Rotation

INPUT :

4                                 // No. of vertices of Polygon
20      20                        // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
3                                 // Choice no. for Rotation
60                                // Angle of Rotation in Degrees
-50      -50                      // Reference point Co-ordinates

OUTPUT :

2DTrans2

3 . Scaling

INPUT :

4                                  // No. of vertices of Polygon
20      20                         // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
4                                  // Choice no. for Scaling
0.5      0.5                       // Scaling factors along X & Y

OUTPUT :

2DTrans3

4 . Reflection

About arbitrary line y=mx+c

INPUT :

4                                 // No. of vertices of Polygon
20      20                        // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
5                                 // Choice no. for Reflection
4                                 // Choice for Reflection about y=mx+c
1        -30                      //  m  &  c values

OUTPUT :

2DTrans4

5 . Shearing

X-direction Shear

INPUT :

4                                  // No. of vertices of Polygon
20      20                         // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
6                                  // Choice no. for Shearing
1                                  // Choice no. for Shear about X axis
1.5                                // Shear Coefficient

OUTPUT :

2DTrans5

6 . Composite Transformation

INPUT :

4                                   // No. of vertices of Polygon
20      20                          // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
1                                   // Choice no. for Comp. Transf.
-50     -50                         // Reference Point Co-ordinates
60                                  // Angle of Rotation in Degrees
-40      -80                        // Translation along X & Y
1.5      1.5                        // Scaling factors along X & Y

OUTPUT :

2DTrans6

One thought on “2D Transformations using OpenGL – Program Source Code

Leave a comment