johnMG
2003.11.24, 06:00 PM
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
//------------------------------------------------
void init();
void my_display_func();
void my_reshape_func( int w, int h );
//------------------------------------------------
double x_min = -0.4, x_max = 4.4, y_min = -0.8, y_max = 16.8;
//------------------------------------------------
int main( int argc, char * argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize( 400, 400 );
glutInitWindowPosition( 40, 10 );
glutCreateWindow( "plotter2d" );
init();
glutDisplayFunc( my_display_func );
glutReshapeFunc( my_reshape_func );
glutMainLoop();
return 0;
}
//------------------------------------------------
void init()
{
glClearColor( 0.3f, 0.3f, 0.3f, 0.0f );
glPointSize( 3.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( x_min, x_max, // left, right
y_min, y_max, // bottom, top
-1.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
//------------------------------------------------
void my_display_func()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3d( 1.0, 1.0, 0.6 );
glBegin( GL_LINES );
glVertex3d( x_min, 0.0, 0.0 ); // x-axis.
glVertex3d( x_max, 0.0, 0.0 );
glVertex3d( 0.0, y_min, 0.0 ); // y-axis.
glVertex3d( 0.0, y_max, 0.0 );
glEnd();
glColor3d( 1.0, 0.2, 0.2 );
glBegin( GL_POINTS );
glVertex3d( 0.0, 0.0, 0.0 );
glVertex3d( 1.0, 1.0, 0.0 );
glVertex3d( 2.0, 4.0, 0.0 );
glVertex3d( 3.0, 9.0, 0.0 );
glVertex3d( 4.0, 16.0, 0.0 );
glEnd();
glutSwapBuffers();
}
//------------------------------------------------
void my_reshape_func( int w, int h )
{
// We want to keep the aspect ratio (1, in this case) constant regardless
// of how the window is reshaped. It also needs to stay as large as possible
// and centered within the window. Note that the clipping volume is set
// once in init(), and left alone after that.
if ( w > h )
{
glViewport( w/2 - h/2, // x_bottom_left_corner
0, // y_bottom_left_corner
h, // width
h ); // height
}
else
{
glViewport( 0, h/2 - w/2, w, w );
}
glutPostRedisplay();
}
When I resize the window, it sometimes will cut off that last point at the top right. In fact, fiddling with resizing the window to be wide and short, then tall and thin, and then adjusting one dimension out then back, the bug is repeatable.
Resize the window to be very wide (even moving the left edge off the screen) to see what I mean.
I've got a dual-monitor setup here with my PowerBook G3, but the bug happens even when just resizing it in the main screen.
Please let me know if you see this bug as well.
Oh, and build the code with:
g++ main.cpp -framework OpenGL -framework GLUT -framework Foundation
Thanks.
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
//------------------------------------------------
void init();
void my_display_func();
void my_reshape_func( int w, int h );
//------------------------------------------------
double x_min = -0.4, x_max = 4.4, y_min = -0.8, y_max = 16.8;
//------------------------------------------------
int main( int argc, char * argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize( 400, 400 );
glutInitWindowPosition( 40, 10 );
glutCreateWindow( "plotter2d" );
init();
glutDisplayFunc( my_display_func );
glutReshapeFunc( my_reshape_func );
glutMainLoop();
return 0;
}
//------------------------------------------------
void init()
{
glClearColor( 0.3f, 0.3f, 0.3f, 0.0f );
glPointSize( 3.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( x_min, x_max, // left, right
y_min, y_max, // bottom, top
-1.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
//------------------------------------------------
void my_display_func()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3d( 1.0, 1.0, 0.6 );
glBegin( GL_LINES );
glVertex3d( x_min, 0.0, 0.0 ); // x-axis.
glVertex3d( x_max, 0.0, 0.0 );
glVertex3d( 0.0, y_min, 0.0 ); // y-axis.
glVertex3d( 0.0, y_max, 0.0 );
glEnd();
glColor3d( 1.0, 0.2, 0.2 );
glBegin( GL_POINTS );
glVertex3d( 0.0, 0.0, 0.0 );
glVertex3d( 1.0, 1.0, 0.0 );
glVertex3d( 2.0, 4.0, 0.0 );
glVertex3d( 3.0, 9.0, 0.0 );
glVertex3d( 4.0, 16.0, 0.0 );
glEnd();
glutSwapBuffers();
}
//------------------------------------------------
void my_reshape_func( int w, int h )
{
// We want to keep the aspect ratio (1, in this case) constant regardless
// of how the window is reshaped. It also needs to stay as large as possible
// and centered within the window. Note that the clipping volume is set
// once in init(), and left alone after that.
if ( w > h )
{
glViewport( w/2 - h/2, // x_bottom_left_corner
0, // y_bottom_left_corner
h, // width
h ); // height
}
else
{
glViewport( 0, h/2 - w/2, w, w );
}
glutPostRedisplay();
}
When I resize the window, it sometimes will cut off that last point at the top right. In fact, fiddling with resizing the window to be wide and short, then tall and thin, and then adjusting one dimension out then back, the bug is repeatable.
Resize the window to be very wide (even moving the left edge off the screen) to see what I mean.
I've got a dual-monitor setup here with my PowerBook G3, but the bug happens even when just resizing it in the main screen.
Please let me know if you see this bug as well.
Oh, and build the code with:
g++ main.cpp -framework OpenGL -framework GLUT -framework Foundation
Thanks.