Strange bug using glVertex2i()

I am writing a project using OpenGL but there is a problem. I succeeded to isolate the problem in the next program

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This source represents the very strange bug on my machine
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#include <GL/glut.h>

void init()
{
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glColor3f( 1.0, 0.0, 0.0 );
}

void reshape( int w, int h )
{
    glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D( 0.0, (GLdouble) w, 0.0, (GLdouble) h );
}

void display()
{
    int i;
    glClear( GL_COLOR_BUFFER_BIT );
    
    glBegin( GL_POINTS );
	for( i = 0; i < 300; i++ )
	{
	    glVertex2i( (GLint) i, (GLint) i/2 );
	}
    glEnd();
    
    glFlush();
}

int main( int argc, char* argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
    glutInitWindowPosition( 100, 100 );
    glutInitWindowSize( 500, 500 );
    glutCreateWindow( "Very strange Bug..." );
    init();
    glutDisplayFunc( display );
    glutReshapeFunc( reshape );
    
    glutMainLoop();
    
    return 0;
}

This simple program just draws a line using GL_POINTS ( I must not use GL_LINES ). But as I said there is a problem. The line starts drawing well not from the beginning. To understand what I am talking about take a look on this screenshot
It is what appears on my machine after starting the program. On the left is the main window and on the right is a small part of it magnified.
I am very confused and do not understand what happens.
I use Linux but this program does not have any Linux specific stuff so it is not a problem. More interesting is that this program runs correctly on Slackware 11 with GeForce FX 5200 and on Slackware 11 with GeForce4, both with drivers installed from NVidia site. The machine I am right now and where the screenshot is from is Slackware 10.2 with some video from Intel, which is very old but I think that the problem is not in the video or drivers but somewhere else.

This is simply due to the program you use to magnify. It cannot devise what’s beyond the pixels. So, when rounding to the near value, it can ommit some values.

I don’t know what you exactly need to do but using lines would certainly help even if another program that magnify it threw a screenshot of the display might still not give the expecting results.

ps: in fact I don’t know if you use another program to magnify the result…

It is not from the program which I use to magnify. It is obvious that the line is broken in the beginning in the main window. I added the magnifier just to help to see more clearly that something strange happens there.

Not sure this is what affects the rendering, but keep in mind that your current code relies on specific floating point rounding behaviour to render the line correct. With the projection matrix you have, integer coordinates will fall on the corner of four neighouring pixels. Which one is chosen for rendering depends on how rounding is performed and on rounding errors in floating point calculations during rasterization.

To make sure your integer coordinates ends up on pixel centers, so you can determine exactly what pixels are actually drawn, you need to offset the projection matrix by 0.5 units.

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, (GLdouble) w, 0.0, (GLdouble) h );
glTranslatef(-0.5, -0.5, 0);

This corner hitting effect can be seen clearly by enabling point smoothing and blending (GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA for example). A point that hits the corner of four pixels will be drawn at 1/4:th or so the intensity on all four neighbouring pixels.

Thanks Bob.
This really solved the problem. Now everything is OK :slight_smile:
Actually I tried this code on some other machines and it occured that in the newer ones there wasn’t problem.
Again thanks for the help.