2020-06-19 open gl question glOrtho
I have created an application on a Linux machine that uses Qt and Open Gl to display a frequency spectrum. The code was copied from a strip chart display. There are no rotations, translations, or any type of moves. This is a 2D display with nothing but lines and sometimes points.
Making this short the discovery was that the call to glOrtho()
must be done after the glLoadIdentity()
, and on every iteration. So far, the values to glOrtho()
do not change. In the strip chart they constantly changed as the chart moved through time. If it does not change, why is the reload required?
Someone with more experience with Open GL, and that is not a high hurdle, suggested some changes that led to the solution. It worked, then I commented out some lines to discover the minimum required set. That was followed by some searches and some reading. Am I incorrect and should any of them be re-enabled?
Thank you for your time.
void C_GL_Widget::update_widget_display( )
{
const QString MY_NAME = "C_GL_Widget::update_widget_display";
md_update_widget_display_count ++;
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
// glPushMatrix();
glLoadIdentity();
glOrtho( m_ortho_left_clip, // This is the key. It must be after load identity.
m_ortho_right_clip, // I don't understand why.
m_ortho_bottom_clip,
m_ortho_top_clip,
m_ortho_near_clip,
m_ortho_far_clip );
glColor3ub( 0, 255, 0);
glLineWidth( 2.5 );
glBegin( GL_LINE_STRIP );
double y;
double x;
for( int i = 0; i <= m_frequency_bin_count; i ++ )
{
x = m_display_buffer[ i ].p[ PX ];
y = m_display_buffer[ i ].p[ PY ];
glVertex2d( x,y );
}
glEnd();
display_amplitude_markers( );
display_frequency_markers( );
// glFlush();
// glMatrixMode( GL_PROJECTION ); // project does not belong here
// glLoadIdentity(); //Why load an identify matrix after drawing points?
// glPopMatrix();
update( );
} // end of: update_widget_display ( )