nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
postprocess.cc
Go to the documentation of this file.
2
3#include <raylib.h>
4#include <rlgl.h>
5
6#include <algorithm>
7
8#include "client/glsl.h"
9
10namespace nanommo {
11
12namespace {
13
14// Keeps what's past white, from none of a colour whose brightest channel is
15// just at it, to all of one at twice as bright.
16constexpr const char* kBrightShader = R"(#version 330
17in vec2 fragTexCoord;
18uniform sampler2D texture0;
19out vec4 finalColor;
20
21void main() {
22 vec3 color = texture(texture0, fragTexCoord).rgb;
23 float brightest = max(color.r, max(color.g, color.b));
24 finalColor = vec4(color * clamp(brightest - 1.0, 0.0, 1.0), 1.0);
25}
26)";
27
28// Spreads a half as it doubles, over the nine of its pixels around each one,
29// so that the light fades out smoothly, where doubling alone would leave
30// blocks.
31constexpr const char* kSpreadShader = R"(#version 330
32in vec2 fragTexCoord;
33uniform sampler2D texture0;
34uniform vec2 pixel;
35out vec4 finalColor;
36
37void main() {
38 vec3 sum = vec3(0.0);
39 for (int x = -1; x <= 1; x++) {
40 for (int y = -1; y <= 1; y++) {
41 float weight = (2.0 - abs(float(x))) * (2.0 - abs(float(y)));
42 sum += texture(texture0, fragTexCoord + vec2(x, y) * pixel).rgb * weight;
43 }
44 }
45 finalColor = vec4(sum / 16.0, 1.0);
46}
47)";
48
49// The frame, with the bloom added on top.
50constexpr const char* kCombineShader = R"(#version 330
51in vec2 fragTexCoord;
52uniform sampler2D texture0;
53uniform sampler2D bloom;
54uniform float strength;
55out vec4 finalColor;
56
57void main() {
58 vec3 color = texture(texture0, fragTexCoord).rgb;
59 color += texture(bloom, fragTexCoord).rgb * strength;
60 // The screen cuts each channel at white on its own, which turns a bright
61 // orange yellow, so a colour past white is scaled down to it as a whole.
62 float brightest = max(color.r, max(color.g, color.b));
63 finalColor = vec4(color / max(brightest, 1.0), 1.0);
64}
65)";
66
67// FXAA, Timothy Lottes' fast approximate anti-aliasing, in its short form: it
68// finds which way an edge runs from the brightness around each pixel, and
69// blends along it.
70constexpr const char* kFxaaShader = R"(#version 330
71in vec2 fragTexCoord;
72uniform sampler2D texture0;
73uniform vec2 pixel;
74out vec4 finalColor;
75
76// How bright each channel looks to us.
77const vec3 luma = vec3(0.299, 0.587, 0.114);
78
79float Brightness(vec2 at) {
80 return dot(texture(texture0, fragTexCoord + at * pixel).rgb, luma);
81}
82
83void main() {
84 float nw = Brightness(vec2(-1.0, -1.0));
85 float ne = Brightness(vec2(1.0, -1.0));
86 float sw = Brightness(vec2(-1.0, 1.0));
87 float se = Brightness(vec2(1.0, 1.0));
88 float middle = Brightness(vec2(0.0));
89 float darkest = min(middle, min(min(nw, ne), min(sw, se)));
90 float brightest = max(middle, max(max(nw, ne), max(sw, se)));
91 // Across an edge, the brightness changes, so the edge runs at right angles
92 // to the change.
93 vec2 along = vec2((sw + se) - (nw + ne), (nw + sw) - (ne + se));
94 float reduce = max((nw + ne + sw + se) / 32.0, 1.0 / 128.0);
95 float scale = 1.0 / (min(abs(along.x), abs(along.y)) + reduce);
96 along = clamp(along * scale, -8.0, 8.0) * pixel;
97 vec3 near = 0.5 * (texture(texture0, fragTexCoord - along / 6.0).rgb +
98 texture(texture0, fragTexCoord + along / 6.0).rgb);
99 vec3 ends = texture(texture0, fragTexCoord - along / 2.0).rgb +
100 texture(texture0, fragTexCoord + along / 2.0).rgb;
101 vec3 far = 0.5 * near + 0.25 * ends;
102 // Reaching further can cross into another edge, which shows as a
103 // brightness that isn't around the pixel.
104 float reached = dot(far, luma);
105 bool crossed = reached < darkest || reached > brightest;
106 finalColor = vec4(crossed ? near : far, 1.0);
107}
108)";
109
110// How much of the bloom goes on the frame. The halves add up, so it's a
111// fraction.
112constexpr float kStrength = 0.25f;
113
114// Like raylib's LoadRenderTexture, with half a float to each of a pixel's
115// channels, which can go past white, where raylib's eight bits stop at it.
116RenderTexture2D LoadFrame(int width, int height) {
117 constexpr int kFormat = PIXELFORMAT_UNCOMPRESSED_R16G16B16A16;
118 RenderTexture2D frame{};
119 frame.id = rlLoadFramebuffer();
120 frame.texture = {.id = rlLoadTexture(nullptr, width, height, kFormat, 1),
121 .width = width,
122 .height = height,
123 .mipmaps = 1,
124 .format = kFormat};
125 frame.depth = {.id = rlLoadTextureDepth(width, height, true),
126 .width = width,
127 .height = height};
128 rlFramebufferAttach(frame.id, frame.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0,
129 RL_ATTACHMENT_TEXTURE2D, 0);
130 rlFramebufferAttach(frame.id, frame.depth.id, RL_ATTACHMENT_DEPTH,
131 RL_ATTACHMENT_RENDERBUFFER, 0);
132 return frame;
133}
134
135// Draws a texture over the whole of a target. OpenGL keeps a texture's rows
136// from the bottom up, so it's drawn with a negative height, which flips it the
137// right way up.
138void DrawOver(const Texture2D& texture, float width, float height) {
139 DrawTexturePro(texture,
140 {0.0f, 0.0f, static_cast<float>(texture.width),
141 -static_cast<float>(texture.height)},
142 {0.0f, 0.0f, width, height}, {0.0f, 0.0f}, 0.0f, WHITE);
143}
144
145void DrawInto(const RenderTexture2D& target, const Texture2D& texture) {
146 BeginTextureMode(target);
147 DrawOver(texture, static_cast<float>(target.texture.width),
148 static_cast<float>(target.texture.height));
149 EndTextureMode();
150}
151
152} // namespace
153
154// raylib's own vertex shader passes the texture's coordinates on.
156 : bright_(LoadGlsl(nullptr, kBrightShader)),
157 spread_(LoadGlsl(nullptr, kSpreadShader)),
158 combine_(LoadGlsl(nullptr, kCombineShader)),
159 fxaa_(LoadGlsl(nullptr, kFxaaShader)) {}
160
162 UnloadRenderTexture(frame_);
163 for (const RenderTexture2D& half : halves_) {
164 UnloadRenderTexture(half);
165 }
166 UnloadShader(bright_);
167 UnloadShader(spread_);
168 UnloadRenderTexture(picture_);
169 UnloadShader(combine_);
170 UnloadShader(fxaa_);
171}
172
174 // In pixels, as a high-DPI screen has more of them than points. It's made
175 // again when the window changes size.
176 Begin(graphics, GetRenderWidth(), GetRenderHeight());
177}
178
179void PostProcess::Begin(const GraphicsSettings& graphics, int width,
180 int height) {
181 graphics_ = graphics;
182 if (frame_.texture.width != width || frame_.texture.height != height) {
183 Resize(width, height);
184 }
185 BeginTextureMode(frame_);
186}
187
188void PostProcess::Resize(int width, int height) {
189 UnloadRenderTexture(frame_);
190 frame_ = LoadFrame(width, height);
191 // Halving a texture with the bilinear filter averages each two by two block
192 // of its pixels.
193 SetTextureFilter(frame_.texture, TEXTURE_FILTER_BILINEAR);
194 // Eight bits a channel, as it's past the bloom, and white is as bright as
195 // the screen goes. FXAA reads it between its pixels.
196 UnloadRenderTexture(picture_);
197 picture_ = LoadRenderTexture(width, height);
198 SetTextureFilter(picture_.texture, TEXTURE_FILTER_BILINEAR);
199 for (RenderTexture2D& half : halves_) {
200 width = std::max(width / 2, 1);
201 height = std::max(height / 2, 1);
202 UnloadRenderTexture(half);
203 half = LoadFrame(width, height);
204 SetTextureFilter(half.texture, TEXTURE_FILTER_BILINEAR);
205 }
206}
207
208void PostProcess::End() { EndTextureMode(); }
209
210void PostProcess::Bloom() {
211 // What's bright, in a frame half the size.
212 BeginShaderMode(bright_);
213 DrawInto(halves_[0], frame_.texture);
214 EndShaderMode();
215 // Halved again and again, so that the smallest half spreads its light the
216 // widest, for few pixels.
217 for (int i = 1; i < kHalves; ++i) {
218 DrawInto(halves_[i], halves_[i - 1].texture);
219 }
220 // And added back up, from the smallest, each spread as it doubles.
221 BeginBlendMode(BLEND_ADDITIVE);
222 BeginShaderMode(spread_);
223 for (int i = kHalves - 1; i > 0; --i) {
224 const Texture2D& texture = halves_[i].texture;
225 const Vector2 pixel = {1.0f / static_cast<float>(texture.width),
226 1.0f / static_cast<float>(texture.height)};
227 SetShaderValue(spread_, GetShaderLocation(spread_, "pixel"), &pixel,
228 SHADER_UNIFORM_VEC2);
229 DrawInto(halves_[i - 1], texture);
230 }
231 EndShaderMode();
232 EndBlendMode();
233}
234
235void PostProcess::Draw() { Show(nullptr); }
236
237void PostProcess::DrawOnto(const RenderTexture2D& into) { Show(&into); }
238
239void PostProcess::Show(const RenderTexture2D* into) {
240 if (graphics_.bloom) {
241 Bloom();
242 }
243 // FXAA smooths the picture as the screen would show it, so the frame and its
244 // bloom go into the picture first.
245 if (graphics_.antialias) {
246 BeginTextureMode(picture_);
247 Combine(static_cast<float>(picture_.texture.width),
248 static_cast<float>(picture_.texture.height));
249 EndTextureMode();
250 }
251 const float width = into != nullptr ? static_cast<float>(into->texture.width)
252 : static_cast<float>(GetScreenWidth());
253 const float height = into != nullptr
254 ? static_cast<float>(into->texture.height)
255 : static_cast<float>(GetScreenHeight());
256 if (into != nullptr) {
257 BeginTextureMode(*into);
258 }
259 // The frame to the screen, fitted to its points, as raylib draws on the
260 // screen in points. The sea and the glows leave the frame see-through where
261 // they are, and both shaders write it opaque, so that it covers what's on the
262 // screen.
263 if (graphics_.antialias) {
264 BeginShaderMode(fxaa_);
265 const Vector2 pixel = {1.0f / static_cast<float>(picture_.texture.width),
266 1.0f / static_cast<float>(picture_.texture.height)};
267 SetShaderValue(fxaa_, GetShaderLocation(fxaa_, "pixel"), &pixel,
268 SHADER_UNIFORM_VEC2);
269 DrawOver(picture_.texture, width, height);
270 EndShaderMode();
271 } else {
272 Combine(width, height);
273 }
274 if (into != nullptr) {
275 EndTextureMode();
276 }
277}
278
279void PostProcess::Combine(float width, float height) {
280 BeginShaderMode(combine_);
281 SetShaderValueTexture(combine_, GetShaderLocation(combine_, "bloom"),
282 halves_[0].texture);
283 const float strength = graphics_.bloom ? kStrength : 0.0f;
284 SetShaderValue(combine_, GetShaderLocation(combine_, "strength"), &strength,
285 SHADER_UNIFORM_FLOAT);
286 DrawOver(frame_.texture, width, height);
287 EndShaderMode();
288}
289
290} // namespace nanommo
void DrawOnto(const RenderTexture2D &into)
void Draw()
Draws the frame to the screen, with bloom if it's on.
void Begin(const GraphicsSettings &graphics)
Definition chat.cc:9
Shader LoadGlsl(const char *vertex, const char *fragment)
Definition glsl.cc:32
bool antialias
Smooths the world's edges, with FXAA.
Definition graphics.h:14