Intermitent output

Use: WinMe, visual C++6.
Dear All,
The program is supposed to display dots at mouse-click position within the created window.
Instead: Initially no output at mouse-clicks(empty background) most of the time. Sometimes later they appear without mouse-clicks, and sometimes later the dots disappear, usually when shifting the window. Sometimes, it shows in the MS-DOS windows but it later disappear.

The code:
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#define screenHeight 480

void myInit(void) {
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0f, 0.0f, 1.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void drawDot(GLint x, GLint y) {
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}

void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}

void myMouse(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
drawDot(x, screenHeight - y);
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(-1);
}

void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow(“my first attempt”);
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}

Question:
What cause the strange behavior? And how to solve it?

Thanks in advance.

jovialone

For delayed output: Commands in OpenGL can be buffered up, and may not be executed at the time you call them. What happens is when you draw a dot, it is placed in some sort of queue (how this actually work this is implementation dependent by the way, but this is the general idea) and is executed when the driver decides to do so. To force the queue to execute all it’s commands, put a glFinish after you draw the point. A glFlush may work also.

For dissapearing dots: When you resize the window, GLUT will first call the reshape callback and then the display callback. In the display callback you explicitly clear the color buffer, clearing the dots you you have drawn previously. If you want the points to remain, you will have to add each point drawn to some sort of list. In the display function, clear the display and redraw the points in the list.

Yep, thats exactly how I did it in my program. Although, since I’m just doing little programs at the moment I just created a screen array but a linked list would be better. When I mouseclick and get the x,y coordinates I place a marker in the x,y coordinate of the array. Later when the render routine is run I iterate through the array and draw the dots for each coordinate that has been marked. Seems to work okay so far. Slowly extending the program though.

Tina