noob question, why double boxes printed out?

Quick noob question, I’ve been looking at this for a very long time now and cannot see this:


// g++ cube_mat.cpp -lGL -lGLU -lglut && ./a.out
/*
 *  cube_mat.cpp
 *  This program demonstrates examining the transformation matrix values.
 *  
 *  A wireframe cube is rendered.
 */
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

void init(void)
{
   glClearColor (1.0, 1.0, 1.0, 0.0);
   glShadeModel (GL_FLAT);
}

//print the transformation matrix
template<class T>
void print_mat ( T m[][4] )
{
  cout.precision ( 4 );
  cout << fixed;
  for ( int i = 0; i < 4; ++i ) {
    cout << "	";
    for ( int j = 0; j < 4; ++j )
      cout <<  m[j][i] << "	";
    cout << endl;
  }
  cout << endl;
}

void display(void)
{
   float p[4][4];
   double pd[4][4];

   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (0.0, 1.0, 0.0);	  	//green color
   glLoadIdentity ();             	// clear the matrix 
   glMatrixMode (GL_PROJECTION);
   // viewing transformation  
   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
   gluLookAt (0.0, 0.0, 5.0, 
              0.0, 0.0, 0.0, 
              0.0, 1.0, 0.0);
   
   glMatrixMode (GL_MODELVIEW);
   glLoadIdentity();
   glGetFloatv(GL_PROJECTION_MATRIX,&p[0][0]);
   cout << "A. Projection Matrix:" << endl;
   print_mat ( p );

   glGetDoublev(GL_MODELVIEW_MATRIX, &pd[0][0]);
   cout << "B. Modelview Transformation Matrix:" << endl;
   print_mat ( pd );

   glScalef (1.0, 2.0, 1.0);      	// modeling transformation 
   glGetFloatv(GL_MODELVIEW_MATRIX,&p[0][0]);
   cout << "C. Scale Matrix:" << endl;
   print_mat ( p );

   glRotatef ( 45, 0, 0, 1 );
   glGetDoublev(GL_MODELVIEW_MATRIX, &pd[0][0]);
   cout << "D. Scale Rotation Matrix:" << endl;
   print_mat ( pd );
   glutWireCube (1.0);   
   glFlush ();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init();
   glutDisplayFunc(display);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}

Why is it like display() is called twice and it’s like it’s printing the matrices out twice?

I think it should only print the A-D matrices 1 single time - it’s like there’s two different situations… Is it something with doublebuffering?

Output:

A. Projection Matrix:
1.5000 0.0000 0.0000 0.0000
0.0000 1.5000 0.0000 0.0000
0.0000 0.0000 -1.1622 2.5676
0.0000 0.0000 -1.0000 5.0000

B. Modelview Transformation Matrix:
1.0000 0.0000 0.0000 0.0000
0.0000 1.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000

C. Scale Matrix:
1.0000 0.0000 0.0000 0.0000
0.0000 2.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000

D. Scale Rotation Matrix:
0.7071 -0.7071 0.0000 0.0000
1.4142 1.4142 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000

A. Projection Matrix:
2.2500 0.0000 0.0000 0.0000
0.0000 2.2500 0.0000 0.0000
0.0000 0.0000 -1.2169 9.8539
0.0000 0.0000 -3.8378 22.4324

B. Modelview Transformation Matrix:
1.0000 0.0000 0.0000 0.0000
0.0000 1.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000

C. Scale Matrix:
1.0000 0.0000 0.0000 0.0000
0.0000 2.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000

D. Scale Rotation Matrix:
0.7071 -0.7071 0.0000 0.0000
1.4142 1.4142 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000

Most probably glut calls display twice to respond to 2 events needing redraw, even if these 2 calls are not really needed.

A side note : always use double buffering instead of single buffer. Fairly simple to do, replace glFlush() with glutSwapBuffers(), and GLUT_SINGLE with GLUT_DOUBLE.