yzhang37 / ShaderProgram

Pyopengl rendering program with beautiful scenes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lighting Shader Program with PyOpenGL

Introduction

To begin, I utilized five different surface shapes such as spheres, ellipsoids, rectangles, cones (cylinders), and torus (donut shapes). I wrote my own code for creating the surface mesh and defining the vertex buffer and index buffer. I then implemented various rendering techniques, including basic rendering, diffuse reflection, specular reflection, ambient lighting, texture mapping, and normal mapping. I also distinguished between point lighting, directional lighting, and spotlight effects. Ultimately, I produced several stunning scenes.

The following are the four scenarios of this program:

Scene Introduction

Instructions for running the program:

  1. First, please install version 3.8 of Python or install Anaconda/Miniconda.

    • If you choose conda, please use conda create -n GRAPHICS after installation to create an environment named GRAPHICS. Then use conda activate GRAPHICS to enter that environment.
  2. Use pip install -r requirements.txt to install the required packages: PyOpenGL, WxPython, and Numpy.

  3. Run the program by executing the command pythonw Sketch.py

    On Mac, please use python.app.

If you encounter any problems during the run, please submit an Issue.

Demo video:

GLSLPreview.mp4

Usage

Keys Functions
Numeric keys (1 to 9) Toggle the light switches in the scene (if the corresponding number exists)
A Turn on/off the ambient rendering mode. Turning off Ambient mode will cause the image to appear washed out because all objects will be displayed in their original colors without any dimming ambient light.
S Turn on/off the specular rendering mode. Turning off Specular mode will remove the highlight effect from the surfaces of objects. Please rotate the object to observe the difference in the specular effect on different material surfaces.
D Turn on/off the diffuse rendering mode.
← / β†’ Switch scenes.

Scene design

  • Scene 1: This scene is a testing scene, primarily used to ensure that the VBO/EBO implementation of this program is functioning correctly. It includes an 🌏 earth (sphere shape), a ring ashtray (torus shape), and a half cone, all with metallic surface applied. There are also three point lights present in the scene, colored blue, red, and yellow, that are flying around.

    Scene1.mp4
  • Scene 2: The second scene is similar to the high school map in the game Phasmophobia. It features a wooden floor of a basketball court, on which there is a πŸ€basketball (sphere model), an 🏈 American football (ellipsoid model), several rings (randomly colored red or blue, torus model for the ring, cylinder model for the stick). In order to create a horror-like atmosphere, I intentionally made the environment dark and added several πŸ”¦ flashlights, to create a atmosphere of dimness.

    Scene2.mp4
  • Scene 3: The third scene is a beautifully set dining table with a plethora of donuts πŸ₯― (All come in Glazed flavor, which is my favorite flavor. You might gain 10lbs if you ate them all πŸ˜‚lol), several nicely decorated 🎁 Christmas gift boxes, chocolate cakes and elegant candles. The surface of the Christmas gift box has a reflective mirror-like effect (resembling tin foil). The donuts are textured using a special normal map that makes them look incredibly realistic! (The specific texture file can be found in the /assets folder). The only downside is that I didn't implement a shadow effect, so the candle light will shine through the cakes and it will give the impression of a very bright light beneath the cakes.

    Scene3.mp4
  • Scene 4: This is a finely crafted scene featuring a 🎱 pool table with a smooth, comfortable texture and an orderly pattern of stripes. A series of pool balls and a rack are placed on the table. Four flashlight beams illuminate the center of the table and two fluorescent lamps are used for lighting. In this scene, I used a normal map to achieve the feel of the fabric on the pool table. The fluorescent lamps use an infinite light and the flashlight beams use a spotlight effect.

    Scene4.mp4

Model Design

The following models are used in this program:

  1. Sphere/Ellipsoid Model

    Sphere surface parameter equation, where $r$ represents the radius of the sphere.

    sphere_eqn

    Normal equation:

    sphere_norm

Texture mapping position: Each point [j/stacks, i/slices]. Where j/stacks represents the iteration of $\theta$ and i/slices represents the iteration of $\phi$. See DisplayableSphere.py for specific definitions.

The parameters of the ellipsoid model are very similar to those of the sphere model, with the only difference being that $r$ is replaced with $a$, $b$, and $c$, which represent the three different radius of the ellipsoid. See DisplayableEllipsoid.py for specific definitions.

  1. Cube Model

    The cube model has a total of 8 points, and different faces have different normals, all of which need to be defined manually. Please refer to DisplayableCube.py for specific VBO/EBO definitions, as they will not be explained here.

  2. Cylinder/Cone Model

    Surface parameter equation:

    where $r_{\mathrm{lower}}$ represents the lower radius of the cone, $r_{\mathrm{upper}}$ represents the upper radius of the cone. If the two radii are the same, it represents a cylinder. $h$ is the height.

    Normal equation:

    • On the top surface is: $\vec{n}=[0, 0, 1]$

    • On the bottom surface is: $\vec{n}=[0, 0, -1]$

    • On the edges is:

      where, $\mathbf{cSin} = (r_{\mathrm{lower}} - r_{\mathrm{upper}}) / h$.

    See DisplayableCylinder.py for specific definitions.

  3. Torus Model

    Torus surface parameter equation:

    where a = (outer + inner) / 2, b = (outer - inner) / 2.

    Torus surface normal equation:

    After normalization it is:

    Texture mapping position: Each point [i/nsides, j / rings]. Where i/nsides represents the iteration of $\theta$ and j / rings represents the iteration of $\phi$. See DisplayableTorus.py for specific definitions.

Features Included

Requirements Done
Generate Triangle Meshes: Ellipsoid, Torus, and Cylinder with end caps βœ…
Implement EBO for defining your meshes βœ…
Generate normals for your meshes, and implement normal visualization βœ…
Illuminate your meshes with diffuse, specular, and ambient components βœ…
Support 3 different light types (point, infinite, spotlight) βœ…
Create 3 different scenes βœ…
Texture mapping βœ…
Normal mapping βœ…
Artist Rendering

About

Pyopengl rendering program with beautiful scenes


Languages

Language:Python 100.0%