Skip to content

non-npc/Python_Pygame_PyOpenGL

Repository files navigation

3D Examples using Pygame and PyOpenGL

This project demonstrates a couple examples of 3D with rainbow-colored borders using Pygame Community Edition and OpenGL.

Current examples:

  1. cube.py - a rotating cube
  2. torus.py - a rotating torus
  3. pyramid.py - a rotating pyramid
  4. dragon.py - a custom shape model loaded from an stl file

This is a personal project to test the functionality of Pygame and PyOpenGL.

cube Screenshot torus Screenshot 2 pyramid Screenshot 3 dragon Screenshot 3

Features

  • 3D rotating shape rendered with OpenGL
  • Rainbow-colored rectangles at the top and bottom of the window
  • Smooth color transitions in the rainbow borders

Requirements

  • Python 3.x
  • Pygame Community Edition
  • PyOpenGL

Installation

  1. Clone this repository or download the source code.

  2. Install the required dependencies:

    pip install -r requirements.txt
    

Usage

Run the scripts using Python:

python cube.py
or
python torus.py
or
python pyramid.py
or
python dragon.py

Controls

  • Close the window to exit the program

How it works

The program uses Pygame Community Edition to create a window and handle events, and OpenGL for 3D rendering. The main components are:

  1. A 3D shape that rotates continuously
  2. Two rainbow-colored rectangles at the top and bottom of the window
  3. Color transitions that create a moving rainbow effect

The shape is defined by its vertices and edges, and drawn using OpenGL lines. The rainbow effect is achieved by calculating HSV colors and converting them to RGB.

Customization

You can modify various aspects of any of the programs:

  • Adjust the window size by changing the display tuple in the main() function
  • Modify the shapes's rotation speed by changing the values in the glRotatef() call
  • Adjust the rainbow effect speed by modifying the hue increment value

Credits

the free STL model asset file (cutedragon2.stl) is from https://cults3d.com/en/3d-model/art/cute-dragon-jkcreate

LICENSE

CC0 1.0 Universal

Releases

No releases published

Packages

No packages published

Languages