Search code examples
c++openglglut

Why doesn't OpenGL display the requested drawings correctly


Greetings I'm new to OpenGL and Glut. I made a function that displays the 4 dots that make the corners of a square without uniting the dots. The problem is when I run the project, the function doesn't display anything. I have a similar function that works with lines and displays some lines in the corners of the window which works perfectly. Please tell me what I'm doing wrong and how to fix it. Here are the two functions:

  1. Using Lines. Works perfectly

    void Display2() {
        glColor3f(1,0.1,0.1); 
        glBegin(GL_LINES); 
            glVertex2f(1.0,1.0); 
            glVertex2f(0.9,0.9); 
            glVertex2f(0.8,0.8); 
            glVertex2f(0.7,0.7); 
            glVertex2f(0.6,0.6); 
            glVertex2f(0.5,0.5); 
            glVertex2f(-0.5,-0.5); 
            glVertex2f(-1.0,-1.0);
        glEnd();
    }
    
  2. The one with dots. Does not display anything.

    void Display3() {
        glColor3f(1,0.1,0.1);
        glBegin(GL_POINTS);
            glVertex2f(100, 100);
            glVertex2f(200, 100);
            glVertex2f(100, 200);
            glVertex2f(200, 200);
        glEnd();
    }
    

Solution

  • The default visible area in OpenGL ranges from -1 to 1 on each axis (the so-called normalized device coordinates).

    In your line example, all the values are in this range and thus are visible. In the point example, the coordinates are simply outside of the screen. You can either change the coordinates to fit in the [-1,1] interval, or add a projection matrix that handles that for you.