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?

有帮助吗?

解决方案

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top