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
|
#include <GL/glut.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
typedef struct _POINT
{
float x, y;
}POINT;
float MinX = -1.0, MaxX = 1.0, MinY = -1.0, MaxY = 1.0;
float scale = 0.03;
int FPS = 30;
int msec = 1000.0 / FPS;
POINT ObjectPoint = {0, 0};
int ptColor[3] = {0, 0, 0};
void init()
{
// White Background
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(MinX, MaxX, MinY, MaxY);
glPointSize(10);
}
void DisplayFunction()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3iv(ptColor);
printf("F1 pressed - [DisplayFunction] {%d, %d, %d}\n", ptColor[0], ptColor[1], ptColor[2]);
glVertex2f(ObjectPoint.x, ObjectPoint.y);
glEnd();
glutSwapBuffers();
}
void KeyboardFunction(unsigned char key, int x, int y)
{
switch(key)
{
case 'q':
case 'Q':
case 27: //Esc
exit(0);
break;
default:
break;
}
glutPostRedisplay();
}
void SpecialFunction(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_UP:
ObjectPoint.y += scale;
break;
case GLUT_KEY_DOWN:
ObjectPoint.y -= scale;
break;
case GLUT_KEY_RIGHT:
ObjectPoint.x += scale;
break;
case GLUT_KEY_LEFT:
ObjectPoint.x -= scale;
break;
case GLUT_KEY_F1:
ptColor[0] = rand() % 256;
ptColor[1] = rand() % 256;
ptColor[2] = rand() % 256;
printf("F1 pressed - [SpecialFunction] {%d, %d, %d}\n", ptColor[0], ptColor[1], ptColor[2]);
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
srand((unsigned)time(0));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL Example");
init();
glutDisplayFunc(DisplayFunction);
glutKeyboardFunc(KeyboardFunction);
glutSpecialFunc(SpecialFunction);
glutMainLoop();
}
|