This repository offers a port into Python of some renderers from davudk/OpenGL-TileMap-Demos, originally written in C#.
As a result, it provides Python code to render a tilemap much faster than what would be possible with immediate rendering.
Vertex-buffered rendering and Geometry Shader Rendering in particular are implemented in src/renderer.py
Vertex-buffered rendering is implemented both using native OpenGL calls and using Pyglet's ShaderProgram class.
On a simple benchmark with a TileMap composed of 36 rows and 28 columns, the following results were obtained. Reported are the cumulative time, per call, in seconds:
| Renderer | draw() | recalculate() |
|---|---|---|
| Geometry Shader | 0.0002031 | 0.0001807 |
| Vertex-Buffered | 0.0001621 | 0.006053 |
| Vertex-Buffered (Pyglet) | 0.0004581 | 0.003455 |
| Instantaneous | 0.2491 | 0 |
For more detailed explainations on each renderer, please refer to the original repository davudk/OpenGL-TileMap-Demos.
To learn more about OpenGL and shaders, I found very helpful Learn OpenGL.
To learn more about Pyglet, please reference their documentation and their Github repository.
This directory contains the following files and directories:
- main.py: Main Python script used to run the app.
- src: Directory collecting all additional Python scripts and custom packages needed to run the app.
- Assets: Directory containing the image atlas used in this demo.
- Shaders: Directory containing the GLSL code for the different shader implementations.
- README.md: The Readme file you are currently reading.
The Python enviroment used for this project was kept as simple as possible.
An environment containing the required packages with compatible versions can be created as follows:
conda create -n tilemap_demo python=3.12.0
conda activate tilemap_demo
pip install pyglet==2.0.8 snakeviz==2.2.0To run the demo app, simply activate the correct conda environment and, from the same directory as the main.py file run:
python main.pyUse the whitespace key to toggle between the different types of renderers. Use any other key to randomly generate a new tilemap.
cProfile is used to generate a performance dump each time the application is run. This allows to compare the speed of the different rendering techniques. Note that the time needed by Pyglet to draw the FPS on screen may represent a sizeable portion of the rendering time and is therefore advised to remove the FPS counter when benchmarking your implementations.
To visualize the results in the profiler dump, run:
snakeviz "Profiler Results {...}.prof"