1 / 19

Chapter #02

Chapter #02. Drawing Figures. How to get started. An environment to write & execute programs is required Hardware (usually CRT) & software library for graphics are required

benw
Download Presentation

Chapter #02

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. Chapter #02 Drawing Figures

  2. How to get started • An environment to write & execute programs is required • Hardware (usually CRT) & software library for graphics are required • In 2D ‘x’ & ‘y’ are measured in pixels. Where ‘x’ increases horizontally towards right & ‘y’ increases vertically downwards • The most basic tool is SetPixel(x,y,color) and putPixel(), setPixel(),drawPoint() etc.

  3. How to get started • To draw a line: line(x1,y1,x2,y2), drawLine() & Line() • Command: line(100,50,150,180); line(150,80,0,290); • Using a light pen or other pointing device these commands become: moveto(100,50); lineto(150,80); lineto(0,290);

  4. How to get started • Open GL is an API i.e. a collection of routines that the programmer can call, along with a model of how routines work together to produce graphics • Mostly, Window-based-programming is used which is event driven in nature . E.g. click of mouse, stroke of a keyboard key. These events are managed in a Queue & are also called callback functions • glutMouseFunc(myMouse); // registers the function myMouse() as the function to be executed when mouse event takes place. “glut” indicates that this function is a part of Open GL Utility Toolkit

  5. How to get started • Four principal types of events & “glut” works for each program i.e: glutDisplayFunc(); glutReshapeFunc(); glutMouseFunc(); glutKeyboardFunc();

  6. How to get started • A skeleton of a program using OpenGL & is event driven” void main() { // INITIALIZE THINGS // CREATE A SCREEN WINDOW glutDisplayFunc(myDisplay); // REDRAW FUNCTION glutReshapeFunc(myReshape); // RESHAPE FUNCTION glutMouseFunc(myMouse); // MOUSE EVENT FUNCTION glutKeyboardFunc(myKeyboard); // KEYOARD EVENT FUNCTION // PERHAPS OTHER THINGS INITIALIZE HERE glutMainLoop(); } // ALL OF THE CALLBACK FUNCTIONS ARE DEFINED HERE

  7. Drawing Objects • To draw objects in OpenGL, we pass a list of vertices. This list occurs between two OpenGL function calls glBegin() and glEnd(): glBegin(GL_POINTS); glVertex2i(100,50); glVertex2i(100,130); glVertex2i(150,130); glEnd(); • Options: GL_POINTS, GL_LINES, GL_POLYGON

  8. Drawing Objects glVertex2i(…….); • When basic command is used without its argument the above statement becomes: glVertex*(); Data type of argument gl library Basic command Number of arguments

  9. The OpenGL State • OpenGL keeps track of many state variables i.e. current size of a point, current color of a drawing, current background color • The value of a state variable remains active until it is altered

  10. Color A Drawing • Syntax: glColor3f(red, green, blue); • The value of a color might either be 0.0 or 1.0 e.g: glColor3f(1.0, 0.0, 0.0); //set color to red glColor3f(0.0, 0.0, 0.0); //set color to black glColor3f(1.0, 1.0, 1.0); // set color to white glColor3f(10, 1.0, 0.0); // set color to yellow

  11. Background Color • Syntax: glClearColor(red, green, blue, alpha); • Where ‘alpha’ specifies the degree of transparency. Use ‘0.0’ for alpha now • To clear the entire window to background color: glClear(GL_COLOR_BUFFER_BIT);

  12. Establishing the Co-ordinate System • myInit(): is a good place to set up the co-ordinate system • OpenGL may perform transformations to do this it uses matrices • The glutOrtho2D() sets transformations we need for a screen 640 pixels wide & 480 pixels high void myInit(void) { glMatrix(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 640.0, 0, 480.0); }

  13. Primitives

  14. Primitives

  15. Primitives

  16. Example-Sine wave myInit() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-5.0,5.0,-0.3,1.0);} void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINE_STRIP); for(GLfloat x=-4.0;x<4.0;x+=0.1) { glVertex2f(x,sin(3.14159*x)/3.15159); } glEnd(); glFlush(); }

  17. Sierpinski Gaskit

  18. Sierpinski Gaskit Class GLintPoint{ Public: Glint x,y; } void drawDot(int a, int b) { glBegin(GL_POINTS); glColor3f(1.0,0.0,0.0); glVertex2i(a,b); glEnd(); glFlush(); }

  19. Sierpinski Gaskit void Gaskit() { GLintPoint T[3]={{200,200},{400,200},{300,350}}; int index=random(3); GLintPoint point=T[index]; drawDot(point.x,point.x); for(int i=0;i<2500;i++) { index=random(3); point.x=(point.x+T[index].x)/2; point.y=(point.y+T[index].y)/2; drawDot(point.x,point.y); } glFlush(); }

More Related