-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.php
83 lines (68 loc) · 1.93 KB
/
model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
define('EXAMPLE_DIR', __DIR__);
require __DIR__ . '/../example_base.php';
use PHPR\Context;
use PHPR\Shader\Shader;
use PHPR\Mesh\{ObjParser, Vertex};
use PHPR\Mesh\Vertex\{VertexPNU};
use PHPR\Mesh\VertexArray;
use PHPR\Math\{Vec3, Vec4, Mat4, Angle};
use PHPR\Sampler\ImageSamplerGD;
/**
* Simpel cube shader
*/
class ModelShader extends Shader
{
public Mat4 $projection;
public Mat4 $view;
public Mat4 $model;
public ImageSamplerGD $diffuse;
/**
* Vertex shader like thing
*/
public function vertex(Vertex $vertex, array &$out) : Vec4
{
$out['uv'] = $vertex->uv;
$mvp = $this->model->multiply($this->view->multiply($this->projection, true), true);
return $mvp->multiplyVec3($vertex->position);
}
/**
* Fragment shader like thing
*/
public function fragment(array &$in, array &$out)
{
$out['color'] = $this->diffuse->sample($in['uv']->x, 1 - $in['uv']->y);
}
}
/**
* Create shader object
*/
$shader = new ModelShader;
// load textures
$shader->diffuse = new ImageSamplerGD(EXAMPLE_MODEL_DIR . '/chest/Treasurechest_DIFF.png');
// camera
$cameraPos = new Vec3(0, 0, 5);
// projection
$shader->projection = Mat4::perspective(Angle::degreesToRadians(-45.0), EXAMPLE_RENDER_ASPECT_RATIO, 0.5, 10);
$shader->view = (new Mat4())->translate($cameraPos)->inverse();
$shader->model = (new Mat4)->translate(new Vec3(0.0, 0.2, 0.0));
$shader->model->rotateX(0.4);
$shader->model->rotateY(-0.2);
/**
* Build the context
*/
$context = create_exmaple_context();
$context->bindShader($shader);
$context->enableDepthTesting();
// $context->setDrawMode(Context::DRAW_MODE_LINES);
// $context->disableBackfaceCulling();
/**
* Define the model
*/
$object = ObjParser::loadFromDisk(EXAMPLE_MODEL_DIR . '/chest/chest.obj');
/**
* draw the model
*/
$context->drawVertexArray($object);
render_example_context($context);
render_example_context_depth($context);