0

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();
genpfault
  • 51,148
  • 11
  • 85
  • 139
AdumbCopper
  • 23
  • 1
  • 4
  • 1
    Please show a minimal, complete and verifiable example. Do you even have a valid context? With which parameters do you call the function? – BDL Sep 06 '17 at 22:05
  • Are you using a core context? Secondly is your OpenGL context "current" context when your function gets called? – Robinson Sep 06 '17 at 22:06
  • @BDL I'm calling the function as follows: `drawShape(GL_LINE, 2, 0, 0, 0, vertex{ 10, 10 }, vertex{ 400, 10 }); ` – AdumbCopper Sep 06 '17 at 22:08
  • @Robinson added more detail above – AdumbCopper Sep 06 '17 at 22:17
  • 2
    Make sure you want to be using this ancient OpenGL api. It's a dead end. You should be learning how to write shaders. The best tutorial is at http://open.gl and starts right on the front page. What you are doing is like learning how to program for Windows 3.0. – xaxxon Sep 06 '17 at 22:35

1 Answers1

2

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.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • So I should simply change the first parameter of the function call to `GL_LINES`? I tried that and got the same error. – AdumbCopper Sep 06 '17 at 22:20
  • I also get the same problem when calling `drawShape(GL_POLYGON, 4, 0, 50, 200, vertex{ 10, 200 }, vertex{ 110, 200 }, vertex{ 10, 400 }, vertex{ 110, 400 });` – AdumbCopper Sep 06 '17 at 22:23
  • You should start by changing this parameter and then identify which line exactly causes the error. – BDL Sep 06 '17 at 22:29