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  glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 128, 128, 0,
26  GL_RED, GL_UNSIGNED_BYTE, buf);
27  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
28  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
29 
30  const vec2 pos[8][4] = {
31  { { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 } },
32  { { -1, 1 }, { 1, 1 }, { 1, -1 }, { -1, -1 } },
33  { { -1, 1 }, { -1, -1 }, { 1, -1 }, { 1, 1 } },
34  { { 1, 1 }, { 1, -1 }, { -1, -1 }, { -1, 1 } },
35  { { 1, 1 }, { -1, 1 }, { -1, -1 }, { 1, -1 } },
36  { { 1, -1 }, { -1, -1 }, { -1, 1 }, { 1, 1 } },
37  { { 1, -1 }, { 1, 1 }, { -1, 1 }, { -1, -1 } },
38  { { -1, -1 }, { -1, 1 }, { 1, 1 }, { 1, -1 } }
39  };
40  glBindBuffer(GL_ARRAY_BUFFER, vbo[0].get());
41  glBufferData(GL_ARRAY_BUFFER, sizeof(pos), pos, GL_STATIC_DRAW);
42 
43 }
44 
45 void GLSnow::paint(OutputSurface& /*output*/)
46 {
47  // Rotate and mirror noise texture in consecutive frames to avoid
48  // seeing 'patterns' in the noise.
49  static unsigned cnt = 0;
50  cnt = (cnt + 1) % 8;
51 
52  vec2 offset(random_float(0.0f, 1.0f) ,random_float(0.0f, 1.0f));
53  const vec2 tex[4] = {
54  offset + vec2(0.0f, 2.0f),
55  offset + vec2(2.0f, 2.0f),
56  offset + vec2(2.0f, 0.0f),
57  offset + vec2(0.0f, 0.0f)
58  };
59 
60  gl::context->progTex.activate();
61  glUniform4f(gl::context->unifTexColor, 1.0f, 1.0f, 1.0f, 1.0f);
62  mat4 I;
63  glUniformMatrix4fv(gl::context->unifTexMvp, 1, GL_FALSE, &I[0][0]);
64 
65  vao.bind();
66  glBindBuffer(GL_ARRAY_BUFFER, vbo[0].get());
67  vec2* base = nullptr;
68  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, base + cnt * 4);
69  glEnableVertexAttribArray(0);
70  glBindBuffer(GL_ARRAY_BUFFER, vbo[1].get());
71  glBufferData(GL_ARRAY_BUFFER, sizeof(tex), tex, GL_STATIC_DRAW);
72  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
73  glEnableVertexAttribArray(1);
74  noiseTexture.bind();
75  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
76  vao.unbind();
77 
78  display.repaintDelayed(100 * 1000); // 10fps
79 }
80 
81 } // namespace openmsx
openmsx.hh
gl_mat.hh
Display.hh
openmsx::GLSnow::paint
void paint(OutputSurface &output) override
Paint this layer.
Definition: GLSnow.cc:45
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:77
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
gl::VertexArray::bind
void bind() const
Bind this VertexArray.
Definition: GLUtil.hh:470
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
gl::VertexArray::unbind
void unbind() const
Unbind this VertexArray.
Definition: GLUtil.hh:474
global_urng
auto & global_urng()
Return reference to a (shared) global random number generator.
Definition: random.hh:8
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
GLContext.hh