AdumbCopper
AdumbCopper

Reputation: 23

GL_INVALID_OPERATION error 1218 (0x0502) when using simple OpenGL commands glvertex2i, glColor3ub

This is my first time using OpenGL, so this may be a stupid question, but my code is pulling a GL_INVALID_OPERATION error (1218 or 0x0502) between glBegin() and glEnd(). I'm only calling a couple simple commands but glGetError() returns 0 at the start of this function, and 1218 at the end.

This function is intended to be used to draw a single instance of any simple shape, and after stepping through in the debugger, the variables are what I intended them to be. Also note that, despite this being a stupid question, I spent several hours last night and several hours today searching Google and my textbook for possible solutions, and have found nothing helpful due to the vague nature of OpenGL errors.

void drawShape(GLenum type, int numPoints, GLubyte r, GLubyte g, GLubyte b, vertex vertices, ...) {
    glBegin(type);
    glColor3ub(r, g, b);

    // iterate through vertices
    va_list vertexList;
    va_start(vertexList, vertices); // use vertexList for iteration
    for (int i = 0; i < numPoints; ++i) {
        vertex point = i == 0 ? vertices : va_arg(vertexList, vertex);
        // add a new point at (x, y)
        glVertex2i(std::get<0>(point), std::get<1>(point));
    }
    glEnd();
}

This function is called as follows:

drawShape(GL_LINE, 2, 0, 0, 0, vertex{ 10, 10 }, vertex{ 400, 10 });

And the context I'm using is FreeGLUT which is initialized as follows:

// gross workaround for calling init with no arguments
int argc = 1;
char *argv[1] = { (char*)"" };
glutInitContextVersion(4, 1);
glutInit(&argc, argv);
// RGB window, double buffer
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
// sets values necessary for window initialization
glutInitWindowPosition(INIT_X, INIT_Y);
glutInitWindowSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// draw the window and create the scene
glutCreateWindow(WORKING_WINDOW_TITLE);
glClearColor(1, 1, 1, 1); // set clear color to white
glutDisplayFunc(render);
glutMainLoop();

Upvotes: 0

Views: 1603

Answers (1)

BDL
BDL

Reputation: 22175

The problem lies in the call to glBegin itself. GL_LINE is not a valid parameter for this function. Due to this, all commands after that are not called inside a valid glBegin/glEnd block.

If you want to draw lines, the correct parameter is GL_LINES.

The first step when debugging an OpenGL application that doesn't support a debug context should always be to identify which function call produces the first error. This can, for example, be done by calling glGetError() after each OpenGL call until the problematic line is found. Also note, that glError returns errors in arbitrary order if more than one error happened.

Upvotes: 2

Related Questions