openMSX
GLSnow.cc
Go to the documentation of this file.
1#include "GLSnow.hh"
2#include "GLContext.hh"
3#include "gl_mat.hh"
4#include "Display.hh"
5#include "gl_vec.hh"
6#include "openmsx.hh"
7#include "random.hh"
8
9using namespace gl;
10
11namespace openmsx {
12
14 : Layer(COVER_FULL, Z_BACKGROUND)
15 , display(display_)
16 , noiseTexture(true, true) // enable interpolation + wrapping
17{
18 // Create noise texture.
19 auto& generator = global_urng(); // fast (non-cryptographic) random numbers
20 std::uniform_int_distribution<int> distribution(0, 255);
21 std::array<uint8_t, 128 * 128> buf;
22 ranges::generate(buf, [&] { return distribution(generator); });
23#if OPENGL_VERSION < OPENGL_3_3
24 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 128, 128, 0,
25 GL_LUMINANCE, GL_UNSIGNED_BYTE, buf.data());
26#else
27 // GL_LUMINANCE no longer supported in newer versions
28 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 128, 128, 0,
29 GL_RED, GL_UNSIGNED_BYTE, buf.data());
30 GLint swizzleMask[] = {GL_RED, GL_RED, GL_RED, GL_ONE};
31 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
32#endif
33
34 static constexpr std::array pos = {
35 std::array{vec2{-1, -1}, vec2{ 1, -1}, vec2{ 1, 1}, vec2{-1, 1}},
36 std::array{vec2{-1, 1}, vec2{ 1, 1}, vec2{ 1, -1}, vec2{-1, -1}},
37 std::array{vec2{-1, 1}, vec2{-1, -1}, vec2{ 1, -1}, vec2{ 1, 1}},
38 std::array{vec2{ 1, 1}, vec2{ 1, -1}, vec2{-1, -1}, vec2{-1, 1}},
39 std::array{vec2{ 1, 1}, vec2{-1, 1}, vec2{-1, -1}, vec2{ 1, -1}},
40 std::array{vec2{ 1, -1}, vec2{-1, -1}, vec2{-1, 1}, vec2{ 1, 1}},
41 std::array{vec2{ 1, -1}, vec2{ 1, 1}, vec2{-1, 1}, vec2{-1, -1}},
42 std::array{vec2{-1, -1}, vec2{-1, 1}, vec2{ 1, 1}, vec2{ 1, -1}},
43 };
44 glBindBuffer(GL_ARRAY_BUFFER, vbo[0].get());
45 glBufferData(GL_ARRAY_BUFFER, sizeof(pos), pos.data(), GL_STATIC_DRAW);
46 glBindBuffer(GL_ARRAY_BUFFER, 0);
47}
48
50{
51 // Rotate and mirror noise texture in consecutive frames to avoid
52 // seeing 'patterns' in the noise.
53 static unsigned cnt = 0;
54 cnt = (cnt + 1) % 8;
55
56 vec2 offset(random_float(0.0f, 1.0f), random_float(0.0f, 1.0f));
57 const std::array tex = {
58 offset + vec2(0.0f, 2.0f),
59 offset + vec2(2.0f, 2.0f),
60 offset + vec2(2.0f, 0.0f),
61 offset + vec2(0.0f, 0.0f),
62 };
63
64 auto& glContext = *gl::context;
65 glContext.progTex.activate();
66 glUniform4f(glContext.unifTexColor, 1.0f, 1.0f, 1.0f, 1.0f);
67 mat4 I;
68 glUniformMatrix4fv(glContext.unifTexMvp, 1, GL_FALSE, &I[0][0]);
69
70 glBindBuffer(GL_ARRAY_BUFFER, vbo[0].get());
71 const vec2* base = nullptr;
72 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, base + 4 * cnt);
73 glEnableVertexAttribArray(0);
74
75 glBindBuffer(GL_ARRAY_BUFFER, vbo[1].get());
76 glBufferData(GL_ARRAY_BUFFER, sizeof(tex), tex.data(), GL_STREAM_DRAW);
77 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
78 glEnableVertexAttribArray(1);
79
80 noiseTexture.bind();
81 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
82 glDisableVertexAttribArray(1);
83 glDisableVertexAttribArray(0);
84 glBindBuffer(GL_ARRAY_BUFFER, 0);
85
86 display.repaintDelayed(100 * 1000); // 10fps
87}
88
89} // namespace openmsx
void bind()
Makes this texture the active GL texture.
Definition: GLUtil.hh:77
Represents the output window/screen of openMSX.
Definition: Display.hh:32
void repaintDelayed(uint64_t delta)
Definition: Display.cc:379
void paint(OutputSurface &output) override
Paint this layer.
Definition: GLSnow.cc:49
GLSnow(Display &display)
Definition: GLSnow.cc:13
Interface for display layers.
Definition: Layer.hh:12
A frame buffer where pixels can be written to.
Definition: gl_mat.hh:23
vecN< 2, float > vec2
Definition: gl_vec.hh:150
std::optional< Context > context
Definition: GLContext.cc:10
This file implemented 3 utility functions:
Definition: Autofire.cc:9
void generate(ForwardRange &&range, Generator &&g)
Definition: ranges.hh:275
auto & global_urng()
Return reference to a (shared) global random number generator.
Definition: random.hh:8
float random_float(float from, float upto)
Return a random float in the range [from, upto) (note: half-open interval).
Definition: random.hh:50