title: Opengl: how to make this triangle centred on the window
body: <pre><code>#include &lt;GL/glut.h&gt;
  
  GLint winWidth = 600, winHeight = 600;
  
  GLfloat x0 = 100.0, y0 = 100.0, z0 = 50.0;
  GLfloat xref = 50, yref = 50.0, zref = 0.0;
  GLfloat Vx = 0.0, Vy = 1.0,  Vz = 0.0;
  GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
  GLfloat dnear = 25.0, dfar = 125.0;
  
  void init (void)
  {   
      glClearColor (1.0, 1.0, 1.0, 0.0);
  
      //glMatrixMode(GL_MODELVIEW);
      //gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
  
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      //glOrtho(0,1,0,1, 0,0.1);
      //gluOrtho2D(0, 1,0,1);
      //gluPerspective(45, 1.2, 1, 10);
      glFrustum(0, 1, 0, 1, 0, 1);
      //gluPerspective(45.0, 1, 1, 15);
  }
  
  void displayFcn (void)
  {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      glColor3f(0.0, 1.0, 0.0);
      //glPolygonMode(GL_FRONT, GL_FILL);
      //glPolygonMode(GL_BACK, GL_FILL);
  
      glBegin(GL_TRIANGLES);
          glVertex3f(0.0, 0.0, 0.0);
          glVertex3f(1.0, 0.0, 0.0);
          glVertex3f(0.5, 1.0, 0.0);
      glEnd();
  
      glFlush();
  }
  
  void reshapeFcn(GLint newWidth, GLint newHeight)
  {
      glViewport(0,0,newWidth, newHeight);
  
      winWidth = newWidth;
      winHeight = newHeight;
  
  }
  void main(int argc, char** argv)
  {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
      glutInitWindowPosition(400,200);
      glutInitWindowSize(winWidth, winHeight);
      glutCreateWindow("Test");
  
      init();
      glutDisplayFunc(displayFcn);
      glutReshapeFunc(reshapeFcn);
      glutMainLoop();
  }
  </code></pre>
  
  <p>This is the full source code, you can copy and paste to your VS solution and compile.
  You'll need to have glut installed.</p>
  
  <p>The result comes up like this:
  <img src="https://i.stack.imgur.com/JESeY.png" alt="enter image description here"></p>