-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmodel.php
More file actions
76 lines (63 loc) · 1.61 KB
/
Copy pathmodel.php
File metadata and controls
76 lines (63 loc) · 1.61 KB
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
<?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};
/**
* Simpel cube shader
*/
class CubeShader extends Shader
{
public Mat4 $mvp;
/**
* Vertex shader like thing
*/
public function vertex(Vertex $vertex, array &$out) : Vec4
{
$out['color'] = $vertex->normal;
return $this->mvp->multiplyVec3($vertex->position);
}
/**
* Fragment shader like thing
*/
public function fragment(array &$in, array &$out)
{
$out['color'] = $in['color']->normalize()->toColorInt();
// $out['color'] = 0xFFFFFF;
}
}
/**
* Build a model view projection
*/
$projection = Mat4::perspective(Angle::degreesToRadians(-45.0), EXAMPLE_RENDER_ASPECT_RATIO, 0.5, 10);
$view = (new Mat4())->inverse();
$model = (new Mat4)->translate(new Vec3(0.1, -0.3, -2.5));
$model->rotateX(0.4);
$model->rotateY(-0.6);
//$model->rotateY(-0.05);
/**
* Create shader object
*/
$shader = new CubeShader;
$shader->mvp = $model->multiply($view->multiply($projection, true), true);
/**
* Build the context
*/
$context = create_exmaple_context();
$context->bindShader($shader);
$context->enableDepthTesting();
// $context->setDrawMode(Context::DRAW_MODE_LINES);
/**
* Define the model
*/
$model = ObjParser::loadFromDisk(EXAMPLE_DIR . '/suv.obj');
/**
* draw the model
*/
$context->drawVertexArray($model);
render_example_context($context);
render_example_context_depth($context);