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;
public:
RectangleExample(void)
{
#version 330\n \
in vec2 Position; \
in vec2 Coord; \
out vec2 vertCoord; \
void main(void) \
{ \
vertCoord = Coord; \
gl_Position = vec4(Position, 0.0, 1.0); \
} \
"));
#version 330\n \
in vec2 vertCoord; \
out vec4 fragColor; \
const int nclr = 5; \
uniform vec4 clrs[5]; \
void main(void) \
{ \
vec2 z = vec2(0.0, 0.0); \
vec2 c = vertCoord; \
int i = 0, max = 128; \
while((i != max) && (distance(z, c) < 2.0)) \
{ \
vec2 zn = vec2( \
z.x * z.x - z.y * z.y + c.x, \
2.0 * z.x * z.y + c.y \
); \
z = zn; \
++i; \
} \
float a = sqrt(float(i) / float(max)); \
for(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; \
} \
} \
} \
"));
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Build();
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, rectangle_verts);
(prog|"Position").Setup<Vec2f>().Enable();
GLfloat rectangle_coords[8] = {
-1.5f, -0.5f,
-1.5f, 1.0f,
0.5f, -0.5f,
0.5f, 1.0f
};
coords.Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, rectangle_coords);
(prog|"Coord").Setup<Vec2f>().Enable();
const std::size_t 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();
}
};
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);
}
}