2

I am trying to draw a circle using GL_POLYGON and the code I tried is working but the radius of the circle seems to be out of scale with the window size and I am not understanding why.

Here is the code:

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    if(start == OFF)
    {
        //Set Drawing Color - Will Remain this color until otherwise specified
        glColor3f(0.2, 0.3, 0.5);  //Some type of blue   

        //Draw Circle
        glBegin(GL_POLYGON);
        for(double i = 0; i < 2 * PI; i += PI / 24) 
            glVertex3f((cos(i) * radius) + gX,(sin(i) * radius) + gY, 0.0);
        glEnd();

        //Draw Circle
        center_x = gX;
        center_y = gY;
    }     

    glutSwapBuffers();
}

and the init function:

void init (void) 
{
  /* selecionar cor de fundo (preto) */
  glClearColor (0.0, 0.0, 0.0, 0.0);

  /* inicializar sistema de viz. */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

I am setting the window size as 600 and the radius as 1 and a quarter of the circle takes us the whole window. I am guessing I have to do some kind of transformation with the radius, I just don't know how.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
emic
  • 43
  • 5
  • Is it the z coordinate at gluLookAt? – emic Sep 16 '17 at 22:22
  • You are defining a transformation: From world coordinates to View (or Camera, or Eye) coordiantes. You do it with `lookAt` matrix. You are not using Projection transformation. Your final coordinates are in NDC, wich is a 2x2x2 size box. You should learn about those and other transformations, like Rotation and Scaling. – Ripi2 Sep 16 '17 at 22:22
  • I changed the code a bit to delete looAt, but now the circle is even bigger, one quarter of it is taking the whole window. @Ripi2 – emic Sep 16 '17 at 22:54
  • Again, follow a good tuorial or book and learn about transformations. By the way, get rid of "fixed pipeline" and go with "modern Opengl" (version >= 3.2) – Ripi2 Sep 16 '17 at 22:56

2 Answers2

2

The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. It transforms from eye space to the clip space, and the coordinates in the clip space are transformed to the normalized device coordinates (NDC) by dividing with the w component of the clip coordinates. The NDC are in range (-1,-1,-1) to (1,1,1).

At Orthographic Projection the coordinates in the eye space are linearly mapped to normalized device coordinates.

Orthographic Projection

The orthographic projection can be set up by glOrtho. You are setting up a viewport with a bottom left corner at (0, 0), a top right corner at (1, 1) and depth range from -1 to 1:

glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

If you want to set up a projection that allows you to draw in window size scales, then you have to do it like this:

int wndWidth  = 800; // for example a window with the size 800*600  
int wndHeight = 600; 

glOrtho( 0.0, (float)wndWidth, 0.0, (float)wndHeight, -1.0, 1.0 );

If you want to have the origin of (0, 0) in the center of the window, then you have to do it like this:

float w = (float)wndWidth/2.0f;
float h = (float)wndHeight/2.0f;

glOrtho( -w, w, -h, h, -1.0, 1.0 );

See further:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

If Rabbid76's answer (datetime stamp: 2017-09-17T07:10) did not yet solve your question, verify also using the following computer instructions:

Set window width and height to what you use, e.g. 600x600

Example:

int iWindowWidth=600;
int iWindowHeight=600;

glScalef(iWindowHeight/(iWindowWidth*1.0f), 1.0f, 1.0f);    

  //Draw Circle
  glBegin(GL_POLYGON);
    for(double i = 0; i < 2 * PI; i += PI / 24)  {
      glVertex3f((cos(i) * radius) + gX,(sin(i) * radius) + gY, 0.0);
    }
  glEnd();
        
//reset scaled shape
glScalef(1.0f, 1.0f, 1.0f);

Reminder: We need to convert from integer, i.e. whole number, to floating-point number, i.e. number with fraction.

We add: (iWindowWidth*1.0f)

This is due to iWindowHeight/iWindowWidth, e.g. 1366/768 = 0.5622..., i.e. not whole number

Thank you.

masarapmabuhay
  • 466
  • 1
  • 9
  • 15
  • Please note that the usual method of dealing with aspect ratio is to use a proper [Projection](https://en.wikipedia.org/wiki/3D_projection) matrix, not a scale. (Since the answer is accepted, the problem is likely to be resolved.) – Rabbid76 Aug 05 '21 at 13:38
  • Hallo, yes, your answer has received the check mark to signify it solved emic's question (datetime stamp: 20170916T2216). In our case, despite the same question, the technique that I wrote as answer solved our problem. With regard to using a Projection matrix, we also use it. – masarapmabuhay Aug 07 '21 at 01:24