Shows how to render line approximating a cubic bezier curve
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)
#include <cmath>
namespace oglplus {
class RectangleExample : public Example
{
private:
Context gl;
std::size_t curve_n;
Lazy<Uniform<Vec3f>> color;
std::size_t ctrl_n;
public:
RectangleExample(void)
: color(prog, "Color")
{
vs.Source(
"#version 330\n"
"in vec4 Position;"
"void main(void)"
"{"
" gl_Position = Position;"
"}"
);
fs.Source(
"#version 330\n"
"out vec4 fragColor;"
"uniform vec3 Color;"
"void main(void)"
"{"
" fragColor = vec4(Color, 1.0);"
"}"
);
prog.AttachShader(vs).AttachShader(fs).Build().Use();
};
BezierCurves<Vec2f, double, 3> bezier(
std::vector<Vec2f>(
bezier_cps,
bezier_cps+sizeof(bezier_cps)/sizeof(bezier_cps[0])
)
);
gl.Bind(curve);
{
auto data = bezier.Approximate(25);
curve_n = data.size();
gl.Bound(Buffer::Target::Array, curve_verts).Data(data);
(prog|"Position").Setup<Vec2f>().Enable();
}
gl.Bind(control);
{
auto data = bezier.ControlPoints();
ctrl_n = data.size();
gl.Bound(Buffer::Target::Array, ctrl_verts).Data(data);
(prog|"Position").Setup<Vec2f>().Enable();
}
gl.ClearColor(0.9f, 0.9f, 0.9f, 0.0f);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
}
{
gl.Clear().ColorBuffer();
color.Set(
Vec3f(0.9f, 0.9f, 0.2f));
gl.Bind(control);
color.Set(
Vec3f(0.1f, 0.1f, 0.1f));
gl.Bind(curve);
color.Set(
Vec3f(0.9f, 0.0f, 0.0f));
gl.Bind(control);
gl.PointSize(8.0);
gl.PointSize(1.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);
}
}