// Computer Graphics - Amir Hesami // This program is showing how to draw // points with different colors using a keyboard // and quit with ctrl-c // keyboard 1.cpp #include #include #pragma comment(lib, "opengl32.lib") // Link OpenGL32.lib #pragma comment(lib, "glu32.lib") // Link Glu32.lib #pragma comment(lib, "glut32.lib") // Link Glut32.lib #pragma comment(linker, "/entry:\"mainCRTStartup\"" ) // set the entry point to be main() #pragma comment(linker, "/subsystem:\"Windows\"" ) // set the entry point to be main() const int screenWidth=640; const int screenHeight=480; static int color; class point{ public: GLint x,y; }; void display(void); void init(void); void drawpoint(GLint x,GLint y); void bigdipper(void); void keyboard(unsigned char key, int x, int y); void special(int,int,int); void reshape(int ww, int wh); int main(int argc, char** argv) { glutInit(&argc, argv); //initialize the toolkit glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB); //set display mode glutInitWindowSize(screenWidth, screenHeight); // set window size glutInitWindowPosition(50,50); //set window position glutCreateWindow("Keyboard: R:Red, G:Green, B:Blue, Q:Quit F1: Quit"); // create the window init(); glutDisplayFunc(display); // register redraw function glutKeyboardFunc(keyboard);//register keyboard events glutSpecialFunc(special);//register special keys glutReshapeFunc(reshape); glutMainLoop(); // go into the loop return 0; } void init(void) { glClear(GL_COLOR_BUFFER_BIT); //clear the screen glClearColor(1.0, 1.0, 1.0, 1.0); //set background color glPointSize(4.0); // set the size of the points glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, float(screenWidth), 0.0, float(screenHeight)); } void display(void) { } void reshape(int ww, int wh) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, float(screenWidth), 0.0, float(screenHeight)); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,ww, wh); glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void drawpoint(GLint x,GLint y) { glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); glFlush(); } void bigdipper(void) { point p[8]={{289,190}, {320,128}, {239,67}, {194,101}, {129,83}, {75,73}, {74,74}, {20,10}}; for(int i=0;i<8;i++) drawpoint(p[i].x,p[i].y); } void keyboard(unsigned char key, int x, int y) { switch(key){ case 'R': case 'r':glColor3f(1.0, 0.0, 0.0); bigdipper(); break; case 'G': case 'g':glColor3f(0.0, 1.0, 0.0); bigdipper(); break; case 'B': case 'b':glColor3f(0.0, 0.0, 1.0); bigdipper(); break; case 'Q': case 'q':exit(0); break; default:break; } } void special(int key, int x, int y) { if(key==GLUT_KEY_F1) exit(0); }