0% found this document useful (0 votes)
24 views7 pages

Lab Practice III

OpenGL Lab Contents

Uploaded by

Gezahegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Lab Practice III

OpenGL Lab Contents

Uploaded by

Gezahegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CS-YIV OpenGL Laboratory Contents October, 2024

Computer Graphics [CoSc4061]


Lab Practice III
Drawing Triangles, Quads and
Polygons using OpenGL

1. Attributes in OpenGL
2. Triangles in OpenGL
3. Draw Triangles using OpenGL
4. Draw Triangle Fan using OpenGL
5. Quads in OpenGL
6. Draw a Square using OpenGL
7. Polygons in OpenGL
8. Draw an Ark Using OpenGL

1
CS-YIV OpenGL Laboratory Contents October, 2024

1. Attributes in OpenGL
Attributes: are parts of the OpenGL and determine the appearance of objects.
 Color deal with all objects (points, lines, triangles, quads, polygons).
 Each color component stored separately in the frame buffer.
 Usually 8 bits per component in buffer.
 Note: in glColor3f the color values range from 0.0 to 1.0, while in
glColor3ub the values range from 0 to 255.
 Size and width (points, lines).
 Stipple pattern (lines, polygons).
Polygon mode:
 Display as filled: (solid color) use the:
 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 Display edges use the functions:
 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
2. Triangles in OpenGL
 There are 3 functions with Triangle: {S | _STRIP | _FAN}.
 Triangle (GL_TRIANGLES):
 To draw normal Triangle. & Represent triangle as 3 vertices.
 Triangle Strip : (GL_TRIANGLE_STRIP):
 Group of triangles sharing 2 vertices from previous triangle.
 Use triangles to represent a solid object as a mesh.
 Triangles frequently appear in strips:

 Triangular Fans: (GL_TRIANGLE_FAN):


 Connected group sharing 1 common vertex, and 1 from previous
triangle.

2
CS-YIV OpenGL Laboratory Contents October, 2024

 For tri-strips and fans,


 A new triangle is defined by 1 new vertex added to the strip or fans.

3. Draw Triangles using OpenGL


#include<GL/glut.h>
void Triangle() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(14);
glBegin(GL_TRIANGLES);
//First Triangle
glColor3f(1, 0, 0);
glVertex2f(100, 100);
glColor3f(0, 1, 0);
glVertex2f(500, 500);
glColor3f(0, 0, 1);
glVertex2f(50, 700);
//Second Triangle
glVertex2f(400, 50);
glVertex2f(550, 50);
glVertex2f(400, 300);
glEnd();
glFlush();
}
void Initial() {
glClearColor(1, 1, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 600, 0, 800);
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(450, 400);
glutInitWindowPosition(0, 0);

3
CS-YIV OpenGL Laboratory Contents October, 2024

glutCreateWindow("Draw and Color Two Triangles");


Initial();
glutDisplayFunc(Triangle);
glutMainLoop();
return 0;
}

4. Draw Triangle Fan using OpenGL


#include<GL/glut.h>
void Triangle_Fan()
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(16);
glBegin(GL_TRIANGLE_FAN);
//Triangle Fan
glColor3f(1, 0, 0);
glVertex2f(300, 400);
glColor3f(1, 0.5, 0.5);
glVertex2f(550, 400);
glVertex2f(500, 600);
glColor3f(0.5, 0.3, 0.5);
glVertex2f(400, 700);
glColor3f(1, 0.5, 0.5);
glVertex2f(300, 700);
glColor3f(0.5, 0.3, 0.5);
glVertex2f(200, 700);
glColor3f(1, 0.5, 0.5);
glVertex2f(100, 600);
glColor3f(0.5, 0.3, 0.5);
glVertex2f(50, 400);
glColor3f(1, 0.5, 0.5);
glVertex2f(100, 200);
glEnd();
glFlush();}

4
CS-YIV OpenGL Laboratory Contents October, 2024

void Initializer() {
glClearColor(1.0, 0.9, 0.9, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 600, 0, 800);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(0, 0);
glutCreateWindow("Draw Triangle Fan");
Initializer();
glutDisplayFunc(Triangle_Fan);
glutMainLoop();
return 0; }
5. Quads in OpenGL
 There are 2 functions with Quad: {QUADS |QUAD_STRIP}.
 Quad :(GL_QUADS):
 To draw normal Quad & Represent as 4 vertices.
 You can draw more than one quad by determine 4 vertex of each
Quad.
 Quad Strip : (GL_QUAD_STRIP):
 Group of Quads sharing 2 vertices from previous Quad.
6. Draw a Square using OpenGL
#include<GL/glut.h>
void Quad() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(20);
glBegin(GL_QUADS);
glColor3f(0.8, 0.6, 0.6);
glVertex2f(200, 200);

5
CS-YIV OpenGL Laboratory Contents October, 2024

glVertex2f(500, 200);
glColor3f(1, 0, 0);
glVertex2f(500, 500);
glVertex2f(200, 500);
glEnd();
glFlush();}
void Initial() {
glClearColor(1, 1, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 600, 0, 800);}
int main()
{ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(0, 0);
glutCreateWindow("Draw a Quad");
Initial();
glutDisplayFunc(Quad);
glutMainLoop();
return 0;
}
7. Polygons in OpenGL
 A Polygon: is a 2D shape that is made up of a number of vertices.
 An ordered set of vertices defines a polygon.
 You can also set other properties of the polygon, such as its color.
 Quadrilateral & Triangle is a special case of polygon.
 Use GL_POLYGON argument to draw polygon by determine its vertexes.

6
CS-YIV OpenGL Laboratory Contents October, 2024

Exercise 1: Draw a Square that satisfies the following specifications:


 Line Width = 10.
 Vertex1 = (200, 200), Vertex2 = (200, 600),
Vertex3 = (600, 600), Vertex4 = (200, 600).
 Color of the Square = Orange.
 Background Color = White.
 Window Title Bar = “My Orange Square”.
Exercise 2: Draw the following ark using OpenGL

You might also like