In this Python Modern Opengl i want to show you GLFW Window creation. So when you are going to work with Modern Opengl, for Window creation you need to use another library that is called GLFW.
Because Opengl is a graphic library and we don’t have window, keyboard inputs, mouse inputs and some other low level functionality.
So you need some installation for this article
1 |
pip install pyopengl |
1 |
pip install glfw |
What is GLFW ?
GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan development on the desktop. It provides a simple API for creating windows, contexts and surfaces, receiving input and events.GLFW is written in C and supports Windows, macOS, the X Window System and the Wayland protocol.
You can check my previous articles on Python Modern Opengl Programming
1: Python Opengl Creating Window (Fixed Function Pipline)
2: Python Opengl Programming Creating Teapot
3: Python Modern Opengl Creating Triangle
So now this is the complete code
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 27 28 29 30 31 32 33 34 35 36 37 38 |
import glfw def main(): if not glfw.init(): return window = glfw.create_window(720, 600, "Opengl GLFW Window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) while not glfw.window_should_close(window): glfw.poll_events() glfw.swap_buffers(window) glfw.terminate() if __name__ == "__main__": main() |
In this line of code we have initialized the library
1 2 |
if not glfw.init(): return |
Now create a windowed mode window and its OpenGL context
1 2 3 4 5 |
window = glfw.create_window(720, 600, "Opengl GLFW Window", None, None) if not window: glfw.terminate() return |
Make the window’s context current
1 |
glfw.make_context_current(window) |
Loop until the user closes the window
1 2 3 4 5 6 |
while not glfw.window_should_close(window): glfw.poll_events() glfw.swap_buffers(window) glfw.terminate() |
In the loop code this is for swap front and back buffers
1 |
glfw.swap_buffers(window) |
This is for poll for and process the events
1 |
glfw.poll_events() |
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email