0% found this document useful (0 votes)
27 views

Computer Graphics Lab Manual 2

OpenGL provides basic primitives like points, lines, triangles, and polygons that are used to draw basic shapes and objects. These primitives include points, lines, line strips, line loops, triangles, triangle strips, triangle fans, quads, quad strips, and polygons. Sample OpenGL code is provided to demonstrate how to render these primitives to create shapes like individual points, connected line segments, closed polygons, and filled triangles. Modern OpenGL encourages using vertex buffers and shaders for more flexible and efficient rendering.

Uploaded by

tebabontu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Computer Graphics Lab Manual 2

OpenGL provides basic primitives like points, lines, triangles, and polygons that are used to draw basic shapes and objects. These primitives include points, lines, line strips, line loops, triangles, triangle strips, triangle fans, quads, quad strips, and polygons. Sample OpenGL code is provided to demonstrate how to render these primitives to create shapes like individual points, connected line segments, closed polygons, and filled triangles. Modern OpenGL encourages using vertex buffers and shaders for more flexible and efficient rendering.

Uploaded by

tebabontu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Graphics Lab manual 2

OpenGL provides several primitive types that you can use to draw basic shapes and objects.
These primitives are the fundamental building blocks for creating graphics in OpenGL. Here are
the most common OpenGL primitives:

Points (GL_POINTS):

Used to draw individual points on the screen.

Each point is specified by a single vertex.

Often used for rendering particle systems, stars, or other small point-based objects.

Lines (GL_LINES):

Used to draw a sequence of disconnected line segments.

Each pair of vertices defines a line segment.

Lines can be used to create basic shapes, such as triangles and rectangles, by connecting the
endpoints.

Line Strip (GL_LINE_STRIP):

Similar to GL_LINES, but the line segments are connected end-to-end.

Requires a minimum of two vertices to draw a line strip.

Useful for creating a continuous path or polyline.

Line Loop (GL_LINE_LOOP):

Similar to GL_LINE_STRIP, but it forms a closed loop by connecting the last vertex to the first
vertex.

Useful for drawing closed shapes like polygons.

Triangles (GL_TRIANGLES):
Used to draw a sequence of triangles.

Each set of three vertices defines a single triangle.

Triangles are the most common primitive for rendering complex 3D models.

Triangle Strip (GL_TRIANGLE_STRIP):

Similar to GL_TRIANGLES, but adjacent triangles share an edge.

Requires a minimum of three vertices to draw a triangle strip.

Often used for rendering surfaces and terrain.

Triangle Fan (GL_TRIANGLE_FAN):

Similar to GL_TRIANGLES, but all triangles share a common vertex, forming a fan-like shape.

Useful for drawing objects with a central point, like a fan or circular objects.

Quads (GL_QUADS):

Used to draw quadrilaterals (four-sided polygons).

Each set of four vertices defines a single quad.

Note that the use of GL_QUADS is deprecated in modern OpenGL.

Quad Strip (GL_QUAD_STRIP):

Similar to GL_QUADS, but adjacent quads share an edge.

Requires a minimum of four vertices to draw a quad strip.

Also deprecated in modern OpenGL.

Polygon (GL_POLYGON):

Used to draw filled polygons with more than three sides.

Defined by a sequence of vertices, and OpenGL automatically fills the interior.

GL_POLYGON is less common in modern OpenGL, as it has limitations.


These are the basic OpenGL primitives. To draw more complex objects, you often need to
combine and transform these primitives. Modern OpenGL also encourages the use of vertex
buffers and shaders for efficient rendering, allowing for greater flexibility and control over the
graphics pipeline.

Sample OpenGL code to display 4 points


#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0); // Set point size
glColor3f(0.0, 0.0, 0.0); // Black color (R, G, B)
glBegin(GL_POINTS);
glVertex2f(-0.5, 0.5); // v1 (top-left)
glVertex2f(0.5, 0.5); // v2 (top-right)
glVertex2f(-0.5, -0.5); // v3 (bottom-left)
glVertex2f(0.5, -0.5); // v4 (bottom-right)
glEnd();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set clear color to white
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("2D Coordinate System");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

Sample OpenGL code to display 2 Lines


To draw line, we must have at least 2 points.
Each line has starting and ending points.
In order to draw 𝑛 lines, we must have 2𝑛 lines.

#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0); // Set point size
glColor3f(0.0, 0.0, 0.0); // Black color (R, G, B)
glBegin(GL_LINES);
glVertex2f(-0.5, 0.5); // v1 (top-left)
glVertex2f(0.5, 0.5); // v2 (top-right)
glVertex2f(-0.5, -0.5); // v3 (bottom-left)
glVertex2f(0.5, -0.5); // v4 (bottom-right)
glEnd();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set clear color to white
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("2D Coordinate System");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Sample OpenGL code to display Line strip
In order to draw 𝑛 lines at least we must have 𝑛 + 1 points
GL_LINE_STRIP primitive draw line starting from the ending point of the previous line.
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0); // Set point size
glColor3f(0.0, 0.0, 0.0); // Black color (R, G, B)
glBegin(GL_LINE_STRIP);
glVertex2f(-0.5, 0.5); // v1 (top-left)
glVertex2f(0.5, 0.5); // v2 (top-right)
glVertex2f(-0.5, -0.5); // v3 (bottom-left)
glVertex2f(0.5, -0.5); // v4 (bottom-right)
glEnd();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set clear color to white
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("OpenGL line Drawing");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

OpenGL Code to display two lines crossing each other at 𝟎, 𝟎 and they for 2D cartesian plane.
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glPointSize(5);
glBegin(GL_LINES);
glVertex2f(-1,0.0);
glVertex2f(1,0.0);
glVertex2f(0.0,-1);
glVertex2f(0.0,1);
glEnd();
glFlush();
}

int main(int argc, char** argv){


glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutCreateWindow("OPenGL Points");
glutInitWindowPosition(956,544);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OpenGL code to create LINE_LOOP
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glPointSize(5);
glBegin(GL_LINE_LOOP);
glVertex2f(-1,0.0);
glVertex2f(1,0.0);
glVertex2f(0.0,-1);
glVertex2f(0.0,1);
glEnd();
glFlush();
}

int main(int argc, char** argv){


glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutCreateWindow("OPenGL Points");
glutInitWindowPosition(956,544);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OpenGL to draw triangles
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glPointSize(5);
glBegin(GL_TRIANGLES);
glVertex2f(-1,0.0);
glVertex2f(1,0.0);
glVertex2f(0.0,-1);
glVertex2f(0.0,1);
glEnd();
glFlush();
}

int main(int argc, char** argv){


glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutCreateWindow("OPenGL Points");
glutInitWindowPosition(956,544);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OpenGL to Draw Triangle strip
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glPointSize(5);
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(-1,0.0);
glVertex2f(1,0.0);
glVertex2f(0.0,-1);
glVertex2f(0.0,1);
glEnd();
glFlush();
}

int main(int argc, char** argv){


glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutCreateWindow("OPenGL Points");
glutInitWindowPosition(956,544);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OpenGL to draw Triangle loop
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glPointSize(5);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(-1,0.0);
glVertex2f(1,0.0);
glVertex2f(0.0,-1);
glVertex2f(0.0,1);
glEnd();
glFlush();
}

int main(int argc, char** argv){


glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutCreateWindow("OPenGL Points");
glutInitWindowPosition(956,544);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like