190 likes | 200 Views
Learn the basics of drawing figures using OpenGL, including setting up an environment, drawing lines, using event-driven programming, and drawing objects. Understand color, background color, and establishing the coordinate system.
E N D
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 • 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.
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);
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
How to get started • Four principal types of events & “glut” works for each program i.e: glutDisplayFunc(); glutReshapeFunc(); glutMouseFunc(); glutKeyboardFunc();
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
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
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
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
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
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);
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); }
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(); }
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(); }
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(); }