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 <cstdlib>
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  byte buf[128 * 128];
20  for (auto& b : buf) {
21  b = byte(rand());
22  }
23  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 128, 128, 0,
24  GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
25 }
26 
27 void GLSnow::paint(OutputSurface& /*output*/)
28 {
29  // Rotate and mirror noise texture in consecutive frames to avoid
30  // seeing 'patterns' in the noise.
31  static const vec2 pos[8][4] = {
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  { { -1, -1 }, { -1, 1 }, { 1, 1 }, { 1, -1 } }
40  };
41  static unsigned cnt = 0;
42  cnt = (cnt + 1) % 8;
43 
44  vec2 offset(float(rand()) / RAND_MAX, float(rand()) / RAND_MAX);
45  const vec2 tex[4] = {
46  offset + vec2(0.0f, 2.0f),
47  offset + vec2(2.0f, 2.0f),
48  offset + vec2(2.0f, 0.0f),
49  offset + vec2(0.0f, 0.0f)
50  };
51 
52  gl::context->progTex.activate();
53  glUniform4f(gl::context->unifTexColor, 1.0f, 1.0f, 1.0f, 1.0f);
54  mat4 I;
55  glUniformMatrix4fv(gl::context->unifTexMvp, 1, GL_FALSE, &I[0][0]);
56  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, pos[cnt]);
57  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, tex);
58  glEnableVertexAttribArray(0);
59  glEnableVertexAttribArray(1);
60  noiseTexture.bind();
61  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
62 
63  display.repaintDelayed(100 * 1000); // 10fps
64 }
65 
66 } // namespace openmsx
signed char offset
Definition: CPUCore.cc:252
Represents the output window/screen of openMSX.
Definition: Display.hh:32
unsigned char byte
8 bit unsigned integer
Definition: openmsx.hh:33
Interface for display layers.
Definition: Layer.hh:11
A frame buffer where pixels can be written to.
void bind()
Makes this texture the active GL texture.
Definition: GLUtil.hh:75
std::unique_ptr< Context > context
Definition: GLContext.cc:9
void repaintDelayed(uint64_t delta)
Definition: Display.cc:390
vecN< 2, float > vec2
Definition: gl_vec.hh:118
virtual void paint(OutputSurface &output)
Paint this layer.
Definition: GLSnow.cc:27