#include <gl/glut.h>
const int InitWidth = 300;
const int InitHeight = 300;
void MyDisplay();
void MyReshape(int NewWidth, int NewHeight);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(InitWidth, InitHeight);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glutCreateWindow("OpenGL Aspect Ratio");
glutReshapeFunc(MyReshape);
// glutReshapeFunc sets the reshape callback for the current window.
glutDisplayFunc(MyDisplay);
glutMainLoop();
return 0;
}
void MyDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
glFlush();
}
void MyReshape(int NewWidth, int NewHeight)
{
glViewport(0, 0, NewWidth, NewHeight);
// glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates.
GLfloat WidthFactor = (GLfloat)NewWidth / (GLfloat)InitWidth;
GLfloat HeightFactor = (GLfloat)NewHeight / (GLfloat)InitHeight;
glMatrixMode(GL_PROJECTION); // glMatrixMode sets the current matrix mode.
glLoadIdentity(); // glLoadIdentity replaces the current matrix with the identity matrix.
glOrtho(-1.0 * WidthFactor, 1.0 * WidthFactor, -1.0 * HeightFactor, 1.0 * HeightFactor, -1.0, 1.0);
// glOrtho describes a transformation that produces a parallel projection.
}