-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcube.php
120 lines (103 loc) · 3.37 KB
/
cube.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
define('EXAMPLE_DIR', __DIR__);
require __DIR__ . '/../example_base.php';
use PHPR\Context;
use PHPR\Shader\Shader;
use PHPR\Mesh\{Vertex, 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();
}
}
/**
* Create shader object
*/
$shader = new CubeShader;
/**
* Build a model view projection
*/
$projection = Mat4::perspective(Angle::degreesToRadians(45.0), EXAMPLE_RENDER_ASPECT_RATIO, 0.1, 100);
$view = (new Mat4())->inverse();
$model = (new Mat4)->translate(new Vec3(0.0, 0.0, -3));
$model->rotateX(0.45);
$model->rotateY(-0.45);
// multiply them togethrer
$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 cube
*/
$cube = new VertexArray(VertexPNU::class, [
// position // normal // uv
0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0,
-0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0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0,
]);
/**
* draw the cube
*/
$context->drawVertexArray($cube);
render_example_context($context);
render_example_context_depth($context);