Practical GPU Graphics with wgpu-py and Python
About this Book
wgpu-py is the next-generation graphics API and future standard in Python for both native devices and the web, aiming to provide modern 3D graphics and computation capabilities using the GPU acceleration. This book provides all the tools you need to create advanced 3D graphics and GPU computing in Python using this new wgpu-py API.
First, this book will take you through the development environment for building wgpu-py applications in Python, and then introduce Python and wgpu-py basics, shader programs, GPU buffers, and rendering pipelines. Next, you will learn how to create primitives and simple objects in wgpu-py. As you progress through the chapters, you will get to grips with advanced wgpu-py topics, including 3D transformations, lighting calculations, colormaps, and textures. At the same time, you will learn how to create advanced 3D wgpu-py objects, including various 3D wireframes, 3D shapes, and simple and parametric 3D surfaces with colormaps and textures, as well as beautiful 2D and 3D fractal images described by complex functions. In addition, you will explore new wgpu features such as the compute shader and storage buffers, and use them to simulate large particle systems. In addition, this book will introduce the pygfx render engine that is based on wgpu-py, and show you how to use its built-in primitives to create various 3D objects. You will also learn how to build custom objects and geometries in pygfx.
By the end of this book, you will have the solid skills you need to build your own GPU-accelerated graphics and computing applications on both native devices and the web in Rust with the wgpu-py API.
The things you will learn from this book:
- Development environment and tools for creating wgpu-py apps in Python.
- Python and wgpu-py basics, WGSL shaders, and rendering pipeline.
- Primitives and simple shapes in wgpu.
- 3D transformations, model, viewing, projection, and various coordinate systems.
- GPU buffers, uniform buffer objects, animation, and camera controls.
- Normal vectors, lighting model, ambient, diffuse, and specular light calculations.
- UV coordinates, texture mapping.
- Color model, colormaps, and color interpolation.
- 3D shapes, wireframes, surfaces, and 3D charts.
- 2D and 3D fractal images created in the fragment shader.
- Compute shaders, storage buffers, and large particle system simulation.
- Introdution to the pygfx render engine.
- Create custom objects and geometries in pygfx.
Table of Contents
Contents v Introduction 1 Overview 1 What this Book Includes 3 Is this Book for You? 3 What Do You Need to Use this Book? 4 How this Book Is Organized 4 Using Code Examples 6 Thanks 6 Customer Support 6 1 Setting Up Development Tools 7 1.1 Hardware Requirements 7 1.2 Install Python 9 1.3 Install Visual Studio Code 10 1.4 Run a Python Application 11 1.5 Creating Windows 14 1.5.1 Creating a glfw Window 14 1.5.2 Creating a Qt Window 15 1.5.3 Creating a WX Window 16 1.6 Python Basics 17 1.6.1 Data Types 17 1.6.2 Functions 19 1.6.3 Control Flows 21 1.6.4 NumPy 24 1.6.5 Structs 27 1.6.6 Enums 28 1.6.7 User Inputs 29 2 wgpu-py Basics 31 2.1 First wgpu-py Example 31 2.1.1 WGSL Shaders 31 2.1.2 Python Code 32 2.1.3 Run App on Auto GUI 34 2.1.4 Run App on Qt Window 34 2.1.5 Run App on wx Window 36 2.2 wgpu-py API 38 2.2.1 wgpu-py Backend 38 2.2.2 Content 39 2.2.3 Load Shaders 40 2.2.4 Rendering Pipeline 40 2.2.5 Rendering Output 42 2.3 Shader Program 43 2.3.1 Why Use WGSL Shaders? 43 2.3.2 Writing Shader Code 44 2.4 Triangle with Different Vertex Colors 46 2.4.1 Shader Code 46 2.4.2 Python Code 47 3 Primitives in wgpu-py 49 3.1 Creating Points 49 3.1.1 Python Code 50 3.1.2 Shader Code 52 3.1.3 Run Application 52 3.2 Creating Lines 53 3.3 Creating Triangles 55 3.3.1 Python Code 55 3.3.2 Shader Code 55 3.3.3 Run Application 56 4 GPU Buffers 59 4.1 GPU Buffer 59 4.2 Creating a Colored Triangle 60 4.2.1 Python Code 60 4.2.2 Shader Code 64 4.2.3 Run Application 64 4.3 Creating a Colored Square 65 4.3.1 Python Code 65 4.3.2 Run Application 66 4.4 Creating a Square with an Index Buffer 66 4.4.1 Python Code 67 4.4.2 Run Application 70 5 3D Transformations 71 5.1 Basics of 3D Matrices and Transformations 71 5.1.1 Introducing glmatrix_py 71 5.1.2 3D Vector and Matrix Operations 72 5.1.3 Scaling 73 5.1.4 Translation 74 5.1.5 Rotation 75 5.1.6 Combining Transformations 76 5.2 Projections and Viewing 77 5.2.1 Transforming Coordinates 77 5.2.2 Viewing Transform 79 5.2.3 Perspective Projection 81 5.2.4 Orthographic Projection 85 5.3 Transformations in wgpu-py 87 6 3D Shapes and Camera 89 6.1 Uniform Buffers and Bind Groups 89 6.2 Creating a 3D Line 91 6.2.1 Common Code 91 6.2.2 Vertex Data 92 6.2.3 Python Code 93 6.2.4 Shader Program 96 6.2.5 Run Application 97 6.3 Creating a Cube with Distinct Face Colors 98 6.3.1 Vertex Data 99 6.3.2 Python Code 100 6.3.3 Shader Program 103 6.3.4 Run Application 103 6.4 Creating a Cube with Distinct Vertex Colors 104 6.4.1 Create Vertex Data 104 6.4.2 Python Code 105 6.4.3 Run Application 106 6.5 Rotating Objects 106 6.5.1 Python Code 107 6.5.2 Run Application 108 6.6 Camera Controls 108 6.6.1 Event Handlers 109 6.6.2 Creating a Camera 109 6.6.3 Python Code 111 6.6.4 Run Application 112 7 3D Wireframe Shapes 113 7.1 Cube Wireframe 113 7.1.1 Mesh Data 113 7.1.2 Python File 114 7.1.3 Run Application 116 7.2 Sphere Wireframe 117 7.2.1 Spherical Coordinate System 117 7.2.2 Mesh Data 119 7.2.3 Python Code 119 7.2.4 Run Application 120 7.3 Cylinder Wireframe 120 7.3.1 Cylindrical Coordinate System 121 7.3.2 Mesh Data 121 7.3.3 Python Code 124 7.3.4 Run Application 124 7.4 Cone Wireframe 125 7.4.1 Mesh Data 126 7.4.2 Python Code 127 7.4.3 Run Application 127 7.5 Torus Wireframe 128 7.5.1 Mesh Data 129 7.5.2 Python Code 130 7.5.3 Run Application 130 8 Lighting in wgpu-py 131 8.1 Light Components 131 8.2 Normal Vectors 132 8.2.1 Surface Normal of a Cube 133 8.2.2 Surface Normal of a Sphere 133 8.2.3 Surface Normal of a Cylinder 133 8.2.4 Surface Normal of a Polyhedral Surface 133 8.3 Lighting Calculation 134 8.3.1 Diffuse Light 134 8.3.2 Specular Light 135 8.4 Lighting in Shaders 136 8.4.1 Transform Normals 136 8.4.2 Shader with Lighting 137 8.5 Cube with Lighting 139 8.5.1 Normal Data 139 8.5.2 Python Code 139 8.5.3 Run Application 145 8.6 Sphere with Lighting 145 8.6.1 Position and Normal Data 146 8.6.2 Python Code 147 8.6.3 Run Application 147 8.7 Cylinder with Lighting 147 8.7.1 Position and Normal Data 148 8.7.2 Python Code 150 8.7.3 Run Application 151 8.8 Cone with Lighting 151 8.8.1 Position and Normal Data 152 8.8.2 Python Code 153 8.8.3 Run Application 154 8.9 Torus with Lighting 154 8.9.1 Position and Normal Data 155 8.9.2 Python Code 156 8.9.3 Run Application 156 9 Colormaps and 3D Surfaces 157 9.1 Color Models 157 9.2 Colormaps 158 9.2.1 Colormap Data 158 9.2.2 Color Interpolation 159 9.3 Shaders with Lighting and Colormap 160 9.4 Simple 3D Surfaces 162 9.4.1 Position, Normal, and Color Data 163 9.4.2 Sinc Surface 165 9.4.3 Peaks Surface 171 9.5 Parametric 3D Surfaces 172 9.5.1 Position and Normal Data 173 9.5.2 Klein Bottle 174 9.5.3 Wellenkugel Surface 176 9.5.4 Seashell Surface 177 9.5.5 Sievert-Enneper Surface 178 9.5.6 Breather Surface 181 10 Textures 183 10.1 Texture Coordinates 183 10.2 Texture Mapping in wgpu-py 184 10.3 Shaders with Texture 187 10.4 Simple 3D Shapes 189 10.4.1 Cube with Texture 189 10.4.2 Sphere with Texture 197 10.4.3 Cylinder with Texture 198 10.5 Simple 3D Surfaces 201 10.5.1 Sinc Surface with Texture 202 10.5.2 Peaks Surface with Texture 203 10.6 Parametric 3D Surfaces 203 10.6.1 Klein Bottle with Texture 204 10.6.2 Wellenkugel Surface with Texture 206 10.7 Multiple Textures 207 10.7.1 Texture Coordinates 207 10.7.2 Python Code 209 10.7.3 Run Application 210 11 3D Surface Charts 213 11.1 Wireframe as Texture 214 11.1.1 Square Textures 214 11.1.2 Texture Coordinates 215 11.2 Shaders for 3D Charts 215 11.3 Simple 3D Surface Charts 217 11.3.1 Sinc Surface Chart 217 11.3.2 Peaks Surface Chart 225 11.4 Parametric 3D Surface Charts 226 11.4.1 Sphere Surface Chart 226 11.4.2 Torus Surface Chart 227 11.4.3 Klein Bottle Surface Chart 229 11.4.4 Wellenkugel Surface Chart 229 12 Creating Multiple Objects 231 12.1 Creating Two Cubes 231 12.1.1 Python Code 231 12.1.2 Run Application 235 12.2 Creating Multiple Cubes with Instancing 236 12.2.1 Python Code 236 12.2.2 Shader Code 240 12.2.3 Run Application 241 12.3 Creating Different Objects 242 12.3.1 Python Code 242 12.3.2 Run Application 243 12.4 Creating Objects Using Multiple Pipelines 243 12.4.1 Python Code 244 12.4.2 Run Application 252 12.5 Creating 3D Charts with Multiple Pipelines 252 12.5.1 Create Wireframe Data 253 12.5.2 Modify Shader Program 253 12.5.3 Python Code 254 12.5.4 Run Application 260 12.6 Charts with Coordinate Axes 262 12.6.1 Shaders for Coordinate Axes 262 12.6.2 Vertex Data for Coordinate Axes 262 12.6.3 Python Code 263 12.6.4 Run Application 265 12.7 Creating 3D Charts with Multiple Render Passes 266 12.7.1 Python Code 266 12.7.2 Run Application 269 13 Compute Shaders and Particles 271 13.1 Compute Shader 271 13.1.1 Compute Space and Workgroups 272 13.1.2 Write and Read Buffer 273 13.2 2D Rotation in GPU 274 13.2.1 Python Code 274 13.2.2 Shader Code 277 13.2.3 Run Application 277 13.3 Compute Boids 278 13.3.1 Python Code 278 13.3.2 Shader Code 283 13.3.3 Run Application 285 13.4 Particles under Gravity 286 13.4.1 Python Code 287 13.4.2 Shader Code 293 13.4.3 Run Application 295 13.5 Particle Collision 296 13.5.1 Python Code 297 13.5.2 Shader Code 302 13.5.3 Run Application 304 14 Visualizing Complex Functions 307 14.1 Complex Functions in Shader 307 14.1.1 Math Operations 308 14.1.2 Commonly Used Functions 308 14.2 Color Functions 310 14.2.1 Color Conversion in Shader 311 14.2.2 Colormaps in Shader 312 14.3 Domain Coloring for Complex Functions 316 14.3.1 Python Code 316 14.3.2 Shader Code 319 14.3.3 Complex function with id = 0 321 14.3.4 Complex Function with id = 1 322 14.3.5 Complex Function with id = 2 323 14.3.6 Complex Function with id = 3 324 14.3.7 Complex Function with id = 4 324 14.3.8 Complex Function with id = 5 324 14.3.9 Complex Function with id = 6 325 14.3.10 Complex Function with id = 7 326 14.3.11 Complex Function with id = 8 327 14.3.12 Complex Function with id = 9 327 14.3.13 Complex Function with id = 10 328 14.4 Domain Coloring for Iterated Functions 328 14.4.1 Python Code 329 14.4.2 Shader Code 329 14.4.3 Iterated Function with id = 0 331 14.4.4 Iterated Function with id = 1 332 14.4.5 Iterated Function with id = 2 333 14.4.6 Iterated Function with id = 3 333 14.4.7 Iterated Function with id = 4 334 14.4.8 Iterated Function with id = 5 335 14.4.9 Iterated Function with id = 6 335 14.4.10 Iterated Function with id = 7 336 14.4.11 Iterated Function with id = 8 337 14.4.12 Iterated Function with id = 9 337 14.4.13 Iterated Function with id = 10 338 14.5 Fractal: Mandelbrot Set 339 14.5.1 Mandelbrot Set Formula 339 14.5.2 Python Code 339 14.5.3 Shader Code 342 14.5.4 Run Application 343 14.6 Fractal: Julia Set 344 14.6.1 Python Code 344 14.6.2 Shader Code 347 14.6.3 Run Application 348 14.7 3D Fractals 350 14.7.1 Mandelbulb 350 14.7.2 Mandelbrot in 3D Space 356 14.7.3 Mandelbox Sweeper 360 15 GPU Render Engine: pygfx Basics 365 15.1 Introduction to pygfx 365 15.1.1 Scene, Renderer, Camera 366 15.1.2 Object, Geometry, Material 366 15.2 Basic Shapes 367 15.2.1 Points 367 15.2.2 Lines 369 15.2.3 Cube 369 15.2.4 Animation and Camera Controls 371 15.2.5 Sphere 373 15.2.6 Cylinder 374 15.2.7 Cone 375 15.2.8 Polyhedrons 376 15.2.9 Torus Knots 377 15.2.10 Klein Bottle 378 15.3 Colormap and Texture 378 15.3.1 Colormap 378 15.3.2 Texture 381 15.3.3 Colormap with Wireframe 384 15.4 Multiple Objects 387 15.4.1 Creating Different Objects 387 15.4.2 Creating Multiple Objects with Instancing 388 15.5 Import 3D Models 390 16 Custom Objects and Geometries in pygfx 395 16.1 Custom Objects 395 16.1.1 Shader Code 395 16.1.2 Python Code 401 16.1.3 Run Application 403 16.2 Custom Geometries 404 16.2.1 Surface Geometry 404 16.2.2 Wireframes 406 16.2.3 Surfaces with Colormap 410 16.2.4 Surfaces with Colormap and Wireframe 412 16.2.5 Surfaces with Textures 414 Index 417
What This Book Includes
Is This Book for You
What Do You Need to Use This Book
How This Book is Organized
Using Code Examples
Welcome to Practical GPU Graphics with wgpu-py and Python. The wgpu-py API is a Python wrapper of Rust wgpu, and exposes a Pythonic API similar to WebGPU spec. The wgpu API is a Rust implementation of the WebGPU specifications which will be the next-generation graphics API for the web. The WebGPU API is being developed by the W3C GPU for the Web Community Group with engineers from Apple, Google, Microsoft, Mozilla, and others. It is a future web standard for graphics and compute, aiming to provide modern 3D graphics and computation capabilities with GPU acceleration on the web.
Like wgpu in Rust, the wgpu-py library brings the WebGPU API to Python. It is a cross-platform, safe, graphics API. Even though wgpu-py is based on the WebGPU standard, it can run not only on the web by converting Python scripts into web apps, but also natively on various backends, including Vulkan, Metal, DirectX12, DirectX11, and OpenGLES. This book will provide all the tools you need to help you create advanced 3D graphics and GPU computing on native devices using this new graphics API. I hope that this book will be useful for Python programmers, graphics creators, computer graphics programmers, game developers, and students of all skill levels who are interested in graphics development on the web and on native devices with modern graphics backends.
Unlike WebGL which is based on OpenGL, WebGPU and wgpu-py are not direct ports of any existing native APIs. They are based on concepts in the Vulkan, Metal, and Direct3D12 APIs and are intended to provide high performance on these modern native graphics APIs across mobile and desktop platforms.
In order to understand wgpu-py technology, we need to review a brief history of native graphics technologies. The first to come was OpenGL, originally developed in the early 1990s. It is a low-level high performance graphics technology, which WebGL is based on. Since its inception, many graphics applications based on OpenGL have been developed. Modern GPUs actually work very differently from how the original OpenGL did – but many of the core concepts of OpenGL remain the same.
As GPUs became more complex and powerful, the graphics driver ended up having to do a lot of extremely complex work. This made graphics drivers notoriously buggy, and in many cases slower, too, as they had to do all the work on the fly. To improve OpenGL’s performance, Khronos, the group behind OpenGL, proposed a new, completely redesigned modern graphics API called Vulkan, which was released in 2016. Vulkan is even more low-level, faster, and simpler, and is a much better match for modern hardware.
However, using Vulkan also meant that applications had to completely rewrite all their graphics code in order to support it. This kind of tectonic shift in technology takes years to play out, and as a result, there is still a lot of OpenGL out there.
While Vulkan was designed to be a standard API able to work on all systems, as has long been the case with standards, Apple also came up with Metal for iOS and MacOS, while Microsoft came up with DirectX12 for Windows and Xbox. Both are more or less the same idea as Vulkan: new, lower-level backends that throw out all the historical baggage and start with a clean slate design that much more closely matches how modern GPU hardware works.
With the graphics community moving on to this new generation of APIs, the question then became what to do with the web. WebGL is essentially OpenGL with many of the same pitfalls, while high-performance web game engines still stand to greatly benefit from the new generation of graphics APIs.
Unfortunately, unlike OpenGL, Vulkan has run into trouble achieving true cross-platform reach thanks to Apple. MacOS and iOS only support Metal and have no official support for Vulkan, although there are third-party libraries for it. Furthermore, Vulkan itself is still not very suitable for the web – it is just too low-level, even dealing with minutiae like GPU memory allocators so that AAA game engines can extract the maximum conceivable performance. This is overkill for web platforms, plus security is a much more significant concern in browsers.
So the solution was an all-new API design, high-level enough to be usable and secure in a browser, and able to be implemented on top of any one of Vulkan, Metal and DirectX12. This is WebGPU, which looks like the only truly cross-platform, modern, and low-level graphics API for web applications.
Based on the WebGPU standard, the wgpu-py API is a native WebGPU implementation in Python via Rust wgpu. It can run natively on cross-platform devices with any modern graphics backend such as Vulkan, Metal, or DirectX12. It can also run on the web by converting Python scripts into web applications.
Note that WebGPU and wgpu-py have not been finalized and are still in the early stages of development, so their API interfaces may change frequently before they are officially released. In addition, WebGPU and wgpu-py use a new shader language called WGSL (WebGPU Shading Language) instead of the traditional GLSL shader language used in OpenGL and WebGL applications.
Practical GPU Graphics with wgpu-py and Python provides everything you need to create advanced 3D graphics objects in your wgpu-py applications with GPU acceleration. In this book, you will learn how to create a variety of 3D graphics and charts that range from simple 3D shapes such as cubes, spheres, cylinders, to complex 3D surface graphics such as 3D wireframes, 3D surface charts, and complex particle systems created using compute shaders. I will try my best to introduce readers to the wgpu-py API, the next-generation Python GPU API for native graphics devices, in a simple way – simple enough to be easily followed by programmers who have little experience in developing advanced graphics applications. You can learn from this book how to create a full range of 3D graphics applications using the wgpu-py API and WGSL shader program.
Creating 3D graphics using wgpu-py requires a series effort and complex programming, and that is where the pygfx render engine comes in. Just like Three.js for WebGL, pygfx is based on the wgpu-py API and targets Vulkan, Metal, DirectX 12 backends. It handles things like scenes, cameras, lights, materials, textures, 3D transformations, etc., which you would have to implement yourself if you were to use wgpu-py directly. In this book, I will explain the basics about the pygfx render engine and demonstrate how to use its built-in primitives to create various 3D objects. In addition, I will also use examples to show you how to create custom objects and custom geometries in pygfx.
There are several bindings of the wgpu-native API in different programming languages, including Rust, C/C++, Python, C# .NET, Java, and Julia, to name a few. Here, I use the Python wrapper – wgpu-py, because of Python’s popularity. Python is an object-oriented, script language that gains much of its power from a large collection of libraries, including popular modules for machine learning and scientific computing. Python has been the number one programming language for the last five years in a row, ranked by IEEE Spectrum based on eleven metrics from eight sources including Google, Twitter, IEEE, Stack Overflow, Reddit, GitHub, CareerBuilder, and Hacker News (https://spectrum.ieee.org/top-programming-languages/).
What This Book Includes
This book and its sample code listings, which are available for download at my website at https://drxudotnet.com, provide you with
- A complete, in-depth instruction to practical 3D graphics programming in Rust with wgpu-py. After reading this book and running the example programs, you will be able to create various sophisticated 3D graphics and charts with GPU acceleration in your native applications.
- Over 50 ready-to-run example projects that allow you to explore the 3D graphics techniques described in this book. You can use these examples to get a better understanding of how 3D graphics and charts are created using the wgpu-py API and shader program. You can also modify the programs or add new features to them to form the basis of your own projects. Some of the example code listings provided with this book are already sophisticated chart and graphics projects, and can be directly used in your own real-world applications.
- Many functions and components in the sample code listings that you will find useful in your 3D graphics development. These functions and components include 3D transformation, projection, colormaps, lighting models created in fragment shaders, wgpu pipeline settings, WGSL shader code, and many other useful utility functions. You can extract these functions and components and plug them into your own applications.
Is This Book for You
Throughout the book, I will emphasize the usefulness of wgpu-py graphics programming to real-world applications. If you follow the instructions presented in this book closely, you will easily be able to develop various graphics and chart applications with GPU acceleration from simple 3D shapes to 3D surfaces with powerful colormap, wireframe, and texture mapping. You can also use the compute and fragment shaders to create complicated particle systems, domain coloring for complex functions, and fractal images. At the same time, I will not spend too much time discussing program style and code optimization in Python because there is a plethora of books out there already dealing with those topics. Most of the example programs you will find in this book omit error handlings, which makes the code easier to understand by focusing only on the key concepts and practical applications.
What Do You Need to Use This Book
You will need no special equipment to make the best use of this book and understand its algorithms. This book takes full advantage of open-source frameworks and libraries. The sample programs companying this book can run on various operating systems, including Windows, Linux, iOS, and MacOS. This book uses Visual Studio Code (VS Code) and Python 3 for its development environment and tools. VS Code is a lightweight IDE and powerful source code editor that runs on various operating systems. It has support for Python and WGSL with the help of relevant extensions.
Since the wgpu-py standard has not been finalized and is still in early development stage, its API may change frequently and is not backward compatible. This book uses the wgpu-py package version 0.8.1 for developing wgpu-py applications.
If you install other versions of the wgpu-py API, you may still be able to run most of the sample code with few modifications. Please remember, however, that this book is intended for that specific version of the wgpu-py API, on which all of the example programs were created and tested, so it is best to run the sample code in the same development environment and using the same version of the wgpu-py API.
In addition, your operating system needs to have a modern GPU as well as DirectX 12, Metal, or Vulkan backend support on your graphics card.
How This Book Is Organized
This book is organized into sixteen chapters, each of which covers a different topic about modern wgpu-py graphics programming. The following summaries of each chapter should give you an overview of the book’s content:
Chapter 1, Set Up Development Tools
This chapter explains how to set up the packages and tools required for wgpu-py application development. VS Code, Python 3 will be used as our development environment and tools. It also provides a brief introduction to Python programming.
Chapter 2, WGPU-PY Basics
This chapter provides a brief overview on the current wgpu-py technology, and then uses a simple triangle example to explain key aspects of the wgpu-py API, including wgpu-py context, the rendering pipeline, the shader program, and rendering graphics on different windows.
Chapter 3, Primitives in wgpu-py
This chapter demonstrates how to draw basic shapes in wgpu-py, including points, lines, and triangles. These basic shapes are referred to as primitives. There is no built-in support for curves or curved surfaces; they must be approximated by primitives. Currently, wgpu-py includes five primitives.
Chapter 4, GPU Buffers
This chapter introduces GPU buffers that hold vertex data and color information, and explains how to use GPU buffers to create colorful triangle and square with each vertex having a distinct color.
Chapter 5, 3D Transformations
This chapter explains how to perform basic 3D transformations, including translation, scaling, and rotation. It also describes how to construct various matrix representations used in 3D graphics, including model matrix, viewing matrix, and projection matrix. These matrices will be used to display 3D graphics objects on a 2D screen.
Chapter 6, 3D Shapes and Camera
This chapter shows how to use transformation, viewing, projection matrices, and the camera to create real-world 3D shapes – a 3D line and two cubes: one with distinct face colors and the other with distinct vertex colors. In doing so, you will learn two important concepts in wgpu-py: bind groups and uniform buffer objects.
Chapter 7, 3D Wireframe Shapes
A wireframe model is a visual representation of 3D objects used in computer graphics. It is created by drawing just the outlines of the polygons that make up the object. This chapter explains how to create wireframe models in wgpu-py for various 3D shapes, including cube, sphere, cylinder, cone, and torus. The key to create 3D wireframe shapes is to specify correct coordinates for their vertices.
Chapter 8, Lighting in wgpu-py
This chapter demonstrates how to build a simple lighting model in wgpu-py and how to use it to simulate light sources and the way that the light that they emit interacts with your objects on the scene. Here, I will discuss three light sources: ambient light, diffuse light, and specular light.
Chapter 9, Colormaps and 3D surfaces
This chapter explains how to use the color model and colormap to render the simple and parametric 3D surfaces by specifying various mathematical functions. Surfaces play an important role in various applications, including computer graphics, virtual reality, computer games, and 3D data visualization.
Chapter 10, Textures
This chapter discusses 2D image textures that can be applied to a surface to make the color of the surface vary from point to point, something like painting a copy of the image onto the surface. It shows how to map 2D textures onto various surfaces in wgpu-py.
Chapter 11, 3D Surface Charts
Surface charts are plots of 3D data. Rather than displaying the individual data points, surface charts show a functional relationship between a dependent variable y, and two independent variables x and z. This chapter explains how to create real-word 3D surface charts with colormaps, and how to add wireframe to the surface charts by mapping transparent square images onto the surface.
Chapter 12, Creating Multiple Objects
This chapter explains several approaches used to create multiple objects in a scene. One approach is to use uniform transformations or instancing to render the same object multiple times. Another approach is to combine the vertex data of different objects together and render them as a single object. The third approach is to use different pipelines or different render passes to render different objects.
Chapter 13, Compute Shaders and Particles
This chapter introduces the compute shader and describes how to use it in a simple 2D rotation example. It then applies the compute shader to particle systems – one system mimics the flocking behavior of birds, another simulates the effect of gravity on particles, and the third one models particle kinematics.
Chapter 14, Visualizing Complex Functions
This chapter illustrates how to generate domain coloring in wgpu for various complex functions. It also explains how to create fractal images for the Mandelbrot and Julia sets, as well as some 3D fractal shapes by writing the computation-intensive code directly in the fragment shaders.
Chapter 15, GPU Render Engine: pygfx Basics
The pygfx library is a wgpu-py based render engine, which targets Vulkan, Metal, and DirectX 12 backends. It can handle the complexities of wgpu-py with ease and provide a user-friendly way of implementing 3D graphics. This chapter covers the basics about the pygfx render engine and demonstrates how to use it to create some typical 3D graphics objects.
Chapter 16, Custom Objects and Geometries in pygfx
This chapter explains how to create custom geometries and custom objects using the pygfx render engine. It creates a custom object that is used to visualize functions with complex variables. It also builds a custom surface geometry that can be used to generate various 3D surfaces.
Using Code Examples
You may use the code in this book in your own applications and documentation. You do not need to contact the author or the publisher for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing the example code listings does require permission. Incorporating a significant amount of example code from this book into your applications and documentation also requires permission. Integrating the example code from this book into commercial products is not allowed without written permission of the author.