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