Skip to content

mario-deluna/php-render

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Render

A 3D Software renderer in pure PHP.

  • Just download/clone and the renderer should just work.
  • Has Shader System in PHP.
  • Allows Custom vertex attributes with interpolation.
  • Supports a Depth Buffer.
  • Supports Multiple Output Buffers per context.
  • CG Math Library.
  • ObJ File Parser.
  • There is an FFmpeg wrapper to easly combine multiple frames into a MP4 video. (This obviously requires ffmpeg...)

Table of Contents

Why?

Because this question is going to pop up a lot, let me address it here first. This project has been a learning experience for me and hopefully also for others interested in computer graphics with a web dev background. I should not have to say it but this has no "production value" whatsoever, there is a mountain of flaws to name a few:

  • This does not run in parallel.
  • This runs on the CPU (Software Renderer).
  • This wastes insane amounts of memory.
  • I tried to optimize things without sacrificing too much code clarity, but this is still very slow.
  • I use PHP Arrays as literal pixel buffers which just can't be optimal.

So what is this?

I've been working on a game running in my own engine for a while now. Many of the issues I encountered came from not fully understanding what the rendering API (Vulkan, OpenGL, DirectX) actually does. To counter this I do what I always do, and try to implement the basics of the used library / API / concept myself. This project is the result.

I should also note I'm still nowhere near an expert in the field.

When I started with programming, I often encountered parts of computer science that seemed super interesting and I wanted to learn more about them. But not having examples/tutorials in a language I was familiar with got me overwhelmed fast. This is an issue that fades away after years in the industry but I still remember the frustration. Long story short I hope this project helps someone learning something new.

Usage

Everything should be up and running, this renderer is implemented entirely in PHP so there are no special extension requirements or other dependencies.

Composer is used for autoloading so you will have to run a composer install.

Some examples make use of some assets (3d model, textures etc.) There is script which will download and extract these:

./bin/download-example-resources

Examples

Interpolartion Triangle

The classic color gradient triangle.

Example Source

$ php examples/01_triangle/triangle.php
Triangle Example Triangle Example

This example exists to showcase the interpolation of the barycentric coordinates inside of a drawn triangle.

public function vertex(Vertex $vertex, array &$out) : Vec4
{
    $out['color'] = $vertex->color; // set the color attribute
    return Vec4::fromVec3($vertex->position); // return vertex positon
}

public function fragment(array &$in, array &$out)
{
    $out['color'] = $in['color']->toColorInt();
}

Basic Cube

Just a simple cube showcasing 3D geometry with depth testing.

Example Source

$ php examples/02_cube_basic/cube.php
Cube Lines Cube Depth Cube Color

Cube Video

Showcasing a simple example how to use the ffmpeg stream to create videos with php-render.

Example Source

$ php examples/03_cube_video/cube.php
Cube Lines

Basic Model Loading

This example shows how to load a model from an obj file.

Example Source

$ php examples/04_simple_model/model.php
Model Lines Model Depth Model Color

Model With Texture Sampling

Showcases basic texture sampling of a loaded model.

Example Source

$ php examples/05_texture_sampling/model.php
Model Lines Model Depth Model Color

Model with Phong Shading

Showcases simple phong shading on a 3D Model

Example Source

$ php examples/10_phong/model.php
Model Depth Model Color Video

Credits

License

The License (AGPLv3). Please see License File for more information.