OpenGL: GL_QUADS does not draw square

Mertcan Ekiz picture Mertcan Ekiz · Aug 15, 2012 · Viewed 13.6k times · Source

I'm trying to draw a square on the screen but it clearly draws a rectangle.

This is my render code:

glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(0,0,-0.1);
glBegin(GL_QUADS);
    glVertex3f(0,0,0);
    glVertex3f(1,0,0);
    glVertex3f(1,1,0);
    glVertex3f(0,1,0);
glEnd();

SDL_GL_SwapBuffers();

And OpenGL Init code:

glClearColor(0,0,0,0.6f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30,640.0/480.0,.3f,200.0);
glMatrixMode(GL_MODELVIEW);

Why is this happening?

Answer

tiguero picture tiguero · Aug 15, 2012

I don't see anywhere in your code where you have set-up the glViewport. I will rather write something like this in your init method:

glViewport(0,0,640,480);        // Reset The Current Viewport

glMatrixMode(GL_PROJECTION);    // Select The Projection Matrix
glLoadIdentity();               // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(30.0f,(GLfloat)640/(GLfloat)480,0.3f,200.0f);

glMatrixMode(GL_MODELVIEW);     // Select The Modelview Matrix
glLoadIdentity();       

also check the second Nehe tutorial it will help you to start with OpenGL for very basic stuff like drawing primitives such as triangle, square etc...