Computer Graphics Topic: Introduction To Opengl Review Lecture-Sessional I
Computer Graphics Topic: Introduction To Opengl Review Lecture-Sessional I
1
Overview
2
Complete OpenGL Program
1. #include <windows.h> // use as needed for your system
2. #include <gl/Gl.h>
3. #include <gl/glut.h>
4. //<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
5. void myInit(void)
6. {
7. glClearColor(1.0,1.0,1.0,0.0); // set white background color
8. glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
9. glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
10. glMatrixMode(GL_PROJECTION);
11. glLoadIdentity();
12. gluOrtho2D(0.0, 640.0, 0.0, 480.0);
13. }
14. //<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
15. void myDisplay(void)
16. {
17. glClear(GL_COLOR_BUFFER_BIT); // clear the screen
18. glBegin(GL_POINTS);
3
Complete OpenGL Program (Contd )
4
Making Line Drawings
5
Drawing Lines
6
Drawing Lines
This code might be encapsulated for
convenience in the routine drawLineInt():
1. void drawLineInt(GLint x1, GLint y1, GLint x2,
GLint y2)
2. {
3. glBegin(GL_LINES);
4. glVertex2i(x1, y1);
5. glVertex2i(x2, y2);
6. glEnd();
7. }
7
Drawing Lines
If more than two vertices are specified
between glBegin(GL_LINES) and glEnd()
they are taken in pairs and a separate line
is drawn between each pair.
8
Drawing Lines
1. glBegin(GL_LINES);
2. glVertex2i(10, 20); // first horizontal line
3. glVertex2i(40, 20)
4. glVertex2i(20, 10); // first vertical line
5. glVertex2i(20, 40);
6. glEnd();
7. glFlush();
9
Drawing Lines
10
Drawing Polylines and Polygons
A polyline is a collection of line segments joined end to end.
It is described by an ordered list of points, as in:
p0=(x0,y0),p1=(x1,y1)…..pn=(xn,yn)
In OpenGL a polyline is called a “line strip”, and is drawn by
specifying the vertices in turn between
glBegin(GL_LINE_STRIP) and glEnd().
For example, the code:
1. glBegin(GL_LINE_STRIP); // draw an open polyline
2. glVertex2i(20,10);
3. glVertex2i(50,10);
4. glVertex2i(20,80);
5. glVertex2i(50,80);
6. glEnd();
7. glFlush();
11
Drawing Polylines and Polygons (Contd )
12
Drawing Polylines and Polygons (Contd)
13
Line Drawing using moveto and lineto()
14
Line Drawing using moveto and lineto()
15
Line Drawing using moveto and lineto()
16
Line Drawing using moveto and lineto()
17
Drawing Aligned Rectangles
18
Drawing Aligned Rectangles (Contd)
1. void drawFlurry(int num, int numColors, int Width, int Height)
2. // draw num random rectangles in a Width by Height rectangle
3. {
4. srand(time(0));
5. for (int i = 0; i < num; i++)
6. {
7. GLint x1 = rand() % Width; // place corner randomly
8. GLint y1 = rand() % Height;
9. GLint x2 = rand()%Width ; // pick the size so it fits
10. GLint y2 = rand() %Height;
11. GLfloat lev = ((rand() % numColors))/numColors; // random value, in range 0 to 1
12. glColor3f(lev,lev,lev); // set the gray level
13. glRecti(x1, y1, x2, y2); // draw the rectangle
14. }
15. glFlush();
16. }
19
Drawing Aligned Rectangles (Contd)
20
Aspect Ratio of an Aligned Rectangle
21
Aspect Ratio of an Aligned Rectangle (Contd)
22
Filling Polygon
23
Filling Polygon ( Contd )
24
Filling Polygon ( Contd )
To draw a convex polygon based on vertices
(x0,y0),(x1,y1)…….(xn,yn) use the usual list of
vertices but place them between
glBegin(GL_POLYGON) and glEnd() :
glBegin(GL_POLYGON)
glVertex2f(x0,y0);
glVertex2f(x1,y1);
………..
glVertex2f(xn,yn);
glEnd();
25
Graphics Primitives in OpenGL
GL_TRIANGLES: takes the listed vertices three at a time, and
draws a separate triangle for each;
GL_QUADS: takes the vertices four at a time and draws a
separate quadrilateral for each;
GL_TRIANGLE_STRIP: draws a series of triangles based on
triplets of vertices: v0, v1, v2, then v2, v1, v3, then v2, v3, v4, etc.
(in an order so that all triangles are “traversed” in the same
way ;e.g. counterclockwise).
GL_TRIANGLE_FAN: draws a series of connected triangles
based on triplets of vertices: v0, v1, v2, then v0, v2, v3, then v0,
v3, v4, etc.
GL_QUAD_STRIP: draws a series of quadrilaterals based on
foursomes of vertices: first v0, v1, v3, v2, then v2, v3, v5, v4, then
v4, v5, v7, v6 (in an order so that all quadrilaterals are “traversed”
in the same way; e.g. counterclockwise).
26
Graphics Primitives in OpenGL(Contd)
27
Simple Interaction with the Mouse and
KeyBoard
28
Simple Interaction with the Mouse and
KeyBoard (Contd)
29
Mouse Interaction
30
Mouse Interaction
31
Example : Specifying a Rectangle
with Mouse
1. void myMouse(int button, int state, int x, int y)
2. {
3. static GLintPoint corner[2];
4. static int numCorners = 0; // initial value is 0
5. if(button == GLUT_LEFT_BUTTON && state ==
GLUT_DOWN)
6. {
7. corner[numCorners].x = x;
8. corner[numCorners].y = screenHeight - y; // flip y
coordinate
9. numCorners++; // have another point
10.if(numCorners == 2)
32
Example : Specifying a Rectangle
with Mouse
1. {
2. glRecti(corner[0].x, corner[0].y, corner[1].x,
corner[1].y);
3. numCorners = 0; // back to 0 corners
4. }
5. }
6. else if(button == GLUT_RIGHT_BUTTON && state
== GLUT_DOWN)
7. glClear(GL_COLOR_BUFFER_BIT); // clear the
window
8. glFlush();
9. }
33
Example : “Freehand” drawing with
a fat brush
1. void myBrush(int mouseX, int mouseY)
2. {
3. GLint x = mouseX; //grab the mouse position
4. GLint y = screenHeight – mouseY; // flip it
as //usual
5. GLint brushSize = 20;
6. glRecti(x,y, x + brushSize, y + brushSize);
7. glFlush();
8. }
34
The End
35