-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.php
185 lines (142 loc) · 5.01 KB
/
test.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
require __DIR__ . '/vendor/autoload.php';
define('DS', DIRECTORY_SEPARATOR);
define('VISU_PATH_FRAMEWORK_RESOURCES_FONT', __DIR__ . '/resources/fonts');
use GL\Math\Vec3;
use GL\Math\Vec4;
use VISU\Graphics\Font\DebugFontRenderer;
use VISU\Graphics\GLState;
use VISU\Graphics\Rendering\Pass\BackbufferData;
use VISU\Graphics\Rendering\Pass\ClearPass;
use VISU\Graphics\Rendering\Pass\FullscreenQuadPass;
use VISU\Graphics\Rendering\PipelineContainer;
use VISU\Graphics\Rendering\PipelineResources;
use VISU\Graphics\Rendering\Renderer\DebugOverlayText;
use VISU\Graphics\Rendering\Renderer\DebugOverlayTextAlignment;
use VISU\Graphics\Rendering\Renderer\DebugOverlayTextRenderer;
use VISU\Graphics\Rendering\RenderPipeline;
use VISU\Graphics\ShaderProgram;
use VISU\Graphics\ShaderStage;
use VISU\Graphics\Texture;
use VISU\Graphics\TextureOptions;
use VISU\OS\Input;
use VISU\OS\Key;
use VISU\OS\Window;
use VISU\Runtime\GameLoop;
use VISU\Signal\Dispatcher;
use VISU\Signals\Input\KeySignal;
glfwInit();
$gl = new GLState;
$window = new Window('Test Window', 640, 480);
$window->initailize($gl);
$dispatcher = new Dispatcher;
$input = new Input($window, $dispatcher);
$window->setEventHandler($input);
$resolution = 16;
$dispatcher->register('input.key', function (KeySignal $signal) use(&$resolution) {
if ($signal->key === Key::UP) {
$resolution++;
} elseif ($signal->key === Key::DOWN) {
$resolution--;
}
var_dump($resolution);
});
$testTexture = new Texture($gl, 'test');
// $testTexture->loadFromFile(__DIR__ . '/resources/fonts/cozette/cozette.png');
$testTexture->loadFromFile(__DIR__ . '/resources/phplogo.png');
// declare a simple shader to render the texture
$shader = new ShaderProgram($gl);
$shader->attach(new ShaderStage(GL_VERTEX_SHADER, <<<EOT
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoords;
void main()
{
gl_Position = vec4(aPos, 1.0);
TexCoords = aTexCoord;
}
EOT
));
$shader->attach(new ShaderStage(GL_FRAGMENT_SHADER, <<<EOT
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D u_texture;
void main()
{
FragColor = texture(u_texture, TexCoords);
}
EOT
));
$shader->link();
class Game implements VISU\Runtime\GameLoopDelegate
{
private GameLoop $loop;
private PipelineResources $renderResources;
private DebugOverlayTextRenderer $debugOverlay;
private int $tick = 0;
public function __construct(
private Window $window,
private GLState $gl,
)
{
$this->loop = new GameLoop($this);
$this->renderResources = new PipelineResources($gl);
$this->debugOverlay = new DebugOverlayTextRenderer($gl, DebugOverlayTextRenderer::loadDebugFontAtlas());
}
public function update(): void
{
$this->window->pollEvents();
}
public function render(float $delta): void
{
$windowRenderTarget = $this->window->getRenderTarget();
$windowRenderTarget->framebuffer()->clearColor = new Vec4(1, 0.2, 0.2, 1.0);
$data = new PipelineContainer;
$pipeline = new RenderPipeline($this->renderResources, $data, $windowRenderTarget);
global $testTexture, $shader, $resolution;
$texRes = $pipeline->importTexture('test', $testTexture);
$intermedia = $pipeline->createRenderTarget('intermediate', $resolution, $resolution);
$colorOptions = new TextureOptions;
$colorOptions->minFilter = GL_NEAREST;
$colorOptions->magFilter = GL_NEAREST;
$intermediaColor = $pipeline->createColorAttachment($intermedia, 'color');
$pipeline->addPass(new ClearPass($data->get(BackbufferData::class)->target));
$pipeline->addPass(new FullscreenQuadPass($intermedia, $texRes, $shader));
$this->debugOverlay->attachPass(
$pipeline,
$intermedia,
[
new DebugOverlayText('DOWNSCALE', 3, 3, new Vec3(1, 0, 0)),
]
);
$pipeline->addPass(new FullscreenQuadPass($data->get(BackbufferData::class)->target, $intermediaColor, $shader));
$this->debugOverlay->attachPass(
$pipeline,
$data->get(BackbufferData::class)->target,
[
new DebugOverlayText('FPS: ' . $this->loop->getAverageFps(), 10, 10),
new DebugOverlayText('FT: ' . $this->loop->getAverageFrameTime(), 10, 200, new Vec3(1, 0, 0)),
]
);
// $pipeline->addPass(new ShadowMapPass($renderables));
// $pipeline->addPass(new DebugDepthPass($data->get(ShadowMapData::class)->shadowMap));
$pipeline->execute($this->tick++);
$this->window->swapBuffers();
}
public function shouldStop(): bool
{
return $this->window->shouldClose();
}
public function start(): void
{
$this->loop->start();
}
}
$game = new Game($window, $gl);
$game->start();
// while (!$window->shouldClose()) {
// $window->pollEvents();
// $window->swapBuffers();
// }