1 / 22

Getting Started: Drawing Figures

Getting Started: Drawing Figures. CVGLab. Goals of the Ghapter. To get started writing programs that produce pictures. To learn the basic ingredients found in every OpenGL program. To develop some elementary graphics tools for drawing lines, and polygons.

toshi
Download Presentation

Getting Started: Drawing Figures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Getting Started: Drawing Figures CVGLab

  2. Goals of the Ghapter • To get started writing programs that produce pictures. • To learn the basic ingredients found in every OpenGL program. • To develop some elementary graphics tools for drawing lines, and polygons. • To develop tools that allow the user to control a program with the mouse and keyboard.

  3. Getting Started making Pictures • Every graphics program begins: -. Establish the desired display mode and set up a coordinate system for specifying points, lines, etc.

  4. Getting Started making Pictures • The elementary drawing tools: -. setPixel (x, y, color); -. line (x1, y1, x2, y2); -. moveto (x, y); lineto (x, y);

  5. Getting Started making Pictures • Device-independent Programming and OpenGL -. Graphics programming: device-independent • Windows-based Programming • Event-driven Programming -. glutDisplayFunc (myDisplay); -. glutReshapeFunc (myReshape); -. glutMouseFunc (myMouse); -. glutKeyboardFunc (myKeyboard);

  6. Getting Started making Pictures • Opening a Window for Drawing -. glutInit (&argc, argv); -. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); -. glutInitWindowSize (640, 480); -. glutInitWindowPosition (100, 150); -. glutCreateWindow (“my first attempt”); -. myInit(); -. glutMainLoop();

  7. Drawing Basic Graphics Primitives • The initial coordinate system for drawing. • Drawing three dots. • glBegin (GL_POINTS); • glVertex2i (100, 50); • glVertex2i (100, 130); • glVertex2i (150, 130); • glEnd ();

  8. Drawing Basic Graphics Primitives • glVertex2i (): have several variations,

  9. Drawing Basic Graphics Primitives • OpenGL Data Types

  10. Drawing Basic Graphics Primitives void drawDot (int x, int y) { // draw dot a integer point (x, y) glBegin (GL_POINTS); glVertex2i (x, y); glEnd (); } void drawDot (GLint x, GLint y) { // draw dot a integer point (x, y) glBegin (GL_POINTS); glVertex2i (x, y); glEnd (); }

  11. Drawing Basic Graphics Primitives • The OpenGL “State” -. The color of a drawing can be specified using glColor3f (red, green, blue); -. The background color glClearColor (red, green, blue, alpha); -. To clear the entire window to the background color glClear (GL_COLOR_BUFFER_BIT);

  12. Drawing Basic Graphics Primitives • Establishing the Coordinate System -. myInit: a good place to set up the coordinate system. void myInit (void) { glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0, 640.0, 0, 480.0); } • Drawing Dot Constellations -. A dot constellayion is some pattern of dots or points.

  13. Making Line Drawings • Line drawings are fundamental in computer graphics. glBegin (GL_LINES); glVertex2i (40, 100); glVertex2i (202, 96); glEnd(); • Be emcapsulated for convenience in the routine drawLineInt() void drawLineInt (GLint x1, GLint y2, GLint x2, GLint y2) { glBegin (GL_LINES); glvertex2i (x1, y1); glVertex2i (x2, y2); glEnd(); }

  14. Making Line Drawings • Drawing Polylines and Polygons -. Polyline: a collection of line segments joined end to end. p0 = (x0, y0), p1= (x1, y1), …, pn = (xn, yn). -. “line strip” glBegin (GL_LINE_STRIP); glVertex2i (20, 10); glVertex2i (50, 100); glVertex2i (20, 80); glVertex2i (50, 80); glEnd(); glFlush();

  15. Making Line Drawings • Line Drawing using moveto () and lineto () -. line-drawing tool -. CP (current position): a hypothetical pen whose position moveto (x, y); lineto (x, y); • Drawing Aligned Rectangles -. Aligned rectangle: its sides are aligned with the coordinate exes. glRecti (GLint x1, GLint y1, GLint x2, GLint y1);

  16. Making Line Drawings

  17. Making Line Drawings • Aspect Ratio of an Aligned Rectangle aspect ratio = width / heihgt

  18. Making Line Drawings • Filling Polygons • -. Convex Polygon: A polygon is convex if a line connecting any two points of the polygon lies entirely within it. glBegin (GL_POLYGON) glVertex2f (x0, y0); glVertex2f (x1, y1); . . . glVertex2f (xn, xn); glEnd();

  19. Making Line Drawings

  20. Making Line Drawings • Other Graphics Primitives in OpenGL -. GL_TRIANGLES -. GL_QUADS -. GL_TRIANGLE_STRIP -. GL_TRIANGLE_FAN -. GL_QUAD_STRIP

  21. Simple Interaction with the Mouse and Keyboard • glutMouseFunc (myMouse) • glutMotionFunc (myMoveMouse) • glutKeyboardFunc (myKeyboard) • Mouse Interaction -. myMouse() Void myMouse (int button, int state, int x, int y); -. button GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON, -. state GLUT_UP, GLUT_DOWN

  22. Simple Interaction with the Mouse and Keyboard • Keyboard Interaction -. myKeyboard () Void myKeyboard (unsigned int key, int x, int y); -. key: the ASCII value of the key pressed. -, x, y: report the position of the mouse at the time that the event occurred.

More Related