1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <glut.h>
GLfloat xPosition = 0.0;
GLfloat yPosition = 0.0;
GLsizei winWidth = 400, winHeight = 400, shapeSize = 25;
GLfloat red = 1.0f, blue = 0.0f, green = 0.0f;
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, winWidth, 0.0, winHeight);
}
void displayFcn (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glColor3f (red, green, blue);
glBegin (GL_POLYGON);
glVertex2i (50,100);
glVertex2i (50,50);
glVertex2i (200,50);
glVertex2i (200,100);
glEnd ();
glFlush ( );
}
void update(int value)
{
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27)
exit(0);
}
void processSpecialKeys(int key,int x,int y)
{
switch (key)
{
case GLUT_KEY_F1:
red = 1.0;
green = 0.0;
blue = 0.0;
break;
case GLUT_KEY_F2:
red = 0.0;
green = 1.0;
blue = 0.0;
break;
case GLUT_KEY_F3:
red = 0.0;
green = 0.0;
blue = 1.0;
break;
default:
break;
}
}
void mouseClickHandler(int button, int state, int x, int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin (GL_POLYGON);
glVertex2i (45, 75);
glVertex2i (75, 45);
glVertex2i (105, 75);
glVertex2i (75, 105);
glEnd ();
glFlush ( );
break;
default:
break;
}
}
void drag(int x, int y)
{
xPosition = (GLfloat) x/winWidth;
yPosition = 1.0 - (GLfloat) y/winHeight;
glutPostRedisplay();
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (350, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("Rectangle Program");
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
glutMouseFunc(mouseClickHandler);
glutMotionFunc(drag);
glutPassiveMotionFunc(drag);
init();
glutDisplayFunc (displayFcn);
glutTimerFunc(25,update,0);
glutMainLoop ( );
}
| |