Website • Getting Started • API Docs • Examples
A fully-featured OpenGL and GLFW extension for PHP. Batteries included 🔋!
PHP-GLFW allows you to create 2D and 3D real-time applications in PHP. It introduces a whole new set of tools for PHP developers to build graphical applications like games, scientific simulations, user interfaces, and much more.
Currently supports PHP8.x
- What is this extension? Discover the features 🚀
- Documentation 📚
- Examples
- Installation
- Credits
- License
PHP-GLFW aims to be a complete solution containing everything you need to start building graphical applications in PHP. It doesn't just provide GLFW library bindings, but also includes OpenGL bindings and a set of essential classes and helpers.
- Full native support for OpenGL (4.1+ core), bringing GPU-accelerated rendering to PHP.
- Targets OpenGL 4.1, but can be built for newer versions as well.
- Support for OpenGL extensions (limited).
- Mirrors the C API as closely as possible, with adjustments made where required or otherwise nonsensical.
- Supports ~90% of the full standard; see GLSUPPORT.md for details.
This extension includes bindings for the powerful GLFW library, offering a range of amazing features, such as:
- Multiplatform Window creation and handling (MacOS / Windows / Linux)
- Support for multiple windows and monitors
- Real-time user input handling
- Keyboard and Mouse event handling
- Joystick input support
- Gamepad Support
PHP-GLFW includes a vector graphics API that allows you to draw in a HTML canvas-like manner in PHP. Its uses the same OpenGL context internally allowing you to build complex rendering pipelines utilizing both the vector graphics API and the more low-level OpenGL API.
- Allows for quick and intuitive rendering of primitives.
- Includes numerous helpers for colors.
- Supports gradients and images.
- Easily renders text with any font.
- Ideal for quickly creating GUIs.
PHP-GLFW comes with a built-in mathematics library, written in C and optimized for graphical applications.
- Supported structs:
Vec2
,Vec3
,Vec4
,Mat4
, andQuat
- Includes most common matrix operations, like:
lookAt
,perspective
,inverse
,rotate
, and more
Integration into the extension offers several advantages:
- High performance
- Low memory footprint
- Overloaded operators for math structs, enabling intuitive operations like:
use GL\Math\Vec2; $v3 = Vec2(15, -5) + Vec2(42, 7); // returns Vec2(57, 2)
- Some OpenGL functions directly accept math structs as arguments
This extension also includes a set of buffer objects that internally hold data in native types.
- Can handle large arrays of data
- Low memory footprint and very fast
- Data is stored internally to be directly uploadable to the GPU
PHP-GLFW supports the loading of images/textures into buffers without requiring an additional extension:
- Can load common image formats, like
jpg
,png
,tga
,bmp
, andgif
(gd or Imagick is not required) - Can write images/textures back to disk
- Writes data into a
BufferInterface
object, providing full access to the bitmap
PHP-GLFW comes with a .obj
wavefront file loader, allowing you to load and parse .obj
files. It also provides helpers to generate tangent and bitangent vectors for the loaded geometry. Currently, only triangulated geometry is supported, with no support for quads.
- Can parse
.obj
and.mtl
files - Can generate normal, tangent, and bitangent vectors for the loaded geometry on the fly
- Allows extraction of separate meshes and groups from the loaded geometry
- Can group vertices by their material
- Can reindex extracted meshes to reduce the number of vertices
PHP-GLFW parses the OpenGL specs to generate most of the C extension, instead of porting functions manually. Manual adjustments are made where necessary.
Explore the examples directory to dive into the code.
A very simple Flappy Bird-like game written in PHP.
https://github.com/phpgl/flappyphpant
Yet another CHIP-8 emulator, but in PHP!
https://github.com/mario-deluna/php-chip8
We are currently developing a 3D game entirely built with PHP, leveraging the power of PHP-GLFW. The game, named PHP-TowerDefense, is open-source and serves as a showcase for the capabilities of PHP-GLFW. Although the game is in active development, it is not yet ready for release.
Check out the game's repository on GitHub for more information and to follow its progress: https://github.com/phpgl/php-towerdefenseClassic Pong game programmed with pure OpenGL wrapped in OOP code.
https://github.com/medilies/php-pong
Please refer to the comprehensive installation guide here: Installation
PHP Version:
>= 8.0
While I would love to support older PHP builds, it was just not feasible for this extension.
You can use a simple installer script to install PHP-GLFW:
php -r "copy('https://raw.githubusercontent.com/mario-deluna/php-glfw/master/install/macos-installer.php', 'phpglfw-installer.php');" && php phpglfw-installer.php
Once you see "Installation finished!", you're ready to go!
For a manual approach, ensure you have installed the php-dev
, git
, and cmake
packages. They are required!
git clone https://github.com/mario-deluna/php-glfw
cd php-glfw
sudo phpize && ./configure --enable-glfw
sudo make install
Don't forget to add glfw.so
in the php.ini
file:
extension="glfw.so"
cmake
is required for the installation. You can skip the first step if it's already installed.
Also, make sure that you install the php-dev
package, for example, php8.1-dev
.
sudo apt install -y cmake git
git clone https://github.com/mario-deluna/php-glfw
cd php-glfw
sudo phpize && ./configure --enable-glfw
sudo make install
Make sure to add glfw.so
in the php.ini
file:
extension="glfw.so"
As this is a PHP extension, your editor / IDE does not support auto-completion and doc lookups without some help. We created a composer package you can include as a dev dependency to have full support:
composer require --dev phpgl/ide-stubs
Please see License File for more information.