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) {
glColor3f(1.0f, 0.0f, 1.0f);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);

void drawDot(GLint x, GLint y) {

void myDisplay(void) {

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)

void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow(“my first attempt”);

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

Thanks in advance.


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.