-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcube.php
More file actions
142 lines (119 loc) · 3.79 KB
/
Copy pathcube.php
File metadata and controls
142 lines (119 loc) · 3.79 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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};
use PHPR\Render\FFMPEGStream;
/**
* 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)
{
$v = $in['color']->normalize();
$v->add(new Vec3(1.0, 1.0, 1.0))->multiply(0.5);
$out['color'] = $v->toColorInt();
// $out['color'] = 0xFFFFFF;
}
}
/**
* 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.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0,
//
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0,
//
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0,
//
0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.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, 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,
]);
/**
* Start a new video stream
*/
$stream = video_example_context($context);
for ($i=0; $i<360; $i++)
{
// clear
$context->clear();
// rotate
$model->rotateX(-0.01);
$model->rotateY(-0.01);
$shader->mvp = $model->multiply($view->multiply($projection, true), true);
// draw the cube
$context->drawVertexArray($cube);
// render to video
$stream->render();
}
$stream->stop();