Drawing points and lines in OpenGL

OpenGL is cross-language and cross-platform application programming interface(API) for rendering 2D & 3D graphics. Drawing something is easy once you understand how OpenGL works. Points and lines are the primitives for any drawing. This article will make you capable of drawing points and lines using OpenGL implemented in C(i.e. gcc).

You need to install OpenGL libraries and header files in order to set up the environment for the OpenGL program. If you haven’t installed already, visit my another article on ‘setting up OpenGL for gcc’ (coming soon).

In prior to drawing something, GL library to be initialized, window to be created, and prepared for drawing, this includes few steps and is usually implemented using predefined functions from GL libraries. We will be using the GLUT library here. The required steps are

1. Include header files

Include glut.h and other required header files

#include <GL/glut.h>
#include<stdio.h>

2. Initialize GLUT Library

glutInit(&argc,argv);

Initialize GLUT library using void glutInit(int *argcp, char **argv); function, where argc is the pointer to the unmodified argc variable from main and argv is the unmodified argv variable from main.

3. Setting up initial display mode and create a window

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (300, 300);//int width, int height
glutInitWindowPosition (1000, 100);//int x, int y
glutCreateWindow ("points and lines");//title window as parameter

setup initial display mode using void glutInitDisplayMode(unsigned int mode); function that requires display modes as parameters, usually the bitwise OR-ing of GLUT display mode bitmasks. Values are GLUT_RGBA, GLUT_RGB, GLUT_INDEX, GLUT_SINGLE, GLUT_DOUBLE, GLUT_ACCUM, GLUT_ALPHA, GLUT_DEPTH, GLUT_STENCIL, GLUT_MULTISAMPLE, GLUT_STEREO, GLUT_LUMINANCE. details of these display modes are discussed here.

void glutInitWindowSize(int width, int height); is used to set the initial window size. It requires two parameters, width and height of the window respectively.

void glutInitWindowPosition(int x, int y); is used to set the initial window position. It also requires two parameters as mentioned above, x and y that signify the coordinate of the screen where the initial window to be placed.

int glutCreateWindow(char *name); is used to create a top-level window. It requires a parameter of ASCII character string that signifies the window name.

4. Setting up projection

glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 300.0, 0.0, 300.0);//coordinate for the clipping planes

void glMatrixMode( GLenum mode); is used to set current matrix mode, requires a parameter that denotes matrix mode. The accepted values are GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE. Intially GL_MODELVIEW is set as matrix mode.

void gluOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); is used to define a 2D orthographic projection matrix, it requires four parameters as mentioned above. left and right denotes the coordinate for the left and right vertical clipping plane, bottom and top denotes the coordinate for the bottom and top horizontal clipping plane.

5. Set up display callback function

glutDisplayFunc(display);//display is a function

void glutDisplayFunc(void (*func)(void)); is used to set a display callback function that requires a parameter, denotes callback function. This callback function draws on the window whatever is to be drawn.

6. display and loop callback function

glutMainLoop();

void glutMainLoop(void); is used to enter the GLUT event processing loop. It will keep calling the registered callbacks for an infinite time. This method should be called at most once in a program.

Drawing Points

Inside display callback, draw GL_POINTS using glVertex2i(int x, int y); where x and y signify the coordinate in which the point to be drawn.

	glBegin(GL_POINTS);
	glVertex2i(100,100);//coordinate as parameter
	glEnd();

using multiple glVertex2i(int x, int y); multiple points can be drawn as used in the following code snippet.

	glBegin(GL_POINTS);
	glVertex2i(100,100);
	for(int x=0;x<100;x++){
		glVertex2i(x,200);//poinnt from (0,200) to (100,200)
	}
	glEnd();

Full Source Code For drawing a point

#include <GL/glut.h>
#include<stdio.h>

void init2D(float r, float g, float b)
{
	glClearColor(r,g,b,0.0);  
	glMatrixMode (GL_PROJECTION);//matrix mode
	gluOrtho2D (0.0, 300.0, 0.0, 300.0);//2D orthogonal projection
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 1.0);

	//draw a points in 100,100
	glBegin(GL_POINTS);
	glVertex2i(100,100);//drawing point at (100,100)
	glEnd();
	glFlush();//flushing
}

void main(int argc,char *argv[])
{
	glutInit(&argc,argv);//initialize GLUT
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);//display mode
	glutInitWindowSize (300, 300);//window size
	glutInitWindowPosition (1000, 100);//window position
	glutCreateWindow ("points and lines");//creating window
	init2D(0.0,0.0,0.0);//Projections
	glutDisplayFunc(display);//register display callback
	glutMainLoop();//routine
}

Full source code for drawing multiple points using loop

#include <GL/glut.h>
#include<stdio.h>

void init2D(float r, float g, float b)
{
	glClearColor(r,g,b,0.0);  
	glMatrixMode (GL_PROJECTION);//matrix mode
	gluOrtho2D (0.0, 300.0, 0.0, 300.0);//2D orthogonal projection
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 1.0);

	//drawing multiple points
	glBegin(GL_POINTS);
	for(int x=0;x<300;x+=5){
		glVertex2i(x,210);
	}
	glEnd();
	glFlush();//flushing
}

void main(int argc,char *argv[])
{
	glutInit(&argc,argv);//initialize GLUT
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);//display mode
	glutInitWindowSize (300, 300);//window size
	glutInitWindowPosition (1000, 100);//window position
	glutCreateWindow ("points and lines");//creating window
	init2D(0.0,0.0,0.0);//Projections
	glutDisplayFunc(display);//register display callback
	glutMainLoop();//routine
}

Compile these code with the following command from terminal. you must use glut, GL, GLU libraries.

gcc filename.c -o output_filename.o -lglut -lGL -lGLU

Run with ./output_filemane.o from terminal.

output windows

drawing single point at (100,100) using glut
drawing multiple points using glut

Drawing lines

Inside display callback, draw GL_LINES by calling glVertex2i(int x, int y); twice, the first time is for starting vertex and second for ending vertex. Here x and y signify the coordinate of the specific vertex.

glBegin(GL_LINES);
		glVertex2i(120,30);//starting vertex
		glVertex2i(450,180);//ending vertex
glEnd();

Multiple lines can be drawn by using multiple pair of vertices.

Full Source Code For drawing a single line

#include <GL/glut.h>
#include<stdio.h>

void init2D(float r, float g, float b)
{
	glClearColor(r,g,b,0.0);  
	glMatrixMode (GL_PROJECTION);//matrix mode
	gluOrtho2D (0.0, 500.0, 0.0, 200.0);//2D orthogonal projection
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 1.0);

	glBegin(GL_LINES);
		glVertex2i(120,30);//starting vertex
		glVertex2i(450,180);//ending vertex
	glEnd();

	glFlush();//flushing
}

void main(int argc,char *argv[])
{
	glutInit(&argc,argv);//initialize GLUT
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);//display mode
	glutInitWindowSize (500, 200);//window size
	glutInitWindowPosition (1000, 100);//window position
	glutCreateWindow ("points and lines");//creating window
	init2D(0.0,0.0,0.0);//Projections
	glutDisplayFunc(display);//register display callback
	glutMainLoop();//routine
}

Output

drawing single line using glut

Full Source Code For drawing multiple lines

#include <GL/glut.h>
#include<stdio.h>

void init2D(float r, float g, float b)
{
	glClearColor(r,g,b,0.0);  
	glMatrixMode (GL_PROJECTION);//matrix mode
	gluOrtho2D (0.0, 500.0, 0.0, 200.0);//2D orthogonal projection
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 1.0);
	glLineWidth(10);//line width
	//drawing multiple lines
	glBegin(GL_LINES);
		glVertex2i(170,180);
		glVertex2i(215,50);
		
		glVertex2i(215,50);
		glVertex2i(250,100);
		
		glVertex2i(250,100);
		glVertex2i(285,50);
		
		glVertex2i(285,50);
		glVertex2i(320,150);

		glVertex2i(190,80);
		glVertex2i(250,150);
		
		glVertex2i(250,150);
		glVertex2i(335,50);
	glEnd();

	glFlush();//flushing
}

void main(int argc,char *argv[])
{
	glutInit(&argc,argv);//initialize GLUT
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);//display mode
	glutInitWindowSize (500, 200);//window size
	glutInitWindowPosition (1000, 100);//window position
	glutCreateWindow ("points and lines");//creating window
	init2D(0.0,0.0,0.0);//Projections
	glutDisplayFunc(display);//register display callback
	glutMainLoop();//routine
}

	
	

output

drawing multiple lines with glut
Share on Social Media
Mukuldeep Maiti

Mukuldeep Maiti

An unrecognized crazy being with random thoughts lost in unknown

You may also like...

Leave a Reply