Copyright 2008-2014 Matus Chochlik. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
namespace oglplus {
class RectangleExample : public Example
{
private:
Context gl;
Lazy<Uniform<GLfloat>> scroll_factor;
public:
RectangleExample(void)
: scroll_factor(prog, "ScrollFactor")
{
vs.Source(" \
#version 330\n \
uniform float ScrollFactor; \
in vec2 Position; \
in vec2 Coord; \
out vec2 vertCoord; \
void main(void) \
{ \
vertCoord = ScrollFactor * Coord; \
gl_Position = vec4(Position, 0.0, 1.0); \
} \
");
vs.Compile();
fs.Source(" \
#version 330\n \
in vec2 vertCoord; \
out vec4 fragColor; \
const int npasses = 100; \
const int nclr = 5; \
uniform vec4 clrs[5]; \
void main(void) \
{ \
vec2 ab = vertCoord; \
float x_n = 0.5; \
float sum = 0; \
for (int i = 0; i < npasses; ++i) \
{ \
x_n = ab.x * x_n * (1 - x_n); \
sum += log(abs(ab.x*(1 - 2 * x_n))); \
x_n = ab.x * x_n * (1 - x_n); \
sum += log(abs(ab.x*(1 - 2 * x_n))); \
x_n = ab.x * x_n * (1 - x_n); \
sum += log(abs(ab.x*(1 - 2 * x_n))); \
x_n = ab.x * x_n * (1 - x_n); \
sum += log(abs(ab.x*(1 - 2 * x_n))); \
x_n = ab.x * x_n * (1 - x_n); \
sum += log(abs(ab.x*(1 - 2 * x_n))); \
x_n = ab.x * x_n * (1 - x_n); \
sum += log(abs(ab.x*(1 - 2 * x_n))); \
x_n = ab.y * x_n * (1.0 - x_n); \
sum += log(abs(ab.y*(1 - 2 * x_n))); \
x_n = ab.y * x_n * (1.0 - x_n); \
sum += log(abs(ab.y*(1 - 2 * x_n))); \
x_n = ab.y * x_n * (1.0 - x_n); \
sum += log(abs(ab.y*(1 - 2 * x_n))); \
x_n = ab.y * x_n * (1.0 - x_n); \
sum += log(abs(ab.y*(1 - 2 * x_n))); \
x_n = ab.y * x_n * (1.0 - x_n); \
sum += log(abs(ab.y*(1 - 2 * x_n))); \
x_n = ab.y * x_n * (1.0 - x_n); \
sum += log(abs(ab.y*(1 - 2 * x_n))); \
} \
float Lambda = sum / (12 * npasses); \
float a = (clamp(Lambda, -2.0, 2.0) + 2.0) / 4; \
for(int i = 0; i != (nclr - 1); ++i) \
{ \
if(a >= clrs[i].a && a < clrs[i+1].a) \
{ \
float m = (a - clrs[i].a) / (clrs[i+1].a - clrs[i].a); \
fragColor = vec4( \
mix(clrs[i].rgb, clrs[i+1].rgb, m), \
1.0 \
); \
break; \
} \
} \
} \
");
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
rectangle.Bind();
GLfloat rectangle_verts[8] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, -1.0f,
1.0f, 1.0f
};
verts.Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, 8, rectangle_verts);
VertexArrayAttrib vert_attr(prog, "Position");
vert_attr.Setup<GLfloat>(2);
vert_attr.Enable();
GLfloat min = 3.0f;
GLfloat max = 4.0f;
GLfloat rectangle_coords[8] = {
min, min,
min, max,
max, min,
max, max
};
coords.Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, 8, rectangle_coords);
VertexArrayAttrib coord_attr(prog, "Coord");
coord_attr.Setup<GLfloat>(2);
coord_attr.Enable();
const GLuint nclr = 5;
GLfloat colormap[nclr*4] = {
0.4f, 0.2f, 1.0f, 0.00f,
1.0f, 0.2f, 0.2f, 0.30f,
1.0f, 1.0f, 1.0f, 0.95f,
1.0f, 1.0f, 1.0f, 0.98f,
0.1f, 0.1f, 0.1f, 1.00f
};
Uniform<Vec4f>(prog, "clrs").SetValues(nclr*4, colormap);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
}
{
gl.Clear().ColorBuffer();
scroll_factor.Set(1.0f / (0.005 * time + 1.0f));
}
{
return time < 15.0;
}
};
void setupExample(ExampleParams& ){ }
std::unique_ptr<ExampleThread> makeExampleThread(
Example& ,
unsigned ,
const ExampleParams&
){ return std::unique_ptr<ExampleThread>(); }
std::unique_ptr<Example> makeExample(const ExampleParams& )
{
return std::unique_ptr<Example>(new RectangleExample);
}
}