PDA

View Full Version : a simple question



drdolittle
10-30-2008, 06:51 PM
iam trying to draw a rectangle in a window. In the following code. The following code does the job, but when i uncomment the commented line, the drawn rectangle changes in size(almost disappears). Can somebody explain whats happening ?

#include<GL/glut.h>
#include<GL/gl.h>

void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);

// glOrtho(-100,100,-100,100,-1,1);

glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(-25.0,25.0,0.0);
glVertex3f(25.0,25.0,0.0);
glVertex3f(25.0,-25.0,0.0);
glVertex3f(-25.0,-25.0,0.0);
glEnd();



glFlush();
}
void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat aspectRatio;

if(h==0)
h=1;

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

aspectRatio=(GLfloat)w/(GLfloat)h;
if(w<h)
glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,1.0,-1.0);
else
glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,1.0,-1.0);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void SetupRC(void)
{
glClearColor(0.0,0.0,0.0,0.0);

}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(200,200);
glutCreateWindow("test");
SetupRC();
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutMainLoop();

return 0;
}


Best,
Pradeep

k_szczech
10-31-2008, 05:00 AM
glOrtho does not set matrix to given state - it modifies current matrix.
So right now your marix is glOrtho * glOrtho * glOrtho * glOrtho.... Every call to RenderScene multiplies your current matrix by ortho matrix.

You should move entire code from ChangeSize() to RenderScene(). This way you'll have glLoadIdentity() called every frame and your matrix will be re-set, and ready to be set-up with glOrtho().
Use ChangeSize() only to remember new size and eventually force redraw.

In OpenGL it's important to understand how things work before you start coding. I bought a cheap book written by 2 students. Costed me $10. I've read it all from beginning to an end.
Then when I started coding I understood everything. I recommend this approach.

drdolittle
10-31-2008, 08:10 AM
thank you for clarifying.....would you tell you me the author and title of the book or an amazon link preferably, anything of that sort would be much helpful.

Best,
Pradeep

_NK47
10-31-2008, 08:43 AM
any OpenGL book will do. from OpenGL bible till anything covering OpenGL topics. this things are pretty easy but important to understand the flow. i have the red book for example which clarifies alot of simple topics. for more advanced stuff i recommend Advanced Graphics Programming using OpenGL though its a bit older.