#include <glut.h>
#include<stdio.h>
//The head position
float x,y;
int drag = 0;
void InitAWindow(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT function callings
glutInitWindowSize( 500,500);
glutInitWindowPosition( 100, 100);
glutCreateWindow( "dragging");
return;
}
void Init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-4.0,4.0, -4.0, 4.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void draw_head()
{
glBegin( GL_TRIANGLE_FAN);
glVertex2f( 0.7, 0.95);
glVertex2f( 0.3, 0.9);
glVertex2f(0.15, 0.65);
glVertex2f( 0.55, 0.75);
glVertex2f( 0.6, 0.6);
glVertex2f( 0.8, 0.75);
glVertex2f( 0.99, 0.7);
glEnd();
glBegin( GL_POLYGON); //Draw the mouth
glVertex2f( 0.53, 0.16);
glVertex2f( 0.67, 0.17);
glVertex2f( 0.72, 0.25);
glVertex2f( 0.45, 0.22);
glEnd();
glBegin( GL_TRIANGLES);
glVertex2f( 0.58, 0.52);
glVertex2f( 0.5, 0.3);
glVertex2f( 0.66, 0.3);
glEnd();
glLineWidth(4);
glBegin(GL_LINES);
glVertex2f( 0.3, 0.64);
glVertex2f( 0.5, 0.6);
glVertex2f( 0.64, 0.6);
glVertex2f( 0.86, 0.64);
glEnd();
glPointSize(7);
glBegin(GL_POINTS);
glVertex2f( 0.4, 0.55);
glVertex2f( 0.75, 0.55);
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
draw_head();
glColor3f(0,0,1);
glBegin(GL_POINTS);
glVertex3f(x,y,0);
glEnd();
glFlush();
glutSwapBuffers();
}
void move() {
glTranslatef( x, y, 0.0);
glRotatef( 0.1, 0, 0, 1.0);
glutPostRedisplay();
glTranslatef( -x, -y, 0);
glutPostRedisplay();
}
void mousef( int button, int state, int xx, int yy)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
x = xx/100.-2. -.5; y= 2.-yy/100. -.5;
glutSwapBuffers();
}
}
int main( int argc, char **argv)
{
InitAWindow(argc, argv);
glutMouseFunc( mousef);
glutDisplayFunc(display);
glutIdleFunc(move);
Init();
glutMainLoop();
return 0;
}