/* CHEERS: This program draws a few lines here and there in the window And yes.... now you can resize or move the window */ # include # include struct Color { float r, g, b; }; struct Point { float x, y; }; void DrawLine(struct Color color, struct Point p, struct Point q) { glColor3f(color.r, color.g, color.b); glVertex2f(p.x, p.y); glVertex2f(q.x, q.y); } void Draw(void) { struct Color color; struct Point p, q; // r g b a glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glLineWidth(1.0); glBegin(GL_LINES); color.r = 0.0; color.g = 0.0; color.b = 1.0; p.x = 50.0; p.y = 125.0; q.x = 200.0; q.y = 125.0; DrawLine(color, p, q); color.r = 1.0; color.g = 1.0; color.b = 1.0; p.x = 50.0; p.y = 150.0; q.x = 175.0; q.y = 150.0; DrawLine(color, p, q); color.r = 1.0; color.g = 0.0; color.b = 1.0; p.x = 50.0; p.y = 175.0; q.x = 150.0; q.y = 175.0; DrawLine(color, p, q); glEnd(); //You can't set the line width in between begin and end glLineWidth(5.0); glBegin(GL_LINES); color.r = 1.0; color.g = 1.0; color.b = 0.0; p.x = 50.0; p.y = 50.0; q.x = 500.0; q.y = 50.0; DrawLine(color, p, q); color.r = 1.0; color.g = 1.0; color.b = 0.0; p.x = 50.0; p.y = 75.0; q.x = 400.0; q.y = 75.0; DrawLine(color, p, q); color.r = 1.0; color.g = 0.0; color.b = 1.0; p.x = 50.0; p.y = 100.0; q.x = 300.0; q.y = 100.0; DrawLine(color, p, q); glEnd(); glFlush(); } void OnWindowResize(int width, int height) { //See how the output changes if you change left and top int left = 0; int bottom = 0; // Feel free to to change/leave this macro # define YOU_ARE_A_LAZY_PERSON 1 glViewport(left, bottom, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(YOU_ARE_A_LAZY_PERSON) gluOrtho2D(0.0, width, 0.0, height); else gluOrtho2D(-width/2.0, width/2.0, -height/2.0, height/2.0); } void OnKeyPress(unsigned char key, int x, int y) { # define ESC_Key 27 switch (key) { case ESC_Key: exit(0); break; } //printf("\nThe mouse is @..(%d, %d)\n", x, y); } int main(int argc, char** argv) { const int left = 0; const int top = 0; const int width = 512; const int height = 256; const char* windowTitle = "A few of my world-famous lines"; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(left, top); glutCreateWindow(windowTitle); glutDisplayFunc(Draw); //NEW FUNCTIONS in this tutorial's main function glutReshapeFunc(OnWindowResize); glutKeyboardFunc(OnKeyPress); glutMainLoop(); return 0; } /* Exercise: Draw a structure like this... +-------+ | ^ ^ | | o o | | __ | +-------+ | ----+--- \ / | \ \ / | \ \/ | \ | \ | \ / \ \ / \ / \ / \ / \ --- --- */ // // MORAL of this tutorial: // ~~~~~~~~~~~~~~~~~~~~~~~~ // When life gives you lines, you make line-man from it. //