Python Opengl Programming Creating Window – In this Python Opengl Programming article iam going to have a simple introduction to Python Opengl, and also we are going to create our first Window in Opengl using Python Programming Language. so before starting our main topic let me give a simple introduction to Opengl.
Check PyQt5 GUI Development Articles
What is Opengl ?
Opengl is an application programming interface also we call call it API which is merely software library for accessing features in graphic hardware and also opengl is designed as streamlined, hardware independent interface that can be implemented on many different types of graphic hardware systems. So in this article we are not going to use modern opengl programming but in the further articles we will discuss about that.
Because we are using Python Programming language first of all you need to install pyopengl library for your python. you can simply install by writing pip install pyopengl in your terminal and after that hit enter it will be installed in your machine. also for working with Opengl in Python you need to download glut.dll and glut32.dll and add that to your working directory.
You can download From dlldownloader.com
So this is the complete code for Python Opengl Programming Creating Window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from OpenGL.GL import * from OpenGL.GLUT import * width, height = 500, 400 def draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glutSwapBuffers() glutInit() glutInitDisplayMode(GLUT_RGBA ) glutInitWindowSize(width, height) glutInitWindowPosition(200, 200) window = glutCreateWindow("Opengl Window In Python") glutDisplayFunc(draw) glutIdleFunc(draw) glutMainLoop() |
So at the top first we have imported the classes and also we set the width and height for our Opengl Window
1 2 3 4 |
from OpenGL.GL import * from OpenGL.GLUT import * width, height = 500, 400 |
After that you need to call draw() method because you need to call this all time , and in that method first we clear the screen by using glClear() and the second is reset the position by glLoadIdentity(). glSwapBuffers() is for double buffering.
1 2 3 4 5 6 7 |
def draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glutSwapBuffers() |
And these are the initialization
1 2 3 4 5 6 7 8 |
glutInit() glutInitDisplayMode(GLUT_RGBA ) glutInitWindowSize(width, height) glutInitWindowPosition(200, 200) window = glutCreateWindow("Opengl Window In Python") glutDisplayFunc(draw) glutIdleFunc(draw) glutMainLoop() |
So this is the type of Window in Opengl
1 |
glutInitDisplayMode(GLUT_RGBA ) |
These are the height and width also the x and y position of our window
1 2 |
glutInitWindowSize(width, height) glutInitWindowPosition(200, 200) |
Creating window object with the title
1 |
window = glutCreateWindow("Opengl Window In Python") |
Set draw function call back
1 |
glutDisplayFunc(draw) |
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email
I’ve been waiting for a PyOpenGL tutorial. I followed the instructions, but I get an error with glut. I checked the directories and everything is there. Help, please?
can you tell me what is the error and also did you copied the two dlls of glut.dll and glut32.dll in your project directory
Where in the project directory do they go? I put them in the top level. The files inside the project directory are .idea, Include, Lib, and Scripts. In pycharm, I don’t know how to put them in the dll folder for the project.
The errors read:
line 14, in glutInit()
ine 333, in glutInit_base_glutInit(ctypes.byref(count), holder)
line 407, in __call__ self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
yea you need to just copy those dlls and paste that in pycharm where you have your opengl working file
I put them in the DLL folder for the project folder in External Libraries and still get the same errors.
dont add in the external folder just add directly you can watch a video in my youtube channel, i have a video on this that how i have added
your video link
C:\Users\Baruch\PycharmProjects\PyOpenGL\Scripts\python.exe C:/Users/Baruch/PycharmProjects/PyOpenGL/MainWindow.py
Traceback (most recent call last):
File “C:/Users/Baruch/PycharmProjects/PyOpenGL/MainWindow.py”, line 14, in
glutInit()
File “C:\Users\Baruch\PycharmProjects\PyOpenGL\lib\site-packages\OpenGL\GLUT\special.py”, line 333, in glutInit
_base_glutInit(ctypes.byref(count), holder)
File “C:\Users\Baruch\PycharmProjects\PyOpenGL\lib\site-packages\OpenGL\platform\baseplatform.py”, line 407, in __call__
self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
I put them where the video directed me to and I still get the same errors. I’ll see what I can do. Thanks for the help.