mario-deluna/php-glfw

A fully-featured OpenGL and GLFW extension for PHP.

Installs: 16

Dependents: 0

Suggesters: 0

Security: 0

Stars: 402

Watchers: 14

Forks: 14

Open Issues: 4

Language:C

Type:php-ext

v2.2.1 2024-11-19 17:43 UTC

README

PHP-GLFW

LICENSE PHP Versions Supported GitHub tag issues - php-glfw

PHP-GLFW Tests MacOS Installer Windows Build

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.

3D Rendering in PHP

Currently supports PHP8.x

What is this extension? Features πŸš€

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.

OpenGL

  • 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.

GLFW

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

Vector Graphics API

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.

PHP Vector Graphics Rendering Library

  • 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.
  1. Read more about the Vector Graphics API

PHPGL - Math

PHP-GLFW comes with a built-in mathematics library, written in C and optimized for graphical applications.

PHP Vector Math Library

  • Supported structs: Vec2, Vec3, Vec4, Mat4, and Quat
  • 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
  1. Read more about math functions

PHPGL - Buffers

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
  1. Read more about the buffers

PHPGL - Textures

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, and gif (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
  1. Read more about texture loading

PHPGL - Geometry

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
  1. Read more about geometry loading

How are the bindings achieved?

PHP-GLFW parses the OpenGL specs to generate most of the C extension, instead of porting functions manually. Manual adjustments are made where necessary.

Documentation πŸ“š

  1. Full Documentation
  2. Getting Started Guide
  3. User Guide
  4. API Docs

Examples

glfw-previews

Explore the examples directory to dive into the code.

FlappyPHPant

A very simple Flappy Bird-like game written in PHP.

FlappyPHPant 2D PHP Game

https://github.com/phpgl/flappyphpant

php-chip8

Yet another CHIP-8 emulator, but in PHP!

A CHIP-8 Game Emulator written in PHP

https://github.com/mario-deluna/php-chip8

PHP-Towerdense Game

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.

PHP-TowerDefense Screenshot

Check out the game's repository on GitHub for more information and to follow its progress: https://github.com/phpgl/php-towerdefense

php-pong

Classic Pong game programmed with pure OpenGL wrapped in OOP code.

Classic Pong Game written in PHP

https://github.com/medilies/php-pong

Installation

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.

MacOS

Installer

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!

Manual installation

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"

Linux

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"

Stubs (IDE Support)

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

Credits

License

Please see License File for more information.