Search code examples
c++openglgraphics2dglut

opengl glBegin(GL_LINES) and glBegin(GL_POINT) in 2D not visualizing point vector


I'm making a simple 2D ping - pong game. Now I need to visualize both the players (two lines, one at the top of the screen and one at the bottom) and the ball also. However I don't know why the ball just isn't visualizing... Here is part of the code.

 void displayScene()
    {
     glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(0, 0, 0);
        glLineWidth(33);
        glBegin(GL_LINES);
                glColor3f(0, 1, 0);
                glVertex2i(topPlayer->getLeftX(), topPlayer->getLeftY());
                glVertex2i(topPlayer->getRightX(), topPlayer->getRightY());
                glColor3f(1, 0, 0);
                glVertex2i(bottomPlayer->getLeftX(), bottomPlayer->getLeftY());
                glVertex2i(bottomPlayer->getRightX(), bottomPlayer->getRightY());
        glEnd();
        if (ball)
        {
            glPointSize(50);
            glColor3f( 0.0f, 0.0f, 0.0f );
            glBegin(GL_POINT);
                    glColor3f(1, 0, 0);
                    glVertex2i(ball->getX(), ball->getY());
                    ball->moveBall();
            glEnd();
        }
            glFlush();
    }

    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowSize(1920, 1080);
        glutCreateWindow("Ping - Pong");
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glOrtho(0, 1920, 1080, 0, -1, 1);
        topPlayer->setLeftY(1);
        topPlayer->setRightY(1);
        bottomPlayer->setLeftY(1920 - 1);
        bottomPlayer->setRightY(1080 - 1);
        glutDisplayFunc(displayScene);
        glutMainLoop();
        return 0;
    }

PS: ball->moveBall(); just changes the ball x and y. As far debugging the business logic seems good and the x, y coordinates change the way they have to. However the problem is with the visualization of the ball.

PS PS: I know that glBegin is deprecated but I have to use legacy code.


Solution

  • glBegin(GL_POINT);
    

    That's just generating a GL_INVALID_ENUM error (you really should add some error checking to your code, at least for debug builds). The correct enum value is GL_POINTS.

    However, all of that is horribly outdated. Begin/End has basically been superseded by vertex arrays back in 1997, and by buffer objects somewhere around 2003, and the whole fixed-function pipeline by shaders back in 2004. If you're doing OpenGL in 2018, there is really no reason to stick to 20 year old concepts which are all removed from modern core profiles of OpenGL.

    UPDATE on GL Error Checking

    The most basic way is to call glGetError() at "strategic places" in your code. Note that this might force implicit synchronizations in your GL implementation, so it might negatively affect performance. A good strategy is to use that only in debug builds (or other compile-time options).

    A much better approach nowadays is GL's Debug Output feature, where the driver can tell you exactly what is wrong. For example, in the case of using GL_POINT for a primitive type, the nivida linux driver reports:

    GL_INVALID_ENUM error generated. Invalid primitive mode.