#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();
}