user interaction with opengl

Hello everyone! I’m new to OpenGL and new to this forum. :smiley:
I am trying to make a program, in which I draw on screen using the mouse. When I press Esc in keyboard, I would like to delete everything I’ve drawn. For that purpose I use this keyboard function:

void ExitDraw(unsigned char key, int x, int y)
{
if(key==27) glClear(GL_COLOR_BUFFER_BIT); //clear the window by pressing Esc
glutPostRedisplay();
}

This deletes every point I’ve drawn except for the last one! What is wrong? I don’t get it… :o
Thanks for everyone’s help in advance! :slight_smile:

You should not issue draw commands (including glClear) from input handlers (mouse or keyboard). Instead your input handlers should fill in data structures that describe where and what to draw and your display function then does all drawing based on the recorded data.

As for your specific problem: you are not showing your mouse handler or display function, so it’s not possible to be sure, but my guess is one of those runs after clearing the screen and redraws the last point.

BTW: please take a look at the Forum Posting Guidelines for suggestion on what information to include in questions and proper use of [ code]/[ /code] tags around source code.

Thank for your help! My whole code is:

 
#include "stdafx.h"
#include <stdio.h>
#include <glut.h>
#include <math.h>

int x_wind=400,y_wind=400; //the size of the window
float x_position=0., y_position=0.; //the initial position of the point
float x_max=1000.,y_max=1000.; //the size of the system

void initmywindow()
{
	glutInitWindowPosition(700,200);
	glutInitWindowSize(x_wind,y_wind);
	glutCreateWindow("Draw with your mouse!");
	glClearColor(0.8,0.9,0.9,0.);
	glClear(GL_COLOR_BUFFER_BIT);//clear the window with the clear color
	gluOrtho2D(0.,x_max,0.,y_max);
}

void Drawing (float x, float y)
{
	glBegin(GL_POINTS);
		glVertex2f(x,y); //draw a point
	glEnd();
}

void MouseDraw()
{
	glColor3f(0.,0.,0.8);
	glPointSize(2.);
	glEnable(GL_POINT_SMOOTH);
	Drawing(x_position,y_position);
	glFlush();
}

void ExitDraw(unsigned char key, int x, int y)
{
	if(key==27) glClear(GL_COLOR_BUFFER_BIT); //clear the window by pressing Esc
	glutPostRedisplay();
}

void MouseControl (int x, int y)
{
	x_position=x_max*x/x_wind; // draw in the position of the mouse in pixels
	y_position=y_max-y_max*y/y_wind;
	glutPostRedisplay(); // run the display function again
}

void main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	initmywindow();        
	glutDisplayFunc(MouseDraw); 
	glutKeyboardFunc(ExitDraw);
	glutMotionFunc(MouseControl);
	glutMainLoop();
}

Ok, as i guessed: your display function (re)draws the last point.
If you have trouble figuring out the sequence of events, put a printf(“name of function”) at the beginning of the functions you register as callbacks with glut. That way you will see that after ExitDraw MouseDraw gets called (you request that your display function is run with the call to glutPostRedisplay()).

I understand… Thank you for your help, I really appreciate it! I’ll get to fix it! :slight_smile:

I spend so many hours trying to figure this out! I finally did it right! Thank you very much!