Lab Practice III
Lab Practice III
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:
2
CS-YIV OpenGL Laboratory Contents October, 2024
3
CS-YIV OpenGL Laboratory Contents October, 2024
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