1

I have been trying to generate an ellipse using OpenGL and I have a feeling I have got something very wrong. I am trying to use an ellipse generating code but for simplicity, I have set the length of the major and minor axes equal. This should give me a circle but somehow that is not what is rendered with OpenGL and I am not sure what is wrong.

So the code is as follows:

glPushAttrib(GL_CURRENT_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glLineWidth(2.0);

// Draw center
glBegin(GL_POINTS);
glVertex2d(0, 0);
glEnd();

glBegin(GL_LINE_LOOP);
// This should generate a circle
for (GLfloat i = 0; i < 360; i++)
{
    float x = cos(i*M_PI/180.f) * 0.5; // keep the axes radius same
    float y = sin(i*M_PI/180.f) * 0.5;
    glVertex2f(x, y);
}

glEnd();
glPopAttrib();

This should generate a circle as far as I can think. However. I get something like the attached image, which is not a circle. I am not sure what I am doing wrong.

enter image description here

Luca
  • 10,458
  • 24
  • 107
  • 234
  • I don't see any issues with your sample code. Perhaps you have a transformation in effect that is set by the caller? See: https://www.opengl.org/archives/resources/faq/technical/transformations.htm – meklarian May 13 '15 at 20:42
  • 1
    How are you setting up your viewport? This seems like an aspect ratio issue. – tzaman May 13 '15 at 20:43

1 Answers1

2

It is a circle in clip space. Note that the horizontal extent is half the screen's width and the vertical extent is half the screen's height. The viewport transformation that maps clip space (-1 to 1 on both axes) to screen space basically performs a scaling and translation, which causes the deformation of the circle.

To prevent this from happening, you need to set up an appropriate projection transform, e.g. with glOrtho.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70