3

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?

Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
Mertcan Ekiz
  • 691
  • 1
  • 9
  • 22

2 Answers2

6

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...

tiguero
  • 11,477
  • 5
  • 43
  • 61
  • Thanks. I don't know what glViewPort does but casting to GLfloat did the trick for me. – Mertcan Ekiz Aug 15 '12 at 21:03
  • Ok i have just read that when an OpenGL context is first attached to a window, width and height are set to the dimensions of that window so it might be not necessary. As for the glViewport command it defines a transformation from normalized device coordinates (that is, post-projection matrix coordinates with the perspective divide applied) to window coordinates – tiguero Aug 15 '12 at 21:35
0

Try using gluOrtho2D to generate a correct orthogonal projection matrix, in your case gluOrtho2D(0,640,0,480), this is assuming you want a square in 2D and not 3D.

This will of course change your coordinate system from (0,1),(0,1) to (0,640),(0,480).

Necrolis
  • 25,836
  • 3
  • 63
  • 101