glEnd() solicits error 1282

Summary: what might cause glEnd() to solicit error 1282 after a series of glVertex2d() calls? Just before glEnd there is no error, just after there is.
Details
Linux, Centos, OpenGL version 1, and Qt version 3. I am aware of how old they are but this is a medium size government project and management says: It works, do not update. This app runs in a complex telemetry system. I can show the immediate code but the app will not compile outside of this environment. Coworkers do not know this error.
This application draws a strip chart in an Open GL widget, within a Qt widget. It uses a series of glVertex2d() calls to draw line segments that make the display. There are calls to glGetError() at multiple places, with calls placed immediately before after glEnd(). There is no error before the glEnd and after returns error 1282.
The app was working. I discovered that Open GL uses single precision values internally rather than the doubles that are the argument to glVertex2d and made changes to accommodate that behavior. In the extracted code below, the uses of gl_offset accommodate that error. The code shown omits code that writes the argument for every call to glVertex() to a log file. All the X and Y values to glVertex2d are within the limits of the widget.
All the Qt windows are displayed properly. The blank Opengl widget looks good. The vertices drawn are completely outside the range of the Opengl widget, except a very few that cross from top to bottom or bottom to top. I have checked that all the arguments to glOrtho(…) are correct and the vertices are within those limits.
My searches for 1282 yield multiple returns about things such as when using Minecraft and shaders. This code does nothing of the sort.
What sort of things should I look for to find the source of my error?

     for(  int i = m_tail_index; i <= m_head_index; i ++ )
      {
         x = m_gl_points[ i ].p[ PX ];
         y = m_gl_points[ i ].p[ PY ];
         gl_time = x - gl_offset;
         glVertex2d( gl_time,y );
         vertex_count ++;
      }
…
   check_for_gl_errors( MY_NAME + "() " + QString("%1").arg( __LINE__ ) ); // no error
   glEnd();
   check_for_gl_errors( MY_NAME + "() " + QString("%1").arg( __LINE__ ) ); // error 1282

gl error codes are defined in gl.h, probably located in /usr/include/GL/gl.h on your system. code 1282 is 0x0502 in hexadecimal notation, and it means there was a GL_INVALID_OPERATION. the lines you posted have no glBegin(…), maybe that is the reason.

One user requested the option to plot lines or points, so this code precedes the above snip:

   if( m_lines_or_points == PLOT_LINES )
   {
      glLineWidth( PLOT_LINE_WIDTH );
      glBegin( GL_LINE_STRIP );
   }
   else
   {
      glPointSize( PLOT_POINT_SIZE );
      glBegin( GL_POINTS );
   }

I added some printf(…) and for these tests the glBegin( GL_LINE_STRIP ) is always called.

Thanks for taking a look.
I don’t know what else I might try. The check_for_gl_errors function is sprinkled all over the place and there are no errors until the glEnd().

Most OpenGL commands are invalid between glBegin and glEnd; the online reference page lists those which are permitted. Ensure that you aren’t calling anything else. Also ensure that you aren’t calling any Qt methods which may make OpenGL calls or invoke callbacks.

In particular, note that glGetError isn’t a permitted command, so the act of checking for the error in the way that the above code does will cause GL_INVALID_OPERATION.

Certain combinations of invalid state will only be detected when issuing a draw call; any such errors are likely to be reported by the glEnd() call.

1 Like

Looking for that. Meanwhile, I commented out the glBegin() and the same error was returned, and not until after the glEnd() statement. I expected that a missing glBegin would solicit an error sooner.
Edit:
The only gl anything between the begin and the vertex calls was:
glcolor3ub( 0, 255, 0 );
As I recall, that should be ok. But, I moved the glBegin() to just before the glVertex2d() and it made no difference.
Thanks again for your time.

Note that I edited my previous reply to add:

I don’t know what you mean by stating that glGetError isn’t a permitted command. Here is the code for that function:

void C_GL_Widget::check_for_gl_errors( std::string caller_name  )
{
   const QString MY_NAME = "C_GL_Widget::check_for_gl_errors";
        
   while( ( m_gl_error_number = glGetError() ) != GL_NO_ERROR )
   {
       m_gl_error_count ++;
       printf( "\n%4d %s() error number is %d count %d", 
          __LINE__, caller_name.c_str(), m_gl_error_number, m_gl_error_count );
   }
}

Edit: the core problem is that the lines are not drawn within the display confines. I added code to ensure that glOrtho(…) has good coordinates. I added code to log every vertex written and all are within the confines of the glOrtho confines. Then I started adding in the glGetError functions, in the form of the function call noted.
Edit again: Thought some more. Do you intend that glGetError is not permitted between the begin and end?
Edit again: Now I understand. Removed all the error checks between begin and end and have the error no more The glGetError() after the glEnd() does not report an error. So my problem about error 1282 was a red-herring.

The problem is that the lines are being drawn are outside the top and bottom limits. As noted, I show the values of the glOrtho(…) call, and log the values of all the vertices. All are within the limits.
Now I am looking again for an error in how I show those values. Hoping to find an mistake there so the error logging will reveal the real error.

  1. Is the model-view matrix the identity matrix? If you intend it to be so, you should set it explicitly within the display function in case it gets changed elsewhere.
  2. Is the viewport set to the bounds of the window? You need to call glViewport if the window is resized, but you should ideally call it at the beginning of the display function.

To backup what GClements said. The following is illegal:

glBegin();
glGetError();
glEnd();

The behaviour will vary on driver, but somewhere in the spec it does say this is not allowed.

A coworker assisted and commented out these two lines from the top and bottom of the display function.

glMatrixMode( GL_MODELVIEW )
glLoadIdentify()

// drawing code here

glMatrixMode( GL_PROJECTION )
glLoadIdentify()

That corrected the immediate problem, but there are other problems. While his help was greatly appreciated, that coworker is busy with other tasks and does not completely understand OpenGL.

This app is 2D only and never does any matrix work. Should one or both of those functions be in the initialization function?

I would like to try double buffering, but that is another topic.

They should ideally be at the top of the display function. IIRC, you were using glOrtho to set one of them, so the glMatrixMode and glLoadIdentity should go before the glOrtho call. If you aren’t doing lighting, it doesn’t matter which one you set to an identity matrix and which is the orthographic matrix, but it’s more common to use glOrtho on the projection matrix.

If OpenGL isn’t being used outside of your display function, then there’s no need to reset anything at the end of that function, just ensure that everything is set at the start of it.

Putting those calls just before the glOrtho2D(…) call made the difference.
The changes needed to accommodate Open GL using only single precision internally caused me to shift the design strategy from being based on the left and right clip values, to other values. That will take a while.
Thank you for your time and patience.