Hello, im reading a book about OpenGL programming and i’ve made a little app now that shows a blue screen with a red rectangle in the middle. Only i dont get some explanations of the book and i was hoping to get some better explanations about it here.
Im gonna post the whole code, but the part i dont understand is in the function “ChangeSize()” I commented everyline i dont understand or that i think i understand and try to explain what could happen. Can someone please tell me how and what the lines mean and do that i dont understand or where im wrong.
Thanks for any help.
#include <glut.h>
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(10.0f, 10.0f, 0.0f, 0.0f);
glFlush();
}
void SetupRC()
{
glClearColor(-25.0f, 25.0f, 25.0f, -25.0f);
}
void ChangeSize(GLsizei w, GLsizei h) //define 2 vars (w and h) for widht and height
{
GLfloat aspectRatio; //Dont know what happends here
if(h==0)
h=1;
glViewport(0, 0, w, h); //the first two numbers (0 and 0) are the values for
//the top and left placement in the window. (0,0) is a center posistion?
//the (w and h) are the values for (in this case) the width and height of the current window
glMatrixMode(GL_PROJECTION); //Dont know
glLoadIdentity(); //Has something to do with the glMatrixMode
aspectRatio = (GLfloat)w / (GLfloat)h; //Dont know what aspectRatio is for but it devides the window width with the window height(?) but for what reason?
if(w <= h) //if width is smaller or equals the heigth
glOrtho (-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); //Dont know what happends here exactly
else
glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0); //same here, for what does glOrtho uses 6 parameters
glMatrixMode(GL_MODELVIEW); //Dont know why this is used again
glLoadIdentity(); //Same here
}
void main(void)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("GLRectangle");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
}