PDA

View Full Version : Some explanation about a simple code



w00
03-27-2005, 08:23 AM
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();
}

zukko
03-27-2005, 11:31 AM
hi, that change size function, defines the dimensions for the window, sets up the perspective, its just some code to set up the window and the view, that function wont change as your code grows

w00
03-27-2005, 11:50 AM
Yes i know that. Its just that i dont understand everyline of the code fully.

zukko
03-27-2005, 02:26 PM
oh, then:
GLfloat aspectratio; declares a floating point variable called aspecratio.
the if(h==0) h=1; is to prevent a divide by zero.
the glViewPort line is to reset the current viewport.
glMatrixMode(GL_PROJECTION); sets the projection matrix.
glLoadIdentity(); is to reset the matrix.
to get a full description of every gl function go
here (http://pyopengl.sourceforge.net/documentation/manual/refe rence-GL.xml) hope it helps

zukko
03-27-2005, 02:27 PM
here (http://pyopengl.sourceforge.net/documentation/manual/reference-GL.xml)

w00
03-28-2005, 12:56 AM
Thanks for the link. That is really helpful.
Your link didnt work so here is the working one for people who want to see the site too.
http://pyopengl.sourceforge.net/documentation/manual/reference-GL.html

or for the index page

http://pyopengl.sourceforge.net/documentation/manual/