A framework for rendering 3D computer graphics with Python.
This 3D CG graphics framework was created using Python for the purposes of teaching Computer Graphics and Software Engineering through the object-oriented design and application of a CG rendering library. The framework is largely inspired by the one developed by Lee Stemkoski and Michael Pascale (see References) with a focus on cross-compatibility, object-oriented software design principles, and the use of pythonic conventions over the ones commonly used with OpenGL.
For more information on the course and the companion material that uses this framework, see the Software Engineering Lab website and the posts there.
( back to top )
( back to top )
The following instructions describe how to create a local copy of the framework and configure it to run in a virtual environment.
This project requires Python 3.8+ and it is recommended to use a virtual environment for installing all the dependencies. Either venv or Pipenv work fine.
- Clone the repo.
git clone https://github.com/rsonger/pycg-framework.git
- Create a virtual environment.
venvPipenvpython -m venv .venv
pipenv install
- Activate the virtual environment.
venvPipenv# Windows source .venv/Scripts/activate # MacOS source .venv/bin/activate
pipenv shell
- In venv, the packages need to be installed after activating the environment.
pip install -r requirements.txt
- On MacOS, a discrepency in the OpenGL package may result in an
ImportError
. You can test this by running the following command in the terminal:If you see the error, look at the Traceback and find the location of thepython -c "import OpenGL.GL"
site-packages\OpenGL\platform
directory inside your Python library. Open thectypesloader.py
file from that directory and find the line that has:Then change the line to:fullName = util.find_library( name )
fullName = f"/System/Library/Frameworks/{name}.framework/{name}"
( back to top )
The WindowApp
class in the graphics.core.app
module contains all the basic logic for setting up a rendering context inside a windowed application. It implements the typical lifecycle of an interactive rendering application as shown in the flowchart below. An application using the framework should extend the WindowApp
class and implement its startup
and update
methods as exemplified by the program in demo.py
.
Run the demo app with python demo.py
to see the scene depicted in the screenshot from above. The demo also makes use of CameraRig
from graphics.extras.camera_rig
which allows the user to move through the scene and look. The keys WASD move the camera forward, left, back, and right while Q and E move it down and up. The keys IJKL rotate the camera to look up, left, down, and right.
( back to top )
- Window context
- OpenGL interfaces
- Time-keeping for animations
- Matrices for geometric transformations
- Scene graph
- Texture Mapping
- Basic texture shader
- Procedurally generated textures
- Generating textures from text
- 2D Sprites
- Heads-Up Display
- Orthogonal projection
- TV textures as render targets
- Postprocessing
- Lighting
- Ambient, directional, and point sources
- Normal vectors for vertices
- Shader attributes and uniforms
- Rendering lights
- Light source helpers
- Bump mapping
- Bloom and glow effects
- Shadows
( back to top )
Distributed under the Academic Free License. See LICENSE.txt
for more information.
( back to top )
Rob Songer
Project Link: https://github.com/rsonger/pycg-framework
( back to top )
- Lee Stemkoski and Michael Pascale (2021). Developing Graphics Frameworks with Python and OpenGL. CRC Press, in print and online. DOI: 10.1201/9781003181378
( back to top )