Javatpoint Logo
Javatpoint Logo

Introduction to PyOpenGL in Python

In this tutorial, we will learn about the pyopengl library in Python and its usage. OpenGL is an open-source library which is supported by multiple platforms such as Windows, Linux, and MacOS. It is also supported by the multiple programming languages. We will implement this using the Python programming language.


OpenGL is a fairly simple library and easy to implement as compared to other graphics libraries. It is a cross-language API, so that it can be used in other programming language. In the OpenGL, we need to specify the objects within space, for example - to create cube, we need to specify the "corners". Corners are referred to as vertices (plural) or as a vertex (singular).


We can install it easily using the pip package manager. Run the following command in the terminal and install OpenGL in the system.

The above command will install the pyopengl in local system. We will get the following output.

Introduction to PyOpenGL in Python

You can download it manually by following these steps:

  1. Go to the specified link and scroll down to the "downloading and installation" heading.
  2. Download all the necessary files from that section.
  3. Navigate to the folder where you downloaded the files.
  4. Run the appropriate command in the terminal or command prompt to install or execute the files.

This command will install the files and setup the necessary environment for the program to run.

Please note that the command above assume that python and setup tools are installed on the system and that the file is in the same folder where the terminal or command prompt is open.

Now, let's move to the implementation through coding.


First step is to import the OpenGL in the code. To do so, run the following command.

Before proceeding, there are a few additional dependencies that must be imported in order to use this library within our program. These dependencies should be installed before attempting to use the library in our program. Below is the some libraries:

When we run the above code snippet, we get the "Import successfully" which means we have successfully installed the library.

Now, let's start by creating a window for displaying the graphics using the OpenGL library. The following code will create a window and the comments will explain how it works.

Example -

Explanation -

In the above code, we create a window with the title "OpenGL Graphics Window", the size of 500x500 pixels, positioned at the top left corner of the screen. The glutDisplayFunc() and glutIdleFunc() methods will cause the showScreen() function to be called continuously. This function is responsible for clearing the screen and displaying any graphics or shapes. The glutMainLoop() function is responsible for keeping the window open and updating its contents. Please note that the code above is using python-OpenGL bindings and that the libraries might be different if you are using another programming language.

Now, we will create the square using the OpenGL but we should familiar with the coordinates system that OpenGL follows -

The (0,0) point in the window represents the bottom left corner of the window. If we move upward from that point, we are traversing along the y-axis, and if we move to the right from that point, we are traversing along the x-axis. Therefore, the top left point of the window would be (0, 500), the top right point would be (500, 500), and the bottom right point would be (500, 0).

It is important to note that the y-axis increases as we move up and the x-axis increases as you move right, which is the opposite of the traditional Cartesian coordinate system.

Let's write the code for creating the square.

Example -

Explanation -

The above code create an OpenGL window with a title "OpenGL Coding Practice" of size 500x500 pixels, positioned at the top left of the screen. It will create a colored window and will call the showScreen() function continuously. Inside the showScreen() function, it will clear the screen, reset the graphic/shape's position, call the "square" function which will draw a square using the vertex coordinates specified, and swap the buffers. It will create a window with a static square in it and keep the window open and running in a loop.

However, our code is not complete. If the square is drawn and just after screen gets clear again. We won't able to see the square when it actually draws because it would appear and disappear in a split second. Let's write another function.

Example -

Now let's compile the all code snippet into the single code file so there are no ambiguities.

Example -


In the above code, we creates a window using the OpenGL.GLUT library and sets the display mode to GLUT_RGBA. We set the window size and position, creates a window with the title "OpenGL Coding Example", and sets the display and idle functions to showScreen.

The showScreen function clears the screen, loads the identity matrix, calls iterate function which sets the viewport, projection matrix and model view matrix. We set the color of the square to be drawn and then call the square function which will draw a square on the screen.

The glutMainLoop() function starts the main loop of the GLUT program and keeps the window open, displaying the square on the screen.


This tutorial provided an introduction to the use of OpenGL, a library for creating 3D graphics. The process of downloading and installing the library was covered, as well as an example program that demonstrated how to create a basic shape using OpenGL. The example program also provided an overview of some of the function calls that are used in the process of drawing shapes using this library. Overall, while using OpenGL can be challenging, it is a powerful tool for creating 3D graphics and offers a wide range of possibilities for experienced users.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA